Skip navigation
2011

Hi,

 

Using Rules in our workflow saves us the trouble of creating Sessions and firing rules explicitly. Everything is done automatically. Sharing an example on using rules in jbpm 4 workflow.

 

1) Process Definition :

 

<?xml version="1.0" encoding="UTF-8"?>

 

<process key="rules" name="TestRules" xmlns="http://jbpm.org/4.3/jpdl">

   <start g="133,0,48,48" name="start1">

      <transition to="Rule"/>

   </start>

   <rules g="60,84,193,73" name="Rule">

   <fact var="check"/>

      <transition to="exclusive1"/>

   </rules>

   <decision expr="#{check.decision}" g="132,192,75,58" name="exclusive1">

      <transition g="-30,-14" name="Yes" to="java1"/>

      <transition g="8,-12" name="No" to="java2"/>

   </decision>

   <java class="action.Print" g="36,252,85,49" method="printYes" name="java1">

      <transition to="end1"/>

   </java>

   <java class="action.Print" g="192,252,84,49" method="printNo" name="java2">

      <transition to="end1"/>

   </java>

   <end g="132,324,55,53" name="end1"/>

</process>

 

Process Definition here consists of a Rule node which matches the fact "check" with the rules in .drl file. Following which the value of the class variable decision is matched and accordingly a transition is taken up by the workflow.

 

2) Rule Resource :

 

import action.Customer;

 

rule "CheckAge1"

  when

    customer : Customer( age >= 18)

  then

      System.out.println("INSIDE RULE CHECKAGE1");

    customer.setDecision("Yes");

end

 

rule "CheckAge2"

  when

    customer : Customer( age < 18)

  then

      System.out.println("INSIDE RULE CHECKAGE2");

    customer.setDecision("No");

  

end

 

3) POJO :

 

package action;

 

import java.io.Serializable;

 

public class Customer implements Serializable {

   

     int age;

     String decision="Yes";

   

    Customer(int age)

    {

        this.age = age;

    }

   

    public int getAge()

    {

        return age;

    }

   

    public void setAge(int age)

    {

        this.age = age;

    }

   

    public String getDecision()

    {

        return decision;

    }

   

    public void setDecision(String decision)

    {

        this.decision = decision;

    }

}

 

4) Main Class :

 

package action;

import java.util.HashMap;

import java.util.Map;

 

import org.jbpm.api.Configuration;

import org.jbpm.api.ExecutionService;

import org.jbpm.api.ProcessEngine;

import org.jbpm.api.RepositoryService;

 

 

public class StartRulesProcess {

   

    public static void main(String[] args) {

       

        Configuration configuration = new Configuration();

        ProcessEngine processEngine = configuration.buildProcessEngine();

       

        RepositoryService repositoryService = processEngine.getRepositoryService();

        ExecutionService executionService = processEngine.getExecutionService();

       

        repositoryService.createDeployment().addResourceFromClasspath("Rule.drl").addResourceFromClasspath("TestRules.jpdl.xml").deploy();

    //    repositoryService.createDeployment().addResourceFromClasspath("TestRules.jpdl.xml").deploy();

   

        Map<String, Object> var = new HashMap<String, Object>();

        var.put("check", new Customer(19));

       

        executionService.startProcessInstanceByKey("rules", var);   

    }

}

 

Regards