Skip navigation

Excel decision tables are widely used to represent rule logic in a compact format.

This format is particularly popular as non-technical business users can read, edit and maintain the conditional logic in Excel spreadsheets.

 

This example shows how to use an Excel decision table rules inside a process.

The ‘Rule Task’ components can use the rules defined in a drl file, Excel decision table or the rules defined in BRMS (Guvnor).

An example to use the rules defined in drl file is shown in http://community.jboss.org/people/bpmn2user/blog/2011/03/07/jbpm5--ruletask-example.

This example shows how to run the same example using Excel decision table.

 

Please refer to Drools documentation (http://downloads.jboss.com/drools/docs/5.1.1.34858.FINAL/drools-expert/html/ch05.html#d0e5683) to know about authoring Drools decision tables.

 

Here are rules represented in Excel decision table.

ScreenHunter_12 Mar. 07 18.25.gif

 

There are three rules in the following decision table. For example, Rule1 (Row 11) means that if the 'userlocation' is set to ‘NewYorkUser’ and the initial count is less 4, then the ‘loopcondition’ is set to 6.

 

Following is the main process flow diagram. This example has a loop condition which is determined using the rules shown in the above decision table in the RuleTask 'LoopConditionRules'.

 

 

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) and it should match with the 'RULEFLOW-GROUP' name (Column C) of the decision table.

 

ScreenHunter_04 Mar. 07 14.14.gif

Excel decision table 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 Excel decision table file

         */

 

        DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();

        config.setInputType(DecisionTableInputType.XLS);

        kbuilder.add(ResourceFactory.newClassPathResource("LoopConditionRules.xls"), ResourceType.DTABLE, config);

 

        return kbuilder.newKnowledgeBase();

    }

 

How to deploy?

This example has a bpmn file (processRuleslooptest777.bpmn), model file (HelloProcessModel.java), rules file (LoopConditionRules.xls) 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_13 Mar. 07 19.30.gif

 

Following results can be seen after successfully running the example.

ScreenHunter_07 Mar. 07 14.18.gif

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