public static StateMachineFactory load(File model) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { return load(new FileInputStream(model)); }
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))); }
private static List<StateDef> readStates(Node statesNode) { List<Node> states = getValidChildNodes(statesNode); List<StateDef> nodes = new ArrayList<StateDef>(); for(int i = 0; i < states.size(); i++) { nodes.add(readState(states.get(i))); } return nodes; }
private static Map<String, StateMachineDef> readMachines(Node machinesNode) { List<Node> machines = getValidChildNodes(machinesNode); Map<String, StateMachineDef> machineMap = new HashMap<String, StateMachineDef>(); for(int i = 0;i < machines.size(); i++) { StateMachineDef def = readMachine(machines.get(i)); machineMap.put(def.getName(), def); } return machineMap; }
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(); } }
private static StateDef readState(Node stateNode) { StateDef node = createStateNode(stateNode); node.setId(getAttribute(stateNode, ID)); node.setDescription(getChildNodeText(stateNode, DESCRIPTION)); node.setReference(getChildNodeText(stateNode, REFERENCE)); node.setEntryActionDef(new ActionDef(getChildNodeText(stateNode, ENTRY_ACTION))); node.setExitActionDef(new ActionDef(getChildNodeText(stateNode, EXIT_ACTION))); return node; }
private static List<EventDef> readEvents(Node eventsNode) { List<Node> events = getValidChildNodes(eventsNode); List<EventDef> eventList = new ArrayList<EventDef>(); for(int i = 0; i < events.size(); i++) { Node eventNode = events.get(i); EventDef event = new EventDef(); event.setId(getAttribute(eventNode, ID)); event.setDescription(eventNode.getTextContent()); eventList.add(event); } return eventList; }
private static String getChildNodeText(Node node, String childName) { Node child = getChildNode(node, childName); if(child == null) return null; return child.getTextContent(); }
private static Node getChildNode(Node node, String name) { List<Node> children = getValidChildNodes(node); Node found = null; for(int i = 0; i < children.size(); i++){ if(!children.get(i).getNodeName().equalsIgnoreCase(name)) continue; found = children.get(i); break; } return found; }
private static List<Node> getValidChildNodes(Node node) { List<Node> nl = new ArrayList<>(); NodeList nodeList = node.getChildNodes(); for(int i = 0; i < nodeList.getLength(); i++){ if(isValidNode(nodeList.item(i))) nl.add(nodeList.item(i)); } return nl; } }
public static StateMachineFactory load(InputStream in) throws SAXException, IOException, ParserConfigurationException { try{ Document doc= DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); StateMachineFactory def = getFromDocument(doc); in.close(); return def; }catch(Throwable e){ if(in != null) try{ in.close(); }catch(Throwable e1){ } e.printStackTrace(); } return null; }
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(); } }
private static List<TransitionDef> linkState(StateMachineDef machineDef, Node transitionsNode) { List<Node> transitions = getValidChildNodes(transitionsNode); Map<String, EventDef> events = new HashMap<String, EventDef>(); for(EventDef event: machineDef.getEventDefs()) { events.put(event.getId(), event); } List<TransitionDef> transitionDefs = new ArrayList<TransitionDef>(); for(int i = 0; i < transitions.size(); i++) { Node node = transitions.get(i); String sourceId = getAttribute(node, SOURCE_ID); String targetId = getAttribute(node, TARGET_ID); EventDef event = events.get(getAttribute(node, EVENT_ID)); TransitionDef transition = new TransitionDef(sourceId, targetId); transition.setEventDef(event); transition.setTransitActionDef(new ActionDef(getAttribute(node, TRANSIT_ACTION))); transitionDefs.add(transition); } return transitionDefs; }
public static StateMachineFactory load(URL url) throws SAXException, IOException, ParserConfigurationException { return load(url.openStream()); }
/** * It will first check model file from file path, if it does not exist, it will try classpath then. * @param path * @return * @throws Exception */ public static StateMachineFactory load(String path) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { InputStream in; File f = new File(path); if(f.exists()) in = new FileInputStream(f); else { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = StateMachineFactory.class.getClassLoader(); } in = classLoader.getResource(path).openStream(); } return load(in); }