Search in sources :

Example 56 with NodeList

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

the class AccumulateInline method addAccumulateClassInitializationToMethod.

private void addAccumulateClassInitializationToMethod(MethodCallExpr accumulateDSL, String identifier) {
    this.packageModel.addGeneratedPOJO(accumulateInlineClass);
    final MethodCallExpr functionDSL = createDslTopLevelMethod(ACC_FUNCTION_CALL);
    functionDSL.addArgument(new MethodReferenceExpr(new NameExpr(accumulateInlineClassName), new NodeList<>(), "new"));
    functionDSL.addArgument(context.getVarExpr(identifier));
    final String bindingId = this.basePattern.getIdentifier();
    final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, BIND_AS_CALL);
    asDSL.addArgument(context.getVarExpr(bindingId));
    accumulateDSL.addArgument(asDSL);
}
Also used : NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 57 with NodeList

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

the class GeneratedMvelParserBase method generateLambda.

/**
 * Workaround for rather complex ambiguity that lambda's create
 */
Expression generateLambda(Expression ret, Statement lambdaBody) {
    if (ret instanceof EnclosedExpr) {
        Expression inner = ((EnclosedExpr) ret).getInner();
        SimpleName id = ((NameExpr) inner).getName();
        NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), new NodeList<>(), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
        ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, true);
    } else if (ret instanceof NameExpr) {
        SimpleName id = ((NameExpr) ret).getName();
        NodeList<Parameter> params = add(new NodeList<>(), new Parameter(ret.getTokenRange().orElse(null), new NodeList<>(), new NodeList<>(), new UnknownType(), false, new NodeList<>(), id));
        ret = new LambdaExpr(range(ret, lambdaBody), params, lambdaBody, false);
    } else if (ret instanceof LambdaExpr) {
        ((LambdaExpr) ret).setBody(lambdaBody);
        propagateRangeGrowthOnRight(ret, lambdaBody);
    } else if (ret instanceof CastExpr) {
        CastExpr castExpr = (CastExpr) ret;
        Expression inner = generateLambda(castExpr.getExpression(), lambdaBody);
        castExpr.setExpression(inner);
    } else {
        addProblem("Failed to parse lambda expression! Please create an issue at https://github.com/javaparser/javaparser/issues");
    }
    return ret;
}
Also used : UnknownType(com.github.javaparser.ast.type.UnknownType) NodeList(com.github.javaparser.ast.NodeList) Parameter(com.github.javaparser.ast.body.Parameter)

Example 58 with NodeList

use of com.github.javaparser.ast.NodeList 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 59 with NodeList

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

the class DMNAlphaNetworkCompiler method initPropertyNames.

private void initPropertyNames(List<InputClause> input) {
    NodeList<Expression> propertyNamesArray = input.stream().map(inputClause -> inputClause.getInputExpression().getText()).map(StringLiteralExpr::new).collect(Collectors.toCollection(NodeList::new));
    ArrayCreationExpr array = new ArrayCreationExpr().setElementType(new ArrayType(parseType(String.class.getCanonicalName()))).setInitializer(new ArrayInitializerExpr(propertyNamesArray));
    template.findAll(StringLiteralExpr.class, n -> n.asString().equals("PROPERTY_NAMES")).forEach(r -> r.replace(array));
}
Also used : ArrayType(com.github.javaparser.ast.type.ArrayType) NodeList(com.github.javaparser.ast.NodeList) DecisionTable(org.kie.dmn.model.api.DecisionTable) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) Logger(org.slf4j.Logger) ArrayType(com.github.javaparser.ast.type.ArrayType) InputClause(org.kie.dmn.model.api.InputClause) CodegenStringUtil.replaceStringLiteralExprWith(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.replaceStringLiteralExprWith) LoggerFactory(org.slf4j.LoggerFactory) CodegenStringUtil.replaceSimpleNameWith(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.replaceSimpleNameWith) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CodegenStringUtil.parseJavaClassTemplateFromResources(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.parseJavaClassTemplateFromResources) CodegenStringUtil(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil) HitPolicy(org.kie.dmn.model.api.HitPolicy) CodegenStringUtil.blockHasComment(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.blockHasComment) List(java.util.List) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BuiltinAggregator(org.kie.dmn.model.api.BuiltinAggregator) Expression(com.github.javaparser.ast.expr.Expression) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 60 with NodeList

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

the class KiePMMLCompoundPredicateFactory method getCompoundPredicateVariableDeclaration.

static BlockStmt getCompoundPredicateVariableDeclaration(final String variableName, final CompoundPredicate compoundPredicate, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = COMPOUND_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPOUNDPREDICATE).get(0).clone();
    final BlockStmt compoundPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(compoundPredicateBody, COMPOUND_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPOUND_PREDICATE, compoundPredicateBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> arguments = new NodeList<>();
    for (Predicate predicate : compoundPredicate.getPredicates()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getKiePMMLPredicate(nestedVariableName, predicate, fields);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    final BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value());
    final NameExpr booleanOperatorExpr = new NameExpr(BOOLEAN_OPERATOR.class.getName() + "." + booleanOperator.name());
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPOUND_PREDICATE, compoundPredicateBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(1, booleanOperatorExpr);
    getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
    compoundPredicateBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Predicate(org.dmg.pmml.Predicate) KiePMMLPredicateFactory.getKiePMMLPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLPredicateFactory.getKiePMMLPredicate) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) 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