use of org.jbpm.process.core.ContextContainer in project jbpm by kiegroup.
the class VariableHandler method start.
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
parser.startElementBuilder(localName, attrs);
ContextContainer contextContainer = (ContextContainer) parser.getParent();
final String name = attrs.getValue("name");
emptyAttributeCheck(localName, "name", name, parser);
VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
Variable variable = new Variable();
if (variableScope != null) {
variable.setName(name);
List<Variable> variables = variableScope.getVariables();
if (variables == null) {
variables = new ArrayList<Variable>();
variableScope.setVariables(variables);
}
variables.add(variable);
} else {
throw new SAXParseException("Could not find default variable scope.", parser.getLocator());
}
return variable;
}
use of org.jbpm.process.core.ContextContainer in project jbpm by kiegroup.
the class CompensationEventListener method signalEvent.
/**
* When signaling compensation, you can do that in 1 of 2 ways:
* 1. signalEvent("Compensation", <node-with-compensation-handler-id>)
* This is specific compensation, that only possibly triggers the compensation handler
* attached to the node referred to by the <node-with-compensation-handler-id>.
* 2. signalEvent("Compensation", "implicit:" + <node-container-containing-compensation-scope-id> )
* This is implicit or general compensation, in which you trigger all visible compensation handlers
* (in the proper order, etc.) in the (sub-)process referred to by
* the <node-container-containing-compensation-scope-id>.
*/
public void signalEvent(String compensationType, Object activityRefStr) {
if (activityRefStr == null || !(activityRefStr instanceof String)) {
throw new WorkflowRuntimeException(null, getProcessInstance(), "Compensation can only be triggered with String events, not an event of type " + activityRefStr == null ? "null" : activityRefStr.getClass().getSimpleName());
}
// 1. parse the activity ref (is it general or specific compensation?)
String activityRef = (String) activityRefStr;
String toCompensateNodeId = activityRef;
boolean generalCompensation = false;
if (activityRef.startsWith(IMPLICIT_COMPENSATION_PREFIX)) {
toCompensateNodeId = activityRef.substring(IMPLICIT_COMPENSATION_PREFIX.length());
generalCompensation = true;
}
org.jbpm.process.core.Process process = (org.jbpm.process.core.Process) instance.getProcess();
// 2. for specific compensation: find the node that will be compensated
// for general compensation: find the compensation scope container that contains all the visible compensation handlers
Node toCompensateNode = null;
ContextContainer compensationScopeContainer = null;
if (generalCompensation) {
if (toCompensateNodeId.equals(instance.getProcessId())) {
compensationScopeContainer = process;
} else {
compensationScopeContainer = (ContextContainer) findNode(toCompensateNodeId);
}
} else {
toCompensateNode = findNode(toCompensateNodeId);
}
// c. handle the exception (which also cleans up the generated node instances)
if (toCompensateNode != null || compensationScopeContainer != null) {
CompensationScope compensationScope = null;
if (compensationScopeContainer != null) {
compensationScope = (CompensationScope) compensationScopeContainer.getDefaultContext(COMPENSATION_SCOPE);
} else {
compensationScope = (CompensationScope) ((NodeImpl) toCompensateNode).resolveContext(COMPENSATION_SCOPE, toCompensateNodeId);
}
assert compensationScope != null : "Compensation scope for node [" + toCompensateNodeId + "] could not be found!";
CompensationScopeInstance scopeInstance;
if (compensationScope.getContextContainerId().equals(process.getId())) {
// process level compensation
scopeInstance = (CompensationScopeInstance) instance.getContextInstance(compensationScope);
} else {
// nested compensation
Stack<NodeInstance> generatedInstances;
if (toCompensateNode == null) {
// logic is the same if it's specific or general
generatedInstances = createNodeInstanceContainers((Node) compensationScopeContainer, true);
} else {
generatedInstances = createNodeInstanceContainers(toCompensateNode, false);
}
NodeInstance nodeInstanceContainer = generatedInstances.peek();
scopeInstance = ((CompensationScopeInstance) ((ContextInstanceContainer) nodeInstanceContainer).getContextInstance(compensationScope));
scopeInstance.addCompensationInstances(generatedInstances);
}
scopeInstance.handleException(activityRef, null);
}
}
use of org.jbpm.process.core.ContextContainer in project jbpm by kiegroup.
the class ExceptionHandlerHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
ContextContainer contextContainer = (ContextContainer) parser.getParent();
final String type = element.getAttribute("type");
emptyAttributeCheck(localName, "type", type, parser);
final String faultName = element.getAttribute("faultName");
emptyAttributeCheck(localName, "faultName", type, parser);
final String faultVariable = element.getAttribute("faultVariable");
ActionExceptionHandler exceptionHandler = null;
if ("action".equals(type)) {
exceptionHandler = new ActionExceptionHandler();
org.w3c.dom.Node xmlNode = element.getFirstChild();
if (xmlNode instanceof Element) {
Element actionXml = (Element) xmlNode;
DroolsAction action = ActionNodeHandler.extractAction(actionXml);
((ActionExceptionHandler) exceptionHandler).setAction(action);
}
} else {
throw new SAXParseException("Unknown exception handler type " + type, parser.getLocator());
}
if (faultVariable != null && faultVariable.length() > 0) {
exceptionHandler.setFaultVariable(faultVariable);
}
ExceptionScope exceptionScope = (ExceptionScope) contextContainer.getDefaultContext(ExceptionScope.EXCEPTION_SCOPE);
if (exceptionScope == null) {
exceptionScope = new ExceptionScope();
contextContainer.addContext(exceptionScope);
contextContainer.setDefaultContext(exceptionScope);
}
exceptionScope.setExceptionHandler(faultName, exceptionHandler);
return null;
}
use of org.jbpm.process.core.ContextContainer 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);
}
}
}
}
use of org.jbpm.process.core.ContextContainer in project jbpm by kiegroup.
the class ProcessHandler method addCompensationScope.
protected static void addCompensationScope(final RuleFlowProcess process, final Node node, final org.kie.api.definition.process.NodeContainer parentContainer, final String compensationHandlerId) {
process.getMetaData().put("Compensation", true);
assert parentContainer instanceof ContextContainer : "Expected parent node to be a CompositeContextNode, not a " + parentContainer.getClass().getSimpleName();
ContextContainer contextContainer = (ContextContainer) parentContainer;
CompensationScope scope = null;
boolean addScope = false;
if (contextContainer.getContexts(CompensationScope.COMPENSATION_SCOPE) == null) {
addScope = true;
} else {
scope = (CompensationScope) contextContainer.getContexts(CompensationScope.COMPENSATION_SCOPE).get(0);
if (scope == null) {
addScope = true;
}
}
if (addScope) {
scope = new CompensationScope();
contextContainer.addContext(scope);
contextContainer.setDefaultContext(scope);
scope.setContextContainer(contextContainer);
}
CompensationHandler handler = new CompensationHandler();
handler.setNode(node);
if (scope.getExceptionHandler(compensationHandlerId) != null) {
throw new IllegalArgumentException("More than one compensation handler per node (" + compensationHandlerId + ")" + " is not supported!");
}
scope.setExceptionHandler(compensationHandlerId, handler);
}
Aggregations