KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.graph.node;
2
3 import java.util.*;
4 import org.dom4j.Element;
5 import org.jbpm.context.def.*;
6 import org.jbpm.context.exe.*;
7 import org.jbpm.graph.def.*;
8 import org.jbpm.graph.exe.*;
9 import org.jbpm.jpdl.xml.*;
10
11 /**
12  * is an unordered set of child nodeMap. the path of execution will
13  * be given to each node exactly once. the sequence of the child
14  * nodeMap will be determined at runtime. this implements the
15  * workflow pattern interleved parallel routing.
16  *
17  * If no script is supplied, the transition names will be sequenced
18  * in arbitrary order.
19  * If a script is provided, the variable transitionNames contains the
20  * available transition names. The returned value has to be one of
21  * those transitionNames.
22  * Instead of supplying a script, its also possible to subclass this
23  * class and override the selectTransition method.
24  */

25 public class InterleaveStart extends Node implements Parsable {
26   
27   private static final long serialVersionUID = 1L;
28
29   private String JavaDoc variableName = "interleave-transition-names";
30   private Interleaver interleaver = new DefaultInterleaver();
31   
32   public interface Interleaver {
33     String JavaDoc selectNextTransition(Collection transitionNames);
34   }
35   
36   public class DefaultInterleaver implements Interleaver {
37     public String JavaDoc selectNextTransition(Collection transitionNames) {
38       return (String JavaDoc) transitionNames.iterator().next();
39     }
40   }
41   
42   public InterleaveStart() {
43   }
44
45   public InterleaveStart(String JavaDoc name) {
46     super(name);
47   }
48
49   public void read(Element element, JpdlXmlReader jpdlReader) {
50     // TODO
51

52     // just making sure that the context definition is present
53
// because the interleave node needs the context instance at runtime
54
ProcessDefinition processDefinition = jpdlReader.getProcessDefinition();
55     if (processDefinition.getDefinition(ContextDefinition.class)==null) {
56       processDefinition.addDefinition(new ContextDefinition());
57     }
58   }
59
60   public void write(Element element) {
61     // TODO
62
}
63
64   public void execute(ExecutionContext executionContext) {
65     Token token = executionContext.getToken();
66     Collection transitionNames = retrieveTransitionNames(token);
67     // if this is the first time we enter
68
if ( transitionNames == null ) {
69       // collect all leaving transition names
70
transitionNames = new ArrayList(getTransitionNames(token));
71     }
72     
73     // select one of the remaining transition names
74
String JavaDoc nextTransition = interleaver.selectNextTransition(transitionNames);
75     // remove it from the remaining transitions
76
transitionNames.remove(nextTransition);
77
78     // store the transition names
79
storeTransitionNames(transitionNames,token);
80
81     // pass the token over the selected transition
82
token.getNode().leave(executionContext, nextTransition);
83   }
84
85   protected Collection getTransitionNames(Token token) {
86     Node node = token.getNode();
87     return node.getLeavingTransitionsMap().keySet();
88   }
89
90   protected void storeTransitionNames(Collection transitionNames, Token token) {
91     ContextInstance ci = (ContextInstance) token.getProcessInstance().getInstance(ContextInstance.class);
92     if (ci==null) throw new RuntimeException JavaDoc("an interleave start node requires the availability of a context");
93     ci.setVariable(variableName,transitionNames, token);
94   }
95
96   public Collection retrieveTransitionNames(Token token) {
97     ContextInstance ci = (ContextInstance) token.getProcessInstance().getInstance(ContextInstance.class);
98     return (Collection) ci.getVariable(variableName, token);
99   }
100
101   public void removeTransitionNames(Token token) {
102     ContextInstance ci = (ContextInstance) token.getProcessInstance().getInstance(ContextInstance.class);
103     ci.setVariable(variableName,null, token);
104   }
105   
106   public Interleaver getInterleaver() {
107     return interleaver;
108   }
109   public void setInterleaver(Interleaver interleaver) {
110     this.interleaver = interleaver;
111   }
112 }
113
Popular Tags