001 /*
002 * FSMState.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Valentin Tablan, 11/07/2000
013 *
014 * $Id: FSMState.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015 */
016
017 package gate.creole.gazetteer;
018
019 import java.io.Serializable;
020 import java.util.HashSet;
021 import java.util.Set;
022 import gate.creole.gazetteer.DefaultGazetteer.CharMap;
023
024 /** Implements a state of the deterministic finite state machine of the
025 * gazetter.
026 *
027 */
028 public class FSMState implements Serializable {
029
030 /** Debug flag
031 */
032 private static final boolean DEBUG = false;
033
034 /** Constructs a new FSMState object and adds it to the list of
035 * states of the {@link DefaultGazetteer} provided as owner.
036 *
037 * @param owner a {@link DefaultGazetteer} object
038 */
039 public FSMState(DefaultGazetteer owner) {
040 myIndex = index++;
041 owner.fsmStates.add(this);
042 }
043
044 /** Adds a new value to the transition function
045 */
046 // >>> DAM: was - to use CharMap
047 /*
048 void put(Character chr, FSMState state) {
049 transitionFunction.put(chr,state);
050 }
051 */
052 // >>> DAM: TransArray optimization
053 public void put(char chr, FSMState state) {
054 transitionFunction.put(chr,state);
055 }
056 // >>> DAM: end
057
058 /** This method is used to access the transition function of this state.
059 */
060 // >>> DAM: was
061 /*
062 FSMState next(Character chr) {//UnicodeType type){
063 return (FSMState)transitionFunction.get(chr);
064 }
065 */
066 // >>> DAM: TransArray optimization
067 public FSMState next(char chr) {//UnicodeType type){
068 return (FSMState)transitionFunction.get(chr);
069 }
070 // >>> DAM: end
071
072 /** Returns a GML (Graph Modelling Language) representation of the edges
073 * emerging from this state.
074 */
075 //<<< DAM: was - to use new char Iter returned by the CharMap iteratior
076 /*
077 String getEdgesGML() {
078 String res = "";
079 Iterator charsIter = transitionFunction.keySet().iterator();
080 Character currentChar;
081 FSMState nextState;
082
083 while(charsIter.hasNext()){
084 currentChar = (Character)charsIter.next();
085 nextState = next(currentChar);
086 res += "edge [ source " + myIndex +
087 " target " + nextState.getIndex() +
088 " label \"'" + currentChar + "'\" ]\n";
089 }
090 */
091 // DAM, TransArray optimization
092 public String getEdgesGML() {
093 String res = "";
094 char currentChar;
095 FSMState nextState;
096
097 for (int i = 0; i < transitionFunction.itemsKeys.length; i++)
098 {
099 currentChar = transitionFunction.itemsKeys[i];
100 nextState = next(currentChar);
101 res += "edge [ source " + myIndex +
102 " target " + nextState.getIndex() +
103 " label \"'" + currentChar + "'\" ]\n";
104 }
105 // >>> DAM, end
106 return res;
107 }
108
109 /** Checks whether this state is a final one
110 */
111 public boolean isFinal() {
112 // >>> was
113 // return !lookupSet.isEmpty();
114 // >>> BOBI, Lookup opitimization
115 if (lookupSet==null)
116 return false;
117 return !lookupSet.isEmpty();
118 // >>> end
119 }
120
121 /** Returns a set of {@link Lookup} objects describing the types of lookups
122 * the phrase for which this state is the final one belongs to
123 */
124 public Set getLookupSet(){return lookupSet;}
125
126 /** Adds a new looup description to this state's lookup descriptions set
127 */
128 public void addLookup(Lookup lookup) {
129 // >>> was nothing
130 // >>> BOBI, Lookup opitimization
131 if (lookupSet == null)
132 lookupSet = new HashSet(4);
133 // >>> end
134
135 lookupSet.add(lookup);
136 } // addLookup
137
138 /** Removes a looup description from this state's lookup descriptions set
139 */
140 public void removeLookup(Lookup lookup) {
141 lookupSet.remove(lookup);
142 } // removeLookup
143
144 /** Returns the unique ID of this state.
145 */
146 public int getIndex(){ return myIndex; }
147
148
149 /** The transition function of this state.
150 */
151 // >>> was
152 // Map transitionFunction = new HashMap();
153 // >>> NASO, hash4 optimization
154 // Map transitionFunction = new HashMap(4);
155 // >>> DAM, TransArray
156 protected CharMap transitionFunction = new CharMap();
157 // >>> end
158
159 /** *
160 */
161 // >>> was
162 // Set lookupSet = new HashSet();
163 // >>> NASO, hash4 optimization
164 // Set lookupSet = new HashSet(4);
165 // >>> BOBI, Lookup opitimization
166 protected Set lookupSet;
167 // >>> end
168
169 /**
170 * The unique id of this state. This value is never used by the algorithms but
171 * it can be useful for graphical representations.
172 */
173 protected int myIndex;
174
175 /**
176 * Class member used to generate unique ids for the instances
177 *
178 */
179 private static int index;
180
181 static{
182 index = 0;
183 }
184
185 } // class FSMState
|