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, 27/06/2000 12 * 13 * $Id: FSMState.java,v 1.13 2005/01/11 13:51:33 ian Exp $ 14 */ 15 16 package gate.creole.tokeniser; 17 18 import java.util.*; 19 20 /** A state of the finite state machine that is the kernel tokeniser 21 */ 22 class FSMState implements java.io.Serializable { 23 24 /** Debug flag */ 25 private static final boolean DEBUG = false; 26 27 /** Creates a new FSMState belonging to a specified tokeniser 28 * @param owner the tokeniser that contains this new state 29 */ 30 public FSMState(SimpleTokeniser owner) { 31 myIndex = index++; 32 owner.fsmStates.add(this); 33 } 34 35 /** Returns the value of the transition function of this state for a given 36 * Unicode type. 37 * As this state can belong to a non-deterministic automaton, the result 38 * will be a set. 39 */ 40 Set nextSet(UnicodeType type) { 41 if(null == type) return transitionFunction[SimpleTokeniser.maxTypeId]; 42 else return transitionFunction[type.type]; 43 } // nextSet(UnicodeType type) 44 45 /** Returns the value of the transition function of this state for a given 46 * Unicode type specified using the internal ids used by the tokeniser. 47 * As this state can belong to a non-deterministic automaton, the result 48 * will be a set. 49 */ 50 Set nextSet(int type) { 51 return transitionFunction[type]; 52 } // nextSet(int type) 53 54 /** Adds a new transition to the transition function of this state 55 * @param type the restriction for the new transition; if <code>null</code> 56 * this transition will be unrestricted. 57 * @param state the vaule of the transition function for the given type 58 */ 59 void put(UnicodeType type, FSMState state) { 60 if(null == type) put(SimpleTokeniser.maxTypeId, state); 61 else put(type.type, state); 62 } // put(UnicodeType type, FSMState state) 63 64 /** Adds a new transition to the transition function of this state 65 * @param index the internal index of the Unicode type representing the 66 * restriction for the new transition; 67 * @param state the vaule of the transition function for the given type 68 */ 69 void put(int index, FSMState state) { 70 if(null == transitionFunction[index]) 71 transitionFunction[index] = new HashSet(); 72 transitionFunction[index].add(state); 73 } // put(int index, FSMState state) 74 75 /** Sets the RHS string value */ 76 void setRhs(String rhs) { this.rhs = rhs; } 77 78 /** Gets the RHS string value */ 79 String getRhs() { return rhs; } 80 81 /** Checks whether this state is a final one */ 82 boolean isFinal() { return (null != rhs); } 83 84 /** Gets the unique id of this state */ 85 int getIndex() { return myIndex; } 86 87 /** Returns a GML representation of all the edges emerging 88 * from this state */ 89 String getEdgesGML() { 90 /// String res = ""; 91 StringBuffer res = new StringBuffer(gate.Gate.STRINGBUFFER_SIZE); 92 Set nextSet; 93 Iterator nextSetIter; 94 FSMState nextState; 95 96 for(int i = 0; i <= SimpleTokeniser.maxTypeId; i++){ 97 nextSet = transitionFunction[i]; 98 if(null != nextSet){ 99 nextSetIter = nextSet.iterator(); 100 while(nextSetIter.hasNext()){ 101 nextState = (FSMState)nextSetIter.next(); 102 /* res += "edge [ source " + myIndex + 103 " target " + nextState.getIndex() + 104 " label \""; 105 */ 106 res.append("edge [ source "); 107 res.append(myIndex); 108 res.append(" target "); 109 res.append(nextState.getIndex()); 110 res.append(" label \""); 111 112 if(i == SimpleTokeniser.maxTypeId) ///res += "[]"; 113 res.append("[]"); 114 else ///res += SimpleTokeniser.typeMnemonics[i]; 115 res.append(SimpleTokeniser.typeMnemonics[i]); 116 117 ///res += "\" ]\n"; 118 res.append("\" ]\n"); 119 }//while(nextSetIter.hasNext()) 120 } 121 }; 122 return res.toString(); 123 } // getIndex 124 125 /** The transition function of this state. It's an array mapping from int 126 * (the ids used internally by the tokeniser for the Unicode types) to sets 127 * of states. 128 */ 129 Set[] transitionFunction = new Set[SimpleTokeniser.maxTypeId + 1]; 130 131 /** The RHS string value from which the annotation associated to 132 * final states is constructed. 133 */ 134 String rhs; 135 136 /**the unique index of this state*/ 137 int myIndex; 138 139 /**used for generating unique ids*/ 140 static int index; 141 142 static{ 143 index = 0; 144 } 145 146 } // class FSMState 147