use of org.jbpm.workflow.core.node.EventNode in project jbpm by kiegroup.
the class EventNodeHandler method writeNode.
public void writeNode(Node node, StringBuilder xmlDump, int metaDataType) {
EventNode eventNode = (EventNode) node;
String attachedTo = (String) eventNode.getMetaData("AttachedTo");
if (attachedTo == null) {
writeNode("intermediateCatchEvent", eventNode, xmlDump, metaDataType);
xmlDump.append(">" + EOL);
writeExtensionElements(eventNode, xmlDump);
writeVariableName(eventNode, xmlDump);
if (eventNode.getEventFilters().size() > 0) {
String type = ((EventTypeFilter) eventNode.getEventFilters().get(0)).getType();
if (type.startsWith("Message-")) {
type = type.substring(8);
xmlDump.append(" <messageEventDefinition messageRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\"/>" + EOL);
} else {
xmlDump.append(" <signalEventDefinition signalRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\"/>" + EOL);
}
}
endNode("intermediateCatchEvent", xmlDump);
} else {
String type = ((EventTypeFilter) eventNode.getEventFilters().get(0)).getType();
if (type.startsWith("Escalation-")) {
type = type.substring(attachedTo.length() + 12);
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
xmlDump.append(" <escalationEventDefinition escalationRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(type) + "\" />" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (type.startsWith("Error-")) {
type = type.substring(attachedTo.length() + 7);
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
xmlDump.append(">" + EOL);
String errorId = getErrorIdForErrorCode(type, eventNode);
xmlDump.append(" <errorEventDefinition errorRef=\"" + XmlBPMNProcessDumper.replaceIllegalCharsAttribute(errorId) + "\" />" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (type.startsWith("Timer-")) {
type = type.substring(attachedTo.length() + 7);
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
String duration = (String) eventNode.getMetaData("TimeDuration");
String cycle = (String) eventNode.getMetaData("TimeCycle");
if (duration != null && cycle != null) {
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(duration) + "</timeDuration>" + EOL + " <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(cycle) + "</timeCycle>" + EOL + " </timerEventDefinition>" + EOL);
} else if (duration != null) {
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeDuration xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(duration) + "</timeDuration>" + EOL + " </timerEventDefinition>" + EOL);
} else {
xmlDump.append(" <timerEventDefinition>" + EOL + " <timeCycle xsi:type=\"tFormalExpression\">" + XmlDumper.replaceIllegalChars(cycle) + "</timeCycle>" + EOL + " </timerEventDefinition>" + EOL);
}
endNode("boundaryEvent", xmlDump);
} else if (node.getMetaData().get("SignalName") != null) {
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
xmlDump.append(" <signalEventDefinition signalRef=\"" + type + "\"/>" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (node.getMetaData().get("Condition") != null) {
boolean cancelActivity = (Boolean) eventNode.getMetaData("CancelActivity");
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
if (!cancelActivity) {
xmlDump.append("cancelActivity=\"false\" ");
}
xmlDump.append(">" + EOL);
xmlDump.append(" <conditionalEventDefinition>" + EOL);
xmlDump.append(" <condition xsi:type=\"tFormalExpression\" language=\"http://www.jboss.org/drools/rule\">" + eventNode.getMetaData("Condition") + "</condition>" + EOL);
xmlDump.append(" </conditionalEventDefinition>" + EOL);
endNode("boundaryEvent", xmlDump);
} else if (type.startsWith("Message-")) {
type = type.substring(8);
writeNode("boundaryEvent", eventNode, xmlDump, metaDataType);
xmlDump.append("attachedToRef=\"" + attachedTo + "\" ");
xmlDump.append(">" + EOL);
xmlDump.append(" <messageEventDefinition messageRef=\"" + type + "\"/>" + EOL);
endNode("boundaryEvent", xmlDump);
} else {
throw new IllegalArgumentException("Unknown boundary event type: \"" + type + "\"");
}
}
}
use of org.jbpm.workflow.core.node.EventNode 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.workflow.core.node.EventNode 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.workflow.core.node.EventNode in project jbpm by kiegroup.
the class CompositeNodeInstance method signalEvent.
@Override
public void signalEvent(String type, Object event) {
List<NodeInstance> currentView = new ArrayList<NodeInstance>(this.nodeInstances);
super.signalEvent(type, event);
for (Node node : getCompositeNode().internalGetNodes()) {
if (node instanceof EventNodeInterface) {
if (((EventNodeInterface) node).acceptsEvent(type, event)) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstanceInterface eventNodeInstance = (EventNodeInstanceInterface) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else if (node instanceof EventSubProcessNode) {
EventNodeInstanceInterface eventNodeInstance = (EventNodeInstanceInterface) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
}
}
}
}
}
}
}
use of org.jbpm.workflow.core.node.EventNode in project jbpm by kiegroup.
the class WorkflowProcessInstanceImpl method signalEvent.
@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
logger.debug("Signal {} received with data {} in process instance {}", type, event, getId());
synchronized (this) {
if (getState() != ProcessInstance.STATE_ACTIVE) {
return;
}
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
if (timer.getId() == slaTimerId) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
}
if ("slaViolation".equals(type)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
List<NodeInstance> currentView = new ArrayList<NodeInstance>(this.nodeInstances);
try {
this.activatingNodeIds = new ArrayList<String>();
List<EventListener> listeners = eventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
listeners = externalEventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
for (Node node : getWorkflowProcess().getNodes()) {
if (node instanceof EventNodeInterface) {
if (((EventNodeInterface) node).acceptsEvent(type, event, (e) -> resolveVariable(e))) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
if (node instanceof EventSubProcessNode && ((resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type)))) {
EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
}
if (node instanceof DynamicNode && type.equals(((DynamicNode) node).getActivationEventName())) {
DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) getNodeInstance(node);
dynamicNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
}
}
}
}
}
}
}
if (((org.jbpm.workflow.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
for (Node node : getWorkflowProcess().getNodes()) {
if (type.equals(node.getName()) && node.getIncomingConnections().isEmpty()) {
NodeInstance nodeInstance = getNodeInstance(node);
if (event != null) {
Map<String, Object> dynamicParams = new HashMap<>();
if (event instanceof Map) {
dynamicParams.putAll((Map<String, Object>) event);
} else {
dynamicParams.put("Data", event);
}
((org.jbpm.workflow.instance.NodeInstance) nodeInstance).setDynamicParameters(dynamicParams);
}
((org.jbpm.workflow.instance.NodeInstance) nodeInstance).trigger(null, NodeImpl.CONNECTION_DEFAULT_TYPE);
}
}
}
} finally {
if (this.activatingNodeIds != null) {
this.activatingNodeIds.clear();
this.activatingNodeIds = null;
}
}
}
}
Aggregations