Search in sources :

Example 16 with MethodReferenceExpr

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

the class KiePMMLTreeModelFactoryTest method setConstructor.

@Test
public void setConstructor() {
    String className = getSanitizedClassName(treeModel1.getModelName());
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(className, PACKAGE_NAME, KIE_PMML_TREE_MODEL_TEMPLATE_JAVA, KIE_PMML_TREE_MODEL_TEMPLATE);
    ClassOrInterfaceDeclaration modelTemplate = cloneCU.getClassByName(className).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + className));
    String targetField = "whatIdo";
    String fullNodeClassName = "full.Node.ClassName";
    CommonCompilationDTO<TreeModel> source = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml1, treeModel1, new HasClassLoaderMock());
    KiePMMLTreeModelFactory.setConstructor(TreeCompilationDTO.fromCompilationDTO(source), modelTemplate, fullNodeClassName);
    ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName()))).clone();
    BlockStmt body = constructorDeclaration.getBody();
    // targetField
    Optional<AssignExpr> optRetrieved = CommonCodegenUtils.getAssignExpression(body, "targetField");
    assertTrue(optRetrieved.isPresent());
    AssignExpr retrieved = optRetrieved.get();
    Expression initializer = retrieved.getValue();
    assertTrue(initializer instanceof StringLiteralExpr);
    String expected = String.format("\"%s\"", targetField);
    assertEquals(expected, initializer.toString());
    // miningFunction
    optRetrieved = CommonCodegenUtils.getAssignExpression(body, "miningFunction");
    assertTrue(optRetrieved.isPresent());
    retrieved = optRetrieved.get();
    initializer = retrieved.getValue();
    assertTrue(initializer instanceof NameExpr);
    MINING_FUNCTION miningFunction = MINING_FUNCTION.byName(treeModel1.getMiningFunction().value());
    expected = miningFunction.getClass().getName() + "." + miningFunction.name();
    assertEquals(expected, initializer.toString());
    // pmmlMODEL
    optRetrieved = CommonCodegenUtils.getAssignExpression(body, "pmmlMODEL");
    assertTrue(optRetrieved.isPresent());
    retrieved = optRetrieved.get();
    initializer = retrieved.getValue();
    assertTrue(initializer instanceof NameExpr);
    expected = PMML_MODEL.TREE_MODEL.getClass().getName() + "." + PMML_MODEL.TREE_MODEL.name();
    assertEquals(expected, initializer.toString());
    // nodeFunction
    optRetrieved = CommonCodegenUtils.getAssignExpression(body, "nodeFunction");
    assertTrue(optRetrieved.isPresent());
    retrieved = optRetrieved.get();
    initializer = retrieved.getValue();
    assertTrue(initializer instanceof MethodReferenceExpr);
    expected = fullNodeClassName;
    assertEquals(expected, ((MethodReferenceExpr) initializer).getScope().toString());
    expected = "evaluateNode";
    assertEquals(expected, ((MethodReferenceExpr) initializer).getIdentifier());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) HasClassLoaderMock(org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) KiePMMLTreeModel(org.kie.pmml.models.tree.model.KiePMMLTreeModel) TreeModel(org.dmg.pmml.tree.TreeModel) Expression(com.github.javaparser.ast.expr.Expression) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) Test(org.junit.Test)

Example 17 with MethodReferenceExpr

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

the class KiePMMLRegressionTableFactory method getResultUpdaterSupportedExpression.

/**
 * Create a <b>resultUpdater</b> <code>CastExpr</code>
 * @param normalizationMethod
 * @return
 */
static MethodReferenceExpr getResultUpdaterSupportedExpression(final RegressionModel.NormalizationMethod normalizationMethod) {
    final String thisExpressionMethodName = String.format("update%sResult", normalizationMethod.name());
    final CastExpr castExpr = new CastExpr();
    final String doubleClassName = Double.class.getSimpleName();
    final ClassOrInterfaceType consumerType = getTypedClassOrInterfaceTypeByTypeNames(SerializableFunction.class.getCanonicalName(), Arrays.asList(doubleClassName, doubleClassName));
    castExpr.setType(consumerType);
    castExpr.setExpression(KiePMMLRegressionTable.class.getSimpleName());
    final MethodReferenceExpr toReturn = new MethodReferenceExpr();
    toReturn.setScope(castExpr);
    toReturn.setIdentifier(thisExpressionMethodName);
    return toReturn;
}
Also used : SerializableFunction(org.kie.pmml.api.iinterfaces.SerializableFunction) KiePMMLRegressionTable(org.kie.pmml.models.regression.model.KiePMMLRegressionTable) CastExpr(com.github.javaparser.ast.expr.CastExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr)

Example 18 with MethodReferenceExpr

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

the class JointJavacJavaParserVisitor method visitMemberReference.

@Override
public Void visitMemberReference(MemberReferenceTree javacTree, Node javaParserNode) {
    MethodReferenceExpr node = castNode(MethodReferenceExpr.class, javaParserNode, javacTree);
    processMemberReference(javacTree, node);
    if (node.getScope().isTypeExpr()) {
        javacTree.getQualifierExpression().accept(this, node.getScope().asTypeExpr().getType());
    } else {
        javacTree.getQualifierExpression().accept(this, node.getScope());
    }
    assert (javacTree.getTypeArguments() != null) == node.getTypeArguments().isPresent();
    if (javacTree.getTypeArguments() != null) {
        visitLists(javacTree.getTypeArguments(), node.getTypeArguments().get());
    }
    return null;
}
Also used : 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