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 isEnded(){ if(currentState == null) return false; return currentState.getType() == StateType.end; }
public StateMachine(String name, String description, List<State> states, TransitionGuard gaurd) { this.name = name; this.description = description; this.gaurd = gaurd; init(states); }
private static StateMachineDef readMachine(Node machineNode) { StateMachineDef machine = new StateMachineDef(); machine.setName(getChildNodeText(machineNode, NAME)); machine.setDescription(getChildNodeText(machineNode, DESCRIPTION)); Node statesNode = getChildNode(machineNode, STATES); Node eventsNode = getChildNode(machineNode, EVENTS); Node transitionsNode = getChildNode(machineNode, TRANSITIONS); machine.setEventDefs(readEvents(eventsNode)); machine.setStateDefs(readStates(statesNode)); machine.setTansitionDefs(linkState(machine, transitionsNode)); return machine; }
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 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()); }
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()); } } }
private static StateMachineFactory getFromDocument(Document doc){ Element root = doc.getDocumentElement(); String name = getChildNodeText(root, NAME); String description = getChildNodeText(root, DESCRIPTION); return new StateMachineFactory(name, description, readMachines(getChildNode(root, STATE_MACHINES))); }
public static StateMachineFactory load(File model) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { return load(new FileInputStream(model)); }
public boolean isAcceptable(Event event) { return outputs.containsKey(event.getId()); }
public Transition( String eventId, TransitAction transitionAction, String sourceStateId, String targetStateId) { this.eventId = eventId; this.transitionAction = NullAction.guardWith(transitionAction); this.sourceStateId = sourceStateId; this.targetStateId = targetStateId; }
public void exist(Event event) { existAction.exit(id, event); } }
public void transit(Event event) { transitionAction.transit(sourceStateId, targetStateId, event); }
public void enter(Event event) { entryAction.enter(id, event); }
private static String getChildNodeText(Node node, String childName) { Node child = getChildNode(node, childName); if(child == null) return null; return child.getTextContent(); }
static void notify(StateMachine sm, String eventId) { sm.notify(new Event(eventId)); System.out.println(sm.getCurrentState().getId()); }
public static StateMachineFactory load(URL url) throws SAXException, IOException, ParserConfigurationException { return load(url.openStream()); }
public Transition getTransition(Event event) { return outputs.get(event.getId()); }
public boolean isStarted() { if(currentState == null) return false; return currentState.getType() != StateType.end; }