- Create a new Maven project in the root folder. In VSCode there is a button for this (Create Maven project), and it will automatically perform the following steps:
a. Update the
moduleselement in the rootpom.xmlwith the name of the new module. b. Include theparentelement in thepom.xmlof the new module - Build your new module for the first time. In VSCode, you can do this by restarting VSCode
- Add your new module as a
runtimedependency for thebrokermodule in thepom.xmlof thebroker. This ensures that Java will find your classes whenbrokeris executed.
-
First, if the analysis you plan to implement was not declared before, you need to declare the analysis in
ontology.analyses. Make sure you get the package right, since this is howbrokerwill find the analysis. In your implementation you will use this to tell others that they can depend on this analysis in their own analysers.-
Example:
package p4query.ontology.analyses; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import javax.inject.Qualifier; @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface MySpecialAnalysis { }
-
You will use this annotation in dependency injection. Specifically, you will use this to communicate to others that your analyser is capable of providing a
@MySpecialAnalysistoken. This token signifyies that your analysis has been completed. Others will claim dependency on this token, but they actually expect you to modify the knowledge graph according to the requirements of this analysis. -
Additionally,
brokerwill use this annotation to discover your modul by looking up which class has a method annotated with this annotation.
-
-
Then, you create the tests to completely define the requirements that your analysis satisfies. It may also be a good idea to extend the
experts-visualizerapplication for your@MySpecialAnalysisanalysis, so that you can actually see the results as you progress with your work.
Note that the main class of the actual Java software is in the broker. You implement your analysis as a module, and the broker will discover it automatically.
-
First, create a new module (see above), and add the
ontologymodule as a dependency. -
Create a class in
p4query.expertsthat implements the selected analysis. Make sure you get the package right, since this is howbrokerwill find the analyser. The dependency injector (DI) inbrokerwill provide your class with everything it needs.-
Example:
package p4query.experts; import org.codejargon.feather.Provides; import javax.inject.Singleton; import p4query.ontology.providers.P4FileProvider.InputP4File; import p4query.ontology.analyses.SyntaxTree; import p4query.ontology.analyses.MySpecialAnalysis; import p4query.ontology.Status; public class MySpecialAnalysisImpl { @Provides @Singleton @MySpecialAnalysis public Status analyse(GraphTraversalSource g, @SyntaxTree Provider<Status> ensureSt, @CLIArgs AppUI args, @InputP4File File inputP4){ if(g.V().count().next() == 0) ensureSt.get(); System.out.println(g.V().count().next()); System.out.println("Done."); return new Status(); } }
-
Note that the method
analysehas a@Providesannotation. This tells the DI thatMySpecialAnalysisImplis capable of providing the@MySpecialAnalysisanalysis on the knowledge graph. All parameters are injected by thebroker. Usually, you depend on the knowledge graphGraphTraversalSource, and a certain number of analyses performed on the knowledge graph, but in special cases you may need direct access to the raw P4 file as well.- By convention, applications and analysers should always return
Statustype. - Special dependency names such as
@InputP4Fileor@CLIArgsare defined inontology. It's good to get to know this package and subpackages, to see what you can use. In this case,@InputP4Fileis a reference to the raw P4 file being processed (usually not needed), and@CLIArgsis an object storing the user provided command line arguments (usually not needed). - It may happen you do not want to initialize all your dependencies in all cases (e.g. you may only want to run syntax tree analysis, if the user requests it). In these cases, you can request a
Providerinstance that will only initialize the dependency if/when you call itsget()method.
- By convention, applications and analysers should always return
-
-
Try it by running an application that depends on your
@MySpecialAnalysisanalysis. It may be a good idea to extend theexperts-visualizerapplication, so that you can see the results.