Search in sources :

Example 1 with MethodWithStringBody

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

the class FEELPropertyAccessibleImplementation method processCompositeCollection.

private MethodDefinition processCompositeCollection() {
    MethodDeclaration processCompositeCollection = cloneMethodTemplate("processCompositeCollection");
    BlockStmt body = processCompositeCollection.getBody().orElseThrow(() -> new InvalidTemplateException("Missing body in generated method"));
    MethodWithStringBody processCompositeCollectionDefinition = new MethodWithStringBody("processCompositeCollection", "void", body.toString());
    processCompositeCollectionDefinition.addParameter("java.util.Collection", "destCol");
    processCompositeCollectionDefinition.addParameter("java.util.Collection", "srcCol");
    processCompositeCollectionDefinition.addParameter("Class<?>", "baseClass");
    return processCompositeCollectionDefinition;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody)

Example 2 with MethodWithStringBody

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

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

the class FEELPropertyAccessibleImplementation method allFeelProperties.

private MethodWithStringBody allFeelProperties() {
    MethodDeclaration allFeelProperties = cloneMethodTemplate("allFEELProperties");
    MethodCallExpr putExpression = allFeelProperties.findFirst(MethodCallExpr.class, mc -> mc.getNameAsString().equals("put")).orElseThrow(() -> new InvalidTemplateException("Missing put method in allFEELProperties"));
    List<Statement> collect = fields.stream().map(fieldDefinition -> toResultPut(putExpression, fieldDefinition)).collect(Collectors.toList());
    if (typeDefinition instanceof AbstractDMNSetType) {
        collect.add(new ExpressionStmt(StaticJavaParser.parseExpression("result.entrySet().removeIf(entry -> java.util.Objects.isNull(entry.getValue()) && !definedKeySet.contains(entry.getKey()))")));
    }
    BlockStmt newBlockStatement = new BlockStmt(nodeList(collect));
    putExpression.getParentNode().ifPresent(p -> p.replace(newBlockStatement));
    BlockStmt body = allFeelProperties.getBody().orElseThrow(() -> new InvalidTemplateException("Missing body in generated method"));
    MethodWithStringBody allFEELProperties = new MethodWithStringBody("allFEELProperties", "java.util.Map<String, Object>", body.toString());
    addOverrideAnnotation(allFEELProperties);
    return allFEELProperties;
}
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) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 4 with MethodWithStringBody

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

Example 5 with MethodWithStringBody

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

the class FEELPropertyAccessibleImplementation method setFeelPropertyDefinition.

private MethodDefinition setFeelPropertyDefinition() {
    MethodDeclaration setFEELProperty = cloneMethodTemplate("setFEELProperty");
    SwitchStmt firstSwitch = setFEELProperty.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException("Missing switch statement in setFEELProperty"));
    firstSwitch.setComment(null);
    List<SwitchEntry> collect = fields.stream().map(this::toSetPropertySwitchEntry).collect(Collectors.toList());
    firstSwitch.setEntries(nodeList(collect));
    BlockStmt body = setFEELProperty.getBody().orElseThrow(() -> new InvalidTemplateException("Empty body in setFEELProperty"));
    if (typeDefinition instanceof AbstractDMNSetType) {
        body.addStatement(0, new ExpressionStmt(StaticJavaParser.parseExpression("definedKeySet.add(property)")));
    }
    MethodWithStringBody setFeelPropertyDefinition = new MethodWithStringBody("setFEELProperty", "void", body.toString()).addParameter(String.class.getCanonicalName(), "property").addParameter(Object.class.getCanonicalName(), "value");
    addOverrideAnnotation(setFeelPropertyDefinition);
    return setFeelPropertyDefinition;
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt)

Aggregations

MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)5 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)5 MethodWithStringBody (org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody)5 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)4 SwitchEntry (com.github.javaparser.ast.stmt.SwitchEntry)4 SwitchStmt (com.github.javaparser.ast.stmt.SwitchStmt)4 StaticJavaParser (com.github.javaparser.StaticJavaParser)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 NodeList (com.github.javaparser.ast.NodeList)3 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 Expression (com.github.javaparser.ast.expr.Expression)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 NameExpr (com.github.javaparser.ast.expr.NameExpr)3 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)3 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 Statement (com.github.javaparser.ast.stmt.Statement)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3