use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method commonVerifyOutputFieldsObjectCreation.
private void commonVerifyOutputFieldsObjectCreation(List<Expression> toVerify, List<OutputField> outputFields) {
toVerify.forEach(argument -> {
assertTrue(argument instanceof ObjectCreationExpr);
ObjectCreationExpr objCrt = (ObjectCreationExpr) argument;
assertEquals(OutputField.class.getCanonicalName(), objCrt.getType().asString());
Optional<OutputField> outputFieldOpt = outputFields.stream().filter(outputField -> outputField.getName().equals(objCrt.getArgument(0).asStringLiteralExpr().asString())).findFirst();
assertTrue(outputFieldOpt.isPresent());
OutputField outputField = outputFieldOpt.get();
String expected = OP_TYPE.class.getCanonicalName() + "." + outputField.getOpType();
assertEquals(expected, objCrt.getArgument(1).asNameExpr().toString());
expected = DATA_TYPE.class.getCanonicalName() + "." + outputField.getDataType();
assertEquals(expected, objCrt.getArgument(2).asNameExpr().toString());
expected = outputField.getTargetField();
assertEquals(expected, objCrt.getArgument(3).asStringLiteralExpr().asString());
expected = RESULT_FEATURE.class.getCanonicalName() + "." + outputField.getResultFeature();
assertEquals(expected, objCrt.getArgument(4).asNameExpr().toString());
MethodCallExpr allowedValuesMethod = objCrt.getArgument(5).asMethodCallExpr();
IntStream.range(0, 3).forEach(i -> {
String exp = outputField.getAllowedValues().get(i);
assertEquals(exp, allowedValuesMethod.getArgument(i).asStringLiteralExpr().asString());
});
});
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLFactoryFactory method getInstantiationExpression.
public static Expression getInstantiationExpression(String kiePMMLModelClass, boolean isInterpreted) {
ClassOrInterfaceType classOrInterfaceType = parseClassOrInterfaceType(kiePMMLModelClass);
if (isInterpreted) {
MethodCallExpr methodCallExpr = new MethodCallExpr();
methodCallExpr.setScope(new NameExpr(kiePMMLModelClass));
methodCallExpr.setName(GET_MODEL);
return methodCallExpr;
} else {
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(classOrInterfaceType);
return objectCreationExpr;
}
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class CommonCodegenUtilsTest method addListPopulation.
@Test
public void addListPopulation() {
final List<ObjectCreationExpr> toAdd = IntStream.range(0, 5).mapToObj(i -> {
ObjectCreationExpr toReturn = new ObjectCreationExpr();
toReturn.setType(String.class);
Expression value = new StringLiteralExpr("String" + i);
toReturn.setArguments(NodeList.nodeList(value));
return toReturn;
}).collect(Collectors.toList());
BlockStmt body = new BlockStmt();
String listName = "LIST_NAME";
CommonCodegenUtils.addListPopulationByObjectCreationExpr(toAdd, body, listName);
NodeList<Statement> statements = body.getStatements();
assertEquals(toAdd.size(), statements.size());
for (Statement statement : statements) {
assertTrue(statement instanceof ExpressionStmt);
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
assertTrue(expressionStmt.getExpression() instanceof MethodCallExpr);
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
assertEquals(listName, methodCallExpr.getScope().get().asNameExpr().getNameAsString());
NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments();
assertEquals(1, arguments.size());
assertTrue(arguments.get(0) instanceof ObjectCreationExpr);
ObjectCreationExpr objectCreationExpr = (ObjectCreationExpr) arguments.get(0);
assertEquals(objectCreationExpr.getType().asString(), String.class.getSimpleName());
arguments = objectCreationExpr.getArguments();
assertEquals(1, arguments.size());
assertTrue(arguments.get(0) instanceof StringLiteralExpr);
}
for (ObjectCreationExpr entry : toAdd) {
int matchingDeclarations = (int) statements.stream().filter(statement -> {
ExpressionStmt expressionStmt = (ExpressionStmt) statement;
com.github.javaparser.ast.expr.Expression expression = expressionStmt.getExpression();
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
final NodeList<com.github.javaparser.ast.expr.Expression> arguments = methodCallExpr.getArguments();
return entry.equals(arguments.get(0).asObjectCreationExpr());
}).count();
assertEquals(1, matchingDeclarations);
}
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class TargetFieldFactoryTest method getTargetFieldVariableInitializer.
@Test
public void getTargetFieldVariableInitializer() throws IOException {
TargetField kieTargetField = ModelUtils.convertToKieTargetField(getRandomTarget());
ObjectCreationExpr retrieved = TargetFieldFactory.getTargetFieldVariableInitializer(kieTargetField);
String text = getFileContent(TEST_01_SOURCE);
List<TargetValue> kieTargetValues = kieTargetField.getTargetValues();
String opType = OP_TYPE.class.getCanonicalName() + "." + kieTargetField.getOpType().toString();
String castInteger = CAST_INTEGER.class.getCanonicalName() + "." + kieTargetField.getCastInteger().toString();
Expression expected = JavaParserUtils.parseExpression(String.format(text, kieTargetValues.get(0).getValue(), kieTargetValues.get(0).getDisplayValue(), kieTargetValues.get(0).getPriorProbability(), kieTargetValues.get(0).getDefaultValue(), kieTargetValues.get(1).getValue(), kieTargetValues.get(1).getDisplayValue(), kieTargetValues.get(1).getPriorProbability(), kieTargetValues.get(1).getDefaultValue(), kieTargetValues.get(2).getValue(), kieTargetValues.get(2).getDisplayValue(), kieTargetValues.get(2).getPriorProbability(), kieTargetValues.get(2).getDefaultValue(), opType, kieTargetField.getField(), castInteger, kieTargetField.getMin(), kieTargetField.getMax(), kieTargetField.getRescaleConstant(), kieTargetField.getRescaleFactor()));
assertEquals(expected.toString(), retrieved.toString());
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(Arrays.class, Collections.class, KiePMMLTarget.class, KiePMMLTargetValue.class, TargetField.class, TargetValue.class);
commonValidateCompilationWithImports(retrieved, imports);
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class PMMLRuleMapperFactory method getPMMLRuleMapperSource.
public static String getPMMLRuleMapperSource(final String fullRuleName) {
final String packageName = fullRuleName.contains(".") ? fullRuleName.substring(0, fullRuleName.lastIndexOf('.')) : "";
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPER_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPER_CLASS_NAME);
ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0);
FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("model").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPER_TEMPLATE_JAVA + " has been modified."));
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(fullRuleName);
ruleNameField.getVariables().get(0).setInitializer(objectCreationExpr);
return cloneCU.toString();
}
Aggregations