import java.io.*; import java.util.*; // We are implementing a state machine. The state machine is logically implemented // as a graph. Individual nodes represent states. Edges represent transitions // between states. This class defines and declares the edge entity. Every transition // is associated with a trigger event, uniquely identified by a alphabetic, case-sensitive, // letter which will cause the transition to occur. We force the user to create all // transition objects with an associated letter. Hence, transition objects are always // valid, and so no means is provided to check the validity of this class. // Assumption: neither the Letter nor the State is deleted after the transition is created. public class Transition { private Letter trigger; // holds the trigger which causes this transition to occur. private State destination; // reference to the node we get to when this transition is triggered // all transition objects must be associated with a destination node and a letter object. public Transition( Letter trigger, State destination ) throws FSMException { if ( (trigger == null) || (destination == null) ) { NullValueException n = new NullValueException("Transition objects can not be created without a valid destination State and event Letter."); throw n; } // end if this.trigger = trigger; this.destination = destination; } // end constructor Transition // Print a message to standard output describing this transition // Primarily exists to support debugging public void displayMe() { System.out.print("(transition "); trigger.displayMe(); System.out.print(" (destination "+destination.getLabel()+")"); // Don't recurse System.out.println(")"); } // end method display_me // return to caller trigger label public String getTriggerLbl() { return trigger.getLabel(); } public String getStateLbl() { return destination.getLabel(); } // returns reference to State of new state if transition will occur based // upon the specified Letter. Otherwise returns Null. public State doesTransitionOccur( Letter event ) { State result; // we use a string compare to determine if the label of the event that occured // matches the label of the trigger that causes this transition. If so, return the // new state. if ( event.getLabel().equals(trigger.getLabel()) ) { result = destination; } else { result = null; } // end if return result; } // end doesTransitionOccur } // end class Transition