1   /*
2    *  FSMState.java
3    *
4    *  Copyright (c) 1998-2001, 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.7 2001/11/05 16:14:22 nasso Exp $
14   */
15  
16  package gate.creole.gazetteer;
17  
18  import java.util.*;
19  import java.io.*;
20  
21  import gate.creole.tokeniser.*;
22  import gate.util.*;
23  
24  /** Implements a state of the deterministic finite state machine of the
25   * gazetter.
26   *
27   */
28  class FSMState implements Serializable {
29  
30    /** Debug flag
31     */
32    private static final boolean DEBUG = false;
33  
34    /** Constructs a new FSMState object and adds it to the list of
35     * states of the {@link DefaultGazetteer} provided as owner.
36     *
37     * @param owner a {@link DefaultGazetteer} object
38     */
39    public FSMState(DefaultGazetteer owner) {
40      myIndex = index++;
41      owner.fsmStates.add(this);
42    }
43  
44    /** Adds a new value to the transition function
45     */
46  // >>> DAM: was - to use charMap
47  /*
48    void put(Character chr, FSMState state) {
49      transitionFunction.put(chr,state);
50    }
51  */
52  // >>> DAM: TransArray optimization
53    void put(char chr, FSMState state) {
54      transitionFunction.put(chr,state);
55    }
56  // >>> DAM: end
57  
58    /** This method is used to access the transition function of this state.
59     */
60  // >>> DAM: was
61  /*
62    FSMState next(Character chr) {//UnicodeType type){
63      return (FSMState)transitionFunction.get(chr);
64    }
65    */
66  // >>> DAM: TransArray optimization
67    FSMState next(char chr) {//UnicodeType type){
68      return (FSMState)transitionFunction.get(chr);
69    }
70  // >>> DAM: end
71  
72    /** Returns a GML (Graph Modelling Language) representation of the edges
73     * emerging from this state.
74     */
75  //<<< DAM: was - to use new char Iter returned by the charMap iteratior
76  /*
77    String getEdgesGML() {
78      String res = "";
79      Iterator charsIter = transitionFunction.keySet().iterator();
80      Character currentChar;
81      FSMState nextState;
82  
83      while(charsIter.hasNext()){
84        currentChar = (Character)charsIter.next();
85        nextState = next(currentChar);
86        res += "edge [ source " + myIndex +
87        " target " + nextState.getIndex() +
88        " label \"'" + currentChar + "'\" ]\n";
89      }
90  */
91  // DAM, TransArray optimization
92    String getEdgesGML() {
93      String res = "";
94      char currentChar;
95      FSMState nextState;
96  
97      for (int i = 0; i < transitionFunction.itemsKeys.length; i++)
98      {
99        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   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   Set getLookupSet(){return lookupSet;}
125 
126   /** Adds a new looup description to this state's lookup descriptions set
127    */
128   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   void removeLookup(Lookup lookup) {
141     lookupSet.remove(lookup);
142   } // removeLookup
143 
144   /** Returns the unique ID of this state.
145    */
146   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 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   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   int myIndex;
174 
175   /**
176    * Class memebre used to generate unique ids for the instances
177    *
178    */
179   static int index;
180 
181   static{
182     index = 0;
183   }
184 
185 } // class FSMState
186