流程定義文件
<?xml version="1.0" encoding="UTF-8"?>
<process-definition
xmlns="urn:jbpm.org:jpdl-3.2" name="forkjoin">
<start-state name="開(kāi)始">
<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 結(jié)束" to="結(jié)束"></transition>
</join>
<end-state name="結(jié)束"></end-state>
<node name="node1">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("經(jīng)過(guò)"+node.getName());
</script>
</event>
</node>
<node name="node2">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("經(jīng)過(guò)"+node.getName());
</script>
</event>
</node>
<node name="node3">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("經(jīng)過(guò)"+node.getName());
</script>
</event>
</node>
<node name="node4">
<transition name="to join" to="join"></transition>
<event type="node-enter">
<script>
print("經(jīng)過(guò)"+node.getName());
</script>
</event>
</node>
</process-definition>
工作機(jī)制:是通過(guò)在fork節(jié)點(diǎn)添加<script></script>標(biāo)簽,在script內(nèi)部設(shè)置
<variable name="transitionNames" access="write"></variable>切記此處access屬性設(shè)置為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>
上面在流程實(shí)例上下文中定義了一個(gè)變量param,當(dāng)param大于100時(shí),執(zhí)行node1和node2節(jié)點(diǎn)
測(cè)試一下:
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("當(dāng)前節(jié)點(diǎn)是:"+processInstance.getRootToken().getNode().getName());
processInstance.signal();
System.err.println("當(dāng)前節(jié)點(diǎn)是:"+processInstance.getRootToken().getNode().getName());
}
}
運(yùn)行結(jié)果
當(dāng)前節(jié)點(diǎn)是:開(kāi)始
11:44:07,703 [main] DEBUG GraphElement : event 'before-signal' on 'StartState(開(kāi)始)' for 'Token(/)'
11:44:07,703 [main] DEBUG GraphElement : event 'node-leave' on 'StartState(開(kāi)始)' 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}
經(jīng)過(guò)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}
經(jīng)過(guò)node2