waitState = stateMachine.getStateOfType(Wait.class); gotoState = stateMachine.getStateOfType(Goto.class); waitForServiceState = stateMachine.getStateOfType(WaitAtService.class); serviceState = stateMachine.getStateOfType(Service.class); stateMachine.getEventAPI().addListener(new Listener() { @Override public void handleEvent(Event e) {
@Override public void stop() { LOGGER.trace("stop"); checkState(stateMachine.isSupported(Trigger.STOP), "Can not stop time in current state: %s", stateMachine.getCurrentState().name()); final boolean rt = stateMachine.stateIs(realtimeState); stateMachine.handle(Trigger.STOP, this); if (!rt) { shutdownExecutor(); } }
/** * Looks up a state of the specified (sub)type if it exists. If there exist * multiple the first encountered is returned. * @param type The (sub)type to look for. * @param <U> The type. * @return The state of the specified type. * @throws IllegalArgumentException if there is no state of the specified * type. */ public <U> U getStateOfType(Class<U> type) { for (final State<T, C> state : getStates()) { if (type.isInstance(state)) { return type.cast(state); } } throw new IllegalArgumentException("There is no instance of " + type + " in this state machine."); }
@Override public void switchToRealTime() { checkState(stateMachine.isSupported(Trigger.REAL_TIME), "Can not switch to real time mode because clock is already stopped."); stateMachine.handle(Trigger.REAL_TIME, this); }
public void testTransition() { final ListenerEventHistory history = new ListenerEventHistory(); fsm.getEventAPI() .addListener(history, StateMachine.StateMachineEvent.values()); assertEquals(stopState, fsm.getCurrentState()); fsm.handle(Events.START, CONTEXT); assertEquals(startState, fsm.getCurrentState()); fsm.handle(CONTEXT); assertEquals(startState, fsm.getCurrentState()); fsm.handle(Events.SPEZIAL, SPECIAL_CONTEXT); assertEquals(startState, fsm.getCurrentState()); assertEquals(2, history.getHistory().size()); assertTrue(((StateTransitionEvent) history.getHistory().get(0)) fsm.handle(Events.SPEZIAL, CONTEXT); assertEquals(specialState, fsm.getCurrentState()); fsm.handle(Events.STOP, CONTEXT); assertEquals(specialState, fsm.getCurrentState()); fsm.handle(Events.START, CONTEXT); assertEquals(startState, fsm.getCurrentState());
/** * Tests correct behavior for events which are not equal. */ @SuppressWarnings("static-method") @Test public void eventNotEqualBehavior() { final TestState state1 = new TestState("state1"); state1.name(); final TestState state2 = new TestState("state2"); final Object event1 = "event1"; final Object event2 = new Object(); final StateMachine<Object, Object> sm = StateMachine.create(state1)/* */ .addTransition(state1, event1, state2)/* */ .addTransition(state2, event2, state1)/* */ .build(); assertTrue(sm.isSupported(event1)); assertTrue(sm.isSupported("event1")); assertTrue(sm.isSupported(new StringBuilder("event").append(1) .toString())); assertFalse(sm.isSupported(event2)); sm.handle("event1", CONTEXT); assertTrue(sm.stateIs(state2)); assertTrue(sm.isSupported(event2)); assertFalse(sm.isSupported(new Object())); }
@Override public ClockMode getClockMode() { return ((ClockState) stateMachine.getCurrentState()).getClockMode(); }
/** * Gives the current {@link State} time to update. * @param context Reference to the context. */ public void handle(C context) { handle(null, context); }
/** * Convenience method for checking whether the current state is one of the * specified states. * @param states The states to be checked. * @return <code>true</code> when the current state is one of the specified * states, <code>false</code> otherwise. */ @SafeVarargs public final boolean stateIsOneOf(State<T, C>... states) { for (final State<T, C> s : states) { if (stateIs(s)) { return true; } } return false; }
/** * Tests the query by class method. */ @Test public void testGetStateOfType() { final StateA a = new StateA(); final StateB b = new StateB(); final StateC c = new StateC(); final StateMachineBuilder<Events, Context> smb = StateMachine.create(a) .addTransition(a, Events.START, b) .addTransition(b, Events.START, c) .addTransition(c, Events.START, a); if (explicitRecursiveTransitions) { smb.explicitRecursiveTransitions(); } fsm = smb.build(); final State<Events, Context> first = fsm.getStates().iterator().next(); assertEquals(first, fsm.getStateOfType(Object.class)); assertEquals(b, fsm.getStateOfType(StateB.class)); assertEquals(c, fsm.getStateOfType(StateC.class)); boolean fail = false; try { fsm.getStateOfType(Enum.class); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
/** * Tests a subclass that changes the state machine. */ @Test public void testExtension() { final VehicleDTO v = VehicleDTO.builder() .startPosition(new Point(1, 1)) .speed(30d) .capacity(1) .availabilityTimeWindow(TimeWindow.create(0, minute(30))) .build(); final SubVehicle vehicle = new SubVehicle(v, allowDelayedRouteChanges); d = vehicle; PDPTWTestUtil.register(rm, pm, vehicle); tick(0, 1); assertEquals(vehicle.waitState, vehicle.stateMachine.getCurrentState()); vehicle.stateMachine.handle(ExtraEvent.TEST_EVENT, vehicle); assertEquals(vehicle.extraState, vehicle.stateMachine.getCurrentState()); tick(1, 2); assertEquals(vehicle.waitState, vehicle.stateMachine.getCurrentState()); }
final SimulatedTime st = new SimulatedTime(); stateMachine = StateMachine .create( builder.getClockMode() == ClockMode.REAL_TIME ? INIT_RT : INIT_ST) .addTransition(INIT_RT, Trigger.SIMULATE, INIT_ST) stateMachine.getEventAPI().addListener(new Listener() { @Override public void handleEvent(Event e) {
public void diversionTestInGotoState2() { final ListenerEventHistory leh = new ListenerEventHistory(); d.stateMachine.getEventAPI().addListener(leh, StateMachineEvent.STATE_TRANSITION); assertEquals(0, leh.getHistory().size()); assertEquals(Optional.absent(), d.newRoute); tick(5, 6); assertEquals(d.gotoState, d.stateMachine.getCurrentState()); assertEquals(1, leh.getHistory().size()); assertEquals(p1, d.gotoState.getDestination()); assertEquals(d.gotoState, ev1.newState); assertEquals(d.gotoState, d.stateMachine.getCurrentState()); assertEquals(p1, d.getRoute().iterator().next()); assertEquals(d.gotoState, d.stateMachine.getCurrentState());
@Override protected StateMachine<StateEvent, RouteFollowingVehicle> createStateMachine() { // when overriding, please see doc of super method! // reuse super implementation final StateMachine<StateEvent, RouteFollowingVehicle> fsm = super.createStateMachine(); // get ref to existing state final Wait wait = fsm.getStateOfType(Wait.class); // add new state final ExtraState extra = new ExtraState(); // add two new transitions return StateMachine.create(wait) .addTransition(wait, ExtraEvent.TEST_EVENT, extra) .addTransition(extra, DefaultEvent.DONE, wait) .addTransitionsFrom(fsm) .build(); }
stateMachine.getEventAPI().addListener(this, StateMachineEvent.STATE_TRANSITION); lazyRouteComputing = lazyRouteComp;
.create(stopState) .addTransition(startState, Events.STOP, stopState) .addTransition(startState, Events.SPEZIAL, specialState) .stateIsOneOf(startState, specialState, stopState, pauseState)); assertFalse(fsm.stateIsOneOf(startState, specialState));
final WaitAtService waitAtService = new WaitAtService(); final Service service = new Service(); return StateMachine.create(wait) .explicitRecursiveTransitions() .addTransition(wait, DefaultEvent.GOTO, gotos)
SubVehicle(VehicleDTO pDto, boolean allowDelayedRouteChanging) { super(pDto, allowDelayedRouteChanging); extraState = stateMachine.getStateOfType(ExtraState.class); }
@Override public boolean isTicking() { return !stateMachine.stateIsOneOf(SimpleState.values()); }
public void testRecursiveTransitions() { final ListenerEventHistory history = new ListenerEventHistory(); fsm.getEventAPI() .addListener(history, StateMachine.StateMachineEvent.values()); assertEquals(stopState, fsm.getCurrentState()); assertTrue(stopState.handleHistory().isEmpty()); assertTrue(stopState.onEntryHistory().isEmpty()); assertTrue(history.getHistory().isEmpty()); fsm.handle(Events.RECURSIVE, CONTEXT); assertEquals(stopState, fsm.getCurrentState());