java state machine car

Table of Contents ⇒ Java Algorithm Library-Artery-Sample

Q08_01.java


package jp.avaj.lib.algo;

import jp.avaj.lib.test.ArTest;

/**
ArStateMechine-State machine-Automobile

Take a car as a very simple sample of a state machine
・ It's too easy compared to an actual car, but it does the following:
・ Step on the accelerator ⇒ Engine rotates ⇒ Wheels rotate
・ Release the accelerator ⇒ The engine stops ⇒ The wheels stop

-In this sample, ArStateMachineDefault is used as an implementation of ArStateMachine.
-ArStateMachineDefault can raise more events when the state machine changes
・ Accelerator ⇒ engine ⇒ wheel event transmission uses this function.

 */
public class Q08_01 {

  public static void main(String[] args) {
    //Wheel condition machine
    ArStateMachineDefault wheel = new ArStateMachineDefault();
    {
      //State transition matrix
      ArStateTransition[] matrix = new ArStateTransition[] {
        //Stop event during rotation ⇒ Stop
        new ArStateTransition(WheelState.ROTATE,WheelEvent.OFF,WheelState.STOP,null),
        //Operation event while stopped ⇒ Rotate
        new ArStateTransition(WheelState.STOP,WheelEvent.ON,WheelState.ROTATE,null),
      };
      //Initial setting,Initially stopped
      wheel.init(WheelState.STOP, matrix);
    }
    //Engine state machine
    ArStateMachineDefault engine = new ArStateMachineDefault();
    {
      //State transition matrix
      ArStateTransition[] matrix = new ArStateTransition[] {
        //Stop event during rotation ⇒ Stop, WheelEvent.Turn off
        new ArStateTransition(EngineState.ROTATE,EngineEvent.OFF,EngineState.STOP,WheelEvent.OFF),
        //Start event while stopped ⇒ Rotate, WheelEvent.Turn off
        new ArStateTransition(EngineState.STOP,EngineEvent.ON,EngineState.ROTATE,WheelEvent.ON)
      };
      //Initial setting, initially stopped
      engine.init(EngineState.STOP,matrix);
      //Connect with wheels(true connects,false disconnects)⇒ Event is sent
      engine.connect(wheel,true);
    }
    //Accelerator state machine
    ArStateMachineDefault accel = new ArStateMachineDefault();
    {
      //State transition matrix
      ArStateTransition[] matrix = new ArStateTransition[] {
        //Event to release while stepping on ⇒ OFF,EngineEvent.Turn off
        new ArStateTransition(AccelState.ON,AccelEvent.OFF,AccelState.OFF,EngineEvent.OFF),
        //Event to step on while separated ⇒ Turn on,EngineEvent.Turn on
        new ArStateTransition(AccelState.OFF,AccelEvent.ON,AccelState.ON,EngineEvent.ON)
      };
      //Initial setting, initially separated
      accel.init(AccelState.OFF,matrix);
      //Connect with the engine(true connects,false disconnects)⇒ Event is sent
      accel.connect(engine,true);
    }

    //Start a test case
    ArTest.startTestCase("Q08_01");

    //Step on the accelerator
    accel.sendEvent(AccelEvent.ON);
    //Make sure the engine is running
    ArTest.equals("accel:on","expected",EngineState.ROTATE,"engine",engine.getState());
    //Make sure the wheels are spinning
    ArTest.equals("accel:on","expecteed",WheelState.ROTATE,"wheel",wheel.getState());

    //Release the accelerator
    accel.sendEvent(AccelEvent.OFF);
    //Make sure the engine is stopped
    ArTest.equals("accel:off","expected",EngineState.STOP,"engine",engine.getState());
    //Make sure the wheels are stopped
    ArTest.equals("accel:off","expecteed",WheelState.STOP,"wheel",wheel.getState());

    //End the test case
    ArTest.endTestCase();
  }
  /**Accelerator status. */
  static enum AccelState { ON,OFF; }
  /**Event to accelerator. */
  static enum AccelEvent { ON,OFF; }

  /**Engine condition. */
  static enum EngineState { ROTATE,STOP; }
  /**Event to engine. */
  static enum EngineEvent { ON,OFF; }

  /**Wheel condition. */
  static enum WheelState { ROTATE,STOP; }
  /**Event to the wheel. */
  static enum WheelEvent { ON,OFF; }
}


The result is as follows.

result.txt


**** Q08_01 start ****
OK accel:on:expected=ROTATE:engine=ROTATE
OK accel:on:expecteed=ROTATE:wheel=ROTATE
OK accel:off:expected=STOP:engine=STOP
OK accel:off:expecteed=STOP:wheel=STOP
**** Q08_01 summary ****
test count = 4
success    = 4

Recommended Posts

java state machine car
I tried Spring State machine
What is JVM (Java Virtual Machine)?
Java
Java
Sample vending machine made in Java
Java marks PPT document as final state
Sample vending machine made in Java (classification)
Vending machine made with Java (domain driven)