Skip to content

Commit

Permalink
fix constraints in node impl
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Jul 29, 2024
1 parent 2978ec5 commit e1eb0c8
Show file tree
Hide file tree
Showing 45 changed files with 277 additions and 293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public abstract class AbstractCompositeNodeVisitor<T extends CompositeContextNod

protected NodeVisitorBuilderService nodevisitorService;

public AbstractCompositeNodeVisitor(NodeVisitorBuilderService nodevisitorService) {
this.nodevisitorService = nodevisitorService;
public AbstractCompositeNodeVisitor(ClassLoader classLoader) {
super(classLoader);
this.nodevisitorService = new NodeVisitorBuilderService(classLoader);
}

protected <U extends Node> void visitNodes(String factoryField, U[] nodes, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.jbpm.compiler.canonical;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand All @@ -33,13 +35,17 @@
import org.jbpm.process.core.context.variable.Variable;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.process.core.datatype.DataTypeResolver;
import org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator;
import org.jbpm.process.instance.impl.ReturnValueEvaluator;
import org.jbpm.process.instance.impl.actions.HandleEscalationAction;
import org.jbpm.process.instance.impl.actions.ProduceEventAction;
import org.jbpm.process.instance.impl.actions.SignalProcessInstanceAction;
import org.jbpm.ruleflow.core.Metadata;
import org.jbpm.ruleflow.core.factory.MappableNodeFactory;
import org.jbpm.util.JbpmClassLoaderUtil;
import org.jbpm.workflow.core.Constraint;
import org.jbpm.workflow.core.impl.ConnectionImpl;
import org.jbpm.workflow.core.impl.ConnectionRef;
import org.jbpm.workflow.core.impl.DataAssociation;
import org.jbpm.workflow.core.impl.DataAssociation.DataAssociationType;
import org.jbpm.workflow.core.impl.DataDefinition;
Expand All @@ -57,10 +63,12 @@
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.CastExpr;
import com.github.javaparser.ast.expr.ClassExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
Expand All @@ -82,6 +90,7 @@
import static org.drools.util.StringUtils.ucFirst;
import static org.jbpm.ruleflow.core.Metadata.CUSTOM_AUTO_START;
import static org.jbpm.ruleflow.core.Metadata.HIDDEN;
import static org.jbpm.ruleflow.core.factory.NodeFactory.METHOD_CONSTRAINT;
import static org.jbpm.ruleflow.core.factory.NodeFactory.METHOD_DONE;
import static org.jbpm.ruleflow.core.factory.NodeFactory.METHOD_NAME;
import static org.kie.kogito.internal.utils.ConversionUtils.sanitizeString;
Expand All @@ -90,6 +99,22 @@ public abstract class AbstractNodeVisitor<T extends Node> extends AbstractVisito

protected abstract String getNodeKey();

private ReturnValueEvaluatorBuilderService returnValueEvaluatorBuilderService;
private ClassLoader classLoader;

public AbstractNodeVisitor(ClassLoader classLoader) {
this.classLoader = classLoader;
this.returnValueEvaluatorBuilderService = ReturnValueEvaluatorBuilderService.instance(classLoader);
}

public ClassLoader getClassLoader() {
return classLoader;
}

public ReturnValueEvaluatorBuilderService getReturnValueEvaluatorBuilderService() {
return returnValueEvaluatorBuilderService;
}

public void visitNode(T node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
visitNode(FACTORY_FIELD_NAME, node, body, variableScope, metadata);
if (isAdHocNode(node) && !(node instanceof HumanTaskNode)) {
Expand All @@ -100,6 +125,8 @@ public void visitNode(T node, BlockStmt body, VariableScope variableScope, Proce
addScript(extendedNodeImpl, body, ON_ACTION_SCRIPT_METHOD, ExtendedNodeImpl.EVENT_NODE_ENTER);
addScript(extendedNodeImpl, body, ON_ACTION_SCRIPT_METHOD, ExtendedNodeImpl.EVENT_NODE_EXIT);
}

addConstraints(node, returnValueEvaluatorBuilderService, body);
}

private void addScript(ExtendedNodeImpl extendedNodeImpl, BlockStmt body, String factoryMethod, String actionType) {
Expand All @@ -123,6 +150,41 @@ private void addScript(ExtendedNodeImpl extendedNodeImpl, BlockStmt body, String
}
}

public void addConstraints(T currentNode, ReturnValueEvaluatorBuilderService returnValueEvaluatorBuilderService, BlockStmt body) {
NodeImpl node = (NodeImpl) currentNode;
for (Map.Entry<ConnectionRef, Collection<Constraint>> entry : node.getConstraints().entrySet()) {
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 returnValueConstraintEvaluator) {
ReturnValueEvaluator evaluator = returnValueConstraintEvaluator.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(currentNode), 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())));

}
}
}

private Expression buildDroolsConsequenceAction(ExtendedNodeImpl extendedNodeImpl, String dialect, String script) {
if (script == null) {
return new NullLiteralExpr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class ActionNodeVisitor extends AbstractNodeVisitor<ActionNode> {

private static final String INTERMEDIATE_COMPENSATION_TYPE = "IntermediateThrowEvent-None";

public ActionNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "actionNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@

public class BoundaryEventNodeVisitor extends AbstractNodeVisitor<BoundaryEventNode> {

public BoundaryEventNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "boundaryEventNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

public class CatchLinkNodeVisitor extends AbstractNodeVisitor<CatchLinkNode> {

public CatchLinkNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "catchLinkNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.HashSet;
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.CompositeContextNodeFactory;
import org.jbpm.workflow.core.node.CompositeContextNode;
Expand All @@ -33,8 +32,8 @@

public class CompositeContextNodeVisitor<T extends CompositeContextNode> extends AbstractCompositeNodeVisitor<T> {

public CompositeContextNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
public CompositeContextNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

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

import org.jbpm.compiler.canonical.builtin.ReturnValueEvaluatorBuilderService;
import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.DynamicNodeFactory;
import org.jbpm.workflow.core.node.DynamicNode;
Expand All @@ -38,8 +37,8 @@ public class DynamicNodeVisitor extends CompositeContextNodeVisitor<DynamicNode>

private ReturnValueEvaluatorBuilderService builder;

public DynamicNodeVisitor(NodeVisitorBuilderService nodeVisitorService, ClassLoader classLoader) {
super(nodeVisitorService);
public DynamicNodeVisitor(ClassLoader classLoader) {
super(classLoader);
this.builder = ReturnValueEvaluatorBuilderService.instance(classLoader);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@

public class EndNodeVisitor extends AbstractNodeVisitor<EndNode> {

public EndNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "endNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

public class EventNodeVisitor extends AbstractNodeVisitor<EventNode> {

public EventNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "eventNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Collection;
import java.util.stream.Stream;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.EventSubProcessNodeFactory;
import org.jbpm.workflow.core.node.EventSubProcessNode;
Expand All @@ -36,8 +35,8 @@

public class EventSubProcessNodeVisitor extends CompositeContextNodeVisitor<EventSubProcessNode> {

public EventSubProcessNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
public EventSubProcessNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@

public class FaultNodeVisitor extends AbstractNodeVisitor<FaultNode> {

public FaultNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "faultNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Arrays;
import java.util.List;

import org.jbpm.compiler.canonical.node.NodeVisitorBuilderService;
import org.jbpm.process.core.context.variable.VariableScope;
import org.jbpm.ruleflow.core.factory.ForEachNodeFactory;
import org.jbpm.workflow.core.Node;
Expand All @@ -45,8 +44,8 @@

public class ForEachNodeVisitor extends AbstractCompositeNodeVisitor<ForEachNode> {

public ForEachNodeVisitor(NodeVisitorBuilderService nodeVisitorService) {
super(nodeVisitorService);
public ForEachNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

public class JoinNodeVisitor extends AbstractNodeVisitor<Join> {

public JoinNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "joinNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@

public class LambdaSubProcessNodeVisitor extends AbstractNodeVisitor<SubProcessNode> {

public LambdaSubProcessNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "subProcessNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

public class MilestoneNodeVisitor extends AbstractNodeVisitor<MilestoneNode> {

public MilestoneNodeVisitor(ClassLoader classLoader) {
super(classLoader);
}

@Override
protected String getNodeKey() {
return "milestoneNode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ public class RuleSetNodeVisitor extends AbstractNodeVisitor<RuleSetNode> {

public static final Logger logger = LoggerFactory.getLogger(ProcessToExecModelGenerator.class);

private final ClassLoader contextClassLoader;
private final AssignableChecker assignableChecker;

public RuleSetNodeVisitor(ClassLoader contextClassLoader) {
this.contextClassLoader = contextClassLoader;
super(contextClassLoader);
this.assignableChecker = AssignableChecker.create(contextClassLoader);
}

Expand Down Expand Up @@ -144,7 +143,7 @@ private MethodCallExpr handleDecision(DecisionRuleType ruleType) {

private MethodCallExpr handleRuleUnit(VariableScope variableScope, ProcessMetaData metadata, RuleSetNode ruleSetNode, String nodeName, RuleType ruleType) {
String unitName = ruleType.getName();
ProcessContextMetaModel processContext = new ProcessContextMetaModel(variableScope, contextClassLoader);
ProcessContextMetaModel processContext = new ProcessContextMetaModel(variableScope, getClassLoader());
RuleUnitDescription description;

try {
Expand All @@ -170,7 +169,7 @@ private MethodCallExpr handleRuleUnit(VariableScope variableScope, ProcessMetaDa
}

private GeneratedRuleUnitDescription generateRuleUnitDescription(String unitName, ProcessContextMetaModel processContext) {
GeneratedRuleUnitDescription d = new GeneratedRuleUnitDescription(unitName, contextClassLoader);
GeneratedRuleUnitDescription d = new GeneratedRuleUnitDescription(unitName, getClassLoader());
for (Variable variable : processContext.getVariables()) {
d.putDatasourceVar(
variable.getName(),
Expand Down Expand Up @@ -214,7 +213,7 @@ private MethodCallExpr handleRuleFlowGroup(RuleType ruleType) {
private Class<?> loadUnitClass(String unitName, String packageName) throws ClassNotFoundException {
ClassNotFoundException ex;
try {
return contextClassLoader.loadClass(unitName);
return getClassLoader().loadClass(unitName);
} catch (ClassNotFoundException e) {
ex = e;
}
Expand All @@ -223,7 +222,7 @@ private Class<?> loadUnitClass(String unitName, String packageName) throws Class
}
// maybe the name is not qualified. Let's try with tacking the packageName at the front
try {
return contextClassLoader.loadClass(packageName + "." + unitName);
return getClassLoader().loadClass(packageName + "." + unitName);
} catch (ClassNotFoundException e) {
// throw the original error
throw ex;
Expand Down
Loading

0 comments on commit e1eb0c8

Please sign in to comment.