1 package org.jbpm.graph.node; 2 3 import java.util.ArrayList ; 4 import java.util.HashSet ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.Set ; 9 10 import org.dom4j.Element; 11 import org.jbpm.graph.action.Script; 12 import org.jbpm.graph.def.Node; 13 import org.jbpm.graph.def.Transition; 14 import org.jbpm.graph.exe.ExecutionContext; 15 import org.jbpm.instantiation.Delegation; 16 import org.jbpm.jpdl.xml.JpdlXmlReader; 17 import org.jbpm.jpdl.xml.Parsable; 18 19 22 public class Decision extends Node implements Parsable { 23 24 private static final String DECISION_CONDITION_RESULT = "decision_condition_result"; 25 private static final long serialVersionUID = 1L; 26 27 List decisionConditions = null; 28 Delegation decisionDelegation = null; 29 30 public Decision() { 31 } 32 33 public Decision(String name) { 34 super(name); 35 } 36 37 public void read(Element decisionElement, JpdlXmlReader jpdlReader) { 38 Element decisionHandlerElement = decisionElement.element("handler"); 39 if (decisionHandlerElement!=null) { 40 decisionDelegation = new Delegation(); 41 decisionDelegation.read(decisionHandlerElement, jpdlReader); 42 43 } else { 44 Iterator iter = decisionElement.elementIterator("transition"); 45 while (iter.hasNext()) { 46 Element transitionElement = (Element) iter.next(); 47 String transitionName = transitionElement.attributeValue("name"); 48 String conditionExpression = transitionElement.elementText("condition"); 49 if (decisionConditions==null) { 50 decisionConditions = new ArrayList (); 51 } 52 decisionConditions.add(new DecisionCondition(transitionName, conditionExpression)); 53 } 54 } 55 } 56 57 public void execute(ExecutionContext executionContext) { 58 String transitionName = null; 59 60 try { 61 if (decisionDelegation!=null) { 62 DecisionHandler decisionHandler = (DecisionHandler) decisionDelegation.instantiate(); 63 transitionName = decisionHandler.decide(executionContext); 64 65 } else { 66 Iterator iter = decisionConditions.iterator(); 67 while ((iter.hasNext()) 68 && (transitionName==null)) { 69 DecisionCondition decisionCondition = (DecisionCondition) iter.next(); 70 if (decisionCondition.expression!=null) { 71 Script decisionConditionScript = new Script(); 72 decisionConditionScript.setExpression("decision_condition_result = ("+decisionCondition.expression+");"); 73 Map inputMap = decisionConditionScript.createInputMap(executionContext); 74 Set outputNames = new HashSet (); 75 outputNames.add(DECISION_CONDITION_RESULT); 76 Map outputMap = decisionConditionScript.eval(inputMap, outputNames); 77 Boolean result = (Boolean ) outputMap.get(DECISION_CONDITION_RESULT); 78 if (result.booleanValue()) { 79 transitionName = decisionCondition.transitionName; 80 } 81 } else { 82 transitionName = decisionCondition.transitionName; 83 } 84 } 85 } 86 } catch (Throwable exception) { 87 raiseException(exception, executionContext); 88 } 89 90 Transition transition = getLeavingTransition(transitionName); 91 if (transition==null) { 92 throw new RuntimeException ("decision '"+name+"' selected non existing transition '"+transitionName+"'" ); 93 } 94 executionContext.leaveNode(transition); 95 } 96 } 97 | Popular Tags |