com.github.javaparser.ast.expr.LambdaExpr Java Examples

The following examples show how to use com.github.javaparser.ast.expr.LambdaExpr. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TriggerMetaData.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
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()))
            .validate();
    metadata.getTriggers().add(triggerMetaData);

    // and add trigger action
    BlockStmt actionBody = new BlockStmt();
    CastExpr 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("kcontext"), "getProcessInstance")).addArgument(variable);
    actionBody.addStatement(producerMethodCall);
    return new LambdaExpr(
            new Parameter(new UnknownType(), KCONTEXT_VAR), // (kcontext) ->
            actionBody
    );
}
 
Example #2
Source File: RuleSetNodeVisitor.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
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);
    }
 
Example #3
Source File: RuleSetNodeVisitor.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private MethodCallExpr handleRuleFlowGroup(RuleSetNode.RuleType ruleType) {
    // build supplier for rule runtime
    BlockStmt actionBody = new BlockStmt();
    LambdaExpr lambda = new LambdaExpr(new Parameter(new UnknownType(), "()"), actionBody);

    MethodCallExpr ruleRuntimeBuilder = new MethodCallExpr(
            new MethodCallExpr(new NameExpr("app"), "ruleUnits"), "ruleRuntimeBuilder");
    MethodCallExpr ruleRuntimeSupplier = new MethodCallExpr(
            ruleRuntimeBuilder, "newKieSession",
            NodeList.nodeList(new StringLiteralExpr("defaultStatelessKieSession"), new NameExpr("app.config().rule()")));
    actionBody.addStatement(new ReturnStmt(ruleRuntimeSupplier));

    return new MethodCallExpr("ruleFlowGroup")
            .addArgument(new StringLiteralExpr(ruleType.getName()))
            .addArgument(lambda);

}
 
Example #4
Source File: AbstractNodeVisitor.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
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(
            new Parameter(new UnknownType(), KCONTEXT_VAR), // (kcontext) ->
            conditionBody
    );
}
 
Example #5
Source File: SplitNodeVisitor.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@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) {
                BlockStmt actionBody = new BlockStmt();
                LambdaExpr lambda = new LambdaExpr(
                        new Parameter(new UnknownType(), KCONTEXT_VAR), // (kcontext) ->
                        actionBody
                );

                for (Variable v : variableScope.getVariables()) {
                    actionBody.addStatement(makeAssignment(v));
                }
                BlockStmt constraintBody = new BlockStmt();
                constraintBody.addStatement(entry.getValue().getConstraint());

                actionBody.addStatement(constraintBody);

                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)));
}
 
Example #6
Source File: PrettyPrintVisitor.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit(LambdaExpr n, Void arg) {
    printJavaComment(n.getComment(), arg);

    final NodeList<Parameter> parameters = n.getParameters();
    final boolean printPar = n.isEnclosingParameters();

    if (printPar) {
        printer.print("(");
    }
    for (Iterator<Parameter> i = parameters.iterator(); i.hasNext(); ) {
        Parameter p = i.next();
        p.accept(this, arg);
        if (i.hasNext()) {
            printer.print(", ");
        }
    }
    if (printPar) {
        printer.print(")");
    }

    printer.print(" -> ");
    final Statement body = n.getBody();
    if (body instanceof ExpressionStmt) {
        // Print the expression directly
        ((ExpressionStmt) body).getExpression().accept(this, arg);
    } else {
        body.accept(this, arg);
    }
}
 
Example #7
Source File: LambdaExprMerger.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override public LambdaExpr doMerge(LambdaExpr first, LambdaExpr second) {
  LambdaExpr le = new LambdaExpr();

  le.setBody(mergeSingle(first.getBody(),second.getBody()));
  le.setParameters(mergeCollectionsInOrder(first.getParameters(),second.getParameters()));
  le.setParametersEnclosed(first.isParametersEnclosed());

  return le;
}
 
Example #8
Source File: LambdaExprMerger.java    From dolphin with Apache License 2.0 5 votes vote down vote up
@Override public boolean doIsEquals(LambdaExpr first, LambdaExpr second) {

    if(first.isParametersEnclosed() != second.isParametersEnclosed()) return false;
    if(!isEqualsUseMerger(first.getParameters(),second.getParameters())) return false;
    if(!isEqualsUseMerger(first.getBody(),second.getBody())) return false;

    return true;
  }
 
Example #9
Source File: AppendRoutingVisitor.java    From enkan with Eclipse Public License 1.0 4 votes vote down vote up
public void visit(final LambdaExpr n, final RoutingDefineContext arg) {
    if (arg.isInRoutingDefine()) {
        arg.setRoutingParameter(n.getParameters().get(0));
        n.getBody().accept(this, arg);
    }
}
 
Example #10
Source File: TraceVisitor.java    From JCTools with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(LambdaExpr n, Void arg) {
    out.println("LambdaExpr: " + (extended ? n : ""));
    super.visit(n, arg);
}