Search in sources :

Example 1 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class FEELPropertyAccessibleImplementation method getFeelPropertyDefinition.

private MethodDefinition getFeelPropertyDefinition() {
    MethodDeclaration getFEELProperty = cloneMethodTemplate("getFEELProperty");
    SwitchStmt firstSwitch = getFEELProperty.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException("Missing Switch Statement in getFEELProperty template"));
    firstSwitch.setComment(null);
    List<SwitchEntry> collect = fields.stream().map(this::toGetPropertySwitchEntry).collect(Collectors.toList());
    SwitchEntry defaultSwitchStmt = firstSwitch.findFirst(SwitchEntry.class, sw -> sw.getLabels().isEmpty()).orElseThrow(() -> new InvalidTemplateException("Missing Default Switch Statement in getFEELProperty template"));
    collect.add(defaultSwitchStmt);
    firstSwitch.setEntries(nodeList(collect));
    String body = getFEELProperty.getBody().orElseThrow(() -> new InvalidTemplateException("Empty body in getFeelProperty clone")).toString();
    MethodWithStringBody getFeelPropertyDefinition = new MethodWithStringBody("getFEELProperty", EvalHelper.PropertyValueResult.class.getCanonicalName(), body).addParameter(String.class.getCanonicalName(), "property");
    addOverrideAnnotation(getFeelPropertyDefinition);
    return getFeelPropertyDefinition;
}
Also used : MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) CastExpr(com.github.javaparser.ast.expr.CastExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ArrayList(java.util.ArrayList) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InputStream(java.io.InputStream) EvalHelper(org.kie.dmn.feel.util.EvalHelper) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) EvalHelper(org.kie.dmn.feel.util.EvalHelper) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 2 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class PropagatorCompilerHandler method startRangeIndexedAlphaNode.

@Override
public void startRangeIndexedAlphaNode(AlphaNode alphaNode) {
    SwitchEntry switchEntry = new SwitchEntry().setLabels(nodeList(new IntegerLiteralExpr(alphaNode.getId())));
    addNewSwitchEntryToStack(switchEntry);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 3 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class SetNodeReferenceHandler method generateSwitchBody.

private void generateSwitchBody(BlockStmt switchBodyStatements, List<NetworkNode> subNodes) {
    SwitchStmt switchStmt = new SwitchStmt();
    switchStmt.setSelector(parseExpression("node.getId()"));
    NodeList<SwitchEntry> entries = new NodeList<>();
    for (NetworkNode n : subNodes) {
        String assignStatementString;
        if (n instanceof AlphaNode) {
            assignStatementString = getVariableAssignmentStatementAlphaNode((AlphaNode) n);
        } else {
            assignStatementString = getVariableAssignmentStatement(n);
        }
        Statement assignStmt = parseStatement(assignStatementString);
        SwitchEntry se = new SwitchEntry(nodeList(new IntegerLiteralExpr(n.getId())), SwitchEntry.Type.STATEMENT_GROUP, nodeList(assignStmt, new ReturnStmt(new BooleanLiteralExpr(true))));
        entries.add(se);
    }
    switchStmt.setEntries(entries);
    switchBodyStatements.addStatement(switchStmt);
    switchBodyStatements.addStatement(new ReturnStmt(new BooleanLiteralExpr(false)));
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) StaticJavaParser.parseStatement(com.github.javaparser.StaticJavaParser.parseStatement) Statement(com.github.javaparser.ast.stmt.Statement) NodeList(com.github.javaparser.ast.NodeList) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) NetworkNode(org.drools.core.common.NetworkNode) AlphaNode(org.drools.core.reteoo.AlphaNode) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 4 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class SwitchEntryT method toJavaExpression.

@Override
public Node toJavaExpression() {
    SwitchEntry entry = new SwitchEntry();
    entry.setLabels(labels);
    entry.setStatements(NodeList.nodeList(statements.stream().map(TypedExpression::toJavaExpression).map(Statement.class::cast).collect(Collectors.toList())));
    return entry;
}
Also used : SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 5 with SwitchEntry

use of com.github.javaparser.ast.stmt.SwitchEntry in project drools by kiegroup.

the class AccessibleMethod method getterSwitchStatement.

private Statement getterSwitchStatement() {
    SwitchStmt switchStmt = switchOnFieldName();
    NodeList<SwitchEntry> switchEntries = switchStmt.getEntries();
    for (DescrFieldDefinition field : fields) {
        if (!field.isOverride()) {
            switchEntries.add(getValueFromField(field));
        }
    }
    Optional<Class<?>> abstractResolvedClass = descrTypeDefinition.getAbstractResolvedClass();
    if (abstractResolvedClass.isPresent()) {
        switchEntries.addAll(superClassGetterEntries(abstractResolvedClass.get()).collect(toList()));
    } else if (descrTypeDefinition.getDeclaredAbstractClass().isPresent()) {
        switchEntries.add(switchEntry(SUPER_GET_VALUE));
    } else {
        switchEntries.add(switchEntry(RETURN_NULL));
    }
    return new BlockStmt(nodeList(switchStmt));
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Aggregations

SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)12 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)5 Expression (com.github.javaparser.ast.expr.Expression)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)4 Statement (com.github.javaparser.ast.stmt.Statement)4 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)3 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)3 NodeList (com.github.javaparser.ast.NodeList)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 StaticJavaParser (com.github.javaparser.StaticJavaParser)1 StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)1