流程定义文件
<?xml version="1.0" encoding="UTF-8"?>
<process-definition
xmlns="urn:jbpm.org:jpdl-3.2" name="forkjoin">
<start-state name="开始">
<transition name="to join" to="fork"></transition>
</start-state>
<fork name="fork">
<script>
<variable name="transitionNames" access="write"></variable>
<expression>
transitionNames=new ArrayList();
if(param>100){
transitionNames.add("to node1");
transitionNames.add("to node2");
}else{
transitionNames.add("to node3");
transitionNames.add("to node4");
}
</expression>
</script>
<transition name="to node1" to="node1"></transition>
<transition name="to node2" to="node2"></transition>
<transition name="to node3" to="node3"></transition>
<transition name="to node4" to="node4"></transition>
</fork>
<join name="join">
<transition name="to 结束" to="结束"></transition>
</join>
<end-state name="结束"></end-state>
<node name="node1">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("经过"+node.getName());
</script>
</event>
</node>
<node name="node2">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("经过"+node.getName());
</script>
</event>
</node>
<node name="node3">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("经过"+node.getName());
</script>
</event>
</node>
<node name="node4">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("经过"+node.getName());
</script>
</event>
</node>
</process-definition>
工作机制:是通过在fork节点添加<script></script>标签,在script内部设置
<variable name="transitionNames" access="write"></variable>切记此处access属性设置为write
和
<expression>
transitionNames=new ArrayList();
if(param>100){
transitionNames.add("to node1");
transitionNames.add("to node2");
}else{
transitionNames.add("to node3");
transitionNames.add("to node4");
}
</expression>
上面在流程实例上下文中定义了一个变量param,当param大于100时,执行node1和node2节点
测试一下:
package com.jbpm;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
public class TestForkJoin {
public static void main(String[] args) {
ProcessDefinition processDefinition=ProcessDefinition.parseXmlResource("forkjoin/processdefinition.xml");
ProcessInstance processInstance=new ProcessInstance(processDefinition);
processInstance.getContextInstance().setVariable("param", 200);
//processInstance.signal();
System.err.println("当前节点是:"+processInstance.getRootToken().getNode().getName());
processInstance.signal();
System.err.println("当前节点是:"+processInstance.getRootToken().getNode().getName());
}
}
运行结果
当前节点是:开始
11:44:07,703 [main] DEBUG GraphElement : event 'before-signal' on 'StartState(开始)' for 'Token(/)'
11:44:07,703 [main] DEBUG GraphElement : event 'node-leave' on 'StartState(开始)' for 'Token(/)'
11:44:07,718 [main] DEBUG GraphElement : event 'transition' on 'Transition(to join)' for 'Token(/)'
11:44:07,718 [main] DEBUG GraphElement : event 'node-enter' on 'Fork(fork)' for 'Token(/)'
11:44:07,719 [main] DEBUG Script : script input: {node=Fork(fork), param=200, token=Token(/), task=null, executionContext=ExecutionContext[Token(/)], taskInstance=null}
11:44:07,814 [main] DEBUG Script : script output: {transitionNames=[to node1, to node2]}
11:44:07,830 [main] DEBUG GraphElement : event 'node-leave' on 'Fork(fork)' for 'Token(/to node1)'
11:44:07,830 [main] DEBUG GraphElement : event 'transition' on 'Transition(to node1)' for 'Token(/to node1)'
11:44:07,830 [main] DEBUG GraphElement : event 'node-enter' on 'Node(node1)' for 'Token(/to node1)'
11:44:07,830 [main] DEBUG GraphElement : executing action 'Script(10f6d3)'
11:44:07,830 [main] DEBUG Token : token[0] is locked by token[0]
11:44:07,830 [main] DEBUG Script : script input: {node=Node(node1), param=200, token=Token(/to node1), task=null, executionContext=ExecutionContext[Token(/to node1)], taskInstance=null}
经过node1
11:44:07,861 [main] DEBUG Script : script output: {}
11:44:07,861 [main] DEBUG Token : token[0] is unlocked by token[0]
11:44:07,861 [main] DEBUG GraphElement : event 'node-leave' on 'Node(node1)' for 'Token(/to node1)'
11:44:07,861 [main] DEBUG GraphElement : event 'transition' on 'Transition(to join)' for 'Token(/to node1)'
11:44:07,861 [main] DEBUG GraphElement : event 'node-enter' on 'Join(join)' for 'Token(/to node1)'
11:44:07,861 [main] DEBUG Join : join will not yet reactivate parent: found concurrent token 'Token(/to node2)'
11:44:07,861 [main] DEBUG GraphElement : event 'node-leave' on 'Fork(fork)' for 'Token(/to node2)'
11:44:07,861 [main] DEBUG GraphElement : event 'transition' on 'Transition(to node2)' for 'Token(/to node2)'
11:44:07,861 [main] DEBUG GraphElement : event 'node-enter' on 'Node(node2)' for 'Token(/to node2)'
11:44:07,861 [main] DEBUG GraphElement : executing action 'Script(cfec48)'
11:44:07,861 [main] DEBUG Token : token[0] is locked by token[0]
11:44:07,861 [main] DEBUG Script : script input: {node=Node(node2), param=200, token=Token(/to node2), task=null, executionContext=ExecutionContext[Token(/to node2)], taskInstance=null}
经过node2