use of org.jbpm.ruleflow.core.RuleFlowProcess 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.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class CompensationEventListener method createNodeInstanceContainers.
private Stack<NodeInstance> createNodeInstanceContainers(Node toCompensateNode, boolean generalCompensation) {
Stack<NodeContainer> nestedNodes = new Stack<NodeContainer>();
Stack<NodeInstance> generatedInstances = new Stack<NodeInstance>();
NodeContainer parentContainer = toCompensateNode.getNodeContainer();
while (!(parentContainer instanceof RuleFlowProcess)) {
nestedNodes.add(parentContainer);
parentContainer = ((Node) parentContainer).getNodeContainer();
}
NodeInstanceContainer parentInstance;
if (nestedNodes.isEmpty()) {
// nestedNodes is empty
parentInstance = (NodeInstanceContainer) getProcessInstance();
} else {
parentInstance = (NodeInstanceContainer) ((WorkflowProcessInstanceImpl) getProcessInstance()).getNodeInstance((Node) nestedNodes.pop());
generatedInstances.add((NodeInstance) parentInstance);
}
NodeInstanceContainer childInstance = parentInstance;
while (!nestedNodes.isEmpty()) {
// generate
childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance((Node) nestedNodes.pop());
assert childInstance instanceof CompositeNodeInstance : "A node with child nodes should end up creating a CompositeNodeInstance type.";
// track and modify
generatedInstances.add((NodeInstance) childInstance);
// loop
parentInstance = (CompositeContextNodeInstance) childInstance;
}
if (generalCompensation) {
childInstance = (NodeInstanceContainer) parentInstance.getNodeInstance(toCompensateNode);
generatedInstances.add((NodeInstance) childInstance);
}
return generatedInstances;
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class WorkflowProcessInstanceUpgrader method upgradeProcessInstanceByNodeNames.
/**
* Do the same as upgradeProcessInstance() but user provides mapping by node names, not by node id's
* @param kruntime
* @param activeProcessId
* @param newProcessId
* @param nodeNamesMapping
*/
public static void upgradeProcessInstanceByNodeNames(KieRuntime kruntime, Long fromProcessId, String toProcessId, Map<String, String> nodeNamesMapping) {
Map<String, Long> nodeIdMapping = new HashMap<String, Long>();
String fromProcessIdString = kruntime.getProcessInstance(fromProcessId).getProcessId();
Process processFrom = kruntime.getKieBase().getProcess(fromProcessIdString);
Process processTo = kruntime.getKieBase().getProcess(toProcessId);
for (Map.Entry<String, String> entry : nodeNamesMapping.entrySet()) {
String from = null;
Long to = null;
if (processFrom instanceof WorkflowProcess) {
from = getNodeId(((WorkflowProcess) processFrom).getNodes(), entry.getKey(), true);
} else if (processFrom instanceof RuleFlowProcess) {
from = getNodeId(((RuleFlowProcess) processFrom).getNodes(), entry.getKey(), true);
} else if (processFrom != null) {
throw new IllegalArgumentException("Suported processes are WorkflowProcess and RuleFlowProcess, it was:" + processFrom.getClass());
} else {
throw new IllegalArgumentException("Can not find process with id: " + fromProcessIdString);
}
if (processTo instanceof WorkflowProcess) {
to = Long.valueOf(getNodeId(((WorkflowProcess) processTo).getNodes(), entry.getValue(), false));
} else if (processTo instanceof RuleFlowProcess) {
to = Long.valueOf(getNodeId(((RuleFlowProcess) processTo).getNodes(), entry.getValue(), false));
} else if (processTo != null) {
throw new IllegalArgumentException("Suported processes are WorkflowProcess and RuleFlowProcess, it was:" + processTo.getClass());
} else {
throw new IllegalArgumentException("Can not find process with id: " + toProcessId);
}
nodeIdMapping.put(from, to);
}
upgradeProcessInstance(kruntime, fromProcessId, toProcessId, nodeIdMapping);
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class XmlProcessReader method processParserMessage.
protected String processParserMessage(LinkedList<Object> parents, Attributes attr, String errorMessage) {
String nodeId = (attr == null || attr.getValue("id") == null) ? "" : attr.getValue("id");
String nodeName = (attr == null || attr.getValue("name") == null) ? "" : attr.getValue("name");
for (Object parent : parents) {
if (parent != null && parent instanceof RuleFlowProcess) {
RuleFlowProcess process = ((RuleFlowProcess) parent);
return messageWithProcessInfo.format(new Object[] { process.getId(), process.getPackageName(), process.getName(), process.getVersion(), nodeId, nodeName, errorMessage });
}
}
return message.format(new Object[] { nodeId, nodeName, errorMessage });
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class ProcessRuntimeImpl method initStartTimers.
public void initStartTimers() {
KieBase kbase = kruntime.getKieBase();
Collection<Process> processes = kbase.getProcesses();
for (Process process : processes) {
RuleFlowProcess p = (RuleFlowProcess) process;
List<StartNode> startNodes = p.getTimerStart();
if (startNodes != null && !startNodes.isEmpty()) {
kruntime.queueWorkingMemoryAction(new RegisterStartTimerAction(p.getId(), startNodes, this.timerManager));
}
}
}
Aggregations