KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > fa > FAState


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.works.visualization.fa;
33
34 import org.antlr.analysis.NFAState;
35
36 import java.util.*;
37
38 /** Class defining an "GUI" FA state. It is basically the same as a NFAState.
39  *
40  */

41
42 public class FAState {
43
44     public int stateNumber = -1;
45     public boolean acceptedState = false;
46     public String JavaDoc enclosingRuleName = null;
47     public List<FATransition> transitions = new ArrayList<FATransition>();
48
49     /** If the state represents a reference to an external state, this field
50      contains the name of the referenced rule. */

51     public String JavaDoc externalRuleRefName = null;
52
53     /** List of skipped states (they won't be displayed but we need to know their
54      number in order to display corrected the error paths) */

55     public Set skippedStates = new HashSet();
56
57     /** Temporary variable that is used by FAFactory to know when to build "loop" transition */
58     public boolean loop = false;
59
60     public FAState(int stateNumber) {
61         this.stateNumber = stateNumber;
62     }
63
64     public FAState(NFAState state) {
65         this.stateNumber = state.stateNumber;
66         this.acceptedState = state.isAcceptState();
67         this.enclosingRuleName = state.getEnclosingRule();
68     }
69
70     public FAState(String JavaDoc externalRuleRefName) {
71         this.externalRuleRefName = externalRuleRefName;
72     }
73
74     public void addTransition(FATransition transition) {
75         transition.setSourceState(this);
76         transitions.add(transition);
77         sortTransitions();
78     }
79
80     public void addTransition(FATransition transition, boolean loop) {
81         transition.setSourceState(this);
82         transition.setLoop(loop);
83         transitions.add(transition);
84         sortTransitions();
85     }
86
87     private void sortTransitions() {
88         if(transitions.size()<=1)
89             return;
90
91         // We assume here that only one transition is a loop. This transition should always be
92
// located at the end of the list.
93

94         for(int t=0; t<transitions.size(); t++) {
95             FATransition transition = transitions.get(t);
96             if(transition.loop && t<transitions.size()-1) {
97                 // This loop transition is not at the end of the list. Move it.
98
Collections.swap(transitions, t, transitions.size()-1);
99                 break;
100             }
101         }
102     }
103
104     public FATransition getFirstTransition() {
105         if(transitions.isEmpty())
106             return null;
107         else
108             return transitions.get(0);
109     }
110
111     public FATransition transition(int index) {
112         return transitions.get(index);
113     }
114
115     public int getNumberOfTransitions(){
116         return transitions.size();
117     }
118
119     public FAState getNextFirstState() {
120         return getFirstTransition().target;
121     }
122
123     public FATransition getTransitionToStateNumber(int stateNumber) {
124         for (FATransition transition : transitions) {
125             if (transition.target.stateNumber == stateNumber)
126                 return transition;
127         }
128         return null;
129     }
130
131     /** Find the transition whose target contains a transition with a label
132      * containing the name of the external rule
133      */

134
135     public FATransition getTransitionToExternalStateRule(String JavaDoc externalRule) {
136         for (FATransition tr : transitions) {
137             if (tr.target == null)
138                 continue;
139
140             // Lookup the target's transitions label to see if one of them match the name
141
// of the external rule
142
for (FATransition targetTr : tr.target.transitions) {
143                 if (targetTr.label != null && targetTr.label.equals(externalRule))
144                     return tr;
145             }
146         }
147         return null;
148     }
149
150     /** This method returns true if the state is an alternative. "loop" transitions are skipped.
151      *
152      */

153
154     public boolean isAlternative() {
155         return getNumberOfTransitions() > 1;
156     }
157
158     /** This method returns true if the state is single state (that is with only one non-loop transition)
159      *
160      */

161
162     public boolean isSingle() {
163         return getNumberOfTransitions() == 1;
164     }
165
166     public int hashCode() {
167         return stateNumber;
168     }
169
170     public boolean equals(Object JavaDoc o) {
171         if(o instanceof FAState) {
172             FAState otherState = (FAState)o;
173             return stateNumber == otherState.stateNumber;
174         } else {
175             return false;
176         }
177     }
178
179     public String JavaDoc toString() {
180         if(externalRuleRefName == null)
181             return String.valueOf(stateNumber);
182         else
183             return "<"+externalRuleRefName+">";
184     }
185
186 }
Popular Tags