Skip navigation
bpmn2user

JBPM5 - Loop Example

Posted by bpmn2user Mar 1, 2011

Here is an example that shows how to process a loop using JBPM5.

A simple example shown in the discussion http://community.jboss.org/thread/163105?tstart=0 is considered here.

This example has two parameters ‘count’ and ‘loopcondition’.

The paramter ‘count’ gets incremented in a loop until it is less than a ‘loopcondition’.

 

ScreenHunter_09 Mar. 01 09.41.gif

 

ScreenHunter_05 Mar. 01 20.34.gif

How to run this example?

 

This example has a bpmn file (looptest777.bpmn) and a test file (ProcessTest2.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_03 Mar. 01 20.16.gif

 

Here is the sample test code. The main process ‘looptest777’  is started as shown below.

 

 

public static final void main(String[] args) {
  try {
   // 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>();
   params.put("count", 1);
   params.put("loopcondition", 3);
  
   // start a new process instance
   ksession.startProcess("looptest777", params);
  
   logger.close();
  } catch (Throwable t) {
   t.printStackTrace();
  }
}

 

Following output can be seen after running the example.

ScreenHunter_04 Mar. 01 20.18.gif

Here is an example that shows how to invoke a sub-process from a main process.

A simple example shown in the discussion http://community.jboss.org/thread/163105?tstart=0 is considered here.

 

This example has a main process as shown below. It sends a couple of parameters (count and loopcondition) to the subprocess.

ScreenHunter_08 Mar. 01 09.41.gif

The sub-process goes through a loop incrementing the value of 'count'. Once the 'count' reaches the 'do until' loop condition value, it will exit the loop and return the 'count' value to the main process. The 'loopcondition' can either be set inside this sub-process or can be retrieved from the parent process. In this example, it is set as a paramter in the main process.

ScreenHunter_09 Mar. 01 09.41.gif

How to run this example?

 

This example has two bpmn files (sampleLoopTest.bpmn for main process and looptest777.bpmn for sub-process loop) and a test file (LoopSubProcessTest.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_01 Mar. 01 11.47.gif

 

 

Here is the sample test code. The main process 'com.sample.bpmn.looptest'  is started as shown below which inturn invokes the sub-process.

 

public class LoopSubProcessTest {

 

   public static final void main(String[] args) {

        try {

            // load up the knowledge base

            KnowledgeBase kbase = readKnowledgeBase();

            StatefulKnowledgeSession ksession = createSession(kbase);

            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory

                    .newFileLogger(ksession, "test");

 

            // Set the initial count and loopcondition

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

            params.put("count", 1);

            params.put("loopcondition", 4);

 

            //start the main process

            ksession.startProcess("com.sample.bpmn.looptest", params);

 

            logger.close();

        } catch (Throwable t) {

            t.printStackTrace();

        }

    }

It is important to note that both the bpmn files need to be added to the KnowledgeBuilder as shown below.

 

IScreenHunter_11 Mar. 01 09.44.gif

 

 

Here are the results you would see after running the test program LoopSubProcessTest.

ScreenHunter_10 Mar. 01 09.44.gif

Filter Blog

By date:
By tag: