KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > node > TaskNode


1 package org.jbpm.graph.node;
2
3 import java.util.*;
4
5 import org.dom4j.Element;
6 import org.jbpm.graph.def.*;
7 import org.jbpm.graph.exe.*;
8 import org.jbpm.jpdl.xml.*;
9 import org.jbpm.taskmgmt.def.*;
10 import org.jbpm.taskmgmt.exe.*;
11
12 /**
13  * is a node that relates to one or more tasks.
14  * Property <code>signal</code> specifies how task completion
15  * triggers continuation of execution.
16  */

17 public class TaskNode extends Node implements Parsable {
18
19   private static final long serialVersionUID = 1L;
20
21   /**
22    * execution always continues, regardless wether tasks are created or still unfinished.
23    */

24   public static final int SIGNAL_UNSYNCHRONIZED = 0;
25   /**
26    * execution never continues, regardless wether tasks are created or still unfinished.
27    */

28   public static final int SIGNAL_NEVER = 1;
29   /**
30    * proceeds execution when the first task instance is completed.
31    * when no tasks are created on entrance of this node, execution is continued.
32    */

33   public static final int SIGNAL_FIRST = 2;
34   /**
35    * proceeds execution when the first task instance is completed.
36    * when no tasks are created on entrance of this node, execution is continued.
37    */

38   public static final int SIGNAL_FIRST_WAIT = 3;
39   /**
40    * proceeds execution when the last task instance is completed.
41    * when no tasks are created on entrance of this node, execution waits in the task node till tasks are created.
42    */

43   public static final int SIGNAL_LAST = 4;
44   /**
45    * proceeds execution when the last task instance is completed.
46    * when no tasks are created on entrance of this node, execution waits in the task node till tasks are created.
47    */

48   public static final int SIGNAL_LAST_WAIT = 5;
49   
50   public static int parseSignal(String JavaDoc text) {
51     if ("unsynchronized".equalsIgnoreCase(text)) {
52       return SIGNAL_UNSYNCHRONIZED;
53     } else if ("never".equalsIgnoreCase(text)) {
54       return SIGNAL_NEVER;
55     } else if ("first".equalsIgnoreCase(text)) {
56       return SIGNAL_FIRST;
57     } else if ("first-wait".equalsIgnoreCase(text)) {
58       return SIGNAL_FIRST_WAIT;
59     } else if ("last-wait".equalsIgnoreCase(text)) {
60       return SIGNAL_LAST_WAIT;
61     } else { // return default
62
return SIGNAL_LAST;
63     }
64   }
65   
66   public static String JavaDoc signalToString(int signal) {
67     if (signal==SIGNAL_UNSYNCHRONIZED) {
68       return "unsynchronized";
69     } else if (signal==SIGNAL_NEVER) {
70       return "never";
71     } else if (signal==SIGNAL_FIRST) {
72       return "first";
73     } else if (signal==SIGNAL_FIRST_WAIT) {
74       return "first-wait";
75     } else if (signal==SIGNAL_LAST) {
76       return "last";
77     } else if (signal==SIGNAL_LAST_WAIT) {
78       return "last-wait";
79     } else {
80       return null;
81     }
82   }
83   
84   private long id = 0;
85   private Set tasks = null;
86   private int signal = SIGNAL_LAST;
87   private boolean createTasks = true;
88   
89   public TaskNode() {
90   }
91
92   public TaskNode(String JavaDoc name) {
93     super(name);
94   }
95
96   public void read(Element element, JpdlXmlReader jpdlReader) {
97     // get the signal
98
String JavaDoc signalText = element.attributeValue("signal");
99     if (signalText!=null) {
100       signal = parseSignal(signalText);
101     }
102
103     // create tasks
104
String JavaDoc createTasksText = element.attributeValue("create-tasks");
105     if (createTasksText!=null) {
106       if (("no".equalsIgnoreCase(createTasksText))
107            || ("false".equalsIgnoreCase(createTasksText)) ) {
108         createTasks = false;
109       }
110     }
111     
112     // parse the tasks
113
jpdlReader.readTasks(element, this);
114   }
115
116   public void addTask(Task task) {
117     if (tasks==null) tasks = new HashSet();
118     tasks.add(task);
119     task.setTaskNode(this);
120   }
121
122   // node behaviour methods
123
/////////////////////////////////////////////////////////////////////////////
124

125   public void execute(ExecutionContext executionContext) {
126     
127     TaskMgmtInstance tmi = getTaskMgmtInstance(executionContext.getToken());
128     
129     // if this tasknode should create instances
130
if ( (createTasks)
131          && (tasks!=null) ) {
132       Iterator iter = tasks.iterator();
133       while (iter.hasNext()) {
134         Task task = (Task) iter.next();
135         executionContext.setTask(task);
136         tmi.createTaskInstance(task, executionContext);
137       }
138     }
139
140     // check if we should continue execution
141
boolean continueExecution = false;
142     switch (signal) {
143       case SIGNAL_UNSYNCHRONIZED:
144         continueExecution = true;
145         break;
146       case SIGNAL_FIRST_WAIT:
147       case SIGNAL_LAST_WAIT:
148       case SIGNAL_NEVER:
149         continueExecution = false;
150         break;
151       case SIGNAL_FIRST:
152       case SIGNAL_LAST:
153         continueExecution = tmi.getSignallingTasks(executionContext).isEmpty();
154     }
155
156     if (continueExecution) {
157       leave(executionContext);
158     }
159   }
160   
161   public void leave(ExecutionContext executionContext, Transition transition) {
162     TaskMgmtInstance tmi = getTaskMgmtInstance(executionContext.getToken());
163     if (tmi.hasBlockingTaskInstances(executionContext.getToken()) ) {
164       throw new IllegalStateException JavaDoc("task-node '"+name+"' still has blocking tasks");
165     }
166     removeTaskInstanceSynchronization(executionContext.getToken());
167     super.leave(executionContext, transition);
168   }
169   
170   // task behaviour methods
171
/////////////////////////////////////////////////////////////////////////////
172

173   public boolean completionTriggersSignal(TaskInstance taskInstance) {
174     boolean completionTriggersSignal = false;
175     if ( (signal==SIGNAL_FIRST)
176          || (signal==SIGNAL_FIRST_WAIT) ) {
177       completionTriggersSignal = true;
178     } else if ( ( (signal==SIGNAL_LAST)
179                   || (signal==SIGNAL_LAST_WAIT) )
180                 && (isLastToComplete(taskInstance) ) ){
181       completionTriggersSignal = true;
182     }
183     return completionTriggersSignal;
184   }
185
186   private boolean isLastToComplete(TaskInstance taskInstance) {
187     Token token = taskInstance.getToken();
188     TaskMgmtInstance tmi = getTaskMgmtInstance(token);
189     
190     boolean isLastToComplete = true;
191     Iterator iter = tmi.getTaskInstances().iterator();
192     while ( iter.hasNext()
193             && (isLastToComplete) ) {
194       TaskInstance other = (TaskInstance) iter.next();
195       if ( (other.getToken()==token)
196            && (other!=taskInstance)
197            && (!other.hasEnded()) ) {
198         isLastToComplete = false;
199       }
200     }
201     
202     return isLastToComplete;
203   }
204
205   public void removeTaskInstanceSynchronization(Token token) {
206     TaskMgmtInstance tmi = getTaskMgmtInstance(token);
207     Collection taskInstances = tmi.getTaskInstances();
208     if (taskInstances!=null) {
209       Iterator iter = taskInstances.iterator();
210       while (iter.hasNext()) {
211         TaskInstance taskInstance = (TaskInstance) iter.next();
212         // remove signalling
213
if (taskInstance.isSignalling()
214             &&(token==taskInstance.getToken())) {
215           taskInstance.setSignalling(false);
216         }
217         // remove blocking
218
if (taskInstance.isBlocking()
219             &&(token==taskInstance.getToken())) {
220           taskInstance.setBlocking(false);
221         }
222       }
223     }
224   }
225
226   private TaskMgmtInstance getTaskMgmtInstance(Token token) {
227     return (TaskMgmtInstance) token.getProcessInstance().getInstance(TaskMgmtInstance.class);
228   }
229
230   // getters and setters
231
/////////////////////////////////////////////////////////////////////////////
232

233   /**
234    * is a Map with the tasks, keyed by task-name or an empty map in case
235    * no tasks are present in this task-node.
236    */

237   public Map getTasksMap() {
238     Map tasksMap = new HashMap();
239     if (tasks!=null) {
240       Iterator iter = tasks.iterator();
241       while (iter.hasNext()) {
242         Task task = (Task) iter.next();
243         tasksMap.put(task.getName(), task);
244       }
245     }
246     return tasksMap;
247   }
248   
249   /**
250    * is the task in this task-node with the given name or null if the given task
251    * does not exist in this node.
252    */

253   public Task getTask(String JavaDoc taskName) {
254     return (Task) getTasksMap().get(taskName);
255   }
256
257   public long getId() {
258     return id;
259   }
260   public Set getTasks() {
261     return tasks;
262   }
263   public int getSignal() {
264     return signal;
265   }
266   public boolean getCreateTasks() {
267     return createTasks;
268   }
269 }
270
Popular Tags