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 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 name) { 24 super(name); 25 } 26 27 29 public static final String [] supportedEventTypes = new String []{ 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 [] getSupportedEventTypes() { 46 return supportedEventTypes; 47 } 48 49 51 public void read(Element element, JpdlXmlReader jpdlReader) { 52 jpdlReader.readNodes(element, this); 53 } 54 55 57 public void execute(ExecutionContext executionContext) { 58 if ( (nodes==null) 59 || (nodes.size()==0) ) { 60 throw new RuntimeException ("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 68 public List getNodes() { 70 return nodes; 71 } 72 73 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 public Node getNode(String name) { 89 return (Node) getNodesMap().get(name); 90 } 91 92 93 public boolean hasNode(String name) { 95 return getNodesMap().containsKey(name); 96 } 97 98 public Node addNode(Node node) { 100 if (node == null) throw new IllegalArgumentException ("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 public Node removeNode(Node node) { 110 Node removedNode = null; 111 if (node == null) throw new IllegalArgumentException ("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 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 o = nodes.remove(oldIndex); 128 nodes.add(newIndex, o); 129 } else { 130 throw new IndexOutOfBoundsException ("couldn't reorder element from index '"+oldIndex+"' to index '"+newIndex+"' in nodeList '"+nodes+"'"); 131 } 132 } 133 134 public String generateNodeName() { 136 return ProcessDefinition.generateNodeName(nodes); 137 } 138 139 public Node findNode(String hierarchicalName) { 141 return ProcessDefinition.findNode(this, hierarchicalName); 142 } 143 144 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 163 public GraphElement getParent() { 164 GraphElement parent = processDefinition; 165 if (superState!=null) { 166 parent = superState; 167 } 168 return parent; 169 } 170 } | Popular Tags |