static void notify(StateMachine sm, String eventId) { sm.notify(new Event(eventId)); System.out.println(sm.getCurrentState().getId()); }
static void showSourceAndTargetEvent(StateMachine sm){ System.out.println("\n\nShow source and target event ids for each state\n"); for(String id: sm.getStateIds()) { System.out.println("State: " + id); System.out.println("Source events: " + sm.findState(id).getSourceEvents()); System.out.println("Target events: " + sm.findState(id).getAcceptableEvents()); } } }
public boolean notify(Event event){ State source = currentState; if(!currentState.isAcceptable(event)) return false; Transition trans = currentState.getTransition(event); State target = findState(trans.getTargetStateId()); if(!gaurd.isTransitAllowed(source.getId(), target.getId(), event)) return false; currentState.exist(event); trans.transit(event); target.enter(event); currentState = target; return true; }
public static void main(String[] args) { try { StateMachineFactory f = StateMachineFactory.load("sample.xstate"); StateMachine sm = f.create("DB Health Lifecycle"); System.out.println("Start state: " + sm.getCurrentState().getId()); notify(sm, "initialize"); notify(sm, "markdown"); notify(sm, "markup"); notify(sm, "shutdown"); System.out.println("End state: " + sm.getCurrentState().getId()); showSourceAndTargetEvent(sm); } catch (Exception e) { e.printStackTrace(); } }
public StateMachine(String name, String description, List<State> states, TransitionGuard gaurd) { this.name = name; this.description = description; this.gaurd = gaurd; init(states); }
public void reset(){ if(isEnded()) throw new IllegalStateException(String.format("State machine: %s is already ended. Can not be reset.", name)); currentState = startState; }
return new StateMachine(name, description, states, NullAction.instance);
public static void main(String[] args) { try { StateMachineFactory f = StateMachineFactory.load("sample.xstate"); StateMachine sm = f.create("vip flow"); System.out.println("Start state: " + sm.getCurrentState().getId()); notify(sm, "pass"); notify(sm, "pass"); notify(sm, "pass"); notify(sm, "pass"); notify(sm, "pass"); System.out.println("End state: " + sm.getCurrentState().getId()); } catch (Exception e) { e.printStackTrace(); } }
static void notify(StateMachine sm, String eventId) { sm.notify(new Event(eventId)); System.out.println(sm.getCurrentState().getId()); }