KiePMMLPredicateFactory { static void setSimpleSetPredicateConstructor(final String generatedClassName, final String predicateName, final ConstructorDeclaration constructorDeclaration, final ARRAY_TYPE arrayType, final IN_NOTIN inNotIn, final List<Object> values) { setConstructorSuperNameInvocation(generatedClassName, constructorDeclaration, predicateName); final BlockStmt body = constructorDeclaration.getBody(); final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body) .orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body))); CommonCodegenUtils.setExplicitConstructorInvocationArgument(superStatement, "arrayType", arrayType.getClass().getCanonicalName() + "." + arrayType.name()); CommonCodegenUtils.setExplicitConstructorInvocationArgument(superStatement, "inNotIn", inNotIn.getClass().getCanonicalName() + "." + inNotIn.name()); AssignExpr assignExpr = CommonCodegenUtils .getAssignExpression(body, "values") .orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, "values", body))); ClassOrInterfaceType arrayClass = parseClassOrInterfaceType(ArrayList.class.getName()); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(arrayClass); assignExpr.setValue(objectCreationExpr); for (Object value : values) { Expression valueExpression; if (arrayType == ARRAY_TYPE.STRING) { valueExpression = new StringLiteralExpr(value.toString()); } else { valueExpression = new NameExpr(value.toString()); } NodeList<Expression> arguments = NodeList.nodeList(valueExpression); MethodCallExpr methodCallExpr = new MethodCallExpr(); methodCallExpr.setScope(assignExpr.getTarget().asNameExpr()); methodCallExpr.setName("add"); methodCallExpr.setArguments(arguments); ExpressionStmt expressionStmt = new ExpressionStmt(); expressionStmt.setExpression(methodCallExpr); body.addStatement(expressionStmt); } } private  KiePMMLPredicateFactory(); static List<KiePMMLPredicate> getPredicates(List<Predicate> predicates, DataDictionary dataDictionary); static KiePMMLPredicate getPredicate(Predicate predicate, DataDictionary dataDictionary); static KiePMMLSimplePredicate getKiePMMLSimplePredicate(SimplePredicate predicate, DataType dataType); static KiePMMLSimpleSetPredicate getKiePMMLSimpleSetPredicate(SimpleSetPredicate predicate); static KiePMMLCompoundPredicate getKiePMMLCompoundPredicate(CompoundPredicate predicate,
                                                                       DataDictionary dataDictionary); static KiePMMLTruePredicate getKiePMMLTruePredicate(); static KiePMMLFalsePredicate getKiePMMLFalsePredicate(); static Map<String, String> getPredicateSourcesMap(final KiePMMLPredicate kiePMMLPredicate,
                                                             final String packageName);  }
@Test public void setSimpleSetPredicateConstructor() { init(KIE_PMML_SIMPLE_SET_PREDICATE_TEMPLATE_JAVA, KIE_PMML_SIMPLE_SET_PREDICATE_TEMPLATE); AssignExpr valueAssignExpr = assignExprs.stream().filter(assignExpr -> assignExpr.getTarget().asNameExpr().getNameAsString().equals("values")) .findFirst() .orElseThrow(() -> new RuntimeException("Missing \"values\" assign variable")); assertTrue(valueAssignExpr.getValue() instanceof NullLiteralExpr); String generatedClassName = "GENERATEDCLASSNAME"; String predicateName = "PREDICATENAME"; ARRAY_TYPE arrayType = ARRAY_TYPE.REAL; IN_NOTIN inNotIn = IN_NOTIN.IN; List<Object> values = getObjects(arrayType, 3); KiePMMLPredicateFactory.setSimpleSetPredicateConstructor(generatedClassName, predicateName, constructorDeclaration, arrayType, inNotIn, values); Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>(); superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", predicateName))); superInvocationExpressionsMap.put(2, new NameExpr(arrayType.getClass().getCanonicalName() + "." + arrayType.name())); superInvocationExpressionsMap.put(3, new NameExpr(inNotIn.getClass().getCanonicalName() + "." + inNotIn.name())); Map<String, Expression> assignExpressionMap = new HashMap<>(); ClassOrInterfaceType kiePMMLSegmentClass = parseClassOrInterfaceType(ArrayList.class.getName()); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(kiePMMLSegmentClass); assignExpressionMap.put("values", objectCreationExpr); assertTrue(commonEvaluateConstructor(constructorDeclaration, generatedClassName, superInvocationExpressionsMap, assignExpressionMap)); List<MethodCallExpr> methodCallExprs = constructorDeclaration.getBody() .getStatements().stream().filter(statement -> statement instanceof ExpressionStmt) .map(statement -> (ExpressionStmt) statement) .filter(expressionStmt -> expressionStmt.getExpression() instanceof MethodCallExpr) .map(expressionStmt -> (MethodCallExpr) expressionStmt.getExpression()) .filter(methodCallExpr -> methodCallExpr.getScope().isPresent() && methodCallExpr.getScope().get().asNameExpr().getName().asString().equals("values") && methodCallExpr.getName().asString().equals("add")) .collect(Collectors.toList()); assertNotNull(methodCallExprs); assertEquals(values.size(), methodCallExprs.size()); values.forEach(o -> { Expression expected = new NameExpr(o.toString()); assertTrue(methodCallExprs.stream() .anyMatch(methodCallExpr -> methodCallExpr.getArguments().size() == 1 && methodCallExpr.getArguments().get(0).equals(expected))); }); }