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