public StateMachine create(String stateMachineName) throws InstantiationException, IllegalAccessException, ClassNotFoundException{ StateMachineDef def = stateMachines.get(stateMachineName); if(def == null) throw new NoSuchElementException(String.format("Can not found state machine definition for name: %s", stateMachineName)); return def.create(); }
public String getStateMachineDescription(String name) { return stateMachines.get(name).getDescription(); }
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; }
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 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; }