Search in sources :

Example 66 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class SplitNodeVisitor method visitNode.

@Override
public void visitNode(String factoryField, Split node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
    body.addStatement(getAssignedFactoryMethod(factoryField, SplitFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Split")).addStatement(getFactoryMethod(getNodeId(node), METHOD_TYPE, new IntegerLiteralExpr(node.getType())));
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    if (node.getType() == Split.TYPE_OR || node.getType() == Split.TYPE_XOR) {
        for (Entry<ConnectionRef, Constraint> entry : node.getConstraints().entrySet()) {
            if (entry.getValue() != null) {
                Expression returnValueEvaluator;
                if (entry.getValue() instanceof ReturnValueConstraintEvaluator && ((ReturnValueConstraintEvaluator) entry.getValue()).getReturnValueEvaluator() instanceof Supplier) {
                    returnValueEvaluator = ((Supplier<Expression>) ((ReturnValueConstraintEvaluator) entry.getValue()).getReturnValueEvaluator()).get();
                } else if ("FEEL".equals(entry.getValue().getDialect())) {
                    returnValueEvaluator = FEELDialectCanonicalUtils.buildFEELReturnValueEvaluator(variableScope, entry);
                } else {
                    BlockStmt actionBody = new BlockStmt();
                    LambdaExpr lambda = new LambdaExpr(// (kcontext) ->
                    new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
                    for (Variable v : variableScope.getVariables()) {
                        actionBody.addStatement(makeAssignment(v));
                    }
                    BlockStmt blockStmt = StaticJavaParser.parseBlock("{" + entry.getValue().getConstraint() + "}");
                    blockStmt.getStatements().forEach(actionBody::addStatement);
                    returnValueEvaluator = lambda;
                }
                body.addStatement(getFactoryMethod(getNodeId(node), METHOD_CONSTRAINT, new LongLiteralExpr(entry.getKey().getNodeId()), new StringLiteralExpr(getOrDefault(entry.getKey().getConnectionId(), "")), new StringLiteralExpr(entry.getKey().getToType()), new StringLiteralExpr(entry.getValue().getDialect()), returnValueEvaluator, new IntegerLiteralExpr(entry.getValue().getPriority()), new BooleanLiteralExpr(entry.getValue().isDefault())));
            }
        }
    }
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Variable(org.jbpm.process.core.context.variable.Variable) Constraint(org.jbpm.workflow.core.Constraint) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ReturnValueConstraintEvaluator(org.jbpm.process.instance.impl.ReturnValueConstraintEvaluator) SplitFactory(org.jbpm.ruleflow.core.factory.SplitFactory) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) Supplier(java.util.function.Supplier) ConnectionRef(org.jbpm.workflow.core.impl.ConnectionRef)

Example 67 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class AbstractNodeVisitor method createLambdaExpr.

protected static LambdaExpr createLambdaExpr(String consequence, VariableScope scope) {
    BlockStmt conditionBody = new BlockStmt();
    List<Variable> variables = scope.getVariables();
    variables.stream().map(ActionNodeVisitor::makeAssignment).forEach(conditionBody::addStatement);
    conditionBody.addStatement(new ReturnStmt(new EnclosedExpr(new NameExpr(consequence))));
    return new LambdaExpr(// (kcontext) ->
    new Parameter(new UnknownType(), KCONTEXT_VAR), conditionBody);
}
Also used : UnknownType(com.github.javaparser.ast.type.UnknownType) Variable(org.jbpm.process.core.context.variable.Variable) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Parameter(com.github.javaparser.ast.body.Parameter) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 68 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class AbstractNodeVisitor method buildCompensationLambdaExpr.

public static LambdaExpr buildCompensationLambdaExpr(String compensationRef) {
    BlockStmt actionBody = new BlockStmt();
    MethodCallExpr getProcessInstance = new MethodCallExpr(new NameExpr(KCONTEXT_VAR), "getProcessInstance");
    MethodCallExpr signalEvent = new MethodCallExpr(getProcessInstance, "signalEvent").addArgument(new StringLiteralExpr(Metadata.EVENT_TYPE_COMPENSATION)).addArgument(new StringLiteralExpr(compensationRef));
    actionBody.addStatement(signalEvent);
    return new LambdaExpr(// (kcontext) ->
    new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
}
Also used : UnknownType(com.github.javaparser.ast.type.UnknownType) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 69 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class ActionNodeVisitor method visitNode.

@Override
public void visitNode(String factoryField, ActionNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
    body.addStatement(getAssignedFactoryMethod(factoryField, ActionNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Script"));
    Optional<ExpressionSupplier> supplierAction = getAction(node, ExpressionSupplier.class);
    if (isIntermediateCompensation(node)) {
        ProcessInstanceCompensationAction action = (ProcessInstanceCompensationAction) node.getAction().getMetaData(Metadata.ACTION);
        String compensateNode = CompensationScope.IMPLICIT_COMPENSATION_PREFIX + metadata.getProcessId();
        if (action != null) {
            compensateNode = action.getActivityRef();
        }
        LambdaExpr lambda = buildCompensationLambdaExpr(compensateNode);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    } else if (supplierAction.isPresent()) {
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, supplierAction.get().get(node, metadata)));
    } else if (node.getMetaData(TRIGGER_REF) != null) {
        // if there is trigger defined on end event create TriggerMetaData for it
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, buildProducerAction(node, metadata)));
    } else if (node.getMetaData(REF) != null && EVENT_TYPE_SIGNAL.equals(node.getMetaData(EVENT_TYPE))) {
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, buildAction((String) node.getMetaData(REF), (String) node.getMetaData(VARIABLE), (String) node.getMetaData(MAPPING_VARIABLE_INPUT), (String) node.getMetaData(CUSTOM_SCOPE))));
    } else if (node.getAction() instanceof DroolsConsequenceAction) {
        String consequence = getActionConsequence(node.getAction());
        if (consequence == null || consequence.trim().isEmpty()) {
            throw new IllegalStateException("Action node " + node.getId() + " name " + node.getName() + " has no action defined");
        }
        BlockStmt actionBody = new BlockStmt();
        List<Variable> variables = variableScope.getVariables();
        variables.stream().filter(v -> consequence.contains(v.getName())).map(ActionNodeVisitor::makeAssignment).forEach(actionBody::addStatement);
        BlockStmt blockStmt = StaticJavaParser.parseBlock("{" + consequence + "}");
        blockStmt.getStatements().forEach(actionBody::addStatement);
        LambdaExpr lambda = new LambdaExpr(// (kcontext) ->
        new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    }
    addNodeMappings(node, body, getNodeId(node));
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : Variable(org.jbpm.process.core.context.variable.Variable) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ProcessInstanceCompensationAction(org.jbpm.process.instance.impl.actions.ProcessInstanceCompensationAction) UnknownType(com.github.javaparser.ast.type.UnknownType) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter)

Example 70 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project kogito-runtimes by kiegroup.

the class ProcessContainerGenerator method addProcessToApplication.

public void addProcessToApplication(ProcessGenerator r) {
    ObjectCreationExpr newProcess = new ObjectCreationExpr().setType(r.targetCanonicalName()).addArgument("application").addArgument(new NullLiteralExpr());
    MethodCallExpr expr = new MethodCallExpr(newProcess, "configure");
    MethodCallExpr method = new MethodCallExpr(new NameExpr("mappedProcesses"), "computeIfAbsent", nodeList(new StringLiteralExpr(r.processId()), new LambdaExpr(new Parameter(new UnknownType(), "k"), expr)));
    IfStmt byProcessId = new IfStmt(new MethodCallExpr(new StringLiteralExpr(r.processId()), "equals", nodeList(new NameExpr("processId"))), new ReturnStmt(method), null);
    byProcessIdBody.addStatement(byProcessId);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) IfStmt(com.github.javaparser.ast.stmt.IfStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)70 Parameter (com.github.javaparser.ast.body.Parameter)37 NameExpr (com.github.javaparser.ast.expr.NameExpr)30 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)29 Expression (com.github.javaparser.ast.expr.Expression)27 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)25 UnknownType (com.github.javaparser.ast.type.UnknownType)24 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)19 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)15 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)12 CompilationUnit (com.github.javaparser.ast.CompilationUnit)10 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)10 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 Test (org.junit.jupiter.api.Test)10 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)9 LongLiteralExpr (com.github.javaparser.ast.expr.LongLiteralExpr)9 CastExpr (com.github.javaparser.ast.expr.CastExpr)8 ArrayList (java.util.ArrayList)8 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)8