gate.fsm
Class FSM

java.lang.Object
  extended by gate.fsm.FSM
All Implemented Interfaces:
JapeConstants, Serializable

public class FSM
extends Object
implements JapeConstants

This class implements a standard Finite State Machine. It is used for both deterministic and non-deterministic machines.

See Also:
Serialized Form

Field Summary
private  Collection allStates
          The set of states for this FSM
(package private)  int bpeId
           
protected  State currentState
          Two members used by forEachState().
protected  Transition currentTransition
           
private static boolean DEBUG
          Debug flag
private  Set<AbstractSet> dStates
           
protected  State finalState
          The final state of this FSM (usually only valid during construction).
private  State initialState
          The initial state of this FSM.
private  Map<AbstractSet,State> newStates
           
 HashMap<String,String> ruleHash
           
private  ArrayList<RuleTime> ruleTimes
           
 
Fields inherited from interface gate.jape.JapeConstants
ALL_STYLE, APPELT_STYLE, BRILL_STYLE, DEFAULT_PRIORITY, FIRST_STYLE, INDENT_PADDING, KLEENE_PLUS, KLEENE_QUERY, KLEENE_STAR, MULTI_SPAN_BINDING, NO_BINDING, NO_KLEENE_OP, ONCE_STYLE, SINGLE_SPAN_BINDING
 
Constructor Summary
protected FSM()
          The constructor that all the other constructors should call.
protected FSM(ComplexPatternElement cpe)
          Builds a FSM starting from a ComplexPatternElement.
  FSM(Rule rule)
          Builds a FSM starting from a rule.
  FSM(SinglePhaseTransducer spt)
          Builds a standalone FSM starting from a single phase transducer.
 
Method Summary
protected  void addRules(PrioritisedRuleList rules)
          Do the work involved in creating an FSM from a PrioritisedRuleList.
 String asGraphViz(boolean includeConstraints)
          Returns a representation of this FSM in the GraphViz graph-visualization language.
private  State convertComplexPE(State startState, ComplexPatternElement cpe, LinkedList<String> labels)
          Receives a state to start from and a complex pattern element.
private  String currentBasicBinding(BasicPatternElement bpe)
           
private  String currentBinding(ComplexPatternElement cpe, int indent)
           
private  String currentLHSBinding(LeftHandSide lhs)
           
private  void decorateStates()
           
 void eliminateVoidTransitions()
          Converts this FSM from a non-deterministic to a deterministic one by eliminating all the unrestricted transitions.
(package private)  String encodeForGraphViz(Map<String,String> m)
          Given a Map, encodes its keys and values as strings suitable for use as a GraphViz label.
protected  void forEachState(Runnable r)
          Iterates over all the states in this FSM, setting currentState and currentTransition, then calling the given Runnable callback.
private  State generateStates(State startState, ComplexPatternElement cpe, LinkedList<String> labels)
          Receives a state to start from and a complex pattern element.
 Map<State,SimpleArraySet<Transition>> getAllStates()
           
 String getGML()
          Returns a GML (Graph Modelling Language) representation of the transition graph of this FSM.
 State getInitialState()
          Gets the initial state of this FSM
 ArrayList<RuleTime> getRuleTimes()
           
private  AbstractSet<State> lambdaClosure(AbstractSet<State> s)
           
protected  void setRule(Rule rule)
          Do the work involved in creating an FSM from a Rule.
protected  FSM spawn(ComplexPatternElement currentPattern)
          A factory method for new FSMs like this one, given a ComplexPatternElement object.
protected  FSM spawn(Rule r)
          A factory method for new FSMs like this one, given a Rule object.
 String toString()
          Returns a textual description of this FSM.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ruleTimes

private ArrayList<RuleTime> ruleTimes

DEBUG

private static final boolean DEBUG
Debug flag

See Also:
Constant Field Values

currentState

protected State currentState
Two members used by forEachState().


currentTransition

protected Transition currentTransition

initialState

private State initialState
The initial state of this FSM.


finalState

protected State finalState
The final state of this FSM (usually only valid during construction).


allStates

private transient Collection allStates
The set of states for this FSM


newStates

private transient Map<AbstractSet,State> newStates

dStates

private transient Set<AbstractSet> dStates

bpeId

int bpeId

ruleHash

public HashMap<String,String> ruleHash
Constructor Detail

FSM

protected FSM()
The constructor that all the other constructors should call.


FSM

public FSM(SinglePhaseTransducer spt)
Builds a standalone FSM starting from a single phase transducer.

Parameters:
spt - the single phase transducer to be used for building this FSM.

FSM

public FSM(Rule rule)
Builds a FSM starting from a rule. This FSM is actually a part of a larger one (usually the one that is built based on the single phase transducer that contains the rule). built by this constructor.

Parameters:
rule - the rule to be used for the building process.

FSM

protected FSM(ComplexPatternElement cpe)
Builds a FSM starting from a ComplexPatternElement. This FSM is usually part of a larger FSM based on the Rule that contains the ComplexPatternElement.

Parameters:
cpe - the ComplexPatternElement to be used for the building process.
Method Detail

getRuleTimes

public ArrayList<RuleTime> getRuleTimes()

decorateStates

private void decorateStates()

addRules

protected void addRules(PrioritisedRuleList rules)
Do the work involved in creating an FSM from a PrioritisedRuleList.


setRule

protected void setRule(Rule rule)
Do the work involved in creating an FSM from a Rule.


spawn

protected FSM spawn(Rule r)
A factory method for new FSMs like this one, given a Rule object.


spawn

protected FSM spawn(ComplexPatternElement currentPattern)
A factory method for new FSMs like this one, given a ComplexPatternElement object.


getInitialState

public State getInitialState()
Gets the initial state of this FSM

Returns:
an object of type gate.fsm.State representing the initial state.

convertComplexPE

private State convertComplexPE(State startState,
                               ComplexPatternElement cpe,
                               LinkedList<String> labels)
Receives a state to start from and a complex pattern element. Parses the complex pattern element and creates all the necessary states and transitions for accepting annotations described by the given PE.

Parameters:
startState - the state to start from
cpe - the pattern to be recognized
labels - the bindings name for all the annotation accepted along the way. This is actually a list of Strings. It is necessary to use a list because of the recursive definition of ComplexPatternElement.
Returns:
the final state reached after accepting a sequence of annotations as described in the pattern

generateStates

private State generateStates(State startState,
                             ComplexPatternElement cpe,
                             LinkedList<String> labels)
Receives a state to start from and a complex pattern element. Parses the complex pattern element and creates all the necessary states and transitions for traversing the annotations described by the given PE exactly once. Does not add any transitions for kleene operators.

Parameters:
startState - the state to start from
cpe - the pattern to be recognized
labels - the bindings name for all the annotation accepted along the way. This is actually a list of Strings. It is necessary to use a list because of the recursive definition of ComplexPatternElement.
Returns:
the final state reached after accepting a sequence of annotations as described in the pattern

eliminateVoidTransitions

public void eliminateVoidTransitions()
Converts this FSM from a non-deterministic to a deterministic one by eliminating all the unrestricted transitions.


lambdaClosure

private AbstractSet<State> lambdaClosure(AbstractSet<State> s)

forEachState

protected void forEachState(Runnable r)
Iterates over all the states in this FSM, setting currentState and currentTransition, then calling the given Runnable callback.


getAllStates

public Map<State,SimpleArraySet<Transition>> getAllStates()
Returns:
a Map whose keys contain the states of this FSM, and whose values contain their corresponding transitions. This method actually walks the FSM, so it may be called before the FSM is finalized with compactTransitions().

asGraphViz

public String asGraphViz(boolean includeConstraints)
Returns a representation of this FSM in the GraphViz graph-visualization language. We use the "digraph" (directed graph) format. Nodes are labeled by their numerical indexes. A node's shape is a diamond if it's the initial state, and round otherwise. A node is green if it's an initial state, red if it's a final state, and black otherwise. Final states are also marked with a double-line outline.

Parameters:
includeConstraints - whether to include a stringified representation of each transition object as part of its label. The default is false.
Returns:
See Also:
the GraphViz web site for software to translate the output of this method into pretty pictures.

encodeForGraphViz

String encodeForGraphViz(Map<String,String> m)
Given a Map, encodes its keys and values as strings suitable for use as a GraphViz label. Embedded "\r\n" sequences are replaced by "\\l" to create line feeds, and embedded backslashes are escaped. The returned String takes the form "key1=value1, key2=value2, ...".


getGML

public String getGML()
Returns a GML (Graph Modelling Language) representation of the transition graph of this FSM.


toString

public String toString()
Returns a textual description of this FSM.

Overrides:
toString in class Object

currentBinding

private String currentBinding(ComplexPatternElement cpe,
                              int indent)

currentBasicBinding

private String currentBasicBinding(BasicPatternElement bpe)

currentLHSBinding

private String currentLHSBinding(LeftHandSide lhs)