use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLSegmentationFactory method setConstructor.
static void setConstructor(final String generatedClassName, final String segmentationName, final ConstructorDeclaration constructorDeclaration, final MULTIPLE_MODEL_METHOD multipleModelMethod, final List<String> segmentsClasses) {
setConstructorSuperNameInvocation(generatedClassName, constructorDeclaration, segmentationName);
final BlockStmt body = constructorDeclaration.getBody();
final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "multipleModelMethod", multipleModelMethod.getClass().getCanonicalName() + "." + multipleModelMethod.name());
final List<AssignExpr> assignExprs = body.findAll(AssignExpr.class);
assignExprs.forEach(assignExpr -> {
if (assignExpr.getTarget().asNameExpr().getNameAsString().equals("segments")) {
for (String segmentClass : segmentsClasses) {
ClassOrInterfaceType kiePMMLSegmentClass = parseClassOrInterfaceType(segmentClass);
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(kiePMMLSegmentClass);
NodeList<Expression> arguments = NodeList.nodeList(objectCreationExpr);
MethodCallExpr methodCallExpr = new MethodCallExpr();
methodCallExpr.setScope(assignExpr.getTarget().asNameExpr());
methodCallExpr.setName("add");
methodCallExpr.setArguments(arguments);
ExpressionStmt expressionStmt = new ExpressionStmt();
expressionStmt.setExpression(methodCallExpr);
body.addStatement(expressionStmt);
}
}
});
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLMiningModelFactoryTest method setConstructor.
@Test
public void setConstructor() {
PMML_MODEL pmmlModel = PMML_MODEL.byName(MINING_MODEL.getClass().getSimpleName());
final ClassOrInterfaceDeclaration modelTemplate = MODEL_TEMPLATE.clone();
MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(MINING_MODEL.getMiningFunction().value());
final CommonCompilationDTO<MiningModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, MINING_MODEL, new HasClassLoaderMock());
final MiningModelCompilationDTO compilationDTO = MiningModelCompilationDTO.fromCompilationDTO(source);
KiePMMLMiningModelFactory.setConstructor(compilationDTO, modelTemplate);
Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", MINING_MODEL.getModelName())));
Map<String, Expression> assignExpressionMap = new HashMap<>();
assignExpressionMap.put("targetField", new StringLiteralExpr(targetFieldName));
assignExpressionMap.put("miningFunction", new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name()));
assignExpressionMap.put("pmmlMODEL", new NameExpr(pmmlModel.getClass().getName() + "." + pmmlModel.name()));
ClassOrInterfaceType kiePMMLSegmentationClass = parseClassOrInterfaceType(compilationDTO.getSegmentationCanonicalClassName());
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(kiePMMLSegmentationClass);
assignExpressionMap.put("segmentation", objectCreationExpr);
ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().get();
assertTrue(commonEvaluateConstructor(constructorDeclaration, getSanitizedClassName(MINING_MODEL.getModelName()), superInvocationExpressionsMap, assignExpressionMap));
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLComplexPartialScoreFactory method getComplexPartialScoreVariableDeclaration.
static BlockStmt getComplexPartialScoreVariableDeclaration(final String variableName, final ComplexPartialScore complexPartialScore) {
final MethodDeclaration methodDeclaration = COMPLEX_PARTIAL_SCORE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPLEXPARTIALSCORE).get(0).clone();
final BlockStmt complexPartialScoreBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(complexPartialScoreBody, COMPLEX_PARTIAL_SCORE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPLEX_PARTIAL_SCORE, complexPartialScoreBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, 0);
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, complexPartialScore.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPLEX_PARTIAL_SCORE, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
objectCreationExpr.getArguments().set(2, new NameExpr(nestedVariableName));
complexPartialScoreBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLNodeFactory method getKiePMMLScoreDistribution.
static ObjectCreationExpr getKiePMMLScoreDistribution(final String variableName, final ScoreDistribution scoreDistribution) {
final NodeList<Expression> scoreDistributionsArguments = new NodeList<>();
scoreDistributionsArguments.add(getExpressionForObject(variableName));
scoreDistributionsArguments.add(new NullLiteralExpr());
scoreDistributionsArguments.add(getExpressionForObject(scoreDistribution.getValue().toString()));
scoreDistributionsArguments.add(getExpressionForObject(scoreDistribution.getRecordCount().intValue()));
Expression confidenceExpression = scoreDistribution.getConfidence() != null ? getExpressionForObject(scoreDistribution.getConfidence().doubleValue()) : new NullLiteralExpr();
scoreDistributionsArguments.add(confidenceExpression);
Expression probabilityExpression = scoreDistribution.getProbability() != null ? getExpressionForObject(scoreDistribution.getProbability().doubleValue()) : new NullLiteralExpr();
scoreDistributionsArguments.add(probabilityExpression);
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLScoreDistribution.class.getCanonicalName()), scoreDistributionsArguments);
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLNodeFactoryTest method commonVerifyEvaluateNodeWithScoreDistributions.
private void commonVerifyEvaluateNodeWithScoreDistributions(final VariableDeclarator variableDeclarator, final List<ScoreDistribution> scoreDistributions) {
assertTrue(variableDeclarator.getInitializer().isPresent());
Expression expression = variableDeclarator.getInitializer().get();
assertTrue(expression instanceof MethodCallExpr);
MethodCallExpr methodCallExpr = (MethodCallExpr) expression;
assertEquals("Arrays", methodCallExpr.getScope().get().toString());
assertEquals("asList", methodCallExpr.getName().toString());
NodeList<Expression> arguments = methodCallExpr.getArguments();
assertEquals(scoreDistributions.size(), arguments.size());
arguments.forEach(argument -> assertTrue(argument instanceof ObjectCreationExpr));
List<ObjectCreationExpr> objectCreationExprs = arguments.stream().map(ObjectCreationExpr.class::cast).collect(Collectors.toList());
scoreDistributions.forEach(scoreDistribution -> {
Optional<ObjectCreationExpr> retrieved = objectCreationExprs.stream().filter(objectCreationExpr -> scoreDistribution.getValue().equals(objectCreationExpr.getArgument(2).asStringLiteralExpr().asString())).findFirst();
assertTrue(retrieved.isPresent());
Expression recordCountExpected = getExpressionForObject(scoreDistribution.getRecordCount().intValue());
Expression confidenceExpected = getExpressionForObject(scoreDistribution.getConfidence().doubleValue());
Expression probabilityExpected = scoreDistribution.getProbability() != null ? getExpressionForObject(scoreDistribution.getProbability().doubleValue()) : new NullLiteralExpr();
retrieved.ifPresent(objectCreationExpr -> {
assertEquals(recordCountExpected, objectCreationExpr.getArgument(3));
assertEquals(confidenceExpected, objectCreationExpr.getArgument(4));
assertEquals(probabilityExpected, objectCreationExpr.getArgument(5));
});
});
}
Aggregations