Search in sources :

Example 11 with MethodReferenceExpr

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

the class LegacyAccumulate method build.

public void build() {
    Pattern pattern = (Pattern) new PatternBuilder().build(ruleBuildContext, basePattern);
    Accumulate accumulate = (Accumulate) pattern.getSource();
    final Set<String> imports = ruleBuildContext.getPkg().getImports().keySet();
    final String packageName = ruleBuildContext.getPkg().getName();
    if (context.getLegacyAccumulateCounter() == 0) {
        GeneratedClassWithPackage generatedClassWithPackage = createAllAccumulateClass(imports, packageName);
        packageModel.addGeneratedAccumulateClasses(generatedClassWithPackage);
    } else {
        for (GeneratedClassWithPackage c : packageModel.getGeneratedAccumulateClasses()) {
            String ruleClassName = StringUtils.ucFirst(context.getRuleDescr().getClassName());
            if (ruleClassName.equals(c.getClassName())) {
                for (String m : ruleBuildContext.getMethods()) {
                    c.getGeneratedClass().addMember(parseBodyDeclaration(m));
                }
                break;
            }
        }
    }
    GeneratedClassWithPackage invokerGenerated = createInvokerClass(imports, packageName);
    packageModel.addGeneratedAccumulateClasses(invokerGenerated);
    final String generatedClassName = invokerGenerated.getGeneratedClass().getName().asString();
    String typeWithPackage = String.format("%s.%s", packageName, generatedClassName);
    Expression accExpr = new MethodReferenceExpr(new NameExpr(typeWithPackage), new NodeList<Type>(), "new");
    MethodCallExpr accFunctionCall = createDslTopLevelMethod(ACC_FUNCTION_CALL, nodeList(accExpr));
    if (accumulate.getRequiredDeclarations().length > 0) {
        accFunctionCall = new MethodCallExpr(accFunctionCall, ACC_WITH_EXTERNAL_DECLRS_CALL);
        for (Declaration requiredDeclaration : accumulate.getRequiredDeclarations()) {
            accFunctionCall.addArgument(context.getVar(requiredDeclaration.getIdentifier()));
        }
    }
    final String identifier;
    if (basePattern.getIdentifier() != null) {
        identifier = basePattern.getIdentifier();
    } else {
        // As we don't have a bind we need a unique name. Let's use the same as the generated invoker
        identifier = generatedClassName;
        context.addDeclaration(generatedClassName, Object.class);
    }
    Expression bindingVariable = context.getVarExpr(identifier);
    accFunctionCall = new MethodCallExpr(accFunctionCall, BIND_AS_CALL, nodeList(bindingVariable));
    context.addExpression(accFunctionCall);
    context.increaseLegacyAccumulateCounter();
}
Also used : Pattern(org.drools.core.rule.Pattern) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) PatternBuilder(org.drools.compiler.rule.builder.PatternBuilder) Accumulate(org.drools.core.rule.Accumulate) Type(com.github.javaparser.ast.type.Type) GeneratedClassWithPackage(org.drools.modelcompiler.builder.GeneratedClassWithPackage) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseBodyDeclaration(com.github.javaparser.StaticJavaParser.parseBodyDeclaration) Declaration(org.drools.core.rule.Declaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 12 with MethodReferenceExpr

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

the class CommonCodegenUtils method addMapPopulation.

/**
 * For every entry in the given map, add
 * <pre>
 *     (<i>mapName</i>).put(<i>entry_key<i/>, this::<i>entry_value_ref</i>>);
 * </pre>
 * e.g.
 * <pre>
 *     MAP_NAME.put("KEY_0", this::METHOD_015);
 *     MAP_NAME.put("KEY_3", this::METHOD_33);
 *     MAP_NAME.put("KEY_2", this::METHOD_219);
 *     MAP_NAME.put("KEY_4", this::METHOD_46);
 * </pre>
 * inside the given <code>BlockStmt</code>
 * @param toAdd
 * @param body
 * @param mapName
 */
public static void addMapPopulation(final Map<String, MethodDeclaration> toAdd, final BlockStmt body, final String mapName) {
    Map<String, Expression> toAddExpr = toAdd.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> {
        MethodReferenceExpr methodReferenceExpr = new MethodReferenceExpr();
        methodReferenceExpr.setScope(new ThisExpr());
        methodReferenceExpr.setIdentifier(entry.getValue().getNameAsString());
        return methodReferenceExpr;
    }));
    addMapPopulationExpressions(toAddExpr, body, mapName);
}
Also used : 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) MISSING_VARIABLE_IN_BODY(org.kie.pmml.commons.Constants.MISSING_VARIABLE_IN_BODY) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) OpType(org.dmg.pmml.OpType) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) LocalTime(java.time.LocalTime) Expression(com.github.javaparser.ast.expr.Expression) MISSING_CHAINED_METHOD_DECLARATION_TEMPLATE(org.kie.pmml.commons.Constants.MISSING_CHAINED_METHOD_DECLARATION_TEMPLATE) MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION(org.kie.pmml.commons.Constants.MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) Collection(java.util.Collection) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) DataType(org.dmg.pmml.DataType) MISSING_BODY_TEMPLATE(org.kie.pmml.commons.Constants.MISSING_BODY_TEMPLATE) 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) StaticJavaParser(com.github.javaparser.StaticJavaParser) LocalDate(java.time.LocalDate) Optional(java.util.Optional) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) TypeExpr(com.github.javaparser.ast.expr.TypeExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Parameter(com.github.javaparser.ast.body.Parameter) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MISSING_BODY_IN_METHOD(org.kie.pmml.commons.Constants.MISSING_BODY_IN_METHOD) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) MISSING_STATIC_INITIALIZER(org.kie.pmml.commons.Constants.MISSING_STATIC_INITIALIZER) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Statement(com.github.javaparser.ast.stmt.Statement) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) InitializerDeclaration(com.github.javaparser.ast.body.InitializerDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MISSING_CONSTRUCTOR_IN_BODY(org.kie.pmml.commons.Constants.MISSING_CONSTRUCTOR_IN_BODY) MISSING_VARIABLE_INITIALIZER_TEMPLATE(org.kie.pmml.commons.Constants.MISSING_VARIABLE_INITIALIZER_TEMPLATE) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ThisExpr(com.github.javaparser.ast.expr.ThisExpr)

Example 13 with MethodReferenceExpr

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

the class KiePMMLNodeFactory method mergeNodeReferences.

/**
 * Adjust the <b>evaluateNode(?)</b> references to the ones declared in the given
 * <code>JavaParserDTO.nodeTemplate</code>
 *
 * @param toPopulate
 * @param nestedNodeNamesDTO
 * @param evaluateNodeInitializer
 */
static void mergeNodeReferences(final JavaParserDTO toPopulate, final NodeNamesDTO nestedNodeNamesDTO, final MethodCallExpr evaluateNodeInitializer) {
    final NodeList<Expression> evaluateNodeReferences = evaluateNodeInitializer.getArguments();
    final String expectedReference = String.format(PACKAGE_CLASS_TEMPLATE, toPopulate.packageName, nestedNodeNamesDTO.nodeClassName);
    Optional<MethodReferenceExpr> found = Optional.empty();
    for (Expression expression : evaluateNodeReferences) {
        if (expectedReference.equals(expression.asMethodReferenceExpr().getScope().toString())) {
            found = Optional.of(expression.asMethodReferenceExpr());
            break;
        }
    }
    final MethodReferenceExpr evaluateNodeReference = found.orElseThrow(() -> new KiePMMLException(String.format(MISSING_METHOD_REFERENCE_TEMPLATE, expectedReference, evaluateNodeInitializer)));
    String identifier = EVALUATE_NODE + nestedNodeNamesDTO.nodeClassName;
    evaluateNodeReference.setScope(new NameExpr(toPopulate.nodeClassName));
    evaluateNodeReference.setIdentifier(identifier);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr)

Example 14 with MethodReferenceExpr

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

the class KiePMMLNodeFactoryTest method mergeNodeReferences.

@Test
public void mergeNodeReferences() {
    KiePMMLNodeFactory.NodeNamesDTO nodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(nodeRoot, createNodeClassName(), null, 1.0);
    KiePMMLNodeFactory.JavaParserDTO toPopulate = new KiePMMLNodeFactory.JavaParserDTO(nodeNamesDTO, PACKAGE_NAME);
    Node nestedNode = nodeRoot.getNodes().get(0);
    // Creating evaluateNodeInitializer
    String nestedNodeClassName = nodeNamesDTO.childrenNodes.get(nestedNode);
    String fullNestedNodeClassName = String.format(PACKAGE_CLASS_TEMPLATE, PACKAGE_NAME, nestedNodeClassName);
    final NodeList<Expression> methodReferenceExprs = NodeList.nodeList(KiePMMLNodeFactory.getEvaluateNodeMethodReference(fullNestedNodeClassName));
    MethodReferenceExpr evaluateNodeReference = new MethodReferenceExpr();
    evaluateNodeReference.setScope(new NameExpr(fullNestedNodeClassName));
    evaluateNodeReference.setIdentifier(EVALUATE_NODE);
    MethodCallExpr evaluateNodeInitializer = new MethodCallExpr();
    evaluateNodeInitializer.setScope(new TypeExpr(parseClassOrInterfaceType(Arrays.class.getName())));
    evaluateNodeInitializer.setName(AS_LIST);
    evaluateNodeInitializer.setArguments(methodReferenceExprs);
    // 
    KiePMMLNodeFactory.NodeNamesDTO nestedNodeNamesDTO = new KiePMMLNodeFactory.NodeNamesDTO(nestedNode, nodeNamesDTO.getNestedNodeClassName(nestedNode), nodeNamesDTO.nodeClassName, nodeNamesDTO.missingValuePenalty);
    KiePMMLNodeFactory.mergeNodeReferences(toPopulate, nestedNodeNamesDTO, evaluateNodeInitializer);
    MethodReferenceExpr retrieved = evaluateNodeInitializer.getArguments().get(0).asMethodReferenceExpr();
    String expected = toPopulate.nodeClassName;
    assertEquals(expected, retrieved.getScope().asNameExpr().toString());
    expected = EVALUATE_NODE + nestedNodeClassName;
    assertEquals(expected, retrieved.getIdentifier());
}
Also used : Node(org.dmg.pmml.tree.Node) KiePMMLNode(org.kie.pmml.models.tree.model.KiePMMLNode) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) TypeExpr(com.github.javaparser.ast.expr.TypeExpr) Expression(com.github.javaparser.ast.expr.Expression) Arrays(java.util.Arrays) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 15 with MethodReferenceExpr

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

the class KiePMMLTreeModelFactory method setConstructor.

static void setConstructor(final TreeCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String fullNodeClassName) {
    KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    // set predicate function
    MethodReferenceExpr nodeReference = new MethodReferenceExpr();
    nodeReference.setScope(new NameExpr(fullNodeClassName));
    nodeReference.setIdentifier("evaluateNode");
    CommonCodegenUtils.setAssignExpressionValue(body, "nodeFunction", nodeReference);
}
Also used : ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr)

Aggregations

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