View Javadoc

    1   /*
    2    *
    3    *   Copyright 2005 The Apache Software Foundation.
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  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   */
    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   * &lt;var&gt; 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