KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > node > Decision


1 package org.jbpm.graph.node;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Set JavaDoc;
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 /**
20  * decision node.
21  */

22 public class Decision extends Node implements Parsable {
23   
24   private static final String JavaDoc DECISION_CONDITION_RESULT = "decision_condition_result";
25   private static final long serialVersionUID = 1L;
26
27   List JavaDoc decisionConditions = null;
28   Delegation decisionDelegation = null;
29
30   public Decision() {
31   }
32
33   public Decision(String JavaDoc 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 JavaDoc iter = decisionElement.elementIterator("transition");
45       while (iter.hasNext()) {
46         Element transitionElement = (Element) iter.next();
47         String JavaDoc transitionName = transitionElement.attributeValue("name");
48         String JavaDoc conditionExpression = transitionElement.elementText("condition");
49         if (decisionConditions==null) {
50           decisionConditions = new ArrayList JavaDoc();
51         }
52         decisionConditions.add(new DecisionCondition(transitionName, conditionExpression));
53       }
54     }
55   }
56
57   public void execute(ExecutionContext executionContext) {
58     String JavaDoc 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 JavaDoc 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 JavaDoc inputMap = decisionConditionScript.createInputMap(executionContext);
74             Set JavaDoc outputNames = new HashSet JavaDoc();
75             outputNames.add(DECISION_CONDITION_RESULT);
76             Map JavaDoc outputMap = decisionConditionScript.eval(inputMap, outputNames);
77             Boolean JavaDoc result = (Boolean JavaDoc) 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 JavaDoc exception) {
87       raiseException(exception, executionContext);
88     }
89
90     Transition transition = getLeavingTransition(transitionName);
91     if (transition==null) {
92       throw new RuntimeException JavaDoc("decision '"+name+"' selected non existing transition '"+transitionName+"'" );
93     }
94     executionContext.leaveNode(transition);
95   }
96 }
97
Popular Tags