1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.scxml2.model;
18
19 import org.apache.commons.scxml2.ActionExecutionContext;
20 import org.apache.commons.scxml2.Context;
21 import org.apache.commons.scxml2.Evaluator;
22 import org.apache.commons.scxml2.SCXMLExpressionException;
23 import org.apache.commons.scxml2.TriggerEvent;
24
25 /**
26 * The class in this SCXML object model that corresponds to the
27 * <var> SCXML element.
28 *
29 */
30 public class Var extends Action {
31
32 /**
33 * Serial version UID.
34 */
35 private static final long serialVersionUID = 1L;
36
37 /**
38 * The name of the variable to be created.
39 */
40 private String name;
41
42 /**
43 * The expression that evaluates to the initial value of the variable.
44 */
45 private String expr;
46
47 /**
48 * Constructor.
49 */
50 public Var() {
51 super();
52 }
53
54 /**
55 * Get the expression that evaluates to the initial value
56 * of the variable.
57 *
58 * @return String Returns the expr.
59 */
60 public final String getExpr() {
61 return expr;
62 }
63
64 /**
65 * Set the expression that evaluates to the initial value
66 * of the variable.
67 *
68 * @param expr The expr to set.
69 */
70 public final void setExpr(final String expr) {
71 this.expr = expr;
72 }
73
74 /**
75 * Get the name of the (new) variable.
76 *
77 * @return String Returns the name.
78 */
79 public final String getName() {
80 return name;
81 }
82
83 /**
84 * Set the name of the (new) variable.
85 *
86 * @param name The name to set.
87 */
88 public final void setName(final String name) {
89 this.name = name;
90 }
91
92 /**
93 * {@inheritDoc}
94 */
95 @Override
96 public void execute(ActionExecutionContext exctx) throws ModelException, SCXMLExpressionException {
97 Context ctx = exctx.getContext(getParentEnterableState());
98 Evaluator eval = exctx.getEvaluator();
99 ctx.setLocal(getNamespacesKey(), getNamespaces());
100 Object varObj = eval.eval(ctx, expr);
101 ctx.setLocal(getNamespacesKey(), null);
102 ctx.setLocal(name, varObj);
103 if (exctx.getAppLog().isDebugEnabled()) {
104 exctx.getAppLog().debug("<var>: Defined variable '" + name
105 + "' with initial value '" + String.valueOf(varObj) + "'");
106 }
107 TriggerEvent ev = new TriggerEvent(name + ".change", TriggerEvent.CHANGE_EVENT);
108 exctx.getInternalIOProcessor().addEvent(ev);
109 }
110 }
111