Search in sources :

Example 31 with NodeList

use of com.github.javaparser.ast.NodeList 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);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 32 with NodeList

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

the class KiePMMLNodeFactory method populateEvaluateNodeWithScoreDistributions.

/**
 * Set the <b>scoreDistribution</b> <code>VariableDeclarator</code> initializer of the given <code>BlockStmt</code>.
 * If <b>scoreDistributionsParam</b> is <code>null</code>, a <code>NullLiteralExpr</code> is set.
 * <p>
 *     <code>List<KiePMMLScoreDistribution> scoreDistributions = null;</code>
 * </p>
 * Otherwise
 * <p>
 *     <code>List<KiePMMLScoreDistribution> scoreDistributions = arrays.asList(new KiePMMLScoreDistribution(....));</code>
 * </p>
 *
 * @param toPopulate
 * @param scoreDistributionsParam
 */
static void populateEvaluateNodeWithScoreDistributions(final BlockStmt toPopulate, final List<ScoreDistribution> scoreDistributionsParam) {
    final Expression scoreDistributionsExpression;
    if (scoreDistributionsParam == null) {
        scoreDistributionsExpression = new NullLiteralExpr();
    } else {
        int counter = 0;
        final NodeList<Expression> scoreDistributionsArguments = new NodeList<>();
        for (ScoreDistribution scoreDistribution : scoreDistributionsParam) {
            String nestedVariableName = String.format("scoreDistribution_%s", counter);
            scoreDistributionsArguments.add(getKiePMMLScoreDistribution(nestedVariableName, scoreDistribution));
            counter++;
        }
        scoreDistributionsExpression = new MethodCallExpr();
        ((MethodCallExpr) scoreDistributionsExpression).setScope(new NameExpr(Arrays.class.getSimpleName()));
        ((MethodCallExpr) scoreDistributionsExpression).setName("asList");
        ((MethodCallExpr) scoreDistributionsExpression).setArguments(scoreDistributionsArguments);
    }
    CommonCodegenUtils.setVariableDeclaratorValue(toPopulate, SCORE_DISTRIBUTIONS, scoreDistributionsExpression);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) KiePMMLScoreDistribution(org.kie.pmml.models.tree.model.KiePMMLScoreDistribution) ScoreDistribution(org.dmg.pmml.ScoreDistribution) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 33 with NodeList

use of com.github.javaparser.ast.NodeList 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));
        });
    });
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) Arrays(java.util.Arrays) Random(java.util.Random) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Node(org.dmg.pmml.tree.Node) PMMLModelTestUtils.getRandomPMMLScoreDistributions(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomPMMLScoreDistributions) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) PMML(org.dmg.pmml.PMML) CommonCodegenUtils(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils) EVALUATE_NODE(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.EVALUATE_NODE) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TreeModel(org.dmg.pmml.tree.TreeModel) MISSING_VALUE_PENALTY(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.MISSING_VALUE_PENALTY) Collectors(java.util.stream.Collectors) FileUtils.getFileContent(org.kie.test.util.filesystem.FileUtils.getFileContent) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) Optional(java.util.Optional) PACKAGE_CLASS_TEMPLATE(org.kie.pmml.commons.Constants.PACKAGE_CLASS_TEMPLATE) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) TypeExpr(com.github.javaparser.ast.expr.TypeExpr) AS_LIST(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.AS_LIST) NODE_FUNCTIONS(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.NODE_FUNCTIONS) IntStream(java.util.stream.IntStream) BeforeClass(org.junit.BeforeClass) SCORE(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.SCORE) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) DerivedField(org.dmg.pmml.DerivedField) ModelUtils.getDerivedFields(org.kie.pmml.compiler.api.utils.ModelUtils.getDerivedFields) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ScoreDistribution(org.dmg.pmml.ScoreDistribution) KiePMMLTreeModelUtils.createNodeClassName(org.kie.pmml.models.tree.compiler.utils.KiePMMLTreeModelUtils.createNodeClassName) JavaParserUtils.getFromSource(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromSource) KiePMMLNode(org.kie.pmml.models.tree.model.KiePMMLNode) JavaParserUtils(org.kie.pmml.compiler.commons.utils.JavaParserUtils) PACKAGE_NAME(org.kie.pmml.commons.Constants.PACKAGE_NAME) Assert.assertNotNull(org.junit.Assert.assertNotNull) CommonTestingUtils.getFieldsFromDataDictionaryAndDerivedFields(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionaryAndDerivedFields) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) DataDictionary(org.dmg.pmml.DataDictionary) Test(org.junit.Test) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) TestUtils(org.kie.pmml.compiler.api.testutils.TestUtils) SCORE_DISTRIBUTIONS(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.SCORE_DISTRIBUTIONS) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) EMPTY_LIST(org.kie.pmml.models.tree.compiler.factories.KiePMMLNodeFactory.EMPTY_LIST) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 34 with NodeList

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

the class FEELPropertyAccessibleImplementation method getFeelPropertyDefinition.

private MethodDefinition getFeelPropertyDefinition() {
    MethodDeclaration getFEELProperty = cloneMethodTemplate("getFEELProperty");
    SwitchStmt firstSwitch = getFEELProperty.findFirst(SwitchStmt.class).orElseThrow(() -> new InvalidTemplateException("Missing Switch Statement in getFEELProperty template"));
    firstSwitch.setComment(null);
    List<SwitchEntry> collect = fields.stream().map(this::toGetPropertySwitchEntry).collect(Collectors.toList());
    SwitchEntry defaultSwitchStmt = firstSwitch.findFirst(SwitchEntry.class, sw -> sw.getLabels().isEmpty()).orElseThrow(() -> new InvalidTemplateException("Missing Default Switch Statement in getFEELProperty template"));
    collect.add(defaultSwitchStmt);
    firstSwitch.setEntries(nodeList(collect));
    String body = getFEELProperty.getBody().orElseThrow(() -> new InvalidTemplateException("Empty body in getFeelProperty clone")).toString();
    MethodWithStringBody getFeelPropertyDefinition = new MethodWithStringBody("getFEELProperty", EvalHelper.PropertyValueResult.class.getCanonicalName(), body).addParameter(String.class.getCanonicalName(), "property");
    addOverrideAnnotation(getFeelPropertyDefinition);
    return getFeelPropertyDefinition;
}
Also used : MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) CastExpr(com.github.javaparser.ast.expr.CastExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ArrayList(java.util.ArrayList) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InputStream(java.io.InputStream) EvalHelper(org.kie.dmn.feel.util.EvalHelper) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) EvalHelper(org.kie.dmn.feel.util.EvalHelper) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry)

Example 35 with NodeList

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

the class FEELPropertyAccessibleImplementation method allFeelProperties.

private MethodWithStringBody allFeelProperties() {
    MethodDeclaration allFeelProperties = cloneMethodTemplate("allFEELProperties");
    MethodCallExpr putExpression = allFeelProperties.findFirst(MethodCallExpr.class, mc -> mc.getNameAsString().equals("put")).orElseThrow(() -> new InvalidTemplateException("Missing put method in allFEELProperties"));
    List<Statement> collect = fields.stream().map(fieldDefinition -> toResultPut(putExpression, fieldDefinition)).collect(Collectors.toList());
    if (typeDefinition instanceof AbstractDMNSetType) {
        collect.add(new ExpressionStmt(StaticJavaParser.parseExpression("result.entrySet().removeIf(entry -> java.util.Objects.isNull(entry.getValue()) && !definedKeySet.contains(entry.getKey()))")));
    }
    BlockStmt newBlockStatement = new BlockStmt(nodeList(collect));
    putExpression.getParentNode().ifPresent(p -> p.replace(newBlockStatement));
    BlockStmt body = allFeelProperties.getBody().orElseThrow(() -> new InvalidTemplateException("Missing body in generated method"));
    MethodWithStringBody allFEELProperties = new MethodWithStringBody("allFEELProperties", "java.util.Map<String, Object>", body.toString());
    addOverrideAnnotation(allFEELProperties);
    return allFEELProperties;
}
Also used : MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) SwitchEntry(com.github.javaparser.ast.stmt.SwitchEntry) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) NodeList.nodeList(com.github.javaparser.ast.NodeList.nodeList) CastExpr(com.github.javaparser.ast.expr.CastExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) ArrayList(java.util.ArrayList) SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) NodeList(com.github.javaparser.ast.NodeList) FieldDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.FieldDefinition) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TypeDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.TypeDefinition) MethodDefinition(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodDefinition) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) List(java.util.List) StaticJavaParser(com.github.javaparser.StaticJavaParser) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) InputStream(java.io.InputStream) EvalHelper(org.kie.dmn.feel.util.EvalHelper) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) MethodWithStringBody(org.drools.modelcompiler.builder.generator.declaredtype.api.MethodWithStringBody) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14