| Transition.java |
1 /*
2 * Transition.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/Apr/2000
12 *
13 * $Id: Transition.java,v 1.15 2004/07/21 17:10:06 akshay Exp $
14 */
15
16 package gate.fsm;
17
18 import java.io.Serializable;
19 import java.util.LinkedList;
20
21 import gate.jape.BasicPatternElement;
22
23 /**
24 * This class implements a Finite State Machine transition.
25 * A transition is owned by a gate.fsm.State object and contains set of
26 * restrictions and a reference to the next state that will be accessed after
27 * consuming a set of input symbols according to the restrictions.
28 * A transition can also hold information about the label that should be bound
29 * to the symbols (annotations) consumed during the state transition.
30 */
31 // >>> DAM
32 /*
33 public class Transition implements Serializable {
34 */
35 // >>> DAM, TransArray optimzation, now implements the Comparable interface
36 public class Transition implements Serializable, Comparable {
37 // >>> DAM, end
38
39 /** Debug flag */
40 private static final boolean DEBUG = false;
41
42 /**
43 * Default constructor. Creates a new transition with a new unique index.
44 * This constructor should be called by all other constructors.
45 */
46 public Transition() {
47 myIndex = Transition.index++;
48 }
49
50 /**
51 * Creates a new transition using the given set of constraints and target
52 * state.
53 * @param constraints the set on constraints associated to this transition
54 * @param state the target state of this transition
55 */
56 public Transition(BasicPatternElement constraints, State state) {
57 this();
58 this.constraints = constraints;
59 target = state;
60 bindings = new LinkedList();
61 }
62
63 /**
64 * Ctreates a new transition from a set of constraints, a target state and a
65 * list of labels to be bound with the recognized input symbols
66 * (aka annotations).
67 */
68 public Transition(BasicPatternElement constraints, State state,
69 LinkedList bindings) {
70 this();
71 this.constraints = constraints;
72 target = state;
73 this.bindings = bindings;
74 }
75
76 /**
77 * Gets the target state of this transition
78 * @return an object of type gate.fsm.State
79 */
80 public State getTarget(){ return target; }
81
82 /**
83 * Gets the constraints associated to this transition
84 */
85 public BasicPatternElement getConstraints(){ return constraints; }
86
87 /**
88 * Returns a textual desciption of this transition.
89 * @return a String
90 */
91 public String toString(){
92 String res = "If: " + constraints + " then ->: " + target.getIndex();
93 return res;
94 }
95
96 /**
97 * Returns a shorter description that toSting().
98 * Actually, it returns the unique index in String form.
99 */
100 public String shortDesc(){
101 String res = "" + myIndex;
102 return res;
103 }
104
105 /**
106 * Returns the list of bindings associated to this transition
107 */
108 public LinkedList getBindings(){ return bindings; }
109
110 /**
111 * The constraints on this transition.
112 */
113 private BasicPatternElement constraints;
114
115 /**
116 * The state this transition leads to
117 */
118 private State target;
119
120 /**
121 * A list with all the labels associated to the annotations recognized by
122 * this transition.
123 * We need to use the actual object and not the interface (java.util.List)
124 * because we need this object to be cloneable
125 */
126 private LinkedList bindings;
127
128 /** The unique index of this transition. This value is not used by any of
129 * the algorithms. It is only provided as a convenient method of identifying
130 * the transitions in textual representations (toString() and GML related
131 * methods)
132 */
133 private int myIndex;
134
135 /** Static member used for generating unique IDs for the objects of type
136 * Transition*/
137 private static int index = 0;
138
139 // >>> DAM, TransArray optimzation, now implements the Comparable interface
140 public int compareTo(Object o)
141 throws ClassCastException
142 {
143 if (!(o instanceof Transition)) throw new ClassCastException("gate.frm.Transition(compareTo)");
144 return myIndex - ((Transition)o).myIndex;
145 }
146 // >>> DAM, end
147 } // Transition
148