Here 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.

 

ScreenHunter_03 Mar. 07 14.14.gif

'LoopConditionRules' is a 'Rule Task' component and it needs to have a valid RuleFlowGroup name (e.g.,  EvalLoopcondistion as shown below).

 

ScreenHunter_04 Mar. 07 14.14.gif

The RuleFlowGroup set in the previous step has to match with the 'ruleflow-group' name in the 'drl' rules file as shown below.

 

 

ScreenHunter_05 Mar. 07 14.15.gif

 

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.

 

 

ScreenHunter_08 Mar. 07 14.43.gif

 

Following results can be seen after successfully running the example.

ScreenHunter_07 Mar. 07 14.18.gif