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 boolean isStarted() { if(currentState == null) return false; return currentState.getType() != StateType.end; }
if(state.getType() == StateType.start) if(startState == null) startState = state; if(stateMap.containsKey(state.getId())) throw new IllegalStateException("Found duplicate state for Id: " + state.getId()); stateMap.put(state.getId(), state); eventIds.addAll(state.getAcceptableEvents()); for(String eid: state.getAcceptableEvents()) { Transition tran = state.getTransition(new Event(eid)); String targetSid = tran.getTargetStateId(); Set<String> sourceEventIds = null; State targetState = stateMap.get(targetSid); if(sourceEventMap.containsKey(targetSid)) targetState.setSourceEvents(sourceEventMap.get(targetSid)); else targetState.setSourceEvents(Collections.EMPTY_SET);
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()); } } }
states.add(new State( stateDef.getId(), stateDef.getType(),
static void notify(StateMachine sm, String eventId) { sm.notify(new Event(eventId)); System.out.println(sm.getCurrentState().getId()); }
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(); } }
public boolean isEnded(){ if(currentState == null) return false; return currentState.getType() == StateType.end; }
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(); } }