use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.
the class EventSubProcessTest method testNestedEventSubProcess.
@Test
public void testNestedEventSubProcess() throws Exception {
RuleFlowProcess process = new RuleFlowProcess();
process.setAutoComplete(true);
String processId = "org.jbpm.process.event.subprocess";
process.setId(processId);
process.setName("Event SubProcess 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);
NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
NodeCreator<CompositeNode> compNodeCreator = new NodeCreator<CompositeNode>(process, CompositeNode.class);
// outer process
StartNode startNode = startNodeCreator.createNode("start0");
CompositeNode compositeNode = compNodeCreator.createNode("comp0");
connect(startNode, compositeNode);
EndNode endNode = endNodeCreator.createNode("end0");
connect(compositeNode, endNode);
// 1rst level nested subprocess
startNodeCreator.setNodeContainer(compositeNode);
endNodeCreator.setNodeContainer(compositeNode);
compNodeCreator.setNodeContainer(compositeNode);
startNode = startNodeCreator.createNode("start1");
compositeNode = compNodeCreator.createNode("comp1");
connect(startNode, compositeNode);
endNode = endNodeCreator.createNode("end1");
connect(compositeNode, endNode);
// 2nd level subprocess
startNodeCreator.setNodeContainer(compositeNode);
endNodeCreator.setNodeContainer(compositeNode);
NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(compositeNode, WorkItemNode.class);
startNode = startNodeCreator.createNode("start2");
WorkItemNode workItemNode = workItemNodeCreator.createNode("workItem2");
Work work = new WorkImpl();
String workItemName = "play";
work.setName(workItemName);
workItemNode.setWork(work);
connect(startNode, workItemNode);
endNode = endNodeCreator.createNode("end2");
connect(workItemNode, endNode);
// (3rd level) event sub process in 2nd level subprocess
NodeCreator<EventSubProcessNode> espNodeCreator = new NodeCreator<EventSubProcessNode>(compositeNode, EventSubProcessNode.class);
EventSubProcessNode espNode = espNodeCreator.createNode("eventSub2");
EventTypeFilter eventFilter = new EventTypeFilter();
String EVENT_NAME = "subEvent";
eventFilter.setType(EVENT_NAME);
espNode.addEvent(eventFilter);
startNodeCreator.setNodeContainer(espNode);
endNodeCreator.setNodeContainer(espNode);
NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(espNode, ActionNode.class);
startNode = startNodeCreator.createNode("start3*");
ActionNode actionNode = actionNodeCreator.createNode("print3*");
actionNode.setName("Print");
final List<String> eventList = new ArrayList<String>();
DroolsAction action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
eventList.add("Executed action");
}
});
actionNode.setAction(action);
connect(startNode, actionNode);
endNode = endNodeCreator.createNode("end3*");
connect(actionNode, endNode);
// run process
KieSession ksession = createKieSession(process);
TestProcessEventListener procEventListener = new TestProcessEventListener();
ksession.addEventListener(procEventListener);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler(workItemName, workItemHandler);
ProcessInstance processInstance = ksession.startProcess(processId);
processInstance.signalEvent(EVENT_NAME, null);
assertEquals("Event " + EVENT_NAME + " did not fire!", 1, eventList.size());
ksession.getWorkItemManager().completeWorkItem(workItemHandler.getWorkItems().removeLast().getId(), null);
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
verifyEventHistory(nestedEventOrder, procEventListener.getEventHistory());
}
use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.
the class EventTest method testEvent3.
@Test
public void testEvent3() {
RuleFlowProcess process = new RuleFlowProcess();
process.setId("org.drools.core.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);
EventNode eventNode = new EventNode();
EventTypeFilter eventFilter = new EventTypeFilter();
eventFilter.setType("myEvent");
eventNode.addEventFilter(eventFilter);
eventNode.setVariableName("event");
eventNode.setId(3);
process.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 {
logger.info("Detected event for person {}", ((Person) context.getVariable("event")).getName());
myList.add("Executed action");
}
});
actionNode.setAction(action);
actionNode.setId(4);
process.addNode(actionNode);
new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
EventNode eventNode2 = new EventNode();
eventFilter = new EventTypeFilter();
eventFilter.setType("myOtherEvent");
eventNode2.addEventFilter(eventFilter);
eventNode2.setVariableName("event");
eventNode2.setId(5);
process.addNode(eventNode2);
ActionNode actionNode2 = new ActionNode();
actionNode2.setName("Print");
action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
logger.info("Detected other event for person {}", ((Person) context.getVariable("event")).getName());
myList.add("Executed action");
}
});
actionNode2.setAction(action);
actionNode2.setId(6);
process.addNode(actionNode2);
new ConnectionImpl(eventNode2, Node.CONNECTION_DEFAULT_TYPE, actionNode2, Node.CONNECTION_DEFAULT_TYPE);
Join join = new Join();
join.setName("AND Join");
join.setType(Join.TYPE_AND);
join.setId(7);
process.addNode(join);
new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
new ConnectionImpl(actionNode2, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
EndNode endNode = new EndNode();
endNode.setName("EndNode");
endNode.setId(8);
process.addNode(endNode);
new ConnectionImpl(join, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
KieSession ksession = createKieSession(process);
TestProcessEventListener procEventListener = new TestProcessEventListener();
ksession.addEventListener(procEventListener);
ProcessInstance processInstance = ksession.startProcess("org.drools.core.process.event");
assertEquals(0, myList.size());
Person jack = new Person();
jack.setName("Jack");
processInstance.signalEvent("myEvent", jack);
assertEquals(1, myList.size());
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
Person john = new Person();
john.setName("John");
processInstance.signalEvent("myOtherEvent", john);
assertEquals(2, myList.size());
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
verifyEventHistory(test3EventOrder, procEventListener.getEventHistory());
}
use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.
the class EventTest method testEvent3a.
@Test
public void testEvent3a() {
RuleFlowProcess process = new RuleFlowProcess();
process.setId("org.drools.core.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);
EventNode eventNode = new EventNode();
EventTypeFilter eventFilter = new EventTypeFilter();
eventFilter.setType("myEvent");
eventNode.addEventFilter(eventFilter);
eventNode.setVariableName("event");
eventNode.setId(3);
process.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 {
logger.info("Detected event for person {}", ((Person) context.getVariable("event")).getName());
myList.add("Executed action");
}
});
actionNode.setAction(action);
actionNode.setId(4);
process.addNode(actionNode);
new ConnectionImpl(eventNode, Node.CONNECTION_DEFAULT_TYPE, actionNode, Node.CONNECTION_DEFAULT_TYPE);
EventNode eventNode2 = new EventNode();
eventFilter = new EventTypeFilter();
eventFilter.setType("myOtherEvent");
eventNode2.addEventFilter(eventFilter);
eventNode2.setVariableName("event");
eventNode2.setId(5);
process.addNode(eventNode2);
ActionNode actionNode2 = new ActionNode();
actionNode2.setName("Print");
action = new DroolsConsequenceAction("java", null);
action.setMetaData("Action", new Action() {
public void execute(ProcessContext context) throws Exception {
logger.info("Detected other event for person {}", ((Person) context.getVariable("event")).getName());
myList.add("Executed action");
}
});
actionNode2.setAction(action);
actionNode2.setId(6);
process.addNode(actionNode2);
new ConnectionImpl(eventNode2, Node.CONNECTION_DEFAULT_TYPE, actionNode2, Node.CONNECTION_DEFAULT_TYPE);
Join join = new Join();
join.setName("AND Join");
join.setType(Join.TYPE_AND);
join.setId(7);
process.addNode(join);
new ConnectionImpl(startNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
new ConnectionImpl(actionNode, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
new ConnectionImpl(actionNode2, Node.CONNECTION_DEFAULT_TYPE, join, Node.CONNECTION_DEFAULT_TYPE);
EndNode endNode = new EndNode();
endNode.setName("EndNode");
endNode.setId(8);
process.addNode(endNode);
new ConnectionImpl(join, Node.CONNECTION_DEFAULT_TYPE, endNode, Node.CONNECTION_DEFAULT_TYPE);
KieSession ksession = createKieSession(process);
TestProcessEventListener procEventListener = new TestProcessEventListener();
ksession.addEventListener(procEventListener);
System.setProperty("jbpm.loop.level.disabled", "true");
ProcessInstance processInstance = ksession.startProcess("org.drools.core.process.event");
assertEquals(0, myList.size());
Person jack = new Person();
jack.setName("Jack");
processInstance.signalEvent("myEvent", jack);
assertEquals(1, myList.size());
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
processInstance.signalEvent("myEvent", jack);
assertEquals(2, myList.size());
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
Person john = new Person();
john.setName("John");
processInstance.signalEvent("myOtherEvent", john);
assertEquals(3, myList.size());
assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
System.clearProperty("jbpm.loop.level.disabled");
verifyEventHistory(test3aEventOrder, procEventListener.getEventHistory());
}
use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.
the class ForEachNode method setOutputVariable.
public void setOutputVariable(String variableName, DataType type) {
this.outputVariableName = variableName;
VariableScope variableScope = (VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE);
List<Variable> variables = variableScope.getVariables();
if (variables == null) {
variables = new ArrayList<Variable>();
variableScope.setVariables(variables);
}
Variable variable = new Variable();
variable.setName(variableName);
variable.setType(type);
variables.add(variable);
Variable tmpvariable = new Variable();
tmpvariable.setName("foreach_output");
tmpvariable.setType(type);
variables.add(tmpvariable);
}
use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.
the class CompositeNodeHandler method writeNode.
public void writeNode(Node node, StringBuilder xmlDump, boolean includeMeta) {
super.writeNode(getNodeName(), node, xmlDump, includeMeta);
CompositeNode compositeNode = (CompositeNode) node;
writeAttributes(compositeNode, xmlDump, includeMeta);
xmlDump.append(">" + EOL);
if (includeMeta) {
writeMetaData(compositeNode, xmlDump);
}
for (String eventType : compositeNode.getActionTypes()) {
writeActions(eventType, compositeNode.getActions(eventType), xmlDump);
}
writeTimers(compositeNode.getTimers(), xmlDump);
if (compositeNode instanceof CompositeContextNode) {
VariableScope variableScope = (VariableScope) ((CompositeContextNode) compositeNode).getDefaultContext(VariableScope.VARIABLE_SCOPE);
if (variableScope != null) {
List<Variable> variables = variableScope.getVariables();
XmlWorkflowProcessDumper.visitVariables(variables, xmlDump);
}
ExceptionScope exceptionScope = (ExceptionScope) ((CompositeContextNode) compositeNode).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope != null) {
XmlWorkflowProcessDumper.visitExceptionHandlers(exceptionScope.getExceptionHandlers(), xmlDump);
}
}
List<Node> subNodes = getSubNodes(compositeNode);
xmlDump.append(" <nodes>" + EOL);
for (Node subNode : subNodes) {
XmlRuleFlowProcessDumper.INSTANCE.visitNode(subNode, xmlDump, includeMeta);
}
xmlDump.append(" </nodes>" + EOL);
List<Connection> connections = getSubConnections(compositeNode);
xmlDump.append(" <connections>" + EOL);
for (Connection connection : connections) {
XmlRuleFlowProcessDumper.INSTANCE.visitConnection(connection, xmlDump, includeMeta);
}
xmlDump.append(" </connections>" + EOL);
Map<String, CompositeNode.NodeAndType> inPorts = getInPorts(compositeNode);
xmlDump.append(" <in-ports>" + EOL);
for (Map.Entry<String, CompositeNode.NodeAndType> entry : inPorts.entrySet()) {
xmlDump.append(" <in-port type=\"" + entry.getKey() + "\" nodeId=\"" + entry.getValue().getNodeId() + "\" nodeInType=\"" + entry.getValue().getType() + "\" />" + EOL);
}
xmlDump.append(" </in-ports>" + EOL);
Map<String, CompositeNode.NodeAndType> outPorts = getOutPorts(compositeNode);
xmlDump.append(" <out-ports>" + EOL);
for (Map.Entry<String, CompositeNode.NodeAndType> entry : outPorts.entrySet()) {
xmlDump.append(" <out-port type=\"" + entry.getKey() + "\" nodeId=\"" + entry.getValue().getNodeId() + "\" nodeOutType=\"" + entry.getValue().getType() + "\" />" + EOL);
}
xmlDump.append(" </out-ports>" + EOL);
endNode(getNodeName(), xmlDump);
}
Aggregations