Search in sources :

Example 66 with ClassOrInterfaceType

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

the class KiePMMLClusteringModelFactory method clusteringFieldCreationExprFrom.

private static ObjectCreationExpr clusteringFieldCreationExprFrom(ClusteringField clusteringField) {
    double fieldWeight = clusteringField.getFieldWeight() == null ? 1.0 : clusteringField.getFieldWeight().doubleValue();
    boolean isCenterField = clusteringField.getCenterField() == null || clusteringField.getCenterField() == ClusteringField.CenterField.TRUE;
    NodeList<Expression> arguments = new NodeList<>();
    arguments.add(literalExprFrom(clusteringField.getField().getValue()));
    arguments.add(new DoubleLiteralExpr(fieldWeight));
    arguments.add(new BooleanLiteralExpr(isCenterField));
    arguments.add(clusteringField.getCompareFunction() == null ? new NullLiteralExpr() : literalExprFrom(compareFunctionFrom(clusteringField.getCompareFunction())));
    arguments.add(new NullLiteralExpr());
    return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLClusteringField.class.getCanonicalName()), arguments);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) NodeList(com.github.javaparser.ast.NodeList) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 67 with ClassOrInterfaceType

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

the class KiePMMLSegmentFactoryTest method setConstructorNoInterpreted.

@Test
public void setConstructorNoInterpreted() {
    ConstructorDeclaration constructorDeclaration = MODEL_TEMPLATE.getDefaultConstructor().get();
    String segmentName = "SEGMENTNAME";
    String generatedClassName = "GENERATEDCLASSNAME";
    String kiePMMLModelClass = "KIEPMMLMODELCLASS";
    double weight = 12.22;
    KiePMMLSegmentFactory.setConstructor(segmentName, generatedClassName, constructorDeclaration, kiePMMLModelClass, false, weight);
    Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
    superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", segmentName)));
    ClassOrInterfaceType classOrInterfaceType = parseClassOrInterfaceType(kiePMMLModelClass);
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(classOrInterfaceType);
    superInvocationExpressionsMap.put(3, new NameExpr(objectCreationExpr.toString()));
    Map<String, Expression> assignExpressionMap = new HashMap<>();
    assignExpressionMap.put("weight", new DoubleLiteralExpr(weight));
    assignExpressionMap.put("id", new StringLiteralExpr(segmentName));
    assertTrue(commonEvaluateConstructor(constructorDeclaration, generatedClassName, superInvocationExpressionsMap, assignExpressionMap));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Expression(com.github.javaparser.ast.expr.Expression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Test(org.junit.Test)

Example 68 with ClassOrInterfaceType

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

the class KiePMMLSegmentFactoryTest method setConstructorInterpreted.

@Test
public void setConstructorInterpreted() throws IOException {
    ConstructorDeclaration constructorDeclaration = MODEL_TEMPLATE.getDefaultConstructor().get();
    String segmentName = "SEGMENTNAME";
    String generatedClassName = "GENERATEDCLASSNAME";
    String kiePMMLModelClass = "KIEPMMLMODELCLASS";
    double weight = 12.22;
    KiePMMLSegmentFactory.setConstructor(segmentName, generatedClassName, constructorDeclaration, kiePMMLModelClass, true, weight);
    Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
    superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", segmentName)));
    ClassOrInterfaceType classOrInterfaceType = parseClassOrInterfaceType(kiePMMLModelClass);
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(classOrInterfaceType);
    superInvocationExpressionsMap.put(3, new NameExpr(objectCreationExpr.toString()));
    Map<String, Expression> assignExpressionMap = new HashMap<>();
    assignExpressionMap.put("weight", new DoubleLiteralExpr(weight));
    assignExpressionMap.put("id", new StringLiteralExpr(segmentName));
    String text = getFileContent(TEST_01_SOURCE);
    BlockStmt expected = JavaParserUtils.parseConstructorBlock(text);
    assertTrue(JavaParserUtils.equalsNode(expected, constructorDeclaration.getBody()));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) HashMap(java.util.HashMap) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Expression(com.github.javaparser.ast.expr.Expression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Test(org.junit.Test)

Example 69 with ClassOrInterfaceType

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

the class KiePMMLMiningModelFactory method setConstructor.

static void setConstructor(final MiningModelCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
    KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    ClassOrInterfaceType kiePMMLSegmentationClass = parseClassOrInterfaceType(compilationDTO.getSegmentationCanonicalClassName());
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(kiePMMLSegmentationClass);
    CommonCodegenUtils.setAssignExpressionValue(body, "segmentation", objectCreationExpr);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 70 with ClassOrInterfaceType

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

the class KiePMMLRegressionTableFactory method getNumericPredictorExpression.

/**
 * Create a <b>NumericPredictor</b> <code>CastExpr</code>
 * @param numericPredictor
 * @return
 */
static CastExpr getNumericPredictorExpression(final NumericPredictor numericPredictor) {
    boolean withExponent = !Objects.equals(1, numericPredictor.getExponent());
    final String lambdaExpressionMethodName = withExponent ? "evaluateNumericWithExponent" : "evaluateNumericWithoutExponent";
    final String parameterName = "input";
    final MethodCallExpr lambdaMethodCallExpr = new MethodCallExpr();
    lambdaMethodCallExpr.setName(lambdaExpressionMethodName);
    lambdaMethodCallExpr.setScope(new NameExpr(KiePMMLRegressionTable.class.getSimpleName()));
    final NodeList<Expression> arguments = new NodeList<>();
    arguments.add(0, new NameExpr(parameterName));
    arguments.add(1, getExpressionForObject(numericPredictor.getCoefficient().doubleValue()));
    if (withExponent) {
        arguments.add(2, getExpressionForObject(numericPredictor.getExponent().doubleValue()));
    }
    lambdaMethodCallExpr.setArguments(arguments);
    final ExpressionStmt lambdaExpressionStmt = new ExpressionStmt(lambdaMethodCallExpr);
    final LambdaExpr lambdaExpr = new LambdaExpr();
    final Parameter lambdaParameter = new Parameter(new UnknownType(), parameterName);
    lambdaExpr.setParameters(NodeList.nodeList(lambdaParameter));
    lambdaExpr.setBody(lambdaExpressionStmt);
    final String doubleClassName = Double.class.getSimpleName();
    final ClassOrInterfaceType serializableFunctionType = getTypedClassOrInterfaceTypeByTypeNames(SerializableFunction.class.getCanonicalName(), Arrays.asList(doubleClassName, doubleClassName));
    final CastExpr toReturn = new CastExpr();
    toReturn.setType(serializableFunctionType);
    toReturn.setExpression(lambdaExpr);
    return toReturn;
}
Also used : SerializableFunction(org.kie.pmml.api.iinterfaces.SerializableFunction) NodeList(com.github.javaparser.ast.NodeList) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) UnknownType(com.github.javaparser.ast.type.UnknownType) Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Parameter(com.github.javaparser.ast.body.Parameter) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)76 Expression (com.github.javaparser.ast.expr.Expression)33 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)29 Type (com.github.javaparser.ast.type.Type)24 NameExpr (com.github.javaparser.ast.expr.NameExpr)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)21 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)21 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)17 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)17 NodeList (com.github.javaparser.ast.NodeList)14 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)13 TypeParameter (com.github.javaparser.ast.type.TypeParameter)13 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)12 Test (org.junit.Test)11 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)9 List (java.util.List)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 HashMap (java.util.HashMap)7