Skip to content

Commit

Permalink
[incubator-kie-issues-1152] Add support for collaborations (#3523)
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian authored May 28, 2024
1 parent ae22647 commit cc84426
Show file tree
Hide file tree
Showing 46 changed files with 846 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.xml.sax.SAXException;

import static org.jbpm.ruleflow.core.Metadata.EVENT_TYPE;
import static org.jbpm.ruleflow.core.Metadata.MESSAGE_REF;

public class BoundaryEventHandler extends AbstractNodeHandler {

Expand Down Expand Up @@ -408,6 +409,7 @@ protected void handleMessageNode(final Node node, final Element element,
eventNode.setMetaData("MessageType", message.getType());
eventNode.setMetaData("TriggerType", "ConsumeMessage");
eventNode.setMetaData("TriggerRef", message.getName());
eventNode.setMetaData(MESSAGE_REF, message.getId());
List<EventFilter> eventFilters = new ArrayList<>();
EventTypeFilter eventFilter = new EventTypeFilter();
eventFilter.setCorrelationManager(((RuleFlowProcess) parser.getMetaData().get("CurrentProcessDefinition")).getCorrelationManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static org.jbpm.ruleflow.core.Metadata.EVENT_TYPE_SIGNAL;
import static org.jbpm.ruleflow.core.Metadata.EVENT_TYPE_TIMER;
import static org.jbpm.ruleflow.core.Metadata.LINK_NAME;
import static org.jbpm.ruleflow.core.Metadata.MESSAGE_REF;
import static org.jbpm.ruleflow.core.Metadata.MESSAGE_TYPE;
import static org.jbpm.ruleflow.core.Metadata.SIGNAL_TYPE;
import static org.jbpm.ruleflow.core.Metadata.TRIGGER_REF;
Expand Down Expand Up @@ -240,6 +241,7 @@ protected void handleMessageNode(final Node node, final Element element,
eventNode.setMetaData(MESSAGE_TYPE, message.getType());
eventNode.setMetaData(TRIGGER_TYPE, CONSUME_MESSAGE);
eventNode.setMetaData(TRIGGER_REF, message.getName());
eventNode.setMetaData(MESSAGE_REF, message.getId());
List<EventFilter> eventFilters = new ArrayList<>();
EventTypeFilter eventFilter = new EventTypeFilter();
eventFilter.setCorrelationManager(((RuleFlowProcess) parser.getMetaData().get("CurrentProcessDefinition")).getCorrelationManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@
import org.jbpm.process.core.correlation.CorrelationManager;
import org.jbpm.process.core.event.EventFilter;
import org.jbpm.process.core.event.EventTypeFilter;
import org.jbpm.process.core.event.MVELMessageExpressionEvaluator;
import org.jbpm.process.core.timer.Timer;
import org.jbpm.process.instance.impl.Action;
import org.jbpm.process.instance.impl.MVELInterpretedReturnValueEvaluator;
import org.jbpm.process.instance.impl.ReturnValueEvaluator;
import org.jbpm.process.instance.impl.actions.CancelNodeInstanceAction;
import org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction;
import org.jbpm.process.instance.impl.actions.SignalProcessInstanceAction;
Expand Down Expand Up @@ -240,7 +241,7 @@ private void postProcessCollaborations(RuleFlowProcess process, Parser parser) {
correlationProperty.getMessageRefs().forEach(messageRef -> {

// for now only MVEL expressions
MVELMessageExpressionEvaluator evaluator = new MVELMessageExpressionEvaluator(correlationProperty.getRetrievalExpression(messageRef).getScript());
ReturnValueEvaluator evaluator = new MVELInterpretedReturnValueEvaluator(correlationProperty.getRetrievalExpression(messageRef).getScript());
correlationManager.addMessagePropertyExpression(key.getId(), messageRef, correlationProperty.getId(), evaluator);
});
}
Expand All @@ -251,7 +252,8 @@ private void postProcessCollaborations(RuleFlowProcess process, Parser parser) {
for (CorrelationSubscription subscription : HandlerUtil.correlationSubscription(process).values()) {
correlationManager.subscribeTo(subscription.getCorrelationKeyRef());
for (Map.Entry<String, Expression> binding : subscription.getPropertyExpressions().entrySet()) {
MVELMessageExpressionEvaluator evaluator = new MVELMessageExpressionEvaluator(binding.getValue().getScript());

ReturnValueEvaluator evaluator = new MVELInterpretedReturnValueEvaluator(binding.getValue().getScript());
correlationManager.addProcessSubscriptionPropertyExpression(subscription.getCorrelationKeyRef(), binding.getKey(), evaluator);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static org.jbpm.ruleflow.core.Metadata.EVENT_TYPE;
import static org.jbpm.ruleflow.core.Metadata.EVENT_TYPE_MESSAGE;
import static org.jbpm.ruleflow.core.Metadata.MAPPING_VARIABLE;
import static org.jbpm.ruleflow.core.Metadata.MESSAGE_REF;
import static org.jbpm.ruleflow.core.Metadata.MESSAGE_TYPE;
import static org.jbpm.ruleflow.core.Metadata.TRIGGER_MAPPING;
import static org.jbpm.ruleflow.core.Metadata.TRIGGER_MAPPING_INPUT;
Expand Down Expand Up @@ -138,6 +139,7 @@ protected Node handleNode(final Node node, final Element element, final String u
startNode.setMetaData(MESSAGE_TYPE, message.getType());
startNode.setMetaData(TRIGGER_TYPE, TriggerMetaData.TriggerType.ConsumeMessage.name());
startNode.setMetaData(TRIGGER_REF, message.getName());
startNode.setMetaData(MESSAGE_REF, message.getId());

addTriggerWithInMappings(startNode, "Message-" + message.getName(), message.getId(), ((RuleFlowProcess) parser.getMetaData().get("CurrentProcessDefinition")).getCorrelationManager());
} else if ("timerEventDefinition".equals(nodeName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void build(final PackageBuildContext context,
String text = descr.getText();

try {
XPATHReturnValueEvaluator expr = new XPATHReturnValueEvaluator(text,
null);
XPATHReturnValueEvaluator expr = new XPATHReturnValueEvaluator(text);

constraintNode.setEvaluator(expr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected String getNodeKey() {
public void visitNode(String factoryField, ActionNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, ActionNodeFactory.class, getNodeId(node), getNodeKey(), getWorkflowElementConstructor(node.getId())))
.addStatement(getNameMethod(node, "Script"));
visitMetaData(node.getMetaData(), body, getNodeId(node));

Optional<ExpressionSupplier> supplierAction = getAction(node, ExpressionSupplier.class);
if (isIntermediateCompensation(node)) {
Expand Down Expand Up @@ -101,7 +102,6 @@ public void visitNode(String factoryField, ActionNode node, BlockStmt body, Vari
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
}
addNodeMappings(node, body, getNodeId(node));
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getDoneMethod(getNodeId(node)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ protected String getNodeKey() {

@Override
public void visitNode(String factoryField, BoundaryEventNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, BoundaryEventNodeFactory.class, getNodeId(node), getNodeKey(), getWorkflowElementConstructor(node.getId())))
.addStatement(getNameMethod(node, "BoundaryEvent"))
body.addStatement(getAssignedFactoryMethod(factoryField, BoundaryEventNodeFactory.class, getNodeId(node), getNodeKey(), getWorkflowElementConstructor(node.getId())));
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getNameMethod(node, "BoundaryEvent"))
.addStatement(getFactoryMethod(getNodeId(node), METHOD_EVENT_TYPE, new StringLiteralExpr(node.getType())))
.addStatement(getFactoryMethod(getNodeId(node), METHOD_ATTACHED_TO, new StringLiteralExpr(node.getAttachedToNodeId())))
.addStatement(getFactoryMethod(getNodeId(node), METHOD_SCOPE, getOrNullExpr(node.getScope())));
Expand All @@ -70,7 +71,6 @@ public void visitNode(String factoryField, BoundaryEventNode node, BlockStmt bod
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ADD_COMPENSATION_HANDLER, new StringLiteralExpr(node.getAttachedToNodeId())));
}
addNodeMappings(node, body, getNodeId(node));
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getDoneMethod(getNodeId(node)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ protected String getNodeKey() {

@Override
public void visitNode(String factoryField, EventNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, EventNodeFactory.class, getNodeId(node), getNodeKey(), getWorkflowElementConstructor(node.getId())))
.addStatement(getNameMethod(node, "Event"))
body.addStatement(getAssignedFactoryMethod(factoryField, EventNodeFactory.class, getNodeId(node), getNodeKey(), getWorkflowElementConstructor(node.getId())));
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getNameMethod(node, "Event"))
.addStatement(getFactoryMethod(getNodeId(node), METHOD_EVENT_TYPE, new StringLiteralExpr(node.getType())));

Variable variable = null;
Expand All @@ -72,7 +73,7 @@ public void visitNode(String factoryField, EventNode node, BlockStmt body, Varia
}
}
addNodeMappings(node, body, getNodeId(node));
visitMetaData(node.getMetaData(), body, getNodeId(node));

body.addStatement(getDoneMethod(getNodeId(node)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.builtin.ReturnValueEvaluatorBuilderService;
import org.jbpm.compiler.canonical.descriptors.ExpressionUtils;
import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.Context;
Expand All @@ -34,6 +35,11 @@
import org.jbpm.process.core.context.exception.ActionExceptionHandler;
import org.jbpm.process.core.context.exception.ExceptionScope;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.core.correlation.Correlation;
import org.jbpm.process.core.correlation.CorrelationManager;
import org.jbpm.process.core.correlation.CorrelationProperties;
import org.jbpm.process.core.correlation.Message;
import org.jbpm.process.instance.impl.ReturnValueEvaluator;
import org.jbpm.process.instance.impl.actions.SignalProcessInstanceAction;
import org.jbpm.ruleflow.core.RuleFlowProcess;
import org.jbpm.ruleflow.core.RuleFlowProcessFactory;
Expand Down Expand Up @@ -80,8 +86,11 @@ public class ProcessVisitor extends AbstractVisitor {

private NodeVisitorBuilderService nodeVisitorService;

private ReturnValueEvaluatorBuilderService returnValueEvaluatorBuilderService;

public ProcessVisitor(ClassLoader contextClassLoader) {
nodeVisitorService = new NodeVisitorBuilderService(contextClassLoader);
returnValueEvaluatorBuilderService = ReturnValueEvaluatorBuilderService.instance(contextClassLoader);
}

public void visitProcess(WorkflowProcess process, MethodDeclaration processMethod, ProcessMetaData metadata) {
Expand Down Expand Up @@ -131,8 +140,9 @@ public void visitProcess(WorkflowProcess process, MethodDeclaration processMetho
((org.jbpm.workflow.core.WorkflowProcess) process).getOutputValidator().ifPresent(
v -> body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "outputValidator", ExpressionUtils.getLiteralExpr(v))));

visitCompensationScope(process, body);
visitMetaData(process.getMetaData(), body, FACTORY_FIELD_NAME);
visitCollaboration(process, body);
visitCompensationScope(process, body);
visitHeader(process, body);

List<Node> processNodes = new ArrayList<>();
Expand All @@ -150,6 +160,40 @@ public void visitProcess(WorkflowProcess process, MethodDeclaration processMetho
body.addStatement(new ReturnStmt(getProcessMethod));
}

private void visitCollaboration(WorkflowProcess process, BlockStmt body) {
RuleFlowProcess ruleFlowProcess = (RuleFlowProcess) process;
CorrelationManager correlationManager = ruleFlowProcess.getCorrelationManager();

for (String messageId : correlationManager.getMessagesId()) {
Message message = correlationManager.findMessageById(messageId);
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "newCorrelationMessage",
new StringLiteralExpr(message.getMessageRef()), new StringLiteralExpr(message.getMessageName()), new StringLiteralExpr(message.getMessageType())));
}

for (String correlationId : correlationManager.getCorrelationsId()) {
Correlation correlation = correlationManager.findCorrelationById(correlationId);
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "newCorrelationKey",
new StringLiteralExpr(correlation.getId()), new StringLiteralExpr(correlation.getName())));

for (String messageId : correlationManager.getMessagesId()) {
CorrelationProperties properties = correlation.getMessageCorrelationFor(messageId);
for (String propertyName : properties.names()) {
ReturnValueEvaluator evaluator = properties.getExpressionFor(propertyName);
Expression returnValueEvaluator = returnValueEvaluatorBuilderService.build(ruleFlowProcess, evaluator.dialect(), evaluator.expression());
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "newCorrelationProperty",
new StringLiteralExpr(correlation.getId()), new StringLiteralExpr(messageId), new StringLiteralExpr(propertyName), returnValueEvaluator));
}
}
CorrelationProperties subscriptions = correlation.getProcessSubscription();
for (String propertyName : subscriptions.names()) {
ReturnValueEvaluator evaluator = subscriptions.getExpressionFor(propertyName);
Expression returnValueEvaluator = returnValueEvaluatorBuilderService.build(ruleFlowProcess, evaluator.dialect(), evaluator.expression());
body.addStatement(getFactoryMethod(FACTORY_FIELD_NAME, "newCorrelationSubscription",
new StringLiteralExpr(correlation.getId()), new StringLiteralExpr(propertyName), returnValueEvaluator));
}
}
}

private void visitSubVariableScopes(org.kie.api.definition.process.Node[] nodes, BlockStmt body, Set<String> visitedVariables) {
for (org.kie.api.definition.process.Node node : nodes) {
if (node instanceof ContextContainer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

import java.util.Collection;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.Predicate;

import org.jbpm.compiler.canonical.builtin.ConstraintEvaluatorBuilderService;
import org.jbpm.compiler.canonical.builtin.ReturnValueEvaluatorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator;
import org.jbpm.process.instance.impl.ReturnValueEvaluator;
import org.jbpm.ruleflow.core.factory.SplitFactory;
import org.jbpm.workflow.core.Constraint;
import org.jbpm.workflow.core.impl.ConnectionRef;
Expand All @@ -39,6 +43,12 @@

public class SplitNodeVisitor extends AbstractNodeVisitor<Split> {

ReturnValueEvaluatorBuilderService returnValueEvaluatorBuilderService;

public SplitNodeVisitor(ReturnValueEvaluatorBuilderService returnValueEvaluatorBuilderService) {
this.returnValueEvaluatorBuilderService = returnValueEvaluatorBuilderService;
}

@Override
protected String getNodeKey() {
return "splitNode";
Expand All @@ -54,20 +64,34 @@ public void visitNode(String factoryField, Split node, BlockStmt body, VariableS

if (node.getType() == Split.TYPE_OR || node.getType() == Split.TYPE_XOR) {
for (Entry<ConnectionRef, Collection<Constraint>> entry : node.getConstraints().entrySet()) {
if (entry.getValue() != null) {
for (Constraint constraint : entry.getValue()) {
if (constraint != null) {
Expression returnValueEvaluator = ConstraintEvaluatorBuilderService.instance().build(node, constraint);
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_CONSTRAINT,
getWorkflowElementConstructor(entry.getKey().getNodeId()),
new StringLiteralExpr(getOrDefault(entry.getKey().getConnectionId(), "")),
new StringLiteralExpr(entry.getKey().getToType()),
new StringLiteralExpr(constraint.getDialect()),
returnValueEvaluator,
new IntegerLiteralExpr(constraint.getPriority()),
new BooleanLiteralExpr(constraint.isDefault())));
}
if (entry.getValue() == null || entry.getValue().isEmpty()) {
continue;
}

for (Constraint constraint : entry.getValue().stream().filter(Predicate.not(Objects::isNull)).toList()) {
Expression returnValueEvaluator;
if (constraint instanceof ReturnValueConstraintEvaluator) {
ReturnValueEvaluator evaluator = ((ReturnValueConstraintEvaluator) constraint).getReturnValueEvaluator();
returnValueEvaluator = returnValueEvaluatorBuilderService.build(node,
evaluator.dialect(),
evaluator.expression(),
evaluator.type(),
evaluator.root());

} else {
returnValueEvaluator = returnValueEvaluatorBuilderService.build(node,
constraint.getDialect(),
constraint.getConstraint());
}
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_CONSTRAINT,
getWorkflowElementConstructor(entry.getKey().getNodeId()),
new StringLiteralExpr(getOrDefault(entry.getKey().getConnectionId(), "")),
new StringLiteralExpr(entry.getKey().getToType()),
new StringLiteralExpr(constraint.getDialect()),
returnValueEvaluator,
new IntegerLiteralExpr(constraint.getPriority()),
new BooleanLiteralExpr(constraint.isDefault())));

}
}
}
Expand Down
Loading

0 comments on commit cc84426

Please sign in to comment.