Log In | Sign Up   View a printable version of the current page.
  Dashboard > [fleXive] > ... > How-Tos > sample jbpm workflow for flexive

Added by Johannes Wernig-Pichler, last edited by Anonymous on Jun 01, 2010  (view change)
Labels: 
(None)

Simple JBPM workflow for [fleXive]

This section demonstrates how to work with jbpm workflows in fleXive. In-depth documentation on how to define jbpm workflows and how to handle them can be found here.

Consider this simple workflow:

start ---> state1 ---> state2 ---> end

The jPDL process file for the workflow looks as follows:

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

<start g="174,23,59,65" name="start1">
	<transition g="-63,-22" name="to_state1" to="state1"/>
</start>

<end g="284,509,48,48" name="end"/>

<state g="317,146,92,52" name="state1">
	<transition g="-63,-22" name="to_state2" to="state2"/>
</state>

<state g="474,324,92,52" name="state2">
	<transition g="-63,-22" name="to_end" to="end"/>
</state>

</process>

Save the file as test.jpdl.xml in the root of your [fleXive] classpath.

The following code demonstrates how to start a new instance of the workflow above. Further it states how the transition from one state to another can be invoked (i.e. by signals). Note: additionally for every state some business methods could be implemented. Same goes for transactions, e.g. using EventListeners.
The following code can, for example, be included in an EJB and started from an JSF-page.

/*
   Deploys the process, starts a process execution, walks through the process states
   until it reaches the end state.	
*/
public void startProcess(){
	// build a process engine from the default configuration file (jbpm.cfg.xml).
	ProcessEngine processEngine = new Configuration()
	      .buildProcessEngine();
	// deploy the process using its xml-decription
	String deploymentId = processEngine.getRepositoryService().createDeployment()
	    .addResourceFromClasspath("test.jpdl.xml")
    	.deploy();

	ProcessInstance pi = null;

	// start a new process instance
	ExecutionService es = processEngine.getExecutionService();
        /* 
	  start the process instance and update the process instance variable.
          note: be sure to update the process instance variable when passing
          from one state to another as it is not automatically updated
	*/
	pi = es.startProcessInstanceByKey("test");

	// get the current execution of the workflow
	Execution e1 = pi.findActiveExecutionIn(getActiveState(pi));
	// signal the execution of the workflow to take the specified transition (i.e. go to 'state2')
	pi = es.signalExecutionById(e1.getId(), getTrans(pi).get(0));

	// goto 'end'
	Execution e2 = pi.findActiveExecutionIn(getActiveState(pi));
	pi = es.signalExecutionById(e2.getId(), getTrans(pi).get(0));
}


// get the active state of the process instance, e.g. state1, state2
public String getActiveState(ProcessInstance pi){
	try {
        	java.util.Set<java.lang.String> names = pi.findActiveActivityNames();
        	String name = (String) names.iterator().next();
		/* 
		find the workflow execution in the specified state; note: there can be 
		several executions of the workflow, all in different states
		*/
        	Execution ex = pi.findActiveExecutionIn(name);
        	ExecutionImpl exi = (ExecutionImpl) ex;
        	ActivityImpl ai = exi.getActivity();
        	return ai.getName();
        } catch (Exception e){
        	return "";
        }
}

// get all outgoing transitions for the current node/state
public List<String> getTrans(ProcessInstance pi){
        List<String> out = new ArrayList<String>();
        try {
        	java.util.Set<java.lang.String> names = pi.findActiveActivityNames();
            	String name = (String) names.iterator().next();
           	Execution ex = pi.findActiveExecutionIn(name);
            	ExecutionImpl exi = (ExecutionImpl) ex;
            	ActivityImpl ai = exi.getActivity();
             	List<Transition> list = ai.getOutgoingTransitions();

            	for(Transition t : list){
                	out.add(t.getName());
            	}
        } catch(Exception e){
            // nothing
        }
        return out;
}

Mapping jbpm workflows to [fleXive] workflows

Jbpm workflows can be mapped to [fleXive] workflows the following way:

  • jbpm states correspond to [fleXive] steps
  • jbpm transitions correspond to [fleXive] routes

Thus a [fleXive] workflow equivalent to the jbpm workflow shown above can be build like this:

Step start = new Step(0, 0, 0, 1);// Step(stepId, stepDefinitionId, workflowId, aclId)
Step state1 = new Step(1, 1, 0, 1); 
Step state2 = new Step(2, 2, 0, 1);
Step end = new Step(3, 3, 0, 1);

List<Step> steps = new ArrayList<Step>()
// add all steps to the steps-list...


Route fromStartToState1 = new Route(1, UserGroup.GROUP_EVERYONE, 0, 1); // Route(routeId, groupId, fromStepId, toStepId)
Route fromState1ToState2 = new Route(2, UserGroup.GROUP_EVERYONE, 1, 2);
Route fromState2ToEnd = new Route(3, UserGroup.GROUP_EVERYONE, 2, 3);

List<Route> routes = new ArrayList<Route>();
// add all routes to the routes-list...

Workflow wf = new Workflow(0, "TestWF", "TestWF", steps, routes); // Workflow(WfId, name, description, steps, routes)
Anonymous

Hi,
Could we use bpel?

wagner

Reply To This

Hi,

since jBPM is used as a library you can of course use bpel just like you would in a "normal" jBPM project.

Cheers,
Markus

Reply To This
Add Comment
Site running on a free Atlassian Confluence Open Source Project License granted to [fleXive] . Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.1 Build:#916 Nov 09, 2007) - Bug/feature request - Contact Administrators