use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLRowFactory method getRowVariableDeclaration.
static BlockStmt getRowVariableDeclaration(final String variableName, final Row row) {
final MethodDeclaration methodDeclaration = ROW_TEMPLATE.getMethodsByName(GETKIEPMMLROW).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final String columnValuesVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, COLUMN_VALUES);
final VariableDeclarator columnValuesVariableDeclarator = getVariableDeclarator(toReturn, COLUMN_VALUES).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ROW, toReturn)));
columnValuesVariableDeclarator.setName(columnValuesVariableName);
final MethodCallExpr columnValuesVariableInit = columnValuesVariableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asMethodCallExpr();
final MethodCallExpr columnValuesVariableScope = columnValuesVariableInit.getScope().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asMethodCallExpr();
final ArrayCreationExpr columnValuesVariableArray = columnValuesVariableScope.getArguments().get(0).asArrayCreationExpr();
final ArrayInitializerExpr columnValuesVariableArrayInit = columnValuesVariableArray.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asArrayInitializerExpr();
Map<String, Object> rowDataMap = getRowDataMap(row);
NodeList<Expression> arguments = new NodeList<>();
rowDataMap.entrySet().forEach(entry -> {
ArrayInitializerExpr argument = new ArrayInitializerExpr();
NodeList<Expression> values = NodeList.nodeList(new StringLiteralExpr(entry.getKey()), getExpressionForObject(entry.getValue()));
argument.setValues(values);
arguments.add(argument);
});
columnValuesVariableArrayInit.setValues(arguments);
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, ROW).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ROW, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, ROW, toReturn))).asObjectCreationExpr();
final NameExpr nameExpr = new NameExpr(columnValuesVariableName);
objectCreationExpr.getArguments().set(0, nameExpr);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLInlineTableFactory method getInlineTableVariableDeclaration.
static BlockStmt getInlineTableVariableDeclaration(final String variableName, final InlineTable inlineTable) {
final MethodDeclaration methodDeclaration = INLINETABLE_TEMPLATE.getMethodsByName(GETKIEPMMLINLINETABLE).get(0).clone();
final BlockStmt inlineTableBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(inlineTableBody, INLINETABLE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, INLINETABLE, inlineTableBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (Row row : inlineTable.getRows()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getRowVariableDeclaration(nestedVariableName, row);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, INLINETABLE, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
inlineTableBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.NodeList 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;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class CommonCodegenUtilsTest method populateMethodDeclarations.
@Test
public void populateMethodDeclarations() {
final List<MethodDeclaration> toAdd = IntStream.range(0, 5).boxed().map(index -> getMethodDeclaration("METHOD_" + index)).collect(Collectors.toList());
final ClassOrInterfaceDeclaration toPopulate = new ClassOrInterfaceDeclaration();
assertTrue(toPopulate.getMembers().isEmpty());
CommonCodegenUtils.populateMethodDeclarations(toPopulate, toAdd);
final NodeList<BodyDeclaration<?>> retrieved = toPopulate.getMembers();
assertEquals(toAdd.size(), retrieved.size());
assertTrue(toAdd.stream().anyMatch(methodDeclaration -> retrieved.stream().anyMatch(bodyDeclaration -> bodyDeclaration.equals(methodDeclaration))));
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class PMMLRuleMappersFactory method getPMMLRuleMappersSource.
public static String getPMMLRuleMappersSource(final String packageName, final List<String> generatedRuleMappers) {
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPERS_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPERS_CLASS_NAME);
ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0);
FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("pmmlRuleMappers").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA + " has been modified."));
final List<Expression> nodeList = generatedRuleMappers.stream().map(generatedRuleMapper -> {
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(generatedRuleMapper);
return objectCreationExpr;
}).collect(Collectors.toList());
NodeList<Expression> expressions = NodeList.nodeList(nodeList);
MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions);
ruleNameField.getVariables().get(0).setInitializer(methodCallExpr);
return cloneCU.toString();
}
Aggregations