protected void handleLinkNode( Element element, Node node, org.w3c.dom.Node xmlLinkNode, ExtensibleXmlParser parser) { NodeContainer nodeContainer = (NodeContainer) parser.getParent(); node.setName(element.getAttribute("name")); NamedNodeMap linkAttr = xmlLinkNode.getAttributes(); String name = linkAttr.getNamedItem("name").getNodeValue(); String id = element.getAttribute("id"); node.setMetaData("UniqueId", id); node.setMetaData(LINK_NAME, name); org.w3c.dom.Node xmlNode = xmlLinkNode.getFirstChild(); IntermediateLink aLink = new IntermediateLink(); aLink.setName(name); aLink.setUniqueId(id); while (null != xmlNode) { String nodeName = xmlNode.getNodeName(); if ("target".equals(nodeName)) { String target = xmlNode.getTextContent(); node.setMetaData("target", target); aLink.setTarget(target); } if ("source".equals(nodeName)) { String source = xmlNode.getTextContent(); node.setMetaData("source", source); aLink.addSource(source); } xmlNode = xmlNode.getNextSibling(); } if (nodeContainer instanceof RuleFlowProcess) { RuleFlowProcess process = (RuleFlowProcess) nodeContainer; List<IntermediateLink> links = (List<IntermediateLink>) process.getMetaData().get(ProcessHandler.LINKS); if (null == links) { links = new ArrayList<IntermediateLink>(); } links.add(aLink); process.setMetaData(ProcessHandler.LINKS, links); } else if (nodeContainer instanceof CompositeNode) { CompositeNode subprocess = (CompositeNode) nodeContainer; List<IntermediateLink> links = (List<IntermediateLink>) subprocess.getMetaData().get(ProcessHandler.LINKS); if (null == links) { links = new ArrayList<IntermediateLink>(); } links.add(aLink); subprocess.setMetaData(ProcessHandler.LINKS, links); } }
public void testEvent5() { RuleFlowProcess process = new RuleFlowProcess(); process.setId("org.drools.process.event"); process.setName("Event Process"); List<Variable> variables = new ArrayList<Variable>(); Variable variable = new Variable(); variable.setName("event"); ObjectDataType personDataType = new ObjectDataType(); personDataType.setClassName("org.drools.Person"); variable.setType(personDataType); variables.add(variable); process.getVariableScope().setVariables(variables); StartNode startNode = new StartNode(); startNode.setName("Start"); startNode.setId(1); process.addNode(startNode); CompositeNode compositeNode = new CompositeNode(); compositeNode.setName("CompositeNode"); compositeNode.setId(2); process.addNode(compositeNode); new ConnectionImpl( startNode, Node.CONNECTION_DEFAULT_TYPE, compositeNode, Node.CONNECTION_DEFAULT_TYPE); MilestoneNode milestoneNode = new MilestoneNode(); milestoneNode.setName("Milestone"); milestoneNode.setConstraint("eval(false)"); compositeNode.addNode(milestoneNode); compositeNode.linkIncomingConnections( Node.CONNECTION_DEFAULT_TYPE, milestoneNode.getId(), Node.CONNECTION_DEFAULT_TYPE); EventNode eventNode = new EventNode(); EventTypeFilter eventFilter = new EventTypeFilter(); eventFilter.setType("myEvent"); eventNode.addEventFilter(eventFilter); eventNode.setVariableName("event"); compositeNode.addNode(eventNode); final List<String> myList = new ArrayList<String>(); ActionNode actionNode = new ActionNode(); actionNode.setName("Print"); DroolsAction action = new DroolsConsequenceAction("java", null); action.setMetaData( "Action", new Action() { public void execute(ProcessContext context) throws Exception { System.out.println( "Detected event for person " + ((Person) context.getVariable("event")).getName()); myList.add("Executed action"); } }); actionNode.setAction(action); compositeNode.addNode(actionNode); new ConnectionImpl( eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE); Join join = new Join(); join.setName("XOR Join"); join.setType(Join.TYPE_XOR); compositeNode.addNode(join); new ConnectionImpl( milestoneNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE); new ConnectionImpl( actionNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE); compositeNode.linkOutgoingConnections( join.getId(), Node.CONNECTION_DEFAULT_TYPE, Node.CONNECTION_DEFAULT_TYPE); EndNode endNode = new EndNode(); endNode.setName("EndNode"); endNode.setId(3); process.addNode(endNode); new ConnectionImpl( compositeNode, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); ((AbstractRuleBase) ((InternalKnowledgeBase) kbase).getRuleBase()).addProcess(process); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); ProcessInstance processInstance = ksession.startProcess("org.drools.process.event"); assertEquals(0, myList.size()); Person jack = new Person(); jack.setName("Jack"); processInstance.signalEvent("myEvent", jack); assertEquals(1, myList.size()); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); }