001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.scxml.model;
018
019 import java.util.Collection;
020
021 import org.apache.commons.logging.Log;
022 import org.apache.commons.scxml.Context;
023 import org.apache.commons.scxml.ErrorReporter;
024 import org.apache.commons.scxml.Evaluator;
025 import org.apache.commons.scxml.EventDispatcher;
026 import org.apache.commons.scxml.SCInstance;
027 import org.apache.commons.scxml.SCXMLExpressionException;
028 import org.apache.commons.scxml.TriggerEvent;
029
030 /**
031 * The class in this SCXML object model that corresponds to the
032 * <var> SCXML element.
033 *
034 */
035 public class Var extends Action {
036
037 /**
038 * Serial version UID.
039 */
040 private static final long serialVersionUID = 1L;
041
042 /**
043 * The name of the variable to be created.
044 */
045 private String name;
046
047 /**
048 * The expression that evaluates to the initial value of the variable.
049 */
050 private String expr;
051
052 /**
053 * Constructor.
054 */
055 public Var() {
056 super();
057 }
058
059 /**
060 * Get the expression that evaluates to the initial value
061 * of the variable.
062 *
063 * @return String Returns the expr.
064 */
065 public final String getExpr() {
066 return expr;
067 }
068
069 /**
070 * Set the expression that evaluates to the initial value
071 * of the variable.
072 *
073 * @param expr The expr to set.
074 */
075 public final void setExpr(final String expr) {
076 this.expr = expr;
077 }
078
079 /**
080 * Get the name of the (new) variable.
081 *
082 * @return String Returns the name.
083 */
084 public final String getName() {
085 return name;
086 }
087
088 /**
089 * Set the name of the (new) variable.
090 *
091 * @param name The name to set.
092 */
093 public final void setName(final String name) {
094 this.name = name;
095 }
096
097 /**
098 * {@inheritDoc}
099 */
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(getParentTransitionTarget());
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