Search in sources :

Example 1 with MethodDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition 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 MethodDefinition

use of org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition in project drools by kiegroup.

the class FEELPropertyAccessibleImplementation method fromMap.

private MethodDefinition fromMap() {
    MethodDeclaration allFeelProperties = cloneMethodTemplate("fromMap");
    BlockStmt originalStatements = allFeelProperties.getBody().orElseThrow(() -> new InvalidTemplateException("Missing body in allFeelProperties"));
    BlockStmt simplePropertyBLock = (BlockStmt) originalStatements.getStatement(0);
    BlockStmt pojoPropertyBlock = (BlockStmt) originalStatements.getStatement(1);
    BlockStmt collectionsCompositePropertyBlock = (BlockStmt) originalStatements.getStatement(2);
    BlockStmt collectionsBasic = (BlockStmt) originalStatements.getStatement(3);
    List<Statement> allStatements = fields.stream().map(f -> f.createFromMapEntry(simplePropertyBLock, pojoPropertyBlock, collectionsCompositePropertyBlock, collectionsBasic)).collect(Collectors.toList());
    BlockStmt body = new BlockStmt(nodeList(allStatements));
    if (typeDefinition instanceof AbstractDMNSetType) {
        body.addStatement("values.keySet().stream().forEach(key -> definedKeySet.add(key));");
    }
    MethodWithStringBody setFeelProperty = new MethodWithStringBody("fromMap", "void", body.toString());
    setFeelProperty.addParameter("java.util.Map<String, Object>", "values");
    addOverrideAnnotation(setFeelProperty);
    return setFeelProperty;
}
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) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody)

Aggregations

StaticJavaParser (com.github.javaparser.StaticJavaParser)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 NodeList (com.github.javaparser.ast.NodeList)2 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 CastExpr (com.github.javaparser.ast.expr.CastExpr)2 Expression (com.github.javaparser.ast.expr.Expression)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2 Statement (com.github.javaparser.ast.stmt.Statement)2 SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)2 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2