Search in sources :

Example 1 with TypeExpr

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

the class CommonCodegenUtils method setConstructorDeclarationReferenceArgument.

/**
 * Set the <b>value</b> of the given <b>parameterName</b> in the given <code>ConstructorDeclaration</code>
 * @param constructorDeclaration
 * @param referenceName
 * @param value
 * @throws KiePMMLException if the given parameter is not found
 */
public static void setConstructorDeclarationReferenceArgument(final ConstructorDeclaration constructorDeclaration, final String referenceName, final String value) {
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    final MethodReferenceExpr methodReferenceExpr = getExplicitConstructorInvocationMethodReference(superStatement, referenceName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION, referenceName, constructorDeclaration)));
    if (value != null) {
        methodReferenceExpr.setScope(new TypeExpr(parseClassOrInterfaceType(value)));
    } else {
        superStatement.getArguments().replace(methodReferenceExpr, new NullLiteralExpr());
    }
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) TypeExpr(com.github.javaparser.ast.expr.TypeExpr)

Example 2 with TypeExpr

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

the class KiePMMLNodeFactory method populateEvaluateNodeWithNodeFunctions.

/**
 * Set the <b>nodeFunctions</b> <code>VariableDeclarator</code> initializer of the given <code>BlockStmt</code> with <code>MethodReferenceExpr</code>s to
 * <b>nested</b> <b>evaluateNode</b> methods.
 *
 * If <b>nestedNodesFullClasses</b>, set an empty list
 * <p>
 *     <code>Object nodeFunctions = java.util.Collections.emptyList();</code>
 * </p>
 *
 *  otherwise, an <code>ArrayList</code> of related references
 * <p>
 *     <code>Object nodeFunctions = java.util.Arrays.asList(full.node.NodeClassName0::evaluateNode, full.node.NodeClassName1::evaluateNode);</code>
 * </p>
 *
 * @param toPopulate
 * @param nestedNodesFullClasses
 */
static void populateEvaluateNodeWithNodeFunctions(final BlockStmt toPopulate, final List<String> nestedNodesFullClasses) {
    final MethodCallExpr valuesInit = new MethodCallExpr();
    if (nestedNodesFullClasses.isEmpty()) {
        valuesInit.setScope(new TypeExpr(parseClassOrInterfaceType(Collections.class.getName())));
        valuesInit.setName(EMPTY_LIST);
    } else {
        final NodeList<Expression> methodReferenceExprs = NodeList.nodeList(nestedNodesFullClasses.stream().map(KiePMMLNodeFactory::getEvaluateNodeMethodReference).collect(Collectors.toList()));
        valuesInit.setScope(new TypeExpr(parseClassOrInterfaceType(Arrays.class.getName())));
        valuesInit.setName(AS_LIST);
        valuesInit.setArguments(methodReferenceExprs);
    }
    CommonCodegenUtils.setVariableDeclaratorValue(toPopulate, NODE_FUNCTIONS, valuesInit);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypeExpr(com.github.javaparser.ast.expr.TypeExpr) Collections(java.util.Collections) Arrays(java.util.Arrays) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 3 with TypeExpr

use of com.github.javaparser.ast.expr.TypeExpr 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 4 with TypeExpr

use of com.github.javaparser.ast.expr.TypeExpr in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final TypeExpr node1, final Node other) {
    TypeExpr node2 = (TypeExpr) other;
    defaultAction(node1, node2);
    node1.getType().accept(this, node2.getType());
}
Also used : TypeExpr(com.github.javaparser.ast.expr.TypeExpr)

Aggregations

TypeExpr (com.github.javaparser.ast.expr.TypeExpr)4 Expression (com.github.javaparser.ast.expr.Expression)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 MethodReferenceExpr (com.github.javaparser.ast.expr.MethodReferenceExpr)2 Arrays (java.util.Arrays)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)1 Collections (java.util.Collections)1 Node (org.dmg.pmml.tree.Node)1 Test (org.junit.Test)1 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)1 KiePMMLNode (org.kie.pmml.models.tree.model.KiePMMLNode)1