jbpm 与工作流模式 多路聚合(Multiple Merge)

多路聚合(Multiple Merge)

Description :    A point in a workow process where two or more branches reconverge without
synchronization. If more than one branch gets activated, possibly concurrently, the activity
following the merge is started for every activation of every incoming branch.

描述:    多路合并是指在流程中某点,两条或更多分支无同步再收敛。若多于一个分支被激活,可能同时被激活,任务后的合并对于每条流入的激活分支都响应一次。

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.Token;  
  28. import org.jbpm.graph.node.Merge;  
  29.   
  30. /** 
  31.  * http://is.tm.tue.nl/research/patterns/download/swf/pat_8.swf 
  32.  */  
  33. public class Wfp08MultiMergeTest extends TestCase {  
  34.   
  35.   private static ProcessDefinition multiMergeProcessDefinition = createMultiMergeProcessDefinition();  
  36.   
  37.   public static ProcessDefinition createMultiMergeProcessDefinition() {  
  38.     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(  
  39.       "<process-definition>" +  
  40.       "  <start-state name='start'>" +  
  41.       "    <transition to='a' />" +  
  42.       "  </start-state>" +  
  43.       "  <state name='a'>" +  
  44.       "    <transition to='multichoice' />" +  
  45.       "  </state>" +  
  46.       "  <fork name='multichoice'>" +  
  47.       "    <script>" +  
  48.       "      <variable name='transitionNames' access='write' />" +  
  49.       "      <expression>" +  
  50.       "        transitionNames = new ArrayList();" +  
  51.       "        if ( scenario == 1 ) {" +  
  52.       "          transitionNames.add( \"to b\" );" +  
  53.       "        } else if ( scenario == 2 ) {" +  
  54.       "          transitionNames.add( \"to c\" );" +  
  55.       "        } else if ( scenario >= 3 ) {" +  
  56.       "          transitionNames.add( \"to b\" );" +  
  57.       "          transitionNames.add( \"to c\" );" +  
  58.       "        }" +  
  59.       "      </expression>" +  
  60.       "    </script>" +  
  61.       "    <transition name='to b' to='b' />" +  
  62.       "    <transition name='to c' to='c' />" +  
  63.       "  </fork>" +  
  64.       "  <state name='b'>" +  
  65.       "    <transition to='multimerge' />" +  
  66.       "  </state>" +  
  67.       "  <state name='c'>" +  
  68.       "    <transition to='multimerge' />" +  
  69.       "  </state>" +  
  70.       "  <merge name='multimerge'>" +  
  71.       "    <transition to='d' />" +  
  72.       "  </merge>" +  
  73.       "  <state name='d' />" +  
  74.       "</process-definition>"  
  75.     );  
  76.             
  77.     return processDefinition;  
  78.   }  
  79.   
  80.   public void testMultiMergeScenario1() {  
  81.     ProcessDefinition pd = multiMergeProcessDefinition;  
  82.     Token root = Wfp06MultiChoiceTest.executeScenario(pd,1);  
  83.     Token tokenB = root.getChild("to b");   
  84.     tokenB.signal();  
  85.     assertSame( pd.getNode("d"), tokenB.getNode() );  
  86.   }  
  87.     
  88.   public void testMultiMergeScenario2() {  
  89.     ProcessDefinition pd = multiMergeProcessDefinition;  
  90.     Token root = Wfp06MultiChoiceTest.executeScenario(pd,2);  
  91.     Token tokenC = root.getChild("to c");   
  92.     tokenC.signal();  
  93.     assertSame( pd.getNode("d"), tokenC.getNode() );  
  94.   }  
  95.     
  96.   private static ProcessDefinition synchronizingMultiMergeProcessDefinition = createSynchronizingMultiMergeProcessDefinition();  
  97.   
  98.   public static ProcessDefinition createSynchronizingMultiMergeProcessDefinition() {  
  99.     ProcessDefinition pd = createMultiMergeProcessDefinition();  
  100.   
  101.     // get the multimerge handler  
  102.     Merge merge = (Merge) pd.getNode("multimerge");  
  103.     merge.setSynchronized( true );  
  104.       
  105.     return pd;  
  106.   }  
  107.   
  108.   public void testMultiMergeScenario3() {  
  109.     ProcessDefinition pd = synchronizingMultiMergeProcessDefinition;  
  110.     Token root = Wfp06MultiChoiceTest.executeScenario(pd,3);  
  111.     Token tokenB = root.getChild("to b");   
  112.     Token tokenC = root.getChild("to c");  
  113.       
  114.     tokenB.signal();  
  115.     assertSame( pd.getNode("multimerge"), tokenB.getNode() );  
  116.     assertSame( pd.getNode("c"), tokenC.getNode() );  
  117.       
  118.     tokenC.signal();  
  119.     assertSame( pd.getNode("d"), tokenB.getNode() );  
  120.     assertSame( pd.getNode("d"), tokenC.getNode() );  
  121.   }  
  122.     
  123.   public void testMultiMergeScenario4() {  
  124.     ProcessDefinition pd = synchronizingMultiMergeProcessDefinition;  
  125.     Token root = Wfp06MultiChoiceTest.executeScenario(pd,4);  
  126.     Token tokenB = root.getChild("to b");   
  127.     Token tokenC = root.getChild("to c");  
  128.       
  129.     tokenC.signal();  
  130.     assertSame( pd.getNode("b"), tokenB.getNode() );  
  131.     assertSame( pd.getNode("multimerge"), tokenC.getNode() );  
  132.       
  133.     tokenB.signal();  
  134.     assertSame( pd.getNode("d"), tokenB.getNode() );  
  135.     assertSame( pd.getNode("d"), tokenC.getNode() );  
  136.   }  
  137.   
  138.   public void testMultiMergeScenario5() {  
  139.     ProcessDefinition pd = multiMergeProcessDefinition;  
  140.     Token root = Wfp06MultiChoiceTest.executeScenario(pd,5);  
  141.     Token tokenB = root.getChild("to b");   
  142.     Token tokenC = root.getChild("to c");  
  143.       
  144.     tokenB.signal();  
  145.     assertSame( pd.getNode("d"), tokenB.getNode() );  
  146.     assertSame( pd.getNode("c"), tokenC.getNode() );  
  147.       
  148.     tokenC.signal();  
  149.     assertSame( pd.getNode("d"), tokenB.getNode() );  
  150.     assertSame( pd.getNode("d"), tokenC.getNode() );  
  151.   }  
  152.   
  153.   public void testMultiMergeScenario6() {  
  154.     ProcessDefinition pd = multiMergeProcessDefinition;  
  155.     Token root = Wfp06MultiChoiceTest.executeScenario(pd,6);  
  156.     Token tokenB = root.getChild("to b");   
  157.     Token tokenC = root.getChild("to c");  
  158.       
  159.     tokenC.signal();  
  160.     assertSame( pd.getNode("b"), tokenB.getNode() );  
  161.     assertSame( pd.getNode("d"), tokenC.getNode() );  
  162.       
  163.     tokenB.signal();  
  164.     assertSame( pd.getNode("d"), tokenB.getNode() );  
  165.     assertSame( pd.getNode("d"), tokenC.getNode() );  
  166.   }  
  167. }  


流程定义文件:


xml 代码
 
  1. <process-definition>  
  2.       <start-state name='start'>  
  3.         <transition to='a' />  
  4.       </start-state>  
  5.       <state name='a'>  
  6.         <transition to='multichoice' />  
  7.       </state>  
  8.       <fork name='multichoice'>  
  9.         <script>  
  10.           <variable name='transitionNames' access='write' />  
  11.           <expression>  
  12.             transitionNames = new ArrayList();  
  13.             if ( scenario == 1 ) {  
  14.               transitionNames.add( \"to b\" );  
  15.             } else if ( scenario == 2 ) {  
  16.               transitionNames.add( \"to c\" );  
  17.             } else if ( scenario >= 3 ) {  
  18.               transitionNames.add( \"to b\" );  
  19.               transitionNames.add( \"to c\" );  
  20.             }  
  21.           </expression>  
  22.         </script>  
  23.         <transition name='to b' to='b' />  
  24.         <transition name='to c' to='c' />  
  25.       </fork>  
  26.       <state name='b'>  
  27.         <transition to='multimerge' />  
  28.       </state>  
  29.       <state name='c'>  
  30.         <transition to='multimerge' />  
  31.       </state>  
  32.       <merge name='multimerge'>  
  33.         <transition to='d' />  
  34.       </merge>  
  35.       <state name='d' />  
  36.  </process-definition>  


testMultiMergeScenario1()

节点流程执行顺序:

    start -> a --> multichoice --> b --> multimerge --> d

testMultiMergeScenario2()

节点流程执行顺序:

    start -> a --> multichoice --> c  --> multimerge--> d

testMultiMergeScenario3()
注意该方法,在multimerge 会同步等待
节点流程执行顺序:

    start -> a --> multichoice --> b  --> multimerge--> d
                 multichoice --> c  --> multimerge--> d

testMultiMergeScenario4()
注意该方法,在multimerge 会同步等待
节点流程执行顺序:

    start -> a --> multichoice --> b  --> multimerge--> d
                 multichoice --> c  --> multimerge--> d

testMultiMergeScenario5()

节点流程执行顺序:

    start -> a --> multichoice --> b  --> multimerge--> d
                 multichoice --> c  --> multimerge--> d

testMultiMergeScenario6()

节点流程执行顺序:

    start -> a --> multichoice --> b  --> multimerge--> d
                 multichoice --> c  --> multimerge--> d

注意该类中同步设置.
 Merge merge = (Merge) pd.getNode("multimerge");
    merge.setSynchronized( true );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值