Search in sources :

Example 1 with TypeDefinition

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

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

the class DMNTypeSafeTypeGenerator method generateSourceCodeOfAllTypes.

public Map<String, String> generateSourceCodeOfAllTypes() {
    Map<String, String> allSources = new HashMap<>();
    DMNTypeSafePackageName packageDeclaration = this.packageName.create(dmnModel);
    for (TypeDefinition typeDefinition : types) {
        ClassOrInterfaceDeclaration generatedClass = new GeneratedClassDeclaration(typeDefinition, Collections.emptyList()).toClassDeclaration();
        CompilationUnit cu = new CompilationUnit(packageDeclaration.packageName());
        cu.getPackageDeclaration().ifPresent(pd -> pd.setBlockComment(this.disclaimerMarker));
        cu.addType(generatedClass);
        LOG.debug("\n{}", cu);
        allSources.put(packageDeclaration.appendPackage(typeDefinition.getTypeName()), cu.toString());
    }
    return allSources;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) GeneratedClassDeclaration(org.drools.modelcompiler.builder.generator.declaredtype.generator.GeneratedClassDeclaration) HashMap(java.util.HashMap) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition)

Example 3 with TypeDefinition

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

CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 TypeDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition)3 StaticJavaParser (com.github.javaparser.StaticJavaParser)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