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.util.LinkedHashMap;
20  import java.util.Map;
21  
22  /**
23   * The class in this SCXML object model that corresponds to the
24   * <state> SCXML element.
25   *
26   */
27  public class State extends TransitionTarget {
28  
29      /**
30       * Serial version UID.
31       */
32      private static final long serialVersionUID = 2L;
33  
34      /**
35       * The Map containing immediate children of this State, keyed by
36       * their IDs. Incompatible with the parallel or invoke property.
37       */
38      private Map children;
39  
40      /**
41       * The Parallel child, which defines a set of parallel substates.
42       * May occur 0 or 1 times. Incompatible with the state or invoke property.
43       */
44      private Parallel parallel;
45  
46      /**
47       * The Invoke child, which defines an external process that should
48       * be invoked, immediately after the onentry executable content,
49       * and the transitions become candidates after the invoked
50       * process has completed its execution.
51       * May occur 0 or 1 times. Incompatible with the state or parallel
52       * property.
53       */
54      private Invoke invoke;
55  
56      /**
57       * Boolean property indicating whether this is a final state or not.
58       * Default value is false . Final states may not have substates or
59       * outgoing transitions.
60       */
61      private boolean isFinal;
62  
63      /**
64       * A child which identifies initial state for state machines that
65       * have substates.
66       */
67      private Initial initial;
68  
69      /**
70       * Applies to composite states only. If one of its final children is
71       * active, its parent is marked done. This property is reset upon
72       * re-entry.
73       *
74       * @deprecated Will be removed in v1.0
75       */
76      private boolean done = false;
77  
78      /**
79       * Constructor.
80       */
81      public State() {
82          this.children = new LinkedHashMap();
83      }
84  
85      /**
86       * Is this state a "final" state.
87       *
88       * @return boolean Returns the isFinal.
89       * @deprecated Use {@link #isFinal()} instead
90       */
91      public final boolean getIsFinal() {
92          return isFinal;
93      }
94  
95      /**
96       * Set whether this is a "final" state.
97       *
98       * @param isFinal
99       *            The isFinal to set.
100      * @deprecated Use {@link #setFinal(boolean)} instead
101      */
102     public final void setIsFinal(final boolean isFinal) {
103         this.isFinal = isFinal;
104     }
105 
106     /**
107      * Is this state a "final" state.
108      *
109      * @return boolean Returns the isFinal.
110      *
111      * @since 0.7
112      */
113     public final boolean isFinal() {
114         return isFinal;
115     }
116 
117     /**
118      * Set whether this is a "final" state.
119      *
120      * @param isFinal
121      *            The isFinal to set.
122      *
123      * @since 0.7
124      */
125     public final void setFinal(final boolean isFinal) {
126         this.isFinal = isFinal;
127     }
128 
129     /**
130      * Get the Parallel child (may be null).
131      *
132      * @return Parallel Returns the parallel.
133      *
134      * @deprecated <parallel> no longer needs an enclosing
135      *             <state> element.
136      */
137     public final Parallel getParallel() {
138         return parallel;
139     }
140 
141     /**
142      * Set the Parallel child.
143      *
144      * @param parallel
145      *            The parallel to set.
146      *
147      * @deprecated <parallel> no longer needs an enclosing
148      *             <state> element.
149      */
150     public final void setParallel(final Parallel parallel) {
151         this.parallel = parallel;
152     }
153 
154     /**
155      * Get the Invoke child (may be null).
156      *
157      * @return Invoke Returns the invoke.
158      */
159     public final Invoke getInvoke() {
160         return invoke;
161     }
162 
163     /**
164      * Set the Invoke child.
165      *
166      * @param invoke
167      *            The invoke to set.
168      */
169     public final void setInvoke(final Invoke invoke) {
170         this.invoke = invoke;
171     }
172 
173     /**
174      * Get the initial state.
175      *
176      * @return Initial Returns the initial state.
177      */
178     public final Initial getInitial() {
179         return initial;
180     }
181 
182     /**
183      * Set the initial state.
184      *
185      * @param target
186      *            The target to set.
187      */
188     public final void setInitial(final Initial target) {
189         this.initial = target;
190         target.setParent(this);
191     }
192 
193     /**
194      * Get the initial state's ID.
195      *
196      * @return The initial state's string ID.
197      */
198     public final String getFirst() {
199         if (initial != null) {
200             return initial.getTransition().getNext();
201         }
202         return null;
203     }
204 
205     /**
206      * Set the initial state by its ID string.
207      *
208      * @param target
209      *            The initial target's ID to set.
210      */
211     public final void setFirst(final String target) {
212         Transition t = new Transition();
213         t.setNext(target);
214         Initial ini = new Initial();
215         ini.setTransition(t);
216         ini.setParent(this);
217         this.initial = ini;
218     }
219 
220     /**
221      * Get the map of child states (may be empty).
222      *
223      * @return Map Returns the children.
224      */
225     public final Map getChildren() {
226         return children;
227     }
228 
229     /**
230      * Add a child state.
231      *
232      * @param state
233      *            a child state
234      *
235      * @deprecated Use {@link #addChild(TransitionTarget)} instead.
236      */
237     public final void addChild(final State state) {
238         this.children.put(state.getId(), state);
239         state.setParent(this);
240     }
241 
242     /**
243      * Add a child transition target.
244      *
245      * @param tt
246      *            a child transition target
247      *
248      * @since 0.7
249      */
250     public final void addChild(final TransitionTarget tt) {
251         this.children.put(tt.getId(), tt);
252         tt.setParent(this);
253     }
254 
255     /**
256      * Check whether this is a simple (leaf) state (UML terminology).
257      *
258      * @return true if this is a simple state, otherwise false
259      */
260     public final boolean isSimple() {
261         if (parallel == null && children.isEmpty()) {
262             return true;
263         }
264         return false;
265     }
266 
267     /**
268      * Check whether this is a composite state (UML terminology).
269      *
270      * @return true if this is a composite state, otherwise false
271      */
272     public final boolean isComposite() {
273         if (parallel == null && children.isEmpty()) {
274             return false;
275         }
276         return true;
277     }
278 
279     /**
280      * Checks whether it is a region state (directly nested to parallel - UML
281      * terminology).
282      *
283      * @return true if this is a region state, otherwise false
284      * @see Parallel
285      */
286     public final boolean isRegion() {
287         if (getParent() instanceof Parallel) {
288             return true;
289         }
290         return false;
291     }
292 
293     /**
294      * Checks whether it is a orthogonal state, that is, it owns a parallel
295      * (UML terminology).
296      *
297      * @return true if this is a orthogonal state, otherwise false
298      * @deprecated <parallel> now represents an orthogonal state, rather
299      *             than denoting that the enclosing state is orthogonal, as
300      *             it did in previous SCXML WDs.
301      */
302     public final boolean isOrthogonal() {
303         if (parallel != null) {
304             return true;
305         }
306         return false;
307     }
308 
309     /**
310      * In case this is a parallel state, check if one its final states
311      * is active.
312      *
313      * @return Returns the done.
314      * @deprecated Will be removed in v1.0, in favor of
315      *             <code>SCInstance#isDone(TransitionTarget)</code>
316      */
317     public final boolean isDone() {
318         return done;
319     }
320 
321     /**
322      * Update the done property, which is set if this is a parallel state,
323      * and one its final states is active.
324      *
325      * @param done The done to set.
326      * @deprecated Will be removed in v1.0, in favor of
327      *             <code>SCInstance#setDone(TransitionTarget)</code>
328      */
329     public final void setDone(final boolean done) {
330         this.done = done;
331     }
332 }
333