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.scxml2.model;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.StringTokenizer;
23  
24  /**
25   * The class in this SCXML object model that corresponds to the
26   * <transition> SCXML element. Transition rules are triggered
27   * by "events" and conditionalized via
28   * "guard-conditions".
29   *
30   */
31  public class Transition extends SimpleTransition implements DocumentOrder {
32  
33      /**
34       * The document order of this transition
35       */
36      private int order;
37  
38      /**
39       * Property that specifies the trigger(s) for this transition.
40       */
41      private String event;
42  
43      /**
44       * This transition event descriptors
45       */
46      private List<String> events = Collections.emptyList();
47  
48      /**
49       * Indicator for a event-less transition
50       */
51      private boolean noEvents;
52  
53      /**
54       * Indicator for a transition matching all events (*)
55       */
56      private boolean allEvents;
57  
58      /**
59       * Optional guard condition.
60       */
61      private String cond;
62  
63      /**
64       * Constructor.
65       */
66      public Transition() {
67          super();
68      }
69  
70  
71      /**
72       * @return the document order of this transition
73       * @see DocumentOrder
74       */
75      @Override
76      public final int getOrder() {
77          return order;
78      }
79  
80      /**
81       * Sets the document order of this transition
82       * @param order the document order
83       * @see DocumentOrder
84       */
85      public final void setOrder(int order) {
86          this.order = order;
87      }
88  
89      /**
90       * Get the guard condition (may be null).
91       *
92       * @return Returns the cond.
93       */
94      public String getCond() {
95          return cond;
96      }
97  
98      /**
99       * Set the guard condition.
100      *
101      * @param cond The cond to set.
102      */
103     public void setCond(final String cond) {
104         this.cond = cond;
105     }
106 
107     /**
108      * Get the event that will trigger this transition (pending
109      * evaluation of the guard condition in favor).
110      *
111      * @return Returns the event.
112      */
113     public String getEvent() {
114         return event;
115     }
116 
117     /**
118      * Set the event that will trigger this transition (pending
119      * evaluation of the guard condition in favor).
120      *
121      * @param event The event to set.
122      */
123     public void setEvent(final String event) {
124         this.event = event == null ? null : event.trim();
125         if (this.event != null) {
126             // 'event' is a space separated list of event descriptors
127             events = new ArrayList<String>();
128             StringTokenizer st = new StringTokenizer(this.event);
129             while (st.hasMoreTokens()) {
130                 String token = st.nextToken();
131                 if (token.equals("*")) {
132                     events.clear();
133                     events.add(token);
134                     break;
135                 }
136                 else {
137                     if (token.endsWith("*")) {
138                         token = token.substring(0, token.length()-1);
139                     }
140                     if (token.endsWith(".")) {
141                         token = token.substring(0, token.length()-1);
142                     }
143                     if (token.length() > 0) {
144                         events.add(token);
145                     }
146                 }
147             }
148         }
149         else {
150             events = Collections.emptyList();
151         }
152         noEvents = events.isEmpty();
153         allEvents = !noEvents && events.get(0).equals("*");
154     }
155 
156     /**
157      * @return The list of this transition event descriptors
158      */
159     public final List<String> getEvents() {
160         return events;
161     }
162 
163     /**
164      * @return True if this transition is event-less
165      */
166     public final boolean isNoEventsTransition() {
167         return noEvents;
168     }
169 
170     /**
171      * @return True if this transition matches any events (*)
172      */
173     public final boolean isAllEventsTransition() {
174         return allEvents;
175     }
176 }