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);
}
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;
}
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);
}
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;
}
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());
}
Aggregations