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