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 25 public class InterleaveStart extends Node implements Parsable { 26 27 private static final long serialVersionUID = 1L; 28 29 private String variableName = "interleave-transition-names"; 30 private Interleaver interleaver = new DefaultInterleaver(); 31 32 public interface Interleaver { 33 String selectNextTransition(Collection transitionNames); 34 } 35 36 public class DefaultInterleaver implements Interleaver { 37 public String selectNextTransition(Collection transitionNames) { 38 return (String ) transitionNames.iterator().next(); 39 } 40 } 41 42 public InterleaveStart() { 43 } 44 45 public InterleaveStart(String name) { 46 super(name); 47 } 48 49 public void read(Element element, JpdlXmlReader jpdlReader) { 50 52 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 } 63 64 public void execute(ExecutionContext executionContext) { 65 Token token = executionContext.getToken(); 66 Collection transitionNames = retrieveTransitionNames(token); 67 if ( transitionNames == null ) { 69 transitionNames = new ArrayList(getTransitionNames(token)); 71 } 72 73 String nextTransition = interleaver.selectNextTransition(transitionNames); 75 transitionNames.remove(nextTransition); 77 78 storeTransitionNames(transitionNames,token); 80 81 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 ("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 |