Search in sources :

Example 61 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.

the class RuleSetNodeVisitor method handleDecision.

private MethodCallExpr handleDecision(RuleSetNode.RuleType.Decision ruleType) {
    StringLiteralExpr namespace = new StringLiteralExpr(ruleType.getNamespace());
    StringLiteralExpr model = new StringLiteralExpr(ruleType.getModel());
    Expression decision = ruleType.getDecision() == null ? new NullLiteralExpr() : new StringLiteralExpr(ruleType.getDecision());
    MethodCallExpr decisionModels = new MethodCallExpr(new NameExpr("app"), "decisionModels");
    MethodCallExpr decisionModel = new MethodCallExpr(decisionModels, "getDecisionModel").addArgument(namespace).addArgument(model);
    BlockStmt actionBody = new BlockStmt();
    LambdaExpr lambda = new LambdaExpr(new Parameter(new UnknownType(), "()"), actionBody);
    actionBody.addStatement(new ReturnStmt(decisionModel));
    return new MethodCallExpr(METHOD_DECISION).addArgument(namespace).addArgument(model).addArgument(decision).addArgument(lambda);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 62 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.

the class SplitNodeVisitor method visitNode.

@Override
public void visitNode(WorkflowProcess process, 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) {
                String dialect = entry.getValue().getDialect();
                if ("jq".equals(dialect)) {
                    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()), new StringLiteralExpr(entry.getValue().getConstraint()), new IntegerLiteralExpr(entry.getValue().getPriority())));
                } else {
                    BlockStmt actionBody = new BlockStmt();
                    LambdaExpr lambda = new // (kcontext) ->
                    LambdaExpr(// (kcontext) ->
                    new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
                    for (Variable v : variableScope.getVariables()) {
                        actionBody.addStatement(makeAssignment(v));
                    }
                    variableScope.getVariables().stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(actionBody::addStatement);
                    if (entry.getValue().getConstraint().contains(System.getProperty("line.separator"))) {
                        BlockStmt constraintBody = new BlockStmt();
                        constraintBody.addStatement(entry.getValue().getConstraint());
                        actionBody.addStatement(constraintBody);
                    } else {
                        actionBody.addStatement(new ReturnStmt(new EnclosedExpr(new NameExpr(entry.getValue().getConstraint()))));
                    }
                    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()), lambda, new IntegerLiteralExpr(entry.getValue().getPriority())));
                }
            }
        }
    }
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) Constraint(io.automatiko.engine.workflow.process.core.Constraint) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) SplitFactory(io.automatiko.engine.workflow.process.executable.core.factory.SplitFactory) UnknownType(com.github.javaparser.ast.type.UnknownType) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) ConnectionRef(io.automatiko.engine.workflow.process.core.impl.ConnectionRef) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 63 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.

the class TriggerMetaData method buildLambdaExpr.

public static LambdaExpr buildLambdaExpr(Node node, ProcessMetaData metadata, VariableScope variableScope) {
    Map<String, Object> nodeMetaData = node.getMetaData();
    TriggerMetaData triggerMetaData = new TriggerMetaData((String) nodeMetaData.get(TRIGGER_REF), (String) nodeMetaData.get(TRIGGER_TYPE), (String) nodeMetaData.get(MESSAGE_TYPE), (String) nodeMetaData.get(MAPPING_VARIABLE), String.valueOf(node.getId()), node.getName()).validate();
    triggerMetaData.addContext(node.getMetaData());
    triggerMetaData.addContext(Collections.singletonMap("_node_", node));
    metadata.addTrigger(triggerMetaData);
    // and add trigger action
    BlockStmt actionBody = new BlockStmt();
    Expression variable;
    if (metadata.isServerless()) {
        variable = new CastExpr(new ClassOrInterfaceType(null, triggerMetaData.getDataType()), new MethodCallExpr(null, "variableExpression").addArgument(new NameExpr(KCONTEXT_VAR)).addArgument(new StringLiteralExpr(triggerMetaData.getModelRef())));
    } else {
        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(triggerMetaData.getModelRef());
        if (matcher.find()) {
            String expression = matcher.group(1);
            List<Variable> variables = variableScope.getVariables();
            variables.stream().filter(v -> expression.contains(v.getName())).map(ActionNodeVisitor::makeAssignment).forEach(actionBody::addStatement);
            variables.stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(actionBody::addStatement);
            variable = new CastExpr(new ClassOrInterfaceType(null, triggerMetaData.getDataType()), new NameExpr(expression));
        } else {
            variable = new CastExpr(new ClassOrInterfaceType(null, triggerMetaData.getDataType()), new MethodCallExpr(new NameExpr(KCONTEXT_VAR), "getVariable").addArgument(new StringLiteralExpr(triggerMetaData.getModelRef())));
        }
    }
    MethodCallExpr producerMethodCall = new MethodCallExpr(new NameExpr("producer_" + node.getId()), "produce").addArgument(new MethodCallExpr(new NameExpr("context"), "getProcessInstance")).addArgument(variable);
    actionBody.addStatement(producerMethodCall);
    return new // (kcontext) ->
    LambdaExpr(// (kcontext) ->
    new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) Matcher(java.util.regex.Matcher) 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) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Parameter(com.github.javaparser.ast.body.Parameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 64 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project gin by gintool.

the class CloneVisitorCopyIDs method visit.

@Override
public Visitable visit(final LambdaExpr n, final Object arg) {
    Visitable r = checkForReplacement(n);
    if (r == null) {
        r = super.visit(n, arg);
        ((LambdaExpr) r).setData(SourceFileTree.NODEKEY_ID, n.containsData(SourceFileTree.NODEKEY_ID) ? n.getData(SourceFileTree.NODEKEY_ID) : SourceFileTree.NODE_NULL_ID);
    }
    return r;
}
Also used : Visitable(com.github.javaparser.ast.visitor.Visitable) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr)

Example 65 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.

the class TriggerMetaData method buildLambdaExpr.

public static LambdaExpr buildLambdaExpr(Node node, ProcessMetaData metadata) {
    Map<String, Object> nodeMetaData = node.getMetaData();
    TriggerMetaData triggerMetaData = new TriggerMetaData((String) nodeMetaData.get(TRIGGER_REF), (String) nodeMetaData.get(TRIGGER_TYPE), (String) nodeMetaData.get(MESSAGE_TYPE), (String) nodeMetaData.get(MAPPING_VARIABLE), String.valueOf(node.getId()), node.getName()).validate();
    triggerMetaData.addContext(node.getMetaData());
    triggerMetaData.addContext(Collections.singletonMap("_node_", node));
    metadata.addTrigger(triggerMetaData);
    // and add trigger action
    BlockStmt actionBody = new BlockStmt();
    Expression variable;
    if (metadata.isServerless()) {
        variable = new CastExpr(new ClassOrInterfaceType(null, triggerMetaData.getDataType()), new MethodCallExpr(null, "variableExpression").addArgument(new NameExpr(KCONTEXT_VAR)).addArgument(new StringLiteralExpr(triggerMetaData.getModelRef())));
    } else {
        variable = new CastExpr(new ClassOrInterfaceType(null, triggerMetaData.getDataType()), new MethodCallExpr(new NameExpr(KCONTEXT_VAR), "getVariable").addArgument(new StringLiteralExpr(triggerMetaData.getModelRef())));
    }
    MethodCallExpr producerMethodCall = new MethodCallExpr(new NameExpr("producer_" + node.getId()), "produce").addArgument(new MethodCallExpr(new NameExpr("context"), "getProcessInstance")).addArgument(variable);
    actionBody.addStatement(producerMethodCall);
    return new // (kcontext) ->
    LambdaExpr(// (kcontext) ->
    new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
}
Also used : 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) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Parameter(com.github.javaparser.ast.body.Parameter) 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