JBPM5 - RuleTask Example Using a Rule File
Posted by bpmn2user in bpmn2 user's Blog on Mar 7, 2011 3:48:46 PMHere is an example that shows how to use 'Rule Task' component with a process.
A simple example shown in http://community.jboss.org/people/bpmn2user/blog/2011/03/01/jbpm5--loop-example is considered here to illustrate the feature.
This example has a loop condition which is determined using the rules based on the location of the user and the starting condition.
'LoopConditionRules' is a 'Rule Task' component and it needs to have a valid RuleFlowGroup name (e.g., EvalLoopcondistion as shown below).
The RuleFlowGroup set in the previous step has to match with the 'ruleflow-group' name in the 'drl' rules file as shown below.
Here is the sample test code to start the process after setting the initial parameters.
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = createSession(kbase);
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory
.newFileLogger(ksession, "test");
// set the parameters
Map<String, Object> params = new HashMap<String, Object>();
HelloProcessModel hpm = new HelloProcessModel();
hpm.setCount(new Integer("3"));
hpm.setUserlocation("NewYorkUser");
params.put("hpm", hpm);
ksession.startProcess("looptest777",params);
ksession.fireAllRules();
logger.close();
Note that the 'drl' rules file needs to be added to the knowledgebase as shown below.
private static KnowledgeBase readKnowledgeBase() throws Exception {
ProcessBuilderFactory
.setProcessBuilderFactoryService(new ProcessBuilderFactoryServiceImpl());
ProcessMarshallerFactory
.setProcessMarshallerFactoryService(new ProcessMarshallerFactoryServiceImpl());
ProcessRuntimeFactory
.setProcessRuntimeFactoryService(new ProcessRuntimeFactoryServiceImpl());
BPMN2ProcessFactory
.setBPMN2ProcessProvider(new BPMN2ProcessProviderImpl());
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("processRuleslooptest777.bpmn"),
ResourceType.BPMN2);
/*
* Add drl file
*/
kbuilder.add(ResourceFactory.newClassPathResource("LoopConditionRules.drl"), ResourceType.DRL);
return kbuilder.newKnowledgeBase();
}
How to run this example?
This example has a bpmn file (processRuleslooptest777.bpmn), model file (HelloProcessModel.java), rules file (LoopConditionRules.drl) and a test file (HelloProcessTest.java). It might be easy to start with a HelloProcess project (http://community.jboss.org/people/bpmn2user/blog/2011/02/27/helloprocess-example-using-jbpm5-eclipse-plug-in) and import these files.
Following results can be seen after successfully running the example.
-
LoopConditionRules.drl.zip 387 bytes
-
HelloProcessModel.java.zip 370 bytes
-
ProcessRuleTest.java.zip 1.2 KB
Comments