I’m back! I have been a bit busy at work. I traveled to BA to attend to the JBPM5/Drools Workshop where the leads of the project gave some excellent presentations. It was great, I was able to meet many of them, discover what’s being done currently and listen about interesting projects (such as Drools Chance, Drools Planner), that I didn’t have time to check yet.
This week I was trying to make rules work JBPM5 when using a persistence and transactions. This was a bit complicated, specially because when calling ksession.fireUntilHalt(), it will create another thread that will work with this session, and was always failing when the fire until halt thread was trying to commit the session. This will be another post, as I could not solve it all yet :).
Anyway, I realized that I only wanted to make a simple evaluation, that will produce a result that will be used to take a decision. And it seemed a work for an StatelessSession that simply evaluates some of the workflow parameters and put the results in the process instance.
So I made a simple workitem, which will have a KnowledgeStatelessSession and will evaluate the rules:
public class StatelessRuleEvaluationWorkItemHandler implements WorkItemHandler { private StatelessKnowledgeSession ksession; public StatelessRuleEvaluationWorkItemHandler(KnowledgeBase kbase) { ksession = kbase.newStatelessKnowledgeSession(); new WorkingMemoryConsoleLogger(ksession); }
@Override public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { List<Object> facts = new ArrayList<Object>(); facts.add(new DataInput(workItem.getParameters())); DataOutput output = new DataOutput(new HashMap<String, Object>()); facts.add(output); ksession.execute(facts); manager.completeWorkItem(workItem.getId(), output.getDataMap()); } }
Then, just assigned the task to this WorkItemHandler:
ksession.getWorkItemManager().registerWorkItemHandler( "ExceedsRule", new StatelessRuleEvaluationWorkItemHandler(ksession.getKnowledgeBase()));
And created the rule:
rule "Exceeds"
when
$input: DataInput(dataMap["limit"] < dataMap["count"])
$output: DataOutput()
then
$output.getDataMap().put("exceeds", new Boolean(true));
end
rule "NotExceeds"
when
$input: DataInput(dataMap["limit"] >= dataMap["count"])
$output: DataOutput()
then
$output.getDataMap().put("exceeds", new Boolean(false));
end
And it worked fine, the rule was executed, the variable was used in the next node, and there were no problems with persistence. For simple rules, this example can definitely help!
You can check the full example here:
https://github.com/calcacuervo/JBPM5-Samples/tree/master/test-rules
Feel free to ask any question/suggestion!
Thanks for reading!
Demian