Search in sources :

Example 16 with ObjectCreationExpr

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());
        });
    });
}
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) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) OutputField(org.kie.pmml.api.models.OutputField) PMMLModelTestUtils.getRandomOutputField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomOutputField) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 17 with ObjectCreationExpr

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;
    }
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 18 with 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);
    }
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) Arrays(java.util.Arrays) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) CommonCodegenUtils.literalExprFrom(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.literalExprFrom) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) IntStream(java.util.stream.IntStream) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) Assert.assertThrows(org.junit.Assert.assertThrows) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Assert.assertSame(org.junit.Assert.assertSame) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME) StaticJavaParser.parseBlock(com.github.javaparser.StaticJavaParser.parseBlock) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) Assert.assertNotNull(org.junit.Assert.assertNotNull) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CodegenTestUtils.commonValidateCompilation(org.kie.pmml.compiler.commons.testutils.CodegenTestUtils.commonValidateCompilation) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) LAMBDA_PARAMETER_NAME(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.LAMBDA_PARAMETER_NAME) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Assert.assertEquals(org.junit.Assert.assertEquals) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) Expression(com.github.javaparser.ast.expr.Expression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 19 with ObjectCreationExpr

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);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) KiePMMLTargetValue(org.kie.pmml.commons.model.KiePMMLTargetValue) TargetValue(org.kie.pmml.api.models.TargetValue) Expression(com.github.javaparser.ast.expr.Expression) TargetField(org.kie.pmml.api.models.TargetField) Test(org.junit.Test)

Example 20 with ObjectCreationExpr

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();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration)

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