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;
19  
20  import java.util.ArrayList;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  import java.util.Set;
24  import java.util.Collection;
25  
26  import org.apache.commons.scxml.model.State;
27  
28  /***
29   * The encapsulation of the current state of a state machine.
30   *
31   */
32  public class Status {
33  
34      /***
35       * The states that are currently active.
36       */
37      private Set states;
38  
39      /***
40       * The events that are currently queued.
41       */
42      private Collection events;
43  
44      /***
45       * Have we reached a final configuration for this state machine.
46       *
47       * True - if all the states are final and there are not events
48       * pending from the last step. False - otherwise.
49       *
50       * @return Whether a final configuration has been reached.
51       */
52      public boolean isFinal() {
53          boolean rslt = true;
54          for (Iterator i = states.iterator(); i.hasNext();) {
55              State s = (State) i.next();
56              if (!s.getIsFinal()) {
57                  rslt = false;
58                  break;
59              }
60              //the status is final only iff these are top-level states
61              if (s.getParent() != null) {
62                  rslt = false;
63                  break;
64              }
65          }
66          if (!events.isEmpty()) {
67              rslt = false;
68          }
69          return rslt;
70      }
71  
72      /***
73       * Constructor.
74       */
75      public Status() {
76          states = new HashSet();
77          events = new ArrayList();
78      }
79  
80      /***
81       * Get the states configuration (leaf only).
82       *
83       * @return Returns the states configuration - simple (leaf) states only.
84       */
85      public Set getStates() {
86          return states;
87      }
88  
89      /***
90       * Get the events that are currently queued.
91       *
92       * @return The events that are currently queued.
93       */
94      public Collection getEvents() {
95          return events;
96      }
97  
98      /***
99       * Get the complete states configuration.
100      *
101      * @return complete states configuration including simple states and their
102      *         complex ancestors up to the root.
103      */
104     public Set getAllStates() {
105         return SCXMLHelper.getAncestorClosure(states, null);
106     }
107 
108 }
109