KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > def > SuperState


1 package org.jbpm.graph.def;
2
3 import java.util.*;
4
5 import org.dom4j.*;
6 import org.jbpm.graph.exe.*;
7 import org.jbpm.jpdl.xml.*;
8
9 /**
10  * brings hierarchy into the elements of a process definition by creating a
11  * parent-child relation between {@link GraphElement}s.
12  */

13 public class SuperState extends Node implements Parsable, NodeCollection {
14
15   private static final long serialVersionUID = 1L;
16   
17   protected List nodes = null;
18   private transient Map nodesMap = null;
19
20   public SuperState() {
21   }
22
23   public SuperState(String JavaDoc name) {
24     super(name);
25   }
26
27   // event types //////////////////////////////////////////////////////////////
28

29   public static final String JavaDoc[] supportedEventTypes = new String JavaDoc[]{
30     Event.EVENTTYPE_NODE_ENTER,
31     Event.EVENTTYPE_NODE_LEAVE,
32     Event.EVENTTYPE_TASK_CREATE,
33     Event.EVENTTYPE_TASK_ASSIGN,
34     Event.EVENTTYPE_TASK_START,
35     Event.EVENTTYPE_TASK_END,
36     Event.EVENTTYPE_TRANSITION,
37     Event.EVENTTYPE_BEFORE_SIGNAL,
38     Event.EVENTTYPE_AFTER_SIGNAL,
39     Event.EVENTTYPE_SUPERSTATE_ENTER,
40     Event.EVENTTYPE_SUPERSTATE_LEAVE,
41     Event.EVENTTYPE_SUBPROCESS_CREATED,
42     Event.EVENTTYPE_SUBPROCESS_END,
43     Event.EVENTTYPE_TIMER
44   };
45   public String JavaDoc[] getSupportedEventTypes() {
46     return supportedEventTypes;
47   }
48
49   // xml //////////////////////////////////////////////////////////////////////
50

51   public void read(Element element, JpdlXmlReader jpdlReader) {
52     jpdlReader.readNodes(element, this);
53   }
54
55   // behaviour ////////////////////////////////////////////////////////////////
56

57   public void execute(ExecutionContext executionContext) {
58     if ( (nodes==null)
59          || (nodes.size()==0) ) {
60       throw new RuntimeException JavaDoc("transition enters superstate +"+this+"' and it there is no first child-node to delegate to");
61     }
62     Node startNode = (Node) nodes.get(0);
63     startNode.enter(executionContext);
64   }
65
66   // nodes ////////////////////////////////////////////////////////////////////
67

68   // javadoc description in NodeCollection
69
public List getNodes() {
70     return nodes;
71   }
72
73   // javadoc description in NodeCollection
74
public Map getNodesMap() {
75     if ( (nodesMap==null)
76          && (nodes!=null) ) {
77       nodesMap = new HashMap();
78       Iterator iter = nodes.iterator();
79       while (iter.hasNext()) {
80         Node node = (Node) iter.next();
81         nodesMap.put(node.getName(),node);
82       }
83     }
84     return nodesMap;
85   }
86
87   // javadoc description in NodeCollection
88
public Node getNode(String JavaDoc name) {
89     return (Node) getNodesMap().get(name);
90   }
91
92   
93   // javadoc description in NodeCollection
94
public boolean hasNode(String JavaDoc name) {
95     return getNodesMap().containsKey(name);
96   }
97
98   // javadoc description in NodeCollection
99
public Node addNode(Node node) {
100     if (node == null) throw new IllegalArgumentException JavaDoc("can't add a null node to a superstate");
101     if (nodes == null) nodes = new ArrayList();
102     nodes.add(node);
103     node.superState = this;
104     nodesMap = null;
105     return node;
106   }
107
108   // javadoc description in NodeCollection
109
public Node removeNode(Node node) {
110     Node removedNode = null;
111     if (node == null) throw new IllegalArgumentException JavaDoc("can't remove a null node from a superstate");
112     if (nodes != null) {
113       if (nodes.remove(node)) {
114         removedNode = node;
115         removedNode.superState = null;
116         nodesMap = null;
117       }
118     }
119     return removedNode;
120   }
121
122   // javadoc description in NodeCollection
123
public void reorderNode(int oldIndex, int newIndex) {
124     if ( (nodes!=null)
125          && (Math.min(oldIndex, newIndex)>=0)
126          && (Math.max(oldIndex, newIndex)<nodes.size()) ) {
127       Object JavaDoc o = nodes.remove(oldIndex);
128       nodes.add(newIndex, o);
129     } else {
130       throw new IndexOutOfBoundsException JavaDoc("couldn't reorder element from index '"+oldIndex+"' to index '"+newIndex+"' in nodeList '"+nodes+"'");
131     }
132   }
133
134   // javadoc description in NodeCollection
135
public String JavaDoc generateNodeName() {
136     return ProcessDefinition.generateNodeName(nodes);
137   }
138
139   // javadoc description in NodeCollection
140
public Node findNode(String JavaDoc hierarchicalName) {
141     return ProcessDefinition.findNode(this, hierarchicalName);
142   }
143
144   /**
145    * recursively checks if the given node is one of the descendants of this supernode.
146    */

147   public boolean containsNode(Node node) {
148     boolean containsNode = false;
149     SuperState parent = node.getSuperState();
150     while( (!containsNode)
151            && (parent!=null) ) {
152       if (this==parent) {
153         containsNode = true;
154       } else {
155         parent = parent.getSuperState();
156       }
157     }
158     return containsNode;
159   }
160
161   // other ////////////////////////////////////////////////////////////////////
162

163   public GraphElement getParent() {
164     GraphElement parent = processDefinition;
165     if (superState!=null) {
166       parent = superState;
167     }
168     return parent;
169   }
170 }
Popular Tags