Search in sources :

Example 6 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt 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);
            }
        }
    });
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLScorecardModelFactoryTest method setConstructor.

@Test
public void setConstructor() {
    String fullCharacteristicsClassName = PACKAGE_NAME + ".fullCharacteristicsClassName";
    final CommonCompilationDTO<Scorecard> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, basicComplexPartialScorePmml, basicComplexPartialScore, new HasClassLoaderMock());
    KiePMMLScorecardModelFactory.setConstructor(ScorecardCompilationDTO.fromCompilationDTO(source), scorecardTemplate, fullCharacteristicsClassName);
    final ConstructorDeclaration constructorDeclaration = scorecardTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, scorecardTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt retrieved = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    Statement expected = JavaParserUtils.parseStatement(String.format("super(\"%1$s\", Collections.emptyList()" + ", new %2$s" + "(), %3$s, %4$s, %5$s, %6$s);\n", getSanitizedClassName(basicComplexPartialScore.getModelName()), fullCharacteristicsClassName, basicComplexPartialScore.getInitialScore(), basicComplexPartialScore.isUseReasonCodes(), REASONCODE_ALGORITHM.class.getName() + "." + REASONCODE_ALGORITHM.byName(basicComplexPartialScore.getReasonCodeAlgorithm().value()), basicComplexPartialScore.getBaselineScore()));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Scorecard(org.dmg.pmml.scorecard.Scorecard) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) Test(org.junit.Test)

Example 8 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLModelFactoryUtils method setConstructorSuperNameInvocation.

/**
 * Set the <b>name</b> parameter on <b>super</b> invocation
 * @param generatedClassName
 * @param constructorDeclaration
 * @param name
 */
public static void setConstructorSuperNameInvocation(final String generatedClassName, final ConstructorDeclaration constructorDeclaration, final String name) {
    constructorDeclaration.setName(generatedClassName);
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "name", String.format("\"%s\"", name));
}
Also used : BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

Example 9 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLModelFactoryUtilsTest method initTest.

@Before
public void initTest() {
    CompilationUnit clonedCompilationUnit = compilationUnit.clone();
    classOrInterfaceDeclaration = clonedCompilationUnit.getClassByName(TEMPLATE_CLASS_NAME).orElseThrow(() -> new RuntimeException("Failed to retrieve ClassOrInterfaceDeclaration " + TEMPLATE_CLASS_NAME + "  from " + TEMPLATE_SOURCE)).clone();
    constructorDeclaration = classOrInterfaceDeclaration.getDefaultConstructor().orElseThrow(() -> new RuntimeException("Failed to retrieve default constructor from " + TEMPLATE_SOURCE));
    assertNotNull(constructorDeclaration);
    assertNotNull(constructorDeclaration.getBody());
    staticGetterMethod = classOrInterfaceDeclaration.getMethodsByName(GET_MODEL).get(0);
    Optional<ExplicitConstructorInvocationStmt> optSuperInvocation = CommonCodegenUtils.getExplicitConstructorInvocationStmt(constructorDeclaration.getBody());
    assertTrue(optSuperInvocation.isPresent());
    superInvocation = optSuperInvocation.get();
    // as in the original template
    assertEquals("Template", constructorDeclaration.getName().asString());
    // as in
    assertEquals("super(name, Collections.emptyList(), operator, second);", superInvocation.toString());
    // the original template
    assertTrue(clonedCompilationUnit.getClassByName(TEMPLATE_CLASS_NAME).isPresent());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) Before(org.junit.Before)

Example 10 with ExplicitConstructorInvocationStmt

use of com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt in project drools by kiegroup.

the class KiePMMLTreeModelFactory method setConstructor.

static void setConstructor(final DroolsCompilationDTO<TreeModel> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
    KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    CommonCodegenUtils.setExplicitConstructorInvocationStmtArgument(superStatement, "algorithmName", String.format("\"%s\"", compilationDTO.getModel().getAlgorithmName()));
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)

Aggregations

ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)18 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)10 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)9 NameExpr (com.github.javaparser.ast.expr.NameExpr)6 Test (org.junit.Test)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)4 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)4 Expression (com.github.javaparser.ast.expr.Expression)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)2 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)2 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)2 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)2 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)2 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 NodeList (com.github.javaparser.ast.NodeList)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)1 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)1