< Previous | Next >

Configuring the Idle state

When it is in the Idle state, the state machine is quite simply waiting for an event, in the form of a coin, to arrive.

A state is a discrete stage in a business transaction. The state begins with the running of any defined entry action. The state will then listen for an event to occur, and then choose the path appropriate to the event. If there is an exit action it will run before the State Machine transitions to the next state.

This application has four states, an Initial state which is the starting point of any State Machine, two Simple states which are Idle and Depositing, and a Final state which is where a State Machine comes to a normal end.

A transition is used to move from one state to another. A transition will evaluate its conditions to determine if control should flow through it. If control does flow through it then it will also run any defined action.

To configure the Idle state, perform the following steps:
  1. Rename InitialState1 to Ready.
  2. Similarly, rename FinalState1 to Off, and State1 to Idle.
  3. Add an entry for this state. This entry action will run the moment this state is entered, will return any change that is currently in the state machine, and reset the total to nill.
    1. Click Idle, and select the Add an Entry icon from the action menu that pops up.
    2. Click this new entry, and rename it to resetTotal.
    3. In the Properties view, click the Details tab, and click the Java radio button.
    4. Paste the following code into the Java editor:
      System.out.println("Entering the Idle State");
      if (total.doubleValue() > 0) { 
      	System.out.println("VendingMachine returning change...$" + total.toString() +
      " has been returned.");	total = new Double(0.0d);
      }
      Note: This same functionality could be achieved using a visual snippet.
  4. Similarly, create an exit state on the Idle state named exitIdle with the following code:
    System.out.println("Exiting the Idle state");
  5. Create an action on the first transition that will display a welcome message to the user that lists the items available, and their cost.
    1. Click the transition between the on and Idle states.
    2. From the action menu, click the Add an Action icon, and name it Welcome.
    3. Add the following code to the Java editor:
      System.out.println("VendingMachine is turned on...Prices:");
      System.out.println("VENDINGMACHINE: pop: $0.5");
      System.out.println("VENDINGMACHINE: chips: $0.75");
      System.out.println("VENDINGMACHINE: candy: $1.0");
      total = new Double(0.0d);
  6. Expand the interface VendingMachineInterface in the tray on the right, drag and drop the off operation to the transition between the Idle and the Off states. The off operation will shut down the vending machine.
Your states and transitions should look like the ones in this image:
The Ready, Idle and Off states as they should look as you begin to create this state machine.
Save your work. You will notice that there are now only three errors reported in the problems view. The errors correspond to the cancel, deposit and selection operations that have not yet been used. The fourth error has been solved as we have now properly added the off operation.
< Previous | Next >





Feedback



This information center is powered by Eclipse technology. (http://www.eclipse.org)