1 package org.jbpm.bpel.def; 2 3 import java.util.*; 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.jbpm.graph.def.Node; 10 import org.jbpm.graph.def.ProcessDefinition; 11 import org.jbpm.graph.def.Transition; 12 import org.jbpm.graph.exe.ExecutionContext; 13 import org.jbpm.graph.exe.Token; 14 15 import org.jbpm.bpel.data.def.Snippet; 16 17 23 public abstract class StructuredActivity extends CompositeActivity { 24 private transient Map nodesMap = null; 25 protected List nodes; 26 protected StructureStart start; 27 protected StructureEnd end; 28 29 public StructuredActivity() { 30 start = new StructureStart(this); 31 end = new StructureEnd(this); 32 } 33 34 public StructuredActivity(String name) { 35 super(name); 36 start = new StructureStart(this); 37 end = new StructureEnd(this); 38 } 39 40 42 public void enter(ExecutionContext token) { 43 start.enter(token); 44 } 45 46 public void execute(ExecutionContext context) { 47 start.leave(context, start.getDefaultLeavingTransition()); 48 } 49 50 public void leave(ExecutionContext token) { 51 end.leave(token); 52 } 53 54 public void skip(ExecutionContext context) { 55 setNegativeLinkStatus(context.getToken()); 56 end.leave(context, getDefaultLeavingTransition()); 57 } 58 59 public void setNegativeLinkStatus(Token token) { 60 for(Iterator it = getNodes().iterator(); it.hasNext();) { 61 ((Activity)it.next()).setNegativeLinkStatus(token); 62 } 63 64 end.setNegativeLinkStatus(token); 65 } 66 67 69 public StructureStart getStart() { 70 return start; 71 } 72 73 public StructureEnd getEnd() { 74 return end; 75 } 76 77 79 protected void attachChild(Activity activity) { 80 addImplicitTransitions(activity); 81 82 if (nodes == null) nodes = new ArrayList(); 83 nodes.add(activity); 84 activity.compositeActivity = this; 85 nodesMap = null; 86 } 87 88 protected void detachChild(Activity activity) { 89 removeImplicitTransitions(activity); 90 if (nodes.remove(activity)) { 91 activity.compositeActivity = null; 92 nodesMap = null; 93 } 94 } 95 96 public Node addNode(Node node) { 98 if (node == null) throw new IllegalArgumentException ("can't add a null activity to a composite activity"); 99 attachChild((Activity) node); 100 return node; 101 } 102 103 public Node removeNode(Node node) { 105 if(node == null) throw new IllegalArgumentException ("can't remove a null activity from a composite activity"); 106 if( nodes == null || !nodes.contains(node) ) return null; 107 detachChild((Activity) node); 108 return node; 109 } 110 111 public void reorderNode(int oldIndex, int newIndex) { 113 if (nodes!=null) { 114 Object reorderedObject = nodes.remove(oldIndex); 115 nodes.add(newIndex, reorderedObject); 116 } else { 117 throw new IndexOutOfBoundsException ("no collection present"); 118 } 119 } 120 121 public List getNodes() { 123 return nodes; 124 } 125 126 public Map getNodesMap() { 128 if ( (nodesMap==null) 129 && (nodes!=null) ) { 130 nodesMap = new HashMap(); 131 Iterator iter = nodes.iterator(); 132 while (iter.hasNext()) { 133 Node node = (Node) iter.next(); 134 nodesMap.put(node.getName(),node); 135 } 136 } 137 return nodesMap; 138 } 139 140 public Node getNode(String name) { 142 return (Node) getNodesMap().get(name); 143 } 144 145 public boolean hasNode(String name) { 147 return getNodesMap().containsKey(name); 148 } 149 150 public String generateNodeName() { 152 return ProcessDefinition.generateNodeName(nodes); 153 } 154 155 public Node findNode(String hierarchicalName) { 157 return ProcessDefinition.findNode(this, hierarchicalName); 158 } 159 160 protected void addImplicitTransitions(Activity activity) { 161 start.connect(activity); 163 activity.connect(end); 164 } 165 166 protected void removeImplicitTransitions(Activity activity) { 167 start.disconnect(activity); 169 activity.disconnect(end); 170 } 171 172 174 public Snippet getJoinCondition() { 175 return start != null ? start.getJoinCondition() : null; 176 } 177 178 public void setJoinCondition(Snippet joinCondition) { 179 if(start != null) { 180 start.setJoinCondition(joinCondition); 181 } 182 } 183 184 public Collection getSources() { 185 return end.getSources(); 186 } 187 188 public void addSource(Link link) { 189 end.addSource(link); 190 } 191 192 public Link getSource(String linkName) { 193 return end.getSource(linkName); 194 } 195 196 public Link getTarget(String linkName) { 197 return start != null ? start.getTarget(linkName) : null; 198 } 199 200 public Collection getTargets() { 201 return start != null ? start.getTargets() : null; 202 } 203 204 public void addTarget(Link link) { 205 start.addTarget(link); 206 } 207 208 210 public Transition getDefaultArrivingTransition() { 211 return start != null ? start.getDefaultArrivingTransition() : null; 212 } 213 214 public Transition getDefaultLeavingTransition() { 215 return end != null ? end.getDefaultLeavingTransition() : null; 216 } 217 218 public Transition addArrivingTransition(Transition transition) { 219 if(start != null) { 220 start.addArrivingTransition(transition); 221 } 222 223 return transition; 224 } 225 226 public Transition addLeavingTransition(Transition transition) { 227 end.addLeavingTransition(transition); 228 return transition; 229 } 230 231 234 public static class StructureStart extends Activity { 235 private static final long serialVersionUID = 1L; 236 private StructuredActivity startStructure; 237 238 StructureStart() { 239 } 240 241 public StructureStart(StructuredActivity activity) { 242 startStructure = activity; 243 } 244 245 public void leave(ExecutionContext context) { 246 getCompositeActivity().execute(context); 247 } 248 249 public void skip(ExecutionContext context) { 250 getCompositeActivity().skip(context); 251 } 252 253 public CompositeActivity getCompositeActivity() { 254 return startStructure; 255 } 256 257 public String getName() { 258 return startStructure.getName(); 259 } 260 } 261 262 265 public static class StructureEnd extends Activity { 266 private static final long serialVersionUID = 1L; 267 private StructuredActivity endStructure; 268 269 StructureEnd() { 270 } 271 272 public StructureEnd(StructuredActivity activity) { 273 endStructure = activity; 274 } 275 276 public void execute(ExecutionContext context) { 277 ((Activity)getParent()).leave(context); 278 } 279 280 public CompositeActivity getCompositeActivity() { 281 return endStructure; 282 } 283 284 public String getName() { 285 return endStructure.getName(); 286 } 287 } 288 289 } 290 | Popular Tags |