KickJava   Java API By Example, From Geeks To Geeks.

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


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_14.swf
18  */

19 public class Wfp15MiWithAPrioriRuntimeKnowledgeTest 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'>" +
50     " <event type='node-enter'>" +
51     " <action class='org.jbpm.jpdl.patterns.Wfp15MiWithAPrioriRuntimeKnowledgeTest$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(c, token.getNode());
72   }
73
74   public void testAprioriRuntimeKnowledgeScenario2() {
75     scenario = 2;
76     
77     ProcessInstance processInstance = new ProcessInstance(processDefinition);
78     Token token = processInstance.getRootToken();
79     processInstance.signal();
80     processInstance.signal();
81     
82     assertSame(t, token.getNode());
83     
84     endOneTask(token);
85
86     assertSame(c, token.getNode());
87   }
88
89   public void testAprioriRuntimeKnowledgeScenario3() {
90     scenario = 3;
91     
92     ProcessInstance processInstance = new ProcessInstance(processDefinition);
93     Token token = processInstance.getRootToken();
94     processInstance.signal();
95     processInstance.signal();
96     
97     assertSame(t, token.getNode());
98     
99     endOneTask(token);
100
101     assertSame(t, token.getNode());
102     
103     endOneTask(token);
104
105     assertSame(c, token.getNode());
106   }
107
108   public void testAprioriRuntimeKnowledgeScenario4() {
109     scenario = 4;
110     
111     ProcessInstance processInstance = new ProcessInstance(processDefinition);
112     Token token = processInstance.getRootToken();
113     processInstance.signal();
114     processInstance.signal();
115     
116     assertSame(t, token.getNode());
117     
118     endOneTask(token);
119
120     assertSame(t, token.getNode());
121     
122     endOneTask(token);
123
124     assertSame(t, token.getNode());
125     
126     endOneTask(token);
127
128     assertSame(c, token.getNode());
129   }
130
131   public static void endOneTask(Token token) {
132     TaskMgmtInstance tmi = (TaskMgmtInstance)token.getProcessInstance().getInstance(TaskMgmtInstance.class);
133     TaskInstance taskInstance = (TaskInstance) tmi.getUnfinishedTasks(token).iterator().next();
134     taskInstance.end();
135   }
136 }
137
Popular Tags