Difference between revisions of "Use of decision node"

From OpenKM Documentation
Jump to: navigation, search
 
(7 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
 
* Creating a class which implements the [http://docs.jboss.com/jbpm/v3.2/javadoc-jpdl/org/jbpm/graph/node/DecisionHandler.html DecisionHandler] interface. The ''decide'' method returns the transition to follow.
 
* Creating a class which implements the [http://docs.jboss.com/jbpm/v3.2/javadoc-jpdl/org/jbpm/graph/node/DecisionHandler.html DecisionHandler] interface. The ''decide'' method returns the transition to follow.
 +
 +
{{Note|Source Eclipse project at [[File:Workflow samples.zip]].}}
  
 
Below we are going to create a process containing a decision node which implements the [http://docs.jboss.com/jbpm/v3.2/javadoc-jpdl/org/jbpm/graph/node/DecisionHandler.html DecisionHandler] interface to decide the next node in the workflow.
 
Below we are going to create a process containing a decision node which implements the [http://docs.jboss.com/jbpm/v3.2/javadoc-jpdl/org/jbpm/graph/node/DecisionHandler.html DecisionHandler] interface to decide the next node in the workflow.
Line 13: Line 15:
 
<source lang="java">
 
<source lang="java">
 
public class ValueAction implements ActionHandler {
 
public class ValueAction implements ActionHandler {
 +
    private static final long serialVersionUID = 1L;
 
     public String value;
 
     public String value;
  
Line 26: Line 29:
 
<source lang="java">
 
<source lang="java">
 
public class MyDecision implements DecisionHandler {
 
public class MyDecision implements DecisionHandler {
 +
    private static final long serialVersionUID = 1L;
 +
 
     public MyDecision(String info) {
 
     public MyDecision(String info) {
 
         super();
 
         super();
Line 47: Line 52:
 
<source lang="java">
 
<source lang="java">
 
public class ShowMessageAction implements ActionHandler {
 
public class ShowMessageAction implements ActionHandler {
 +
    private static final long serialVersionUID = 1L;
 +
 
     public ShowMessageAction(String info) {
 
     public ShowMessageAction(String info) {
 
         super();
 
         super();
Line 62: Line 69:
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
<process-definition xmlns="" name="process node">
+
<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="decision node">
 
+
    <start-state name="start">
<start-state name="start">
+
        <transition to="decision" name="initiate">
<transition to="decision" name="initiate">
+
            <action name="createvalue" class="com.openkm.sample.ValueAction"></action>
<action name="createvalue" class="com.openkm.sample.ValueAction"></action>
+
        </transition>
</transition>
+
    </start-state>
</start-state>
 
 
 
<decision name="decision">
 
<handler class="com.openkm.sample.MyDecision"></handler>
 
<transition to="end_1" name="trans1"></transition>
 
<transition to="end_2" name="trans2"></transition>
 
</decision>
 
  
<end-state name="end_1">
+
    <decision name="decision">
<event type="node-enter">
+
        <handler class="com.openkm.sample.MyDecision"></handler>
<action class="com.openkm.sample.ShowMessageAction"></action>
+
        <transition to="end_1" name="trans1"></transition>
</event>
+
        <transition to="end_2" name="trans2"></transition>
</end-state>
+
    </decision>
  
<end-state name="end_2">
+
    <end-state name="end_1">
<event type="node-enter">
+
        <event type="node-enter">
<action class="com.openkm.sample.ShowMessageAction"></action>
+
            <action class="com.openkm.sample.ShowMessageAction"></action>
</event>
+
        </event>
</end-state>
+
    </end-state>
  
 +
    <end-state name="end_2">
 +
        <event type="node-enter">
 +
            <action class="com.openkm.sample.ShowMessageAction"></action>
 +
        </event>
 +
    </end-state>
 
</process-definition>
 
</process-definition>
 
</source>
 
</source>
  
 
[[Category: Workflow Guide]]
 
[[Category: Workflow Guide]]

Latest revision as of 11:47, 3 December 2012

A decision node is used to guide the process running path. This means you can tell the workflow which way should go. Decision criteria can be specified as follows:

  • Adding conditions to transitions or BeanShell script which returns a boolean. Nodes go across its transitions examining the conditions until found the first one which match a criteria.
  • Creating a class which implements the DecisionHandler interface. The decide method returns the transition to follow.

Nota clasica.png Source Eclipse project at File:Workflow samples.zip.

Below we are going to create a process containing a decision node which implements the DecisionHandler interface to decide the next node in the workflow.

Jbpm sample decision.png

This process begins at the start node and go across the transition name "initiate". This transition has an action called "createvalue" handler by the class ValueAction, who set the variable "value" in the execution context.

public class ValueAction implements ActionHandler {
    private static final long serialVersionUID = 1L;
    public String value;

    @Override
    public void execute(ExecutionContext executionContext) throws Exception {
        executionContext.getContextInstance().setVariable("value", value);
    }
}

After that arrives to the decision node handled by the class Decission, who check the existence of the "value" variable in the execution context. If exists this variable leave the node across the transition "trans1", otherwise go across "trans2".

public class MyDecision implements DecisionHandler {
    private static final long serialVersionUID = 1L;

    public MyDecision(String info) {
        super();
    }

    @Override
    public String decide(ExecutionContext executionContext) throws Exception {
        String value = (String)executionContext.getContextInstance().getVariable("value");

        if(value != null) {
            return "trans1";
        } else {
            return "trans2";
        }
    }
}

Finally when arrives to any of the two end nodes, an action is executed which shows the node where the execution flow ends. This action is handled by the class ShowMessageAction:

public class ShowMessageAction implements ActionHandler {
    private static final long serialVersionUID = 1L;

    public ShowMessageAction(String info) {
        super();
    }

    @Override
    public void execute(ExecutionContext executionContext) throws Exception {
        System.out.println("Flow go to node: " + executionContext.getNode().getName());
    }
}

If in the configuration you have set the "value" variable in the console you will see a message saying that has finished in node "end_1". In the other side, will end in node "end_2".

<?xml version="1.0" encoding="UTF-8"?>
<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="decision node">
    <start-state name="start">
        <transition to="decision" name="initiate">
            <action name="createvalue" class="com.openkm.sample.ValueAction"></action>
        </transition>
    </start-state>

    <decision name="decision">
        <handler class="com.openkm.sample.MyDecision"></handler>
        <transition to="end_1" name="trans1"></transition>
        <transition to="end_2" name="trans2"></transition>
    </decision>

    <end-state name="end_1">
        <event type="node-enter">
            <action class="com.openkm.sample.ShowMessageAction"></action>
        </event>
    </end-state>

    <end-state name="end_2">
        <event type="node-enter">
            <action class="com.openkm.sample.ShowMessageAction"></action>
        </event>
    </end-state>
</process-definition>