use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class ProcessRuntimeImpl method initProcessEventListener.
private void initProcessEventListener(Process process) {
if (process instanceof RuleFlowProcess) {
for (Node node : ((RuleFlowProcess) process).getNodes()) {
if (node instanceof StartNode) {
StartNode startNode = (StartNode) node;
if (startNode != null) {
List<Trigger> triggers = startNode.getTriggers();
if (triggers != null) {
for (Trigger trigger : triggers) {
if (trigger instanceof EventTrigger) {
final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
String type = null;
for (EventFilter filter : filters) {
if (filter instanceof EventTypeFilter) {
type = ((EventTypeFilter) filter).getType();
}
}
StartProcessEventListener listener = new StartProcessEventListener(process.getId(), filters, trigger.getInMappings(), startNode.getEventTransformer());
signalManager.addEventListener(type, listener);
((RuleFlowProcess) process).getRuntimeMetaData().put("StartProcessEventType", type);
((RuleFlowProcess) process).getRuntimeMetaData().put("StartProcessEventListener", listener);
}
}
}
}
}
}
}
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class JbpmBpmn2TestCase method buildAndDumpBPMN2Process.
// Important to test this since persistence relies on this
protected List<Resource> buildAndDumpBPMN2Process(String process) throws SAXException, IOException {
KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
((KnowledgeBuilderConfigurationImpl) conf).initSemanticModules();
((KnowledgeBuilderConfigurationImpl) conf).addSemanticModule(new BPMNSemanticModule());
((KnowledgeBuilderConfigurationImpl) conf).addSemanticModule(new BPMNDISemanticModule());
((KnowledgeBuilderConfigurationImpl) conf).addSemanticModule(new BPMNExtensionsSemanticModule());
Resource classpathResource = ResourceFactory.newClassPathResource(process);
// Dump and reread
XmlProcessReader processReader = new XmlProcessReader(((KnowledgeBuilderConfigurationImpl) conf).getSemanticModules(), getClass().getClassLoader());
List<Process> processes = processReader.read(this.getClass().getResourceAsStream("/" + process));
List<Resource> resources = new ArrayList<Resource>();
for (Process p : processes) {
RuleFlowProcess ruleFlowProcess = (RuleFlowProcess) p;
String dumpedString = XmlBPMNProcessDumper.INSTANCE.dump(ruleFlowProcess);
Resource resource = ResourceFactory.newReaderResource(new StringReader(dumpedString));
resource.setSourcePath(classpathResource.getSourcePath());
resource.setTargetPath(classpathResource.getTargetPath());
resources.add(resource);
}
return resources;
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class ProcessFactoryTest method testCompositeNode.
@Test
public void testCompositeNode() throws Exception {
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
factory.name("My process").packageName("org.jbpm").startNode(1).name("Start").done().compositeNode(2).name("SubProcess").startNode(1).name("SubProcess Start").done().actionNode(2).name("SubProcess Action").action("java", "System.out.println(\"SubProcess Action\");").done().endNode(3).name("SubProcess End").terminate(true).done().connection(1, 2).connection(2, 3).done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
RuleFlowProcess process = factory.validate().getProcess();
assertEquals("SubProcess", process.getNode(2).getName());
Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
// source path or target path must be set to be added into kbase
res.setSourcePath("/tmp/processFactory.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(res);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ProcessInstance pi = ksession.startProcess("org.jbpm.process");
assertEquals(ProcessInstance.STATE_COMPLETED, pi.getState());
ksession.dispose();
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class ProcessFactoryTest method testActionNodeIsDroolsAction.
@Test(timeout = 10000)
public void testActionNodeIsDroolsAction() throws Exception {
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.process");
factory.name("ActionNodeActionProcess").version("1").startNode(1).name("Start").done().endNode(3).name("End").done().actionNode(2).name("printTextActionNode").action("java", "System.out.println(\"test print\");", true).done().connection(1, 2).connection(2, 3);
RuleFlowProcess process = factory.validate().getProcess();
assertNotNull(process);
Resource res = ResourceFactory.newByteArrayResource(XmlBPMNProcessDumper.INSTANCE.dump(process).getBytes());
res.setSourcePath("/tmp/processFactory.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(res);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ProcessInstance pi = ksession.startProcess("org.jbpm.process");
assertNotNull(pi);
assertEquals(ProcessInstance.STATE_COMPLETED, pi.getState());
ksession.dispose();
}
use of org.jbpm.ruleflow.core.RuleFlowProcess in project jbpm by kiegroup.
the class ProcessHandler method postProcessNodes.
private void postProcessNodes(RuleFlowProcess process, NodeContainer container) {
List<String> eventSubProcessHandlers = new ArrayList<String>();
for (Node node : container.getNodes()) {
if (node instanceof StateNode) {
StateNode stateNode = (StateNode) node;
String condition = (String) stateNode.getMetaData("Condition");
Constraint constraint = new ConstraintImpl();
constraint.setConstraint(condition);
constraint.setType("rule");
for (org.kie.api.definition.process.Connection connection : stateNode.getDefaultOutgoingConnections()) {
stateNode.setConstraint(connection, constraint);
}
} else if (node instanceof NodeContainer) {
// prepare event sub process
if (node instanceof EventSubProcessNode) {
EventSubProcessNode eventSubProcessNode = (EventSubProcessNode) node;
Node[] nodes = eventSubProcessNode.getNodes();
for (Node subNode : nodes) {
// avoids cyclomatic complexity
if (subNode == null || !(subNode instanceof StartNode)) {
continue;
}
List<Trigger> triggers = ((StartNode) subNode).getTriggers();
if (triggers == null) {
continue;
}
for (Trigger trigger : triggers) {
if (trigger instanceof EventTrigger) {
final List<EventFilter> filters = ((EventTrigger) trigger).getEventFilters();
for (EventFilter filter : filters) {
if (filter instanceof EventTypeFilter) {
eventSubProcessNode.addEvent((EventTypeFilter) filter);
String type = ((EventTypeFilter) filter).getType();
if (type.startsWith("Error-") || type.startsWith("Escalation")) {
String faultCode = (String) subNode.getMetaData().get("FaultCode");
String replaceRegExp = "Error-|Escalation-";
final String signalType = type;
ExceptionScope exceptionScope = (ExceptionScope) ((ContextContainer) eventSubProcessNode.getNodeContainer()).getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope == null) {
exceptionScope = new ExceptionScope();
((ContextContainer) eventSubProcessNode.getNodeContainer()).addContext(exceptionScope);
((ContextContainer) eventSubProcessNode.getNodeContainer()).setDefaultContext(exceptionScope);
}
String faultVariable = null;
if (trigger.getInAssociations() != null && !trigger.getInAssociations().isEmpty()) {
faultVariable = trigger.getInAssociations().get(0).getTarget();
}
ActionExceptionHandler exceptionHandler = new ActionExceptionHandler();
DroolsConsequenceAction action = new DroolsConsequenceAction("java", PROCESS_INSTANCE_SIGNAL_EVENT + signalType + "\", " + (faultVariable == null ? "null" : "kcontext.getVariable(\"" + faultVariable + "\")") + ");");
exceptionHandler.setAction(action);
exceptionHandler.setFaultVariable(faultVariable);
if (faultCode != null) {
String trimmedType = type.replaceFirst(replaceRegExp, "");
exceptionScope.setExceptionHandler(trimmedType, exceptionHandler);
eventSubProcessHandlers.add(trimmedType);
} else {
exceptionScope.setExceptionHandler(faultCode, exceptionHandler);
}
} else if (type.equals("Compensation")) {
// 1. Find the parent sub-process to this event sub-process
NodeContainer parentSubProcess;
NodeContainer subProcess = eventSubProcessNode.getNodeContainer();
Object isForCompensationObj = eventSubProcessNode.getMetaData("isForCompensation");
if (isForCompensationObj == null) {
eventSubProcessNode.setMetaData("isForCompensation", true);
logger.warn("Overriding empty or false value of \"isForCompensation\" attribute on Event Sub-Process [" + eventSubProcessNode.getMetaData("UniqueId") + "] and setting it to true.");
}
if (subProcess instanceof RuleFlowProcess) {
// ..how do you expect to signal compensation on the completed process (instance)?!?
throw new IllegalArgumentException("Compensation Event Sub-Processes at the process level are not supported.");
}
parentSubProcess = ((Node) subProcess).getNodeContainer();
// 2. The event filter (never fires, purely for dumping purposes) has already been added
// 3. Add compensation scope
String compensationHandlerId = (String) ((CompositeNode) subProcess).getMetaData("UniqueId");
addCompensationScope(process, eventSubProcessNode, parentSubProcess, compensationHandlerId);
}
}
}
} else if (trigger instanceof ConstraintTrigger) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) trigger;
if (constraintTrigger.getConstraint() != null) {
String processId = ((RuleFlowProcess) container).getId();
String type = "RuleFlowStateEventSubProcess-Event-" + processId + "-" + eventSubProcessNode.getUniqueId();
EventTypeFilter eventTypeFilter = new EventTypeFilter();
eventTypeFilter.setType(type);
eventSubProcessNode.addEvent(eventTypeFilter);
}
}
}
}
// for( Node subNode : nodes)
}
postProcessNodes(process, (NodeContainer) node);
} else if (node instanceof EndNode) {
handleIntermediateOrEndThrowCompensationEvent((EndNode) node);
} else if (node instanceof ActionNode) {
handleIntermediateOrEndThrowCompensationEvent((ActionNode) node);
} else if (node instanceof EventNode) {
final EventNode eventNode = (EventNode) node;
if (!(eventNode instanceof BoundaryEventNode) && eventNode.getDefaultIncomingConnections().size() == 0) {
throw new IllegalArgumentException("Event node '" + node.getName() + "' [" + node.getId() + "] has no incoming connection");
}
}
}
// process fault node to disable termnate parent if there is event subprocess handler
for (Node node : container.getNodes()) {
if (node instanceof FaultNode) {
FaultNode faultNode = (FaultNode) node;
if (eventSubProcessHandlers.contains(faultNode.getFaultName())) {
faultNode.setTerminateParent(false);
}
}
}
}
Aggregations