KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > patterns > Wfp16MiWithoutAPrioriRuntimeKnowledge


1 package org.jbpm.jpdl.patterns;
2
3 import junit.framework.TestCase;
4
5 import org.jbpm.graph.def.ActionHandler;
6 import org.jbpm.graph.def.Node;
7 import org.jbpm.graph.def.ProcessDefinition;
8 import org.jbpm.graph.exe.ExecutionContext;
9 import org.jbpm.graph.exe.ProcessInstance;
10 import org.jbpm.graph.exe.Token;
11 import org.jbpm.taskmgmt.def.Task;
12 import org.jbpm.taskmgmt.def.TaskMgmtDefinition;
13 import org.jbpm.taskmgmt.exe.TaskInstance;
14 import org.jbpm.taskmgmt.exe.TaskMgmtInstance;
15
16 /**
17  * http://is.tm.tue.nl/research/patterns/download/swf/pat_15.swf
18  */

19 public class Wfp16MiWithoutAPrioriRuntimeKnowledge extends TestCase {
20   
21   public static int scenario = -1;
22   
23   public static class CreateTasks implements ActionHandler {
24     private static final long serialVersionUID = 1L;
25     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
26       TaskMgmtDefinition tmd = (TaskMgmtDefinition) executionContext.getDefinition(TaskMgmtDefinition.class);
27       Task task = tmd.getTask("watch movie amadeus");
28     
29       // create as many task instances as the scenario prescribes :
30
// 0 tasks for scenario 1
31
// 1 task for scenario 2
32
// 2 tasks for scenario 3
33
// 3 tasks for scenario 4
34
TaskMgmtInstance tmi = executionContext.getTaskMgmtInstance();
35       for (int i = 1; i<scenario; i++) {
36         tmi.createTaskInstance(task, executionContext.getToken());
37       }
38     }
39   }
40
41   public static ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
42     "<process-definition>" +
43     " <start-state name='a'>" +
44     " <transition to='b' />" +
45     " </start-state>" +
46     " <state name='b'>" +
47     " <transition to='t' />" +
48     " </state>" +
49     " <task-node name='t' create-tasks='false' signal='last-wait'>" +
50     " <event type='node-enter'>" +
51     " <action class='org.jbpm.jpdl.patterns.Wfp16MiWithoutAPrioriRuntimeKnowledge$CreateTasks' />" +
52     " </event>" +
53     " <task name='watch movie amadeus' />" +
54     " <transition to='c' />" +
55     " </task-node>" +
56     " <state name='c' />" +
57     "</process-definition>"
58   );
59   
60   public static Node t = processDefinition.getNode("t");
61   public static Node c = processDefinition.getNode("c");
62
63   public void testAprioriRuntimeKnowledgeScenario1() {
64     scenario = 1;
65     
66     ProcessInstance processInstance = new ProcessInstance(processDefinition);
67     Token token = processInstance.getRootToken();
68     processInstance.signal();
69     processInstance.signal();
70     
71     assertSame(t, token.getNode());
72     assertEquals(0, getNbrOfTasks(token));
73     
74     addOneTask(token);
75
76     assertSame(t, token.getNode());
77     assertEquals(1, getNbrOfTasks(token));
78     
79     addOneTask(token);
80
81     assertSame(t, token.getNode());
82     assertEquals(2, getNbrOfTasks(token));
83     
84     endOneTask(token);
85
86     assertSame(t, token.getNode());
87     assertEquals(1, getNbrOfTasks(token));
88     
89     endOneTask(token);
90
91     assertSame(c, token.getNode());
92     assertEquals(0, getNbrOfTasks(token));
93   }
94
95   public void testAprioriRuntimeKnowledgeScenario2() {
96     scenario = 2;
97     
98     ProcessInstance processInstance = new ProcessInstance(processDefinition);
99     Token token = processInstance.getRootToken();
100     processInstance.signal();
101     processInstance.signal();
102     
103     assertSame(t, token.getNode());
104     assertEquals(1, getNbrOfTasks(token));
105     
106     addOneTask(token);
107
108     assertSame(t, token.getNode());
109     assertEquals(2, getNbrOfTasks(token));
110     
111     endOneTask(token);
112
113     assertSame(t, token.getNode());
114     assertEquals(1, getNbrOfTasks(token));
115     
116     endOneTask(token);
117
118     assertSame(c, token.getNode());
119     assertEquals(0, getNbrOfTasks(token));
120   }
121
122   public void testAprioriRuntimeKnowledgeScenario3() {
123     scenario = 3;
124     
125     ProcessInstance processInstance = new ProcessInstance(processDefinition);
126     Token token = processInstance.getRootToken();
127     processInstance.signal();
128     processInstance.signal();
129     
130     assertSame(t, token.getNode());
131     assertEquals(2, getNbrOfTasks(token));
132     
133     addOneTask(token);
134
135     assertSame(t, token.getNode());
136     assertEquals(3, getNbrOfTasks(token));
137     
138     endOneTask(token);
139     endOneTask(token);
140
141     assertSame(t, token.getNode());
142     assertEquals(1, getNbrOfTasks(token));
143     
144     endOneTask(token);
145
146     assertSame(c, token.getNode());
147     assertEquals(0, getNbrOfTasks(token));
148   }
149
150   public void testAprioriRuntimeKnowledgeScenario4() {
151     scenario = 4;
152     
153     ProcessInstance processInstance = new ProcessInstance(processDefinition);
154     Token token = processInstance.getRootToken();
155     processInstance.signal();
156     processInstance.signal();
157     
158     assertSame(t, token.getNode());
159     assertEquals(3, getNbrOfTasks(token));
160     
161     addOneTask(token);
162
163     assertSame(t, token.getNode());
164     assertEquals(4, getNbrOfTasks(token));
165     
166     endOneTask(token);
167     endOneTask(token);
168     endOneTask(token);
169
170     assertSame(t, token.getNode());
171     assertEquals(1, getNbrOfTasks(token));
172     
173     endOneTask(token);
174
175     assertSame(c, token.getNode());
176     assertEquals(0, getNbrOfTasks(token));
177   }
178
179   private void addOneTask(Token token) {
180     TaskMgmtDefinition tmd = (TaskMgmtDefinition) processDefinition.getDefinition(TaskMgmtDefinition.class);
181     Task task = tmd.getTask("watch movie amadeus");
182     
183     TaskMgmtInstance tmi = token.getProcessInstance().getTaskMgmtInstance();
184     tmi.createTaskInstance(task, token);
185   }
186
187   private int getNbrOfTasks(Token token) {
188     TaskMgmtInstance tmi = (TaskMgmtInstance) token.getProcessInstance().getInstance(TaskMgmtInstance.class);
189     return tmi.getUnfinishedTasks(token).size();
190   }
191
192   public static void endOneTask(Token token) {
193     TaskMgmtInstance tmi = (TaskMgmtInstance)token.getProcessInstance().getInstance(TaskMgmtInstance.class);
194     TaskInstance taskInstance = (TaskInstance) tmi.getUnfinishedTasks(token).iterator().next();
195     taskInstance.end();
196   }
197 }
198
Popular Tags