| FSMState.java |
1 /*
2 * FSMState.java
3 *
4 * Copyright (c) 1998-2005, The University of Sheffield.
5 *
6 * This file is part of GATE (see http://gate.ac.uk/), and is free
7 * software, licenced under the GNU Library General Public License,
8 * Version 2, June 1991 (in the distribution as file licence.html,
9 * and also available at http://gate.ac.uk/gate/licence.html).
10 *
11 * Valentin Tablan, 11/07/2000
12 *
13 * $Id: FSMState.java,v 1.12 2006/03/22 11:21:38 valyt Exp $
14 */
15
16 package gate.creole.gazetteer;
17
18 import java.io.Serializable;
19 import java.util.HashSet;
20 import java.util.Set;
21 import gate.creole.gazetteer.DefaultGazetteer.CharMap;
22
23 /** Implements a state of the deterministic finite state machine of the
24 * gazetter.
25 *
26 */
27 public class FSMState implements Serializable {
28
29 /** Debug flag
30 */
31 private static final boolean DEBUG = false;
32
33 /** Constructs a new FSMState object and adds it to the list of
34 * states of the {@link DefaultGazetteer} provided as owner.
35 *
36 * @param owner a {@link DefaultGazetteer} object
37 */
38 public FSMState(DefaultGazetteer owner) {
39 myIndex = index++;
40 owner.fsmStates.add(this);
41 }
42
43 /** Adds a new value to the transition function
44 */
45 // >>> DAM: was - to use CharMap
46 /*
47 void put(Character chr, FSMState state) {
48 transitionFunction.put(chr,state);
49 }
50 */
51 // >>> DAM: TransArray optimization
52 public void put(char chr, FSMState state) {
53 transitionFunction.put(chr,state);
54 }
55 // >>> DAM: end
56
57 /** This method is used to access the transition function of this state.
58 */
59 // >>> DAM: was
60 /*
61 FSMState next(Character chr) {//UnicodeType type){
62 return (FSMState)transitionFunction.get(chr);
63 }
64 */
65 // >>> DAM: TransArray optimization
66 public FSMState next(char chr) {//UnicodeType type){
67 return (FSMState)transitionFunction.get(chr);
68 }
69 // >>> DAM: end
70
71 /** Returns a GML (Graph Modelling Language) representation of the edges
72 * emerging from this state.
73 */
74 //<<< DAM: was - to use new char Iter returned by the CharMap iteratior
75 /*
76 String getEdgesGML() {
77 String res = "";
78 Iterator charsIter = transitionFunction.keySet().iterator();
79 Character currentChar;
80 FSMState nextState;
81
82 while(charsIter.hasNext()){
83 currentChar = (Character)charsIter.next();
84 nextState = next(currentChar);
85 res += "edge [ source " + myIndex +
86 " target " + nextState.getIndex() +
87 " label \"'" + currentChar + "'\" ]\n";
88 }
89 */
90 // DAM, TransArray optimization
91 public String getEdgesGML() {
92 String res = "";
93 char currentChar;
94 FSMState nextState;
95
96 for (int i = 0; i < transitionFunction.itemsKeys.length; i++)
97 {
98 currentChar = transitionFunction.itemsKeys[i];
99 nextState = next(currentChar);
100 res += "edge [ source " + myIndex +
101 " target " + nextState.getIndex() +
102 " label \"'" + currentChar + "'\" ]\n";
103 }
104 // >>> DAM, end
105 return res;
106 }
107
108 /** Checks whether this state is a final one
109 */
110 public boolean isFinal() {
111 // >>> was
112 // return !lookupSet.isEmpty();
113 // >>> BOBI, Lookup opitimization
114 if (lookupSet==null)
115 return false;
116 return !lookupSet.isEmpty();
117 // >>> end
118 }
119
120 /** Returns a set of {@link Lookup} objects describing the types of lookups
121 * the phrase for which this state is the final one belongs to
122 */
123 public Set getLookupSet(){return lookupSet;}
124
125 /** Adds a new looup description to this state's lookup descriptions set
126 */
127 public void addLookup(Lookup lookup) {
128 // >>> was nothing
129 // >>> BOBI, Lookup opitimization
130 if (lookupSet == null)
131 lookupSet = new HashSet(4);
132 // >>> end
133
134 lookupSet.add(lookup);
135 } // addLookup
136
137 /** Removes a looup description from this state's lookup descriptions set
138 */
139 public void removeLookup(Lookup lookup) {
140 lookupSet.remove(lookup);
141 } // removeLookup
142
143 /** Returns the unique ID of this state.
144 */
145 public int getIndex(){ return myIndex; }
146
147
148 /** The transition function of this state.
149 */
150 // >>> was
151 // Map transitionFunction = new HashMap();
152 // >>> NASO, hash4 optimization
153 // Map transitionFunction = new HashMap(4);
154 // >>> DAM, TransArray
155 protected CharMap transitionFunction = new CharMap();
156 // >>> end
157
158 /** *
159 */
160 // >>> was
161 // Set lookupSet = new HashSet();
162 // >>> NASO, hash4 optimization
163 // Set lookupSet = new HashSet(4);
164 // >>> BOBI, Lookup opitimization
165 protected Set lookupSet;
166 // >>> end
167
168 /**
169 * The unique id of this state. This value is never used by the algorithms but
170 * it can be useful for graphical representations.
171 */
172 protected int myIndex;
173
174 /**
175 * Class member used to generate unique ids for the instances
176 *
177 */
178 private static int index;
179
180 static{
181 index = 0;
182 }
183
184 } // class FSMState
185