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