Search in sources :

Example 1 with FieldDefinition

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

the class FEELPropertyAccessibleImplementation method toResultPut.

private ExpressionStmt toResultPut(MethodCallExpr putExpression, DMNDeclaredField fieldDefinition) {
    MethodCallExpr clone = putExpression.clone();
    String fieldName = fieldDefinition.getOriginalMapKey();
    String accessorName = fieldDefinition.overriddenGetterName().orElse(getAccessorName(fieldDefinition, "get"));
    clone.findAll(StringLiteralExpr.class, se -> se.asString().equals("<PROPERTY_NAME>")).forEach(s -> s.replace(new StringLiteralExpr(fieldName)));
    clone.findAll(MethodCallExpr.class, se -> se.getNameAsString().equals("getPropertyName")).forEach(s -> s.setName(accessorName));
    return new ExpressionStmt(clone);
}
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) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with FieldDefinition

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

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

the class DescrTypeDefinition method typeFieldsSortedByPosition.

private List<TypeFieldDescr> typeFieldsSortedByPosition(List<FieldDefinition> inheritedFields) {
    Collection<TypeFieldDescr> typeFields = typeDeclarationDescr.getFields().values().stream().filter(f -> inheritedFields.stream().map(FieldDefinition::getFieldName).noneMatch(name -> name.equals(f.getFieldName()))).collect(Collectors.toList());
    TypeFieldDescr[] sortedTypes = new TypeFieldDescr[typeFields.size()];
    List<TypeFieldDescr> nonPositionalFields = new ArrayList<>();
    for (TypeFieldDescr descr : typeFields) {
        AnnotationDescr ann = descr.getAnnotation("Position");
        if (ann == null) {
            nonPositionalFields.add(descr);
        } else {
            int pos = Integer.parseInt(ann.getValue().toString());
            if (pos >= sortedTypes.length) {
                errors.add(new TypeDeclarationError(typeDeclarationDescr, "Out of range position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
            } else if (sortedTypes[pos] != null) {
                errors.add(new TypeDeclarationError(typeDeclarationDescr, "Duplicated position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
            } else {
                sortedTypes[pos] = descr;
            }
        }
    }
    if (!errors.isEmpty()) {
        return Collections.emptyList();
    }
    int counter = 0;
    for (TypeFieldDescr descr : nonPositionalFields) {
        for (; sortedTypes[counter] != null; counter++) ;
        sortedTypes[counter++] = descr;
    }
    return Arrays.asList(sortedTypes);
}
Also used : QualifiedName(org.drools.drl.ast.descr.QualifiedName) Optional.empty(java.util.Optional.empty) Arrays(java.util.Arrays) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) Optional.of(java.util.Optional.of) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) StreamUtils.optionalToStream(org.drools.modelcompiler.util.StreamUtils.optionalToStream) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) Collectors(java.util.stream.Collectors) AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DroolsError(org.drools.drl.parser.DroolsError) Optional(java.util.Optional) Collections(java.util.Collections) POJOGenerator.quote(org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator.quote) TypeResolver(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeResolver) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 4 with FieldDefinition

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

the class DescrTypeDefinition method processFields.

private List<DescrFieldDefinition> processFields() {
    List<FieldDefinition> inheritedFields = findInheritedDeclaredFields();
    List<TypeFieldDescr> sortedTypeFields = typeFieldsSortedByPosition(inheritedFields);
    int position = inheritedFields.size();
    List<DescrFieldDefinition> allFields = new ArrayList<>();
    for (TypeFieldDescr typeFieldDescr : sortedTypeFields) {
        ProcessedTypeField processedTypeField = processTypeField(position, typeFieldDescr);
        allFields.add(processedTypeField.fieldDefinition);
        position = processedTypeField.position;
    }
    if (typeDeclarationDescr.getFields().size() != sortedTypeFields.size()) {
        typeDeclarationDescr.getFields().values().stream().filter(f -> inheritedFields.stream().map(FieldDefinition::getFieldName).anyMatch(name -> name.equals(f.getFieldName()))).map(DescrFieldDefinition::new).map(d -> d.setOverride(true)).forEach(allFields::add);
    }
    return allFields;
}
Also used : QualifiedName(org.drools.drl.ast.descr.QualifiedName) Optional.empty(java.util.Optional.empty) Arrays(java.util.Arrays) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) Optional.of(java.util.Optional.of) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) ArrayList(java.util.ArrayList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) StreamUtils.optionalToStream(org.drools.modelcompiler.util.StreamUtils.optionalToStream) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) AnnotationDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.AnnotationDefinition) Collectors(java.util.stream.Collectors) AnnotationDeclarationError(org.drools.compiler.compiler.AnnotationDeclarationError) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) DroolsError(org.drools.drl.parser.DroolsError) Optional(java.util.Optional) Collections(java.util.Collections) POJOGenerator.quote(org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator.quote) TypeResolver(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeResolver) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) ArrayList(java.util.ArrayList)

Example 5 with FieldDefinition

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

the class EnumGenerator method createConstructor.

private void createConstructor(EnumDeclarationDescr enumDeclarationDescr) {
    List<FieldDefinition> enumFields = enumDeclarationDescr.getFields().values().stream().map(DescrFieldDefinition::new).collect(Collectors.toList());
    GeneratedConstructor fullArgumentConstructor = GeneratedConstructor.factoryEnum(enumDeclaration, enumFields);
    fullArgumentConstructor.generateConstructor(Collections.emptyList(), Collections.emptyList());
}
Also used : GeneratedConstructor(org.drools.modelcompiler.builder.generator.declaredtype.generator.GeneratedConstructor) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition)

Aggregations

FieldDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition)8 List (java.util.List)5 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)4 Statement (com.github.javaparser.ast.stmt.Statement)4 ArrayList (java.util.ArrayList)4 Collectors (java.util.stream.Collectors)4 MethodDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition)4 TypeDefinition (org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition)4 NodeList (com.github.javaparser.ast.NodeList)3 NodeList.nodeList (com.github.javaparser.ast.NodeList.nodeList)3 Collection (java.util.Collection)3 Optional (java.util.Optional)3 StaticJavaParser (com.github.javaparser.StaticJavaParser)2 StaticJavaParser.parseStatement (com.github.javaparser.StaticJavaParser.parseStatement)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 Modifier (com.github.javaparser.ast.Modifier)2 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2