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;
}
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;
}
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));
}
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()));
}
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);
}
Aggregations