Search in sources :

Example 56 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class PackageModel method generateRulesInUnit.

private void generateRulesInUnit(String ruleUnitName, BlockStmt rulesListInitializerBody, RuleSourceResult results, ClassOrInterfaceDeclaration rulesClass) {
    results.withModel(name + "." + ruleUnitName, name + "." + rulesClass.getNameAsString());
    Collection<MethodDeclaration> ruleMethodsInUnit = ofNullable(ruleMethods.get(ruleUnitName)).map(Map::values).orElse(null);
    if (ruleMethodsInUnit == null || ruleMethodsInUnit.isEmpty()) {
        BodyDeclaration<?> getQueriesMethod = parseBodyDeclaration("    @Override\n" + "    public java.util.List<org.drools.model.Rule> getRules() {\n" + "        return java.util.Collections.emptyList();\n" + "    }\n");
        rulesClass.addMember(getQueriesMethod);
        return;
    }
    if (!ruleUnitName.equals(DEFAULT_RULE_UNIT)) {
        BodyDeclaration<?> modelNameMethod = parseBodyDeclaration("    @Override\n" + "    public String getName() {\n" + "        return super.getName() + \"." + ruleUnitName + "\";\n" + "    }\n");
        rulesClass.addMember(modelNameMethod);
        BodyDeclaration<?> modelPackageNameMethod = parseBodyDeclaration("    @Override\n" + "    public String getPackageName() {\n" + "        return super.getName();\n" + "    }\n");
        rulesClass.addMember(modelPackageNameMethod);
    }
    createAndAddGetRulesMethod(rulesClass);
    int ruleCount = ruleMethodsInUnit.size();
    boolean requiresMultipleRulesLists = ruleCount >= RULES_DECLARATION_PER_CLASS - 1;
    boolean parallelRulesLoad = ruleCount >= (RULES_DECLARATION_PER_CLASS * 3 - 1);
    MethodCallExpr parallelRulesGetter = null;
    MethodCallExpr rules = buildRulesField(rulesClass);
    if (requiresMultipleRulesLists) {
        rulesClass.addImplementedType(RulesSupplier.class);
        if (parallelRulesLoad) {
            parallelRulesGetter = new MethodCallExpr(new NameExpr(RulesSupplier.class.getCanonicalName()), "getRules");
            parallelRulesGetter.addArgument(new ThisExpr());
            rulesListInitializerBody.addStatement(new AssignExpr(new NameExpr("this.rules"), parallelRulesGetter, AssignExpr.Operator.ASSIGN));
        } else {
            MethodCallExpr add = new MethodCallExpr(new NameExpr("rules"), "addAll");
            add.addArgument("getRulesList()");
            rulesListInitializerBody.addStatement(add);
        }
    }
    ruleMethodsInUnit.parallelStream().forEach(DrlxParseUtil::transformDrlNameExprToNameExpr);
    int maxLength = ruleMethodsInUnit.parallelStream().map(MethodDeclaration::toString).mapToInt(String::length).max().orElse(1);
    int rulesPerClass = oneClassPerRule ? 1 : Math.max(50000 / maxLength, 1);
    // each method per Drlx parser result
    int count = -1;
    Map<Integer, ClassOrInterfaceDeclaration> splitted = new LinkedHashMap<>();
    for (MethodDeclaration ruleMethod : ruleMethodsInUnit) {
        String methodName = ruleMethod.getNameAsString();
        ClassOrInterfaceDeclaration rulesMethodClass = splitted.computeIfAbsent(++count / rulesPerClass, i -> {
            String className = rulesClass.getNameAsString() + (oneClassPerRule ? "_" + methodName : "RuleMethods" + i);
            return createClass(className, results);
        });
        rulesMethodClass.addMember(ruleMethod);
        if (count % RULES_DECLARATION_PER_CLASS == RULES_DECLARATION_PER_CLASS - 1) {
            int index = count / RULES_DECLARATION_PER_CLASS;
            rules = buildRulesField(results, index);
            ObjectCreationExpr newObject = new ObjectCreationExpr(null, toClassOrInterfaceType(rulesFileName + "Rules" + index), NodeList.nodeList());
            if (parallelRulesLoad) {
                parallelRulesGetter.addArgument(newObject);
            } else {
                MethodCallExpr add = new MethodCallExpr(new NameExpr("rules"), "addAll");
                add.addArgument(new MethodCallExpr(newObject, "getRulesList"));
                rulesListInitializerBody.addStatement(add);
            }
        }
        // manage in main class init block:
        rules.addArgument(new MethodCallExpr(new NameExpr(rulesMethodClass.getNameAsString()), methodName));
    }
    BodyDeclaration<?> rulesList = requiresMultipleRulesLists ? parseBodyDeclaration("java.util.List<org.drools.model.Rule> rules = new java.util.ArrayList<>(" + ruleCount + ");") : parseBodyDeclaration("java.util.List<org.drools.model.Rule> rules = getRulesList();");
    rulesClass.addMember(rulesList);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) LinkedHashMap(java.util.LinkedHashMap) RulesSupplier(org.drools.model.RulesSupplier) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 57 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class KiePMMLModelFactoryUtilsTest method createIntervalsExpression.

@Test
public void createIntervalsExpression() {
    List<Interval> intervals = IntStream.range(0, 3).mapToObj(i -> {
        int leftMargin = new Random().nextInt(40);
        int rightMargin = leftMargin + 13;
        return new Interval(leftMargin, rightMargin);
    }).collect(Collectors.toList());
    Expression retrieved = KiePMMLModelFactoryUtils.createIntervalsExpression(intervals);
    assertNotNull(retrieved);
    assertTrue(retrieved instanceof MethodCallExpr);
    MethodCallExpr mtdExp = (MethodCallExpr) retrieved;
    String expected = "java.util.Arrays";
    assertEquals(expected, mtdExp.getScope().get().asNameExpr().toString());
    expected = "asList";
    assertEquals(expected, mtdExp.getName().asString());
    NodeList<Expression> arguments = mtdExp.getArguments();
    assertEquals(intervals.size(), arguments.size());
    arguments.forEach(argument -> {
        assertTrue(argument instanceof ObjectCreationExpr);
        ObjectCreationExpr objCrt = (ObjectCreationExpr) argument;
        assertEquals(Interval.class.getCanonicalName(), objCrt.getType().asString());
        Optional<Interval> intervalOpt = intervals.stream().filter(interval -> String.valueOf(interval.getLeftMargin()).equals(objCrt.getArgument(0).asNameExpr().toString()) && String.valueOf(interval.getRightMargin()).equals(objCrt.getArgument(1).asNameExpr().toString())).findFirst();
        assertTrue(intervalOpt.isPresent());
    });
}
Also used : RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) Random(java.util.Random) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) GET_CREATED_MININGFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_MININGFIELDS) KiePMMLUtil(org.kie.pmml.compiler.commons.utils.KiePMMLUtil) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) GET_CREATED_LOCAL_TRANSFORMATIONS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_LOCAL_TRANSFORMATIONS) GET_CREATED_TRANSFORMATION_DICTIONARY(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_TRANSFORMATION_DICTIONARY) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) GET_CREATED_OUTPUTFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_OUTPUTFIELDS) GET_CREATED_KIEPMMLOUTPUTFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLOUTPUTFIELDS) Expression(com.github.javaparser.ast.expr.Expression) Assert.fail(org.junit.Assert.fail) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Interval(org.kie.pmml.api.models.Interval) ModelUtils(org.kie.pmml.compiler.api.utils.ModelUtils) NodeList(com.github.javaparser.ast.NodeList) PMML(org.dmg.pmml.PMML) CommonCodegenUtils(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils) KiePMMLTargetValue(org.kie.pmml.commons.model.KiePMMLTargetValue) GET_CREATED_KIEPMMLTARGETS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLTARGETS) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TreeModel(org.dmg.pmml.tree.TreeModel) Collectors(java.util.stream.Collectors) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) List(java.util.List) Stream(java.util.stream.Stream) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Spliterator(java.util.Spliterator) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IntStream(java.util.stream.IntStream) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BeforeClass(org.junit.BeforeClass) OutputField(org.kie.pmml.api.models.OutputField) JavaParserUtils.getFromFileName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromFileName) TargetField(org.kie.pmml.api.models.TargetField) MiningField(org.kie.pmml.api.models.MiningField) CAST_INTEGER(org.kie.pmml.api.enums.CAST_INTEGER) StreamSupport(java.util.stream.StreamSupport) GET_CREATED_KIEPMMLMININGFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLMININGFIELDS) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) Before(org.junit.Before) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) CommonCodegenUtils.getChainedMethodCallExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getChainedMethodCallExprFrom) Assert.assertNotNull(org.junit.Assert.assertNotNull) PMMLModelTestUtils.getRandomOpType(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOpType) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) NameExpr(com.github.javaparser.ast.expr.NameExpr) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Statement(com.github.javaparser.ast.stmt.Statement) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CodegenTestUtils.commonValidateCompilationWithImports(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilationWithImports) PMMLModelTestUtils.getRandomCastInteger(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomCastInteger) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) GET_MODEL(org.kie.pmml.commons.Constants.GET_MODEL) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TargetValue(org.kie.pmml.api.models.TargetValue) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Random(java.util.Random) Expression(com.github.javaparser.ast.expr.Expression) Interval(org.kie.pmml.api.models.Interval) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 58 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class KiePMMLModelFactoryUtilsTest method getObjectCreationExprFromInterval.

@Test
public void getObjectCreationExprFromInterval() {
    Interval interval = new Interval(null, -14);
    ObjectCreationExpr retrieved = KiePMMLModelFactoryUtils.getObjectCreationExprFromInterval(interval);
    assertNotNull(retrieved);
    assertEquals(Interval.class.getCanonicalName(), retrieved.getType().asString());
    NodeList<Expression> arguments = retrieved.getArguments();
    assertEquals(2, arguments.size());
    assertTrue(arguments.get(0) instanceof NullLiteralExpr);
    assertEquals(String.valueOf(interval.getRightMargin()), arguments.get(1).asNameExpr().toString());
    interval = new Interval(-13, 10);
    retrieved = KiePMMLModelFactoryUtils.getObjectCreationExprFromInterval(interval);
    assertNotNull(retrieved);
    assertEquals(Interval.class.getCanonicalName(), retrieved.getType().asString());
    arguments = retrieved.getArguments();
    assertEquals(2, arguments.size());
    assertEquals(String.valueOf(interval.getLeftMargin()), arguments.get(0).asNameExpr().toString());
    assertEquals(String.valueOf(interval.getRightMargin()), arguments.get(1).asNameExpr().toString());
    interval = new Interval(-13, null);
    retrieved = KiePMMLModelFactoryUtils.getObjectCreationExprFromInterval(interval);
    assertNotNull(retrieved);
    assertEquals(Interval.class.getCanonicalName(), retrieved.getType().asString());
    arguments = retrieved.getArguments();
    assertEquals(2, arguments.size());
    assertEquals(String.valueOf(interval.getLeftMargin()), arguments.get(0).asNameExpr().toString());
    assertTrue(arguments.get(1) instanceof NullLiteralExpr);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) Interval(org.kie.pmml.api.models.Interval) Test(org.junit.Test)

Example 59 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class KiePMMLModelFactoryUtilsTest method commonVerifyMiningFieldsObjectCreation.

private void commonVerifyMiningFieldsObjectCreation(List<Expression> toVerify, List<MiningField> miningFields) {
    toVerify.forEach(expression -> {
        assertTrue(expression instanceof ObjectCreationExpr);
        ObjectCreationExpr objCrt = (ObjectCreationExpr) expression;
        assertEquals(MiningField.class.getCanonicalName(), objCrt.getType().asString());
        Optional<MiningField> miningFieldOpt = miningFields.stream().filter(miningField -> miningField.getName().equals(objCrt.getArgument(0).asStringLiteralExpr().asString())).findFirst();
        assertTrue(miningFieldOpt.isPresent());
        MiningField miningField = miningFieldOpt.get();
        assertEquals(MiningField.class.getCanonicalName(), objCrt.getType().asString());
        String expected = miningField.getUsageType() != null ? FIELD_USAGE_TYPE.class.getCanonicalName() + "." + miningField.getUsageType() : "null";
        assertEquals(expected, objCrt.getArgument(1).toString());
        expected = miningField.getOpType() != null ? OP_TYPE.class.getCanonicalName() + "." + miningField.getOpType() : "null";
        assertEquals(expected, objCrt.getArgument(2).toString());
        expected = miningField.getDataType() != null ? DATA_TYPE.class.getCanonicalName() + "." + miningField.getDataType() : "null";
        assertEquals(expected, objCrt.getArgument(3).toString());
        expected = miningField.getMissingValueTreatmentMethod() != null ? MISSING_VALUE_TREATMENT_METHOD.class.getCanonicalName() + "." + miningField.getMissingValueTreatmentMethod() : "null";
        assertEquals(expected, objCrt.getArgument(4).toString());
        expected = miningField.getInvalidValueTreatmentMethod() != null ? INVALID_VALUE_TREATMENT_METHOD.class.getCanonicalName() + "." + miningField.getInvalidValueTreatmentMethod() : "null";
        assertEquals(expected, objCrt.getArgument(5).toString());
        expected = miningField.getMissingValueReplacement() != null ? miningField.getMissingValueReplacement() : "null";
        assertEquals(expected, objCrt.getArgument(6).asStringLiteralExpr().asString());
        expected = miningField.getInvalidValueReplacement() != null ? miningField.getInvalidValueReplacement() : "null";
        assertEquals(expected, objCrt.getArgument(7).asStringLiteralExpr().asString());
        MethodCallExpr allowedValuesMethod = objCrt.getArgument(8).asMethodCallExpr();
        IntStream.range(0, 3).forEach(i -> {
            String exp = miningField.getAllowedValues().get(i);
            assertEquals(exp, allowedValuesMethod.getArgument(i).asStringLiteralExpr().asString());
        });
        MethodCallExpr intervalsMethod = objCrt.getArgument(9).asMethodCallExpr();
        IntStream.range(0, 3).forEach(i -> {
            Interval interval = miningField.getIntervals().get(i);
            ObjectCreationExpr objectCreationExpr = intervalsMethod.getArgument(i).asObjectCreationExpr();
            String exp = interval.getLeftMargin().toString();
            assertEquals(exp, objectCreationExpr.getArgument(0).asNameExpr().toString());
            exp = interval.getRightMargin().toString();
            assertEquals(exp, objectCreationExpr.getArgument(1).asNameExpr().toString());
        });
    });
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) Random(java.util.Random) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) GET_CREATED_MININGFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_MININGFIELDS) KiePMMLUtil(org.kie.pmml.compiler.commons.utils.KiePMMLUtil) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) GET_CREATED_LOCAL_TRANSFORMATIONS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_LOCAL_TRANSFORMATIONS) GET_CREATED_TRANSFORMATION_DICTIONARY(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_TRANSFORMATION_DICTIONARY) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) GET_CREATED_OUTPUTFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_OUTPUTFIELDS) GET_CREATED_KIEPMMLOUTPUTFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLOUTPUTFIELDS) Expression(com.github.javaparser.ast.expr.Expression) Assert.fail(org.junit.Assert.fail) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Interval(org.kie.pmml.api.models.Interval) ModelUtils(org.kie.pmml.compiler.api.utils.ModelUtils) NodeList(com.github.javaparser.ast.NodeList) PMML(org.dmg.pmml.PMML) CommonCodegenUtils(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils) KiePMMLTargetValue(org.kie.pmml.commons.model.KiePMMLTargetValue) GET_CREATED_KIEPMMLTARGETS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLTARGETS) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TreeModel(org.dmg.pmml.tree.TreeModel) Collectors(java.util.stream.Collectors) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) List(java.util.List) Stream(java.util.stream.Stream) CommonCompilationDTO(org.kie.pmml.compiler.api.dto.CommonCompilationDTO) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Spliterator(java.util.Spliterator) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IntStream(java.util.stream.IntStream) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) BeforeClass(org.junit.BeforeClass) OutputField(org.kie.pmml.api.models.OutputField) JavaParserUtils.getFromFileName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromFileName) TargetField(org.kie.pmml.api.models.TargetField) MiningField(org.kie.pmml.api.models.MiningField) CAST_INTEGER(org.kie.pmml.api.enums.CAST_INTEGER) StreamSupport(java.util.stream.StreamSupport) GET_CREATED_KIEPMMLMININGFIELDS(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLModelFactoryUtils.GET_CREATED_KIEPMMLMININGFIELDS) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) Before(org.junit.Before) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) CommonCodegenUtils.getChainedMethodCallExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getChainedMethodCallExprFrom) Assert.assertNotNull(org.junit.Assert.assertNotNull) PMMLModelTestUtils.getRandomOpType(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOpType) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) NameExpr(com.github.javaparser.ast.expr.NameExpr) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Statement(com.github.javaparser.ast.stmt.Statement) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) PMMLModelTestUtils.getRandomTarget(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTarget) KiePMMLTarget(org.kie.pmml.commons.model.KiePMMLTarget) FileUtils.getFileInputStream(org.kie.test.util.filesystem.FileUtils.getFileInputStream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) CodegenTestUtils.commonValidateCompilationWithImports(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilationWithImports) PMMLModelTestUtils.getRandomCastInteger(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomCastInteger) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) GET_MODEL(org.kie.pmml.commons.Constants.GET_MODEL) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TargetValue(org.kie.pmml.api.models.TargetValue) MiningField(org.kie.pmml.api.models.MiningField) PMMLModelTestUtils.getRandomMiningField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Interval(org.kie.pmml.api.models.Interval)

Example 60 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class KiePMMLTargetFactory method getKiePMMLTargetVariableInitializer.

static MethodCallExpr getKiePMMLTargetVariableInitializer(final TargetField targetField) {
    final MethodDeclaration methodDeclaration = TARGET_TEMPLATE.getMethodsByName(GETKIEPMMLTARGET).get(0).clone();
    final BlockStmt targetBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(targetBody, TARGET).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGET, targetBody)));
    variableDeclarator.setName(targetField.getName());
    final MethodCallExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGET, targetBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", toReturn);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(targetField.getName());
    builder.setArgument(0, nameExpr);
    final ObjectCreationExpr targetFieldInstantiation = getTargetFieldVariableInitializer(targetField);
    builder.setArgument(2, targetFieldInstantiation);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10