jbpm与工作流模式 基本控制模式
 
2008-11-26 作者:chenjin 来源:javaeye.com
 

工作流模式

本文以jdpl/jar/src/test/java   jbpm3 关于workflow patterns 说明 jbpm 对 workflow patterns 的实现示例.

基本控制模式 (Basic Control Flow Patterns)

顺序 (sequence)

Description : An activity in a workow process is enabled after the completion of another
                            activity in the same process.

描述: 同一个流程中, 一个活动在其他活动完成以后才能发生. 

同义词 :Sequential routing, serial routing.

java 代码  

  1. /* 
  2.  * JBoss, Home of Professional Open Source 
  3.  * Copyright 2005, JBoss Inc., and individual contributors as indicated 
  4.  * by the @authors tag. See the copyright.txt in the distribution for a 
  5.  * full listing of individual contributors. 
  6.  * 
  7.  * This is free software; you can redistribute it and/or modify it 
  8.  * under the terms of the GNU Lesser General Public License as 
  9.  * published by the Free Software Foundation; either version 2.1 of 
  10.  * the License, or (at your option) any later version. 
  11.  * 
  12.  * This software is distributed in the hope that it will be useful, 
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  15.  * Lesser General Public License for more details. 
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public 
  18.  * License along with this software; if not, write to the Free 
  19.  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
  20.  * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
  21.  */  
  22. package org.jbpm.jpdl.patterns;  
  23.   
  24. import junit.framework.TestCase;  
  25.   
  26. import org.jbpm.graph.def.ProcessDefinition;  
  27. import org.jbpm.graph.exe.ProcessInstance;  
  28. import org.jbpm.graph.exe.Token;  
  29.   
  30. /** 
  31.  * http://is.tm.tue.nl/research/patterns/download/swf/pat_1.swf 
  32.  */  
  33. public class Wfp01SequenceTest extends TestCase {  
  34.   
  35.   public void testSequence() {  
  36.     // create a simple definition with 3 states in sequence  
  37.     ProcessDefinition pd = ProcessDefinition.parseXmlString(  
  38.       " "  +  
  39.       "   +  
  40.       "     +  
  41.       "  " +  
  42.       "   +  
  43.       "     +  
  44.       "  " +  
  45.       "   +  
  46.       "     +  
  47.       "  " +  
  48.       "   +  
  49.       "     +  
  50.       "  " +  
  51.       "   +  
  52.       ""  
  53.     );  
  54.   
  55.     ProcessInstance pi = new ProcessInstance( pd );  
  56.     pi.signal();  
  57.     Token token = pi.getRootToken();  
  58.     assertSame( pd.getNode("a"), token.getNode() );  
  59.       
  60.     token.signal();  
  61.     assertSame( pd.getNode("b"), token.getNode() );  
  62.   
  63.     token.signal();  
  64.     assertSame( pd.getNode("c"), token.getNode() );  
  65.   
  66.     token.signal();  
  67.     assertSame( pd.getNode("end"), token.getNode() );  
  68.   }  
  69. }  

流程定义文件如下

xml 代码

  1. <process-definition>  
  2.     <start-state name='start'>  
  3.         <transition to='a' />  
  4.     start-state>  
  5.     <state name='a'>  
  6.         <transition to='b' />  
  7.     state>  
  8.     <state name='b'>  
  9.         <transition to='c' />  
  10.      state>  
  11.     <state name='c'>  
  12.         <transition to='end' />  
  13.      state>  
  14.     <end-state name='end' />  
  15. process-definition>

节点流程      start --> a --> b --> c --> end

本例中包括start , a , b ,c ,end 四个node.  token.signal() 可以理解为流程中当前节点(token对应节点) 活动结束. 代码一目了然. 不需要过多解释.

并行分支(Parallet Split) 

Description :    A point in the workow process where a single thread of control splits into multiple threads of control which can be executed in parallel, thus allowing activities to be executed simultaneously or in any order.

描述 : 在分支点上,一个工作流程执行线程 分出多个执行线程同时并行执行, 多个分支线程可以同时以任意顺序执行.

同义词:AND-split, parallel routing, fork.

java 代码

  1. /* 
  2.  * JBoss, Home of Professional Open Source 
  3.  * Copyright 2005, JBoss Inc., and individual contributors as indicated 
  4.  * by the @authors tag. See the copyright.txt in the distribution for a 
  5.  * full listing of individual contributors. 
  6.  * 
  7.  * This is free software; you can redistribute it and/or modify it 
  8.  * under the terms of the GNU Lesser General Public License as 
  9.  * published by the Free Software Foundation; either version 2.1 of 
  10.  * the License, or (at your option) any later version. 
  11.  * 
  12.  * This software is distributed in the hope that it will be useful, 
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  15.  * Lesser General Public License for more details. 
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public 
  18.  * License along with this software; if not, write to the Free 
  19.  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
  20.  * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
  21.  */  
  22. package org.jbpm.jpdl.patterns;  
  23.   
  24. import junit.framework.TestCase;  
  25.   
  26. import org.jbpm.graph.def.ProcessDefinition;  
  27. import org.jbpm.graph.exe.ProcessInstance;  
  28. import org.jbpm.graph.exe.Token;  
  29.   
  30. /** 
  31.  * http://is.tm.tue.nl/research/patterns/download/swf/pat_2.swf 
  32.  */  
  33. public class Wfp02ParallelSplitTest extends TestCase {  
  34.   
  35.   public void testParallelSplit() {  
  36.     ProcessDefinition pd = ProcessDefinition.parseXmlString(  
  37.       " "  +  
  38.       "   +  
  39.       "     +  
  40.       "  " +  
  41.       "   +  
  42.       "     +  
  43.       "     +  
  44.       "  " +  
  45.       "   +  
  46.       "   +  
  47.       ""  
  48.     );  
  49.   
  50.     ProcessInstance pi = new ProcessInstance( pd );  
  51.     pi.signal();  
  52.     Token root = pi.getRootToken();  
  53.     assertNotNull( root );  
  54.       
  55.     Token firstToken = root.getChild( "first" );  
  56.     assertNotNull( firstToken );  
  57.     assertSame( pd.getNode("a"), firstToken.getNode() );  
  58.       
  59.     Token secondToken = root.getChild( "second" );  
  60.     assertNotNull( secondToken );  
  61.     assertSame( pd.getNode("b"), secondToken.getNode() );  
  62.   }  
  63. }  

流程定义文件:

xml 代码

  1. <process-definition>  
  2.     <start-state name='start'>  
  3.         <transition to='and' />  
  4.      start-state>  
  5.     <fork name='and'>  
  6.         <transition name='first' to='a' />  
  7.         <transition name='second' to='b' />  
  8.      fork>  
  9.     <state name='a' />  
  10.     <state name='b' />  
  11. process-definition>"  

节点流程      start --> and -->  a
                                                     -- >  b

本例中and --a   , and --> b  一定回被执行. 所以判断如下
    Token firstToken = root.getChild( "first" );
    assertNotNull( firstToken );

并行分支 (AND-Split ) 多以 同步聚合((Synchronization)  结束.

同步聚合(Synchronization)

Description  : A point in the workow process where multiple parallel subprocesses/activities converge into one single thread of control, thus synchronizing multiple threads. It is an assumption of this pattern that each incoming branch of a synchronizer is executed only once

描述:一个汇聚点, 多路工作流成执行线程汇聚成单个工作流执行线程. 只有汇聚线程都执行完毕,改汇聚点聚合后的单一线程才会执行.       

同义词: AND-join, rendezvous, synchronizer.

java 代码  

  1. /* 
  2.  * JBoss, Home of Professional Open Source 
  3.  * Copyright 2005, JBoss Inc., and individual contributors as indicated 
  4.  * by the @authors tag. See the copyright.txt in the distribution for a 
  5.  * full listing of individual contributors. 
  6.  * 
  7.  * This is free software; you can redistribute it and/or modify it 
  8.  * under the terms of the GNU Lesser General Public License as 
  9.  * published by the Free Software Foundation; either version 2.1 of 
  10.  * the License, or (at your option) any later version. 
  11.  * 
  12.  * This software is distributed in the hope that it will be useful, 
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  15.  * Lesser General Public License for more details. 
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public 
  18.  * License along with this software; if not, write to the Free 
  19.  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
  20.  * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
  21.  */  
  22. package org.jbpm.jpdl.patterns;  
  23.   
  24. import junit.framework.*;  
  25.   
  26. import org.jbpm.graph.def.*;  
  27. import org.jbpm.graph.exe.*;  
  28.   
  29. /** 
  30.  * http://is.tm.tue.nl/research/patterns/download/swf/pat_3.swf 
  31.  */  
  32. public class Wfp03SynchronizationTest extends TestCase {  
  33.   
  34.   public ProcessDefinition createSynchronizationProcessDefinition() {  
  35.     ProcessDefinition pd = ProcessDefinition.parseXmlString(  
  36.       " "  +  
  37.       "   +  
  38.       "     +  
  39.       "  " +  
  40.       "   +  
  41.       "     +  
  42.       "     +  
  43.       "  " +  
  44.       "   +  
  45.       "     +  
  46.       "  " +  
  47.       "   +  
  48.       "     +  
  49.       "  " +  
  50.       "   +  
  51.       "     +  
  52.       "  " +  
  53.       "   +  
  54.       ""  
  55.     );  
  56.     return pd;  
  57.   }  
  58.   
  59.   public void testSynchronizationFirstTokenFirst() {  
  60.     ProcessDefinition pd = createSynchronizationProcessDefinition();  
  61.             
  62.     ProcessInstance pi = new ProcessInstance( pd );  
  63.     pi.signal();  
  64.     Token root = pi.getRootToken();  
  65.     Token firstToken = root.getChild( "first" );  
  66.     Token secondToken = root.getChild( "second" );  
  67.   
  68.     // check that the two tokens are in the states one and two respectively  
  69.     assertSame( pd.getNode("fork"), root.getNode() );  
  70.     assertSame( pd.getNode("one"), firstToken.getNode() );  
  71.     assertSame( pd.getNode("two"), secondToken.getNode() );  
  72.   
  73.     firstToken.signal();  
  74.     assertSame( pd.getNode("fork"), root.getNode() );  
  75.     assertSame( pd.getNode("join"), firstToken.getNode() );  
  76.     assertSame( pd.getNode("two"), secondToken.getNode() );  
  77.       
  78.     secondToken.signal();  
  79.     assertSame( pd.getNode("end"), root.getNode() );  
  80.     assertSame( pd.getNode("join"), firstToken.getNode() );  
  81.     assertSame( pd.getNode("join"), secondToken.getNode() );  
  82.   }  
  83.   
  84.   /** 
  85.    * variation of the pattern where the second token fires first. 
  86.    */  
  87.   public void testSynchronizationSecondTokenFirst() {  
  88.     ProcessDefinition pd = createSynchronizationProcessDefinition();  
  89.             
  90.     ProcessInstance pi = new ProcessInstance( pd );  
  91.     pi.signal();  
  92.     Token root = pi.getRootToken();  
  93.     Token firstToken = root.getChild( "first" );  
  94.     Token secondToken = root.getChild( "second" );  
  95.   
  96.     // check that the two tokens are in the states one and two respectively  
  97.     assertSame( pd.getNode("fork"), root.getNode() );  
  98.     assertSame( pd.getNode("one"), firstToken.getNode() );  
  99.     assertSame( pd.getNode("two"), secondToken.getNode() );  
  100.   
  101.     secondToken.signal();  
  102.     assertSame( pd.getNode("fork"), root.getNode() );  
  103.     assertSame( pd.getNode("one"), firstToken.getNode() );  
  104.     assertSame( pd.getNode("join"), secondToken.getNode() );  
  105.       
  106.     firstToken.signal();  
  107.     assertSame( pd.getNode("end"), root.getNode() );  
  108.     assertSame( pd.getNode("join"), firstToken.getNode() );  
  109.     assertSame( pd.getNode("join"), secondToken.getNode() );  
  110.   }  
  111.   
  112.   /** 
  113.    * nested form of the synchronization pattern. 
  114.    */  
  115.   public ProcessDefinition createNestedSynchronizationProcessDefinition() {  
  116.     // tip : draw this visually if you want to understand it.  
  117.     ProcessDefinition pd = new ProcessDefinition(  
  118.       new String[]{"start-state start",  
  119.                    "fork fork",  
  120.                    "fork fork1",  
  121.                    "fork fork2",  
  122.                    "state state1.1",  
  123.                    "state state1.2",  
  124.                    "state state2.1",  
  125.                    "state state2.2",  
  126.                    "join join2",  
  127.                    "join join1",  
  128.                    "join join",  
  129.                    "end-state end"},   
  130.       new String[]{"start --> fork",  
  131.                    "fork --first--> fork1",  
  132.                    "fork --second--> fork2",  
  133.                    "fork1 --first--> state1.1",  
  134.                    "fork1 --second--> state1.2",  
  135.                    "fork2 --first--> state2.1",  
  136.                    "fork2 --second--> state2.2",  
  137.                    "state1.1 --> join1",  
  138.                    "state1.2 --> join1",  
  139.                    "state2.1 --> join2",  
  140.                    "state2.2 --> join2",  
  141.                    "join1 --> join",  
  142.                    "join2 --> join",  
  143.                    "join --> end"});  
  144.     return pd;  
  145.   }  
  146.   
  147.   public void testSynchronizationNested() {  
  148.     ProcessDefinition pd = createNestedSynchronizationProcessDefinition();  
  149.     ProcessInstance pi = new ProcessInstance( pd );  
  150.     pi.signal();  
  151.     Token rootToken = pi.getRootToken();  
  152.     Token token1 = rootToken.getChild( "first" );  
  153.     Token token2 = rootToken.getChild( "second" );  
  154.     Token token11 = token1.getChild( "first" );  
  155.     Token token12 = token1.getChild( "second" );  
  156.     Token token21 = token2.getChild( "first" );  
  157.     Token token22 = token2.getChild( "second" );  
  158.       
  159.     assertSame( pd.getNode("fork"), rootToken.getNode() );  
  160.     assertSame( pd.getNode("fork1"), token1.getNode() );  
  161.     assertSame( pd.getNode("fork2"), token2.getNode() );  
  162.     assertSame( pd.getNode("state1.1"), token11.getNode() );  
  163.     assertSame( pd.getNode("state1.2"), token12.getNode() );  
  164.     assertSame( pd.getNode("state2.1"), token21.getNode() );  
  165.     assertSame( pd.getNode("state2.2"), token22.getNode() );  
  166.       
  167.     token11.signal();  
  168.   
  169.     assertSame( pd.getNode("fork"), rootToken.getNode() );  
  170.     assertSame( pd.getNode("fork1"), token1.getNode() );  
  171.     assertSame( pd.getNode("fork2"), token2.getNode() );  
  172.     assertSame( pd.getNode("join1"), token11.getNode() );  
  173.     assertSame( pd.getNode("state1.2"), token12.getNode() );  
  174.     assertSame( pd.getNode("state2.1"), token21.getNode() );  
  175.     assertSame( pd.getNode("state2.2"), token22.getNode() );  
  176.   
  177.     token12.signal();  
  178.   
  179.     assertSame( pd.getNode("fork"), rootToken.getNode() );  
  180.     assertSame( pd.getNode("join"), token1.getNode() );  
  181.     assertSame( pd.getNode("fork2"), token2.getNode() );  
  182.     assertSame( pd.getNode("join1"), token11.getNode() );  
  183.     assertSame( pd.getNode("join1"), token12.getNode() );  
  184.     assertSame( pd.getNode("state2.1"), token21.getNode() );  
  185.     assertSame( pd.getNode("state2.2"), token22.getNode() );  
  186.   
  187.     token21.signal();  
  188.   
  189.     assertSame( pd.getNode("fork"), rootToken.getNode() );  
  190.     assertSame( pd.getNode("join"), token1.getNode() );  
  191.     assertSame( pd.getNode("fork2"), token2.getNode() );  
  192.     assertSame( pd.getNode("join1"), token11.getNode() );  
  193.     assertSame( pd.getNode("join1"), token12.getNode() );  
  194.     assertSame( pd.getNode("join2"), token21.getNode() );  
  195.     assertSame( pd.getNode("state2.2"), token22.getNode() );  
  196.   
  197.     token22.signal();  
  198.   
  199.     assertSame( pd.getNode("end"), rootToken.getNode() );  
  200.     assertSame( pd.getNode("join"), token1.getNode() );  
  201.     assertSame( pd.getNode("join"), token2.getNode() );  
  202.     assertSame( pd.getNode("join1"), token11.getNode() );  
  203.     assertSame( pd.getNode("join1"), token12.getNode() );  
  204.     assertSame( pd.getNode("join2"), token21.getNode() );  
  205.     assertSame( pd.getNode("join2"), token22.getNode() );  
  206.   }  
  207. }  

xml 代码  

  1. <process-definition>  
  2.     <start-state name='start'>  
  3.         <transition to='fork' />  
  4.     start-state>  
  5.     <fork name='fork'>  
  6.         <transition name='first' to='one' />  
  7.         <transition name='second' to='two' />  
  8.     fork>  
  9.     <state name='one'>  
  10.         <transition to='join' />  
  11.     state>  
  12.     <state name='two'>  
  13.         <transition to='join' />  
  14.     state>  
  15.     <join name='join'>  
  16.         <transition to='end' />  
  17.     join>  
  18.     <end-state name='end' />  
  19. process-definition>  

其中testSynchronizationFirstTokenFirst()

节点执行顺序为

    start --> fork --> one --> join(先) --> end
                          --> two --> join(后)

其中  firstToken.signal() 后 one --> join 先执行.

在   two --> join 执行技术之前  join --> end 不会执行

testSynchronizationSecondTokenFirst() 与上面方法类似, 只是one 和 two 互换.

testSynchronizationNested()

中createNestedSynchronizationProcessDefinition()

创建如下流程

xml 代码  

  1. <process-definition>  
  2.     <start-state name='start'>  
  3.         <transition to='fork' />  
  4.     start-state>  
  5.     <fork name='fork'>  
  6.         <transition name='first' to='fork1' />  
  7.         <transition name='second' to='fork2' />  
  8.     fork>  
  9.     <fork name='fork1'>  
  10.         <transition name='first' to='state1.1' />  
  11.         <transition name='second' to='state1.2' />  
  12.     fork>  
  13.     <fork name='fork2'>  
  14.         <transition name='first' to='state2.1' />  
  15.         <transition name='second' to='state2.2' />  
  16.     fork>  
  17.     <state name='state1.1'>  
  18.         <transition to='join1' />  
  19.     state>  
  20.     <state name='state1.2'>  
  21.         <transition to='join1' />  
  22.     state>  
  23.     <state name='state2.1'>  
  24.         <transition to='join2' />  
  25.     state>  
  26.     <state name='state2.2'>  
  27.         <transition to='join2' />  
  28.     state>  
  29.     <join name='join'>  
  30.         <transition to='end' />  
  31.     join>  
  32.     <join name='join1'>  
  33.         <transition to='join' />  
  34.     join>  
  35.     <join name='join2'>  
  36.         <transition to='join' />  
  37.     join>  
  38.     <end-state name='end' />  
  39. process-definition>  

节点执行顺序:

    start --> fork --> fork1 --> state1.1 --> join1 --> join --> end
                                          --> state1.2 --> join1
                          --> fork2 --> state2.1 --> join2 --> join
                                          --> state2.2 --> join2

    token11.signal()   token11 到 join1
 
    token12.signal()   token12 到 join1 , token1到join

    token21.signal()   token21 到 join2

     token22.signal()  token22 到 join2 , token2到join

排它选择(Exclusive Choice)

Description:A point in the workow process where a single thread of control splits into
multiple threads of control which can be executed in parallel, thus allowing activities to be
executed simultaneously or in any order.

描述:在流程的某一点,依据工作流控制数据, 从多个分支路径中选定一个路径

同义词:XOR-split, conditional routing, switch, decision.

java 代码

  1. /* 
  2.  * JBoss, Home of Professional Open Source 
  3.  * Copyright 2005, JBoss Inc., and individual contributors as indicated 
  4.  * by the @authors tag. See the copyright.txt in the distribution for a 
  5.  * full listing of individual contributors. 
  6.  * 
  7.  * This is free software; you can redistribute it and/or modify it 
  8.  * under the terms of the GNU Lesser General Public License as 
  9.  * published by the Free Software Foundation; either version 2.1 of 
  10.  * the License, or (at your option) any later version. 
  11.  * 
  12.  * This software is distributed in the hope that it will be useful, 
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  15.  * Lesser General Public License for more details. 
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public 
  18.  * License along with this software; if not, write to the Free 
  19.  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
  20.  * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
  21.  */  
  22. package org.jbpm.jpdl.patterns;  
  23.   
  24. import junit.framework.TestCase;  
  25.   
  26. import org.jbpm.context.def.ContextDefinition;  
  27. import org.jbpm.context.exe.ContextInstance;  
  28. import org.jbpm.graph.def.ProcessDefinition;  
  29. import org.jbpm.graph.exe.ProcessInstance;  
  30. import org.jbpm.graph.exe.Token;  
  31.   
  32. /** 
  33.  * http://is.tm.tue.nl/research/patterns/download/swf/pat_4.swf 
  34.  *  
  35.  *  we make a distinction in the api between process and client based  
  36.  * decisions. the first tests show the situations as described in the  
  37.  * pattern. after that a demonstration of client based decision is  
  38.  * added. 
  39.  *       
  40.  *  
  41.  *  process based  
  42.  * decisions makes use of a decision node.  the node has a piece of  
  43.  * programming logic associated that calculates the leaving transition 
  44.  * name.  the programming logic is executed within the calculation of  
  45.  * the next state of the process instance. 
  46.  * 
  47.  *  
  48.  * client based decisions allow clients to select on of the multiple 
  49.  * transitions that leave the current state. 
  50.  *     
  51.  */  
  52. public class Wfp04ExclusiveChoiceTest extends TestCase {  
  53.     
  54.   static ProcessDefinition exclusiveChoiceProcessDefinition = ProcessDefinition.parseXmlString(  
  55.       " "  +  
  56.       "   +  
  57.       "     +  
  58.       "  " +  
  59.       "   +  
  60.       "     +  
  61.       "  " +  
  62.       "   +  
  63.       "     +  
  64.       "     +  
  65.       "      #{scenario==1}" +  
  66.       "    " +  
  67.       "     +  
  68.       "      #{scenario==2}" +  
  69.       "    " +  
  70.       "  " +  
  71.       "   +  
  72.       "   +  
  73.       "   +  
  74.       "" );  
  75.   static {  
  76.     exclusiveChoiceProcessDefinition.addDefinition( new ContextDefinition() );  
  77.   }  
  78.   
  79.   /** 
  80.    * situation 1 
  81.    */  
  82.   public void testExclusiveChoiceSituation1() {  
  83.     ProcessDefinition pd = exclusiveChoiceProcessDefinition;  
  84.   
  85.     ProcessInstance pi = new ProcessInstance( pd );  
  86.     ContextInstance ci = (ContextInstance) pi.getInstance( ContextInstance.class );  
  87.     pi.signal();  
  88.     Token root = pi.getRootToken();  
  89.   
  90.     assertSame( pd.getNode("a"), root.getNode() );  
  91.       
  92.     ci.setVariable( "scenario"new Integer(1) );  
  93.     root.signal();  
  94.       
  95.     assertSame( pd.getNode("b"), root.getNode() );  
  96.   }  
  97.   
  98.   /** 
  99.    * situation 2 
  100.    */  
  101.   public void testExclusiveChoiceSituation2() {  
  102.     ProcessDefinition pd = exclusiveChoiceProcessDefinition;  
  103.   
  104.     ProcessInstance pi = new ProcessInstance( pd );  
  105.     ContextInstance ci = (ContextInstance) pi.getInstance( ContextInstance.class );  
  106.     pi.signal();  
  107.     Token root = pi.getRootToken();  
  108.   
  109.     assertSame( pd.getNode("a"), root.getNode() );  
  110.       
  111.     ci.setVariable( "scenario"new Integer(2) );  
  112.     root.signal();  
  113.       
  114.     assertSame( pd.getNode("c"), root.getNode() );  
  115.   }  
  116.   
  117. }  

流程定义文件:

    xml 代码

  1. <process-definition>  
  2.     <start-state name='start'>  
  3.         <transition to='a' />  
  4.     start-state>  
  5.     <state name='a'>  
  6.         <transition to='xor' />  
  7.     state>  
  8.     <decision name='xor'>  
  9.         <transition name='forget about it' to='d' />  
  10.         <transition name='urgent' to='b'>  
  11.             <condition>#{scenario==1}condition>  
  12.         transition>  
  13.         <transition name='dont care' to='c'>  
  14.             <condition>#{scenario==2}condition>  
  15.         transition>  
  16.     decision>  
  17.     <state name='b' />  
  18.     <state name='c' />  
  19.     <state name='d' />  
  20. process-definition>  

节点执行顺序:

start --> a --> xor --> d
                             xor --> b
                             xor --> c
  其中 xor --> d , xor --> b , xor--> c 只能从中选一.
  当scenario==1 执行 xor --> b,  当 scenario==2 执行xor --> c , 其他情况(默认)执行xor --> d

代码中根据变量scenario , 判断流程转向, 比较直观,不再详述.

简单聚合(Simple Merge)

Description :  A point in the workow process where two or more alternative branches come together
without synchronization. It is an assumption of this pattern that none of the alternative
branches is ever executed in parallel

描述: 在流程中某一点,将两个或更多可选分支聚合而不同步.

同义词: XOR-join, asynchronous join, merge.

好像源代码功能有点问题.

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.jpdl.patterns;

import junit.framework.*;

import org.jbpm.graph.def.*;
import org.jbpm.graph.exe.*;

/**
 * http://is.tm.tue.nl/research/patterns/download/swf/pat_5.swf
 *
 * <p>in jbpm every node has an implicit merge in front of it.
 * so it's not necessary to use the merge node.  in fact, i can
 * not think of a situation where implicit merge nodeMap are not
 * sufficient.  for the sake of workflow patterns, we leave if in.
 * jbpm supports merging of both alternative paths of execution
 * and concurrent paths of execution.
 * </p>
 *
 * <p>first the merge node is demonstrated exactly as in the pattern.
 * then the implicit variant is demonstrated, then the merging of
 * concurrent paths is demonstrated.
 * </p>
 */
public class Wfp05SimpleMergeTest extends TestCase {

  private static ProcessDefinition simpleMergeProcessDefinition = createSimpleMergeProcessDefinition();
 
  public static ProcessDefinition createSimpleMergeProcessDefinition() {
    ProcessDefinition pd = new ProcessDefinition(
      new String[]{"start-state start",
                   "state a",
                   "state b",
                   "merge xor",
                   "state c"},
      new String[]{"start --to a--> a",
                   "start --to b--> b",
                   "a --> xor",
                   "b --> xor",
                   "xor --> c"});
    return pd;
  }

  public void testSimpleMergeScenario1() {
    ProcessDefinition pd = simpleMergeProcessDefinition;
    ProcessInstance pi = new ProcessInstance( pd );
    pi.signal("to a");
    Token root = pi.getRootToken();
    assertSame( pd.getNode("a"), root.getNode() );
    root.signal();
    assertSame( pd.getNode("c"), root.getNode() );
  }

  public void testSimpleMergeScenario2() {
    ProcessDefinition pd = simpleMergeProcessDefinition;
    ProcessInstance pi = new ProcessInstance( pd );
    pi.signal("to b");
    Token root = pi.getRootToken();
    assertSame( pd.getNode("b"), root.getNode() );
    root.signal();
    assertSame( pd.getNode("c"), root.getNode() );
  }
 
  private static ProcessDefinition implicitMergeProcessDefinition = createImplicitMergeProcessDefinition();
 
  public static ProcessDefinition createImplicitMergeProcessDefinition() {
    ProcessDefinition pd = new ProcessDefinition(
      new String[]{"start-state start",
                   "state a",
                   "state b",
                   "state c"},
      new String[]{"start --to a--> a",
                   "start --to b--> b",
                   "a --> c",
                   "b --> c"});
    return pd;
  }

  public void testImplicitMergeScenario1() {
    ProcessDefinition pd = implicitMergeProcessDefinition;
    ProcessInstance pi = new ProcessInstance( implicitMergeProcessDefinition );
    pi.signal("to a");
    Token root = pi.getRootToken();
    assertSame( pd.getNode("a"), root.getNode() );
    root.signal();
    assertSame( pd.getNode("c"), root.getNode() );
  }
 
  public void testImplicitMergeScenario2() {
    ProcessDefinition pd = implicitMergeProcessDefinition;
    ProcessInstance pi = new ProcessInstance( implicitMergeProcessDefinition );
    pi.signal("to b");
    Token root = pi.getRootToken();
    assertSame( pd.getNode("b"), root.getNode() );
    root.signal();
    assertSame( pd.getNode("c"), root.getNode() );
  }
 
}

createSimpleMergeProcessDefinition()

创建如下流程定义文件

xml 代码

  1. <process-definition name="process">  
  2.     <start-state name='start'>  
  3.         <transition name='to a' to='a' />  
  4.         <transition name='to b' to='b' />  
  5.     </start-state>  
  6.     <state name='a'>  
  7.         <transition to='xor' />  
  8.     </state>  
  9.     <state name='b'>  
  10.         <transition to='xor' />  
  11.     </state>  
  12.     <state name='xor'>  
  13.         <transition to='c' />  
  14.     </state>  
  15.     <state name='c' />  
  16. </process-definition>  
testSimpleMergeScenario1()

节点执行顺序
   
    start --> a --> xor --> c

testSimpleMergeScenario2()
 
    start --> b --> xor --> c

createImplicitMergeProcessDefinition()

创建如下流程定义文件

xml 代码
  1. <process-definition name="process">  
  2.     <start-state name='start'>  
  3.         <transition name='to a' to='a' />  
  4.         <transition name='to b' to='b' />  
  5.     </start-state>  
  6.     <state name='a'>  
  7.         <transition to='c' />  
  8.     </state>  
  9.     <state name='b'>  
  10.         <transition to='c' />  
  11.     </state>  
  12.     <state name='c' />  
  13. </process-definition>  
testImplicitMergeScenario1()
节点执行顺序

  start --> a --> c

testImplicitMergeScenario2()
节点执行顺序

  start --> b --> c

火龙果软件/UML软件工程组织致力于提高您的软件工程实践能力,我们不断地吸取业界的宝贵经验,向您提供经过数百家企业验证的有效的工程技术实践经验,同时关注最新的理论进展,帮助您“领跑您所在行业的软件世界”。
资源网站: UML软件工程组织