JBoss.orgCommunity Documentation
CDI, Contexts and Dependency Injection, is java specification that provides declarative controls and strucutres to an application. KIE can use it to automatically instantiate and bind things, without the need to use the programmatic api.
@KContainer, @KBaser and @KSession all support an optional 'name' attribute. CDI typically does "getOrCreate" when it injects, all injections receive the same instance for the same set of annotations. the 'name' annotation forces a unique instance for each name, although all intsance for that name will be identity equals.
Used to bind an instance to a specific version of a KieModule. If kie-ci is on the classpath this will resolve dependencies automatically, downloading from remote repositories.
@KContainer is optional as it can be detected and added by the use of @Inject and variable type inferrence.
@Inject
@KReleaseId(groupId = "jar1", artifactId = "art1", version = "1.1")
private KieContainer kContainer;
Figure 10.2. Injects KieContianer for Dyanmic KieModule
@Inject
@KContainer(name = "kc1")
@KReleaseId(groupId = "jar1", artifactId = "art1", version = "1.1")
private KieContainer kContainer;
Figure 10.3. Injects named KieContianer for Dyanmic KieModule
@KBase is optional as it can be detected and added by the use of @Inject and variable type inferrence.
The default argument, if given, maps to the value attribute and specifie the name of the KieBase from the kmodule.xml file.
@Inject
private KieBase kbase;
Figure 10.4. Injects the Default KieBase from the Classpath KieContainer
@Inject
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private KieBase kbase;
Figure 10.5. Injects the Default KieBase from a Dynamic KieModule
@Inject
@KSession("kbase1")
@KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.0")
private KieBase kbase1v10;
@Inject
@KBase("kbase1")
@KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.1")
private KieBase kbase1v10;
Figure 10.6. Side by side version loading for 'jar1.KBase1' KieBase
@Inject
@KSession(value="kbase1", name="kb1")
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private KieBase kbase1kb1;
@Inject
@KSession(value="kbase1", name="kb2")
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private KieBase kbase1kb2;
Figure 10.7. Use 'name' attribute to force new Instance for 'jar1.KBase1' KieBase
@KSession is optional as it can be detected and added by the use of @Inject and variable type inferrence.
The default argument, if given, maps to the value attribute and specifie the name of the KieSession from the kmodule.xml file
@Inject
private KieSession ksession;
Figure 10.8. Injects the Default KieSession from the Classpath KieContainer
@Inject
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private KieSession ksession;
Figure 10.9. Injects the Default KieSession from a Dynamic KieModule
@Inject
@KSession("ksession1")
@KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.0")
private KieSession ksessionv10;
@Inject
@KSession("ksession1")
@KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.1")
private KieSession ksessionv11;
Figure 10.10. Side by side version loading for 'jar1.KBase1' KieBase
@Inject
@KSession(value="ksession1", name="ks1")
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private KieSession ksession1ks1
@Inject
@KSession(value="ksession1", name="ks2")
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private KieSession ksession1ks2
Figure 10.11. Use 'name' attribute to force new Instance for 'jar1.KBase1' KieSession
@KSession is optional as it can be detected and added by the use of @Inject and variable type inferrence.
The default argument, if given, maps to the value attribute and specifie the name of the KieSession from the kmodule.xml file.
@Inject
private StatelessKieSession ksession;
Figure 10.12. Injects the Default StatelessKieSession from the Classpath KieContainer
@Inject
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private StatelessKieSession ksession;
Figure 10.13. Injects the Default StatelessKieSession from a Dynamic KieModule
@Inject
@KSession("ksession1")
@KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.0")
private StatelessKieSession ksessionv10;
@Inject
@KSession("ksession1")
@KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.1")
private StatelessKieSession ksessionv11;
Figure 10.14. Side by side version loading for 'jar1.KBase1' KieBase
@Inject
@KSession(value="ksession1", name="ks1")
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private StatelessKieSession ksession1ks1
@Inject
@KSession(value="ksession1", name="ks2")
@KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")
private StatelessKieSession ksession1ks2
Figure 10.15. Use 'name' attribute to force new Instance for 'jar1.KBase1' StatelessKieSession
CDI can inject instances into fields, or even pass them as arguments. In this example field injection is used.
@Inject
@KSession("ksession1")
KieSession kSession;
public void go(PrintStream out) {
kSession.setGlobal("out", out);
kSession.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?"));
kSession.fireAllRules();
}
Figure 10.16. CDI example for a named KieSession
This is less code and more declarative than the api approach.
public void go(PrintStream out) {
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession1");
kSession.setGlobal("out", out);
kSession.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?"));
kSession.fireAllRules();
}
Figure 10.17. API equivalent example for a named KieSession