Search in sources :

Example 71 with ObjectCreationExpr

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

the class DMNResultCollectorAlphaSink method toANCInlinedForm.

@Override
public Expression toANCInlinedForm() {
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(StaticJavaParser.parseClassOrInterfaceType(DMNResultCollector.class.getCanonicalName()));
    objectCreationExpr.addArgument(new IntegerLiteralExpr(row));
    objectCreationExpr.addArgument(new StringLiteralExpr(columnName));
    objectCreationExpr.addArgument(StaticJavaParser.parseExpression("ctx.getResultCollector()"));
    Expression lambdaExpr = StaticJavaParser.parseExpression(String.format("(org.kie.dmn.feel.lang.EvaluationContext x) -> %s.getInstance().apply(x)", outputClass));
    objectCreationExpr.addArgument(lambdaExpr);
    return objectCreationExpr;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Example 72 with ObjectCreationExpr

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

the class DTAnalysisMeta method boundAsExpression.

private static Expression boundAsExpression(Bound<?> bound) {
    Comparable<?> value = bound.getValue();
    Expression valueExpr = null;
    if (value == Interval.NEG_INF) {
        valueExpr = parseExpression("Interval.NEG_INF");
    } else if (value == Interval.POS_INF) {
        valueExpr = parseExpression("Interval.POS_INF");
    } else if (value instanceof BigDecimal) {
        BigDecimal bigDecimal = (BigDecimal) value;
        ObjectCreationExpr newExpression = parseExpression("new BigDecimal()");
        StringLiteralExpr stringRep = new StringLiteralExpr(bigDecimal.toString());
        newExpression.addArgument(stringRep);
        valueExpr = newExpression;
    } else if (value instanceof String) {
        String string = (String) value;
        StringLiteralExpr newExpression = new StringLiteralExpr();
        newExpression.setString(string);
        valueExpr = newExpression;
    } else if (value instanceof Boolean) {
        Boolean b = (Boolean) value;
        valueExpr = new BooleanLiteralExpr(b);
    } else if (value instanceof LocalDate) {
        LocalDate localDateTime = (LocalDate) value;
        MethodCallExpr newExpression = parseExpression("java.time.LocalDate.parse()");
        StringLiteralExpr stringRep = new StringLiteralExpr(localDateTime.toString());
        newExpression.addArgument(stringRep);
        valueExpr = newExpression;
    } else if (value instanceof ComparablePeriod) {
        ComparablePeriod comparablePeriod = (ComparablePeriod) value;
        MethodCallExpr newExpression = parseExpression("org.kie.dmn.feel.lang.types.impl.ComparablePeriod.parse()");
        StringLiteralExpr stringRep = new StringLiteralExpr(comparablePeriod.asPeriod().toString());
        newExpression.addArgument(stringRep);
        valueExpr = newExpression;
    } else {
        throw new UnsupportedOperationException("boundAsExpression value " + value + " not supported.");
    }
    Expression typeExpr = null;
    if (bound.getBoundaryType() == RangeBoundary.OPEN) {
        typeExpr = parseExpression("RangeBoundary.OPEN");
    } else if (bound.getBoundaryType() == RangeBoundary.CLOSED) {
        typeExpr = parseExpression("RangeBoundary.CLOSED");
    } else {
        throw new IllegalStateException("illegal getBoundaryType");
    }
    ObjectCreationExpr newExpression = parseExpression("new Bound()");
    newExpression.addArgument(valueExpr);
    newExpression.addArgument(typeExpr);
    newExpression.addArgument(new NullLiteralExpr());
    return newExpression;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 73 with ObjectCreationExpr

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

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

use of com.github.javaparser.ast.expr.ObjectCreationExpr 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)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10