View Javadoc

    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.scxml.model;
    18
    19  import java.io.Serializable;
    20  import java.util.HashMap;
    21  import java.util.LinkedHashMap;
    22  import java.util.Map;
    23
    24  import org.apache.commons.scxml.SCXMLHelper;
    25
    26  /***
    27   * The class in this SCXML object model that corresponds to the
    28   * &lt;scxml&gt; root element, and serves as the &quot;document
    29   * root&quot;.
    30   *
    31   */
    32  public class SCXML implements Serializable {
    33
    34      /***
    35       * Serial version UID.
    36       */
    37      private static final long serialVersionUID = 2L;
    38
    39      /***
    40       * The SCXML XMLNS.
    41       */
    42      public static final String XMLNS = "http://www.w3.org/2005/07/scxml";
    43
    44      /***
    45       * The xmlns attribute on the root &lt;smxml&gt; element.
    46       * This must match XMLNS above.
    47       */
    48      private String xmlns;
    49
    50      /***
    51       * The SCXML version of this document.
    52       */
    53      private String version;
    54
    55      /***
    56       * The initial TransitionTarget for the SCXML executor.
    57       */
    58      private TransitionTarget initialTarget;
    59
    60      /***
    61       * The initial transition target ID (used by XML Digester only).
    62       */
    63      private String initialstate;
    64
    65      /***
    66       * Optional property holding the data model for this SCXML document.
    67       * This gets merged with the root context and potentially hides any
    68       * (namesake) variables in the root context.
    69       */
    70      private Datamodel datamodel;
    71
    72      /***
    73       * The immediate child targets of this SCXML document root.
    74       */
    75      private Map children;
    76
    77      /***
    78       * A global map of all States and Parallels associated with this
    79       * state machine, keyed by their id.
    80       */
    81      private Map targets;
    82
    83      /***
    84       * Constructor.
    85       */
    86      public SCXML() {
    87          this.children = new LinkedHashMap();
    88          this.targets = new HashMap();
    89      }
    90
    91      /***
    92       * Get the initial State.
    93       *
    94       * @return State Returns the initialstate.
    95       *
    96       * @deprecated Use getInitialTarget() instead. Returns <code>null</code>
    97       *             if the initial target is a Parallel.
    98       */
    99      public final State getInitialState() {
    100         if (initialTarget != null && initialTarget instanceof State) {
    101             return (State) initialTarget;
    102         }
    103         return null;
    104     }
    105
    106     /***
    107      * Set the initial State.
    108      *
    109      * @param initialState The initialstate to set.
    110      *
    111      * @deprecated Use setInitialTarget(TransitionTarget) instead.
    112      */
    113     public final void setInitialState(final State initialState) {
    114         this.initialTarget = initialState;
    115     }
    116
    117     /***
    118      * Get the initial TransitionTarget.
    119      *
    120      * @return Returns the initial target for this state machine.
    121      *
    122      * @since 0.7
    123      */
    124     public final TransitionTarget getInitialTarget() {
    125         return initialTarget;
    126     }
    127
    128     /***
    129      * Set the initial TransitionTarget.
    130      *
    131      * @param initialTarget The initial target to set.
    132      *
    133      * @since 0.7
    134      */
    135     public final void setInitialTarget(final TransitionTarget initialTarget) {
    136         this.initialTarget = initialTarget;
    137     }
    138
    139     /***
    140      * Get the data model placed at document root.
    141      *
    142      * @return Returns the data model.
    143      */
    144     public final Datamodel getDatamodel() {
    145         return datamodel;
    146     }
    147
    148     /***
    149      * Set the data model at document root.
    150      *
    151      * @param datamodel The Datamodel to set.
    152      */
    153     public final void setDatamodel(final Datamodel datamodel) {
    154         this.datamodel = datamodel;
    155     }
    156
    157     /***
    158      * Get the children states.
    159      *
    160      * @return Map Returns map of the child states.
    161      *
    162      * @deprecated Use getChildren() instead.
    163      */
    164     public final Map getStates() {
    165         return children;
    166     }
    167
    168     /***
    169      * Add a child state.
    170      *
    171      * @param state The state to be added to the states Map.
    172      *
    173      * @deprecated Use addChild(TransitionTarget) instead.
    174      */
    175     public final void addState(final State state) {
    176         children.put(state.getId(), state);
    177     }
    178
    179     /***
    180      * Get the immediate child targets of the SCXML root.
    181      *
    182      * @return Map Returns map of the child targets.
    183      *
    184      * @since 0.7
    185      */
    186     public final Map getChildren() {
    187         return children;
    188     }
    189
    190     /***
    191      * Add an immediate child target of the SCXML root.
    192      *
    193      * @param tt The transition target to be added to the states Map.
    194      *
    195      * @since 0.7
    196      */
    197     public final void addChild(final TransitionTarget tt) {
    198         children.put(tt.getId(), tt);
    199     }
    200
    201     /***
    202      * Get the targets map, which is a Map of all States and Parallels
    203      * associated with this state machine, keyed by their id.
    204      *
    205      * @return Map Returns the targets.
    206      */
    207     public final Map getTargets() {
    208         return targets;
    209     }
    210
    211     /***
    212      * Add a target to this SCXML document.
    213      *
    214      * @param target The target to be added to the targets Map.
    215      */
    216     public final void addTarget(final TransitionTarget target) {
    217         String id = target.getId();
    218         if (!SCXMLHelper.isStringEmpty(id)) {
    219             // Target is not anonymous, so makes sense to map it
    220             targets.put(id, target);
    221         }
    222     }
    223
    224     /***
    225      * Get the SCXML document version.
    226      *
    227      * @return Returns the version.
    228      */
    229     public final String getVersion() {
    230         return version;
    231     }
    232
    233     /***
    234      * Set the SCXML document version.
    235      *
    236      * @param version The version to set.
    237      */
    238     public final void setVersion(final String version) {
    239         this.version = version;
    240     }
    241
    242     /***
    243      * Get the xmlns of this SCXML document.
    244      *
    245      * @return Returns the xmlns.
    246      */
    247     public final String getXmlns() {
    248         return xmlns;
    249     }
    250
    251     /***
    252      * Set the xmlns of this SCXML document.
    253      *
    254      * @param xmlns The xmlns to set.
    255      */
    256     public final void setXmlns(final String xmlns) {
    257         this.xmlns = xmlns;
    258     }
    259
    260     /***
    261      * Get the ID of the initial state.
    262      *
    263      * @return String Returns the initial state ID (used by XML Digester only).
    264      * @see #getInitialState()
    265      */
    266     public final String getInitialstate() {
    267         return initialstate;
    268     }
    269
    270     /***
    271      * Set the ID of the initial state.
    272      *
    273      * @param initialstate The initial state ID (used by XML Digester only).
    274      * @see #setInitialState(State)
    275      */
    276     public final void setInitialstate(final String initialstate) {
    277         this.initialstate = initialstate;
    278     }
    279
    280 }
    281