I recently came across a rule engine called
drools . Drool 6.0 is quite different from its previous version. For more information you can have a look
here.
I am currently working on a use case scenario in which I want the rules to be updated from my webapp (lets call it a drool-workbench) such that new rules can be applied on new tuple of an input stream without building the source again.
Consider this:
Suppose there is an input stream of tuple1,tuple2 & tuple3, and a rule that can be applied on these tuples.
Now, rule r1 is applied on tuple1 & tuple2. And then we need this rule to be updated with some new conditions. With the help of web-app (drools-wb) the rule r1 can be modified as per need. And then the latest artifact in maven repository can be deployed by "build and deploy" feature of drool-wb. As a new tuple (tuple3) is inserted into the stream, kieScanner will scan the maven repository for latest artifact and modified rule will be triggered on tuple3.
Sample Code:
package com.drool;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.KieScanner;
import sample.sample.Person;
public class Dynamicdrool{
public static void main(String [] args){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("sample","sample","1.0-SNAPSHOT");
KieContainer kc = ks.newKieContainer(releaseId);
KieScanner kScan = ks.newKieScanner(kc);
try
{
while((s = in.readLine())!=null && s.length()!=0)
{
Person p = new Person();
p.setName(s);
kScan.scanNow();
KieSession ksess = kc.newKieSession("sampleKs");
ksess.insert(p);
ksess.fireAllRules();
System.out.println(p.getName());
}
}catch(IOException e){
e.printStackTrace();
}
}
}
Points to remember:
- Use maven build in your project, to be able to use maven repository.
- KieScanner plays an important role, and it generally scans for newer versions in repository.
So use 1.0-SNAPSHOT as version or configure your maven for incremental version.
- There is a bug with kie-Scanner in 6.0.1-Final build. Either wait for latest release or build repos from master branch