1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.scxml.model;
19
20 import java.util.Collection;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.scxml.Context;
24 import org.apache.commons.scxml.ErrorReporter;
25 import org.apache.commons.scxml.Evaluator;
26 import org.apache.commons.scxml.EventDispatcher;
27 import org.apache.commons.scxml.SCInstance;
28 import org.apache.commons.scxml.SCXMLExpressionException;
29 import org.apache.commons.scxml.TriggerEvent;
30
31 /***
32 * The class in this SCXML object model that corresponds to the
33 * <var> SCXML element.
34 *
35 */
36 public class Var extends Action {
37
38 /***
39 * The name of the variable to be created.
40 */
41 private String name;
42
43 /***
44 * The expression that evaluates to the initial value of the variable.
45 */
46 private String expr;
47
48 /***
49 * Constructor.
50 */
51 public Var() {
52 super();
53 }
54
55 /***
56 * Get the expression that evaluates to the initial value
57 * of the variable.
58 *
59 * @return String Returns the expr.
60 */
61 public final String getExpr() {
62 return expr;
63 }
64
65 /***
66 * Set the expression that evaluates to the initial value
67 * of the variable.
68 *
69 * @param expr The expr to set.
70 */
71 public final void setExpr(final String expr) {
72 this.expr = expr;
73 }
74
75 /***
76 * Get the name of the (new) variable.
77 *
78 * @return String Returns the name.
79 */
80 public final String getName() {
81 return name;
82 }
83
84 /***
85 * Set the name of the (new) variable.
86 *
87 * @param name The name to set.
88 */
89 public final void setName(final String name) {
90 this.name = name;
91 }
92
93 /***
94 * {@inheritDoc}
95 */
96 public void execute(final EventDispatcher evtDispatcher,
97 final ErrorReporter errRep, final SCInstance scInstance,
98 final Log appLog, final Collection derivedEvents)
99 throws ModelException, SCXMLExpressionException {
100 Context ctx = scInstance.getContext(getParentState());
101 Evaluator eval = scInstance.getEvaluator();
102 Object varObj = eval.eval(ctx, expr);
103 ctx.setLocal(name, varObj);
104 TriggerEvent ev = new TriggerEvent(name + ".change",
105 TriggerEvent.CHANGE_EVENT);
106 derivedEvents.add(ev);
107 }
108
109 }
110