Search in sources :

Example 31 with LambdaExpr

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

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);
    variables.stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(conditionBody::addStatement);
    conditionBody.addStatement(new ReturnStmt(new EnclosedExpr(new NameExpr(consequence))));
    return new // (kcontext) ->
    LambdaExpr(// (kcontext) ->
    new Parameter(new UnknownType(), KCONTEXT_VAR), conditionBody);
}
Also used : UnknownType(com.github.javaparser.ast.type.UnknownType) Variable(io.automatiko.engine.workflow.base.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 32 with LambdaExpr

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

the class ActionNodeVisitor method visitNode.

@Override
public void visitNode(WorkflowProcess process, 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"));
    // if there is trigger defined on end event create TriggerMetaData for it
    if (node.getMetaData(TRIGGER_REF) != null) {
        LambdaExpr lambda = TriggerMetaData.buildLambdaExpr(node, metadata, variableScope);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    } else if (node.getMetaData(TRIGGER_TYPE) != null && node.getMetaData(TRIGGER_TYPE).equals("Compensation")) {
        // compensation
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.workflow.base.instance.impl.actions.ProcessInstanceCompensationAction"), NodeList.nodeList(new StringLiteralExpr((String) node.getMetaData("CompensationEvent"))))));
    } else if (node.getMetaData(TRIGGER_TYPE) != null && node.getMetaData(TRIGGER_TYPE).equals("Signal")) {
        // throw signal
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.workflow.base.instance.impl.actions.SignalProcessInstanceAction"), NodeList.nodeList(new StringLiteralExpr((String) node.getMetaData("Ref")), node.getMetaData("Variable") != null ? new StringLiteralExpr((String) node.getMetaData("Variable")) : new NullLiteralExpr()))));
    } else {
        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);
        variables.stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(actionBody::addStatement);
        actionBody.addStatement(new NameExpr(consequence));
        LambdaExpr lambda = new // (kcontext) ->
        LambdaExpr(// (kcontext) ->
        new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    }
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    for (DataAssociation association : node.getInAssociations()) {
    }
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter)

Example 33 with LambdaExpr

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

the class EndNodeVisitor method visitNode.

@Override
public void visitNode(WorkflowProcess process, String factoryField, EndNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
    body.addStatement(getAssignedFactoryMethod(factoryField, EndNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "End")).addStatement(getFactoryMethod(getNodeId(node), METHOD_TERMINATE, new BooleanLiteralExpr(node.isTerminate())));
    // if there is trigger defined on end event create TriggerMetaData for it
    if (node.getMetaData(TRIGGER_REF) != null) {
        LambdaExpr lambda = TriggerMetaData.buildLambdaExpr(node, metadata, variableScope);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    } else if (node.getMetaData(TRIGGER_TYPE) != null && node.getMetaData(TRIGGER_TYPE).equals("Compensation")) {
        // compensation
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.workflow.base.instance.impl.actions.ProcessInstanceCompensationAction"), NodeList.nodeList(new StringLiteralExpr((String) node.getMetaData("CompensationEvent"))))));
    } else if (node.getMetaData(REF) != null && EVENT_TYPE_SIGNAL.equals(node.getMetaData(EVENT_TYPE))) {
        MethodCallExpr getProcessInstance = getFactoryMethod(KCONTEXT_VAR, "getProcessInstance");
        MethodCallExpr signalEventMethod = new MethodCallExpr(getProcessInstance, "signalEvent").addArgument(new StringLiteralExpr((String) node.getMetaData(REF))).addArgument(new NullLiteralExpr());
        BlockStmt actionBody = new BlockStmt();
        actionBody.addStatement(signalEventMethod);
        LambdaExpr lambda = new LambdaExpr(new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
        body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
    }
    visitMetaData(node.getMetaData(), body, getNodeId(node));
    body.addStatement(getDoneMethod(getNodeId(node)));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) UnknownType(com.github.javaparser.ast.type.UnknownType) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) EndNodeFactory(io.automatiko.engine.workflow.process.executable.core.factory.EndNodeFactory) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 34 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class PatternExpressionBuilder method createIndexedByLambda.

private LambdaExpr createIndexedByLambda(SingleDrlxParseSuccess drlxParseResult, TypedExpression left, TypedExpression right) {
    LambdaExpr indexedByLeftOperandExtractor = new LambdaExpr();
    indexedByLeftOperandExtractor.setEnclosingParameters(true);
    indexedByLeftOperandExtractor.setBody(new ExpressionStmt(left.containThis() ? left.getExpression() : right.getExpression()));
    indexedByLeftOperandExtractor.addParameter(new Parameter(drlxParseResult.getPatternJPType(), THIS_PLACEHOLDER));
    return indexedByLeftOperandExtractor;
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) Parameter(com.github.javaparser.ast.body.Parameter) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Example 35 with LambdaExpr

use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.

the class PatternExpressionBuilder method buildIndexedBy.

private Optional<MethodCallExpr> buildIndexedBy(SingleDrlxParseSuccess drlxParseResult) {
    if (drlxParseResult.isUnification()) {
        TypedExpression left = drlxParseResult.getLeft();
        TypedExpression right = drlxParseResult.getRight();
        LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
        MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
        indexedByDSL.addArgument(org.drools.model.Index.ConstraintType.class.getCanonicalName() + ".EQUAL");
        indexedByDSL.addArgument("-1");
        indexedByDSL.addArgument(indexedByLeftOperandExtractor);
        indexedByDSL.addArgument("" + null);
        return Optional.of(indexedByDSL);
    }
    if (!shouldCreateIndex(drlxParseResult)) {
        return Optional.empty();
    }
    TypedExpression left = drlxParseResult.getLeft();
    TypedExpression right = drlxParseResult.getRight();
    Expression rightExpression = right.getExpression();
    if (!drlxParseResult.isBetaConstraint() && !(rightExpression instanceof LiteralExpr || isStringToDateExpression(rightExpression) || isNumberToStringExpression(rightExpression))) {
        return Optional.empty();
    }
    // not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
    FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr(org.drools.model.Index.ConstraintType.class.getCanonicalName()), drlxParseResult.getDecodeConstraintType().toString());
    LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
    MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
    indexedByDSL.addArgument(indexedBy_constraintType);
    indexedByDSL.addArgument(getIndexIdArgument(drlxParseResult, left));
    indexedByDSL.addArgument(indexedByLeftOperandExtractor);
    Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
    java.lang.reflect.Type leftType = left.getType();
    if (drlxParseResult.isBetaConstraint()) {
        addIndexedByDeclaration(left, right, left.containThis(), indexedByDSL, usedDeclarations);
    } else {
        indexedByDSL.addArgument(narrowExpressionToType(right, leftType));
    }
    return Optional.of(indexedByDSL);
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) 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