public String getStateMachineDescription(String name) { return stateMachines.get(name).getDescription(); }
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 StateDef createStateNode(Node stateNode) { StateDef def = new StateDef(); StateType type; if(stateNode.getNodeName().equals(START_STATE)) type = StateType.start; else if(stateNode.getNodeName().equals(END_STATE)) type = StateType.end; else type = StateType.normal; def.setType(type); return def; } private static List<TransitionDef> linkState(StateMachineDef machineDef, Node transitionsNode) {
String sourceId = tansitionDef.getSourceId(); if(!transitions.containsKey(sourceId)) transitions.put(sourceId, new LinkedList<Transition>()); tansitionDef.getEventDef().getId(), (TransitAction)tansitionDef.getTransitActionDef().create(), tansitionDef.getSourceId(), tansitionDef.getTargetId()); for(StateDef stateDef: stateDefs) { states.add(new State( stateDef.getId(), stateDef.getType(), stateDef.getDescription(), (EntryAction)stateDef.getEntryActionDef().create(), (ExitAction)stateDef.getExitActionDef().create(), transitions.get(stateDef.getId())));
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; }
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 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; }
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(); }
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; }