KiePMMLPredicateFactory { static void setCompoundPredicateConstructor(final String generatedClassName, final String predicateName, final ConstructorDeclaration constructorDeclaration, final BOOLEAN_OPERATOR booleanOperator, final Set<String> predicatesClasses) { if (booleanOperator.equals(BOOLEAN_OPERATOR.SURROGATE)) { throw new IllegalArgumentException(SURROGATE + " not supported, yet"); } 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, "booleanOperator", booleanOperator.getClass().getCanonicalName() + "." + booleanOperator.name()); AssignExpr assignExpr = CommonCodegenUtils .getAssignExpression(body, "operatorFunction") .orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, "operatorFunction", body))); CompilationUnit operatorFunctionCU = getFromFileName(KIE_PMML_OPERATOR_FUNCTION_TEMPLATE).clone(); ClassOrInterfaceDeclaration operatorFunctionClass = operatorFunctionCU.getClassByName(KIE_PMML_OPERATOR_FUNCTION) .orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND)); String methodName = "getInnerBinaryOperator" + booleanOperator.name(); final MethodDeclaration methodDeclaration = CommonCodegenUtils.getMethodDeclaration(operatorFunctionClass, methodName) .orElseThrow(() -> new KiePMMLException(String.format(MISSING_METHOD_IN_CLASS,methodName, operatorFunctionClass))); final BlockStmt methodBody = methodDeclaration.getBody() .orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_IN_METHOD, methodDeclaration))); final ReturnStmt returnStmt = methodBody.getStatements() .stream() .filter(statement -> statement instanceof ReturnStmt) .map(statement -> (ReturnStmt) statement) .findFirst() .orElseThrow(() -> new KiePMMLException(String.format(MISSING_RETURN_IN_METHOD, methodDeclaration))); final Expression expression = returnStmt.getExpression().orElseThrow(() -> new KiePMMLException(String.format(MISSING_EXPRESSION_IN_RETURN, returnStmt))); assignExpr.setValue(expression); assignExpr = CommonCodegenUtils .getAssignExpression(body, "kiePMMLPredicates") .orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, "kiePMMLPredicates", body))); ClassOrInterfaceType arrayClass = parseClassOrInterfaceType(ArrayList.class.getName()); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(arrayClass); assignExpr.setValue(objectCreationExpr); for (String predicateClass : predicatesClasses) { ClassOrInterfaceType kiePMMLPredicateClass = parseClassOrInterfaceType(predicateClass); objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(kiePMMLPredicateClass); NodeList<Expression> arguments = NodeList.nodeList(objectCreationExpr); 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 setCompoundPredicateConstructorOr() { init(KIE_PMML_COMPOUND_PREDICATE_TEMPLATE_JAVA, KIE_PMML_COMPOUND_PREDICATE_TEMPLATE); String generatedClassName = "GENERATEDCLASSNAME"; String predicateName = "PREDICATENAME"; BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.OR; Set<String> predicatesClasses = new HashSet<>(Arrays.asList("PREDICATE_A", "PREDICATE_B")); KiePMMLPredicateFactory.setCompoundPredicateConstructor(generatedClassName, predicateName, constructorDeclaration, booleanOperator, predicatesClasses); Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>(); superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", predicateName))); superInvocationExpressionsMap.put(2, new NameExpr(booleanOperator.getClass().getCanonicalName() + "." + booleanOperator.name())); Map<String, Expression> assignExpressionMap = new HashMap<>(); Expression expression = parseExpression("(aBoolean, aBoolean2) -> aBoolean != null ? aBoolean || aBoolean2 : " + "aBoolean2"); assignExpressionMap.put("operatorFunction", expression); ClassOrInterfaceType kiePMMLSegmentClass = parseClassOrInterfaceType(ArrayList.class.getName()); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(kiePMMLSegmentClass); assignExpressionMap.put("kiePMMLPredicates", 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("kiePMMLPredicates") && methodCallExpr.getName().asString().equals("add")) .collect(Collectors.toList()); assertNotNull(methodCallExprs); assertEquals(predicatesClasses.size(), methodCallExprs.size()); predicatesClasses.forEach(predicateClass -> { ClassOrInterfaceType kiePMMLPredicateClass = parseClassOrInterfaceType(predicateClass); ObjectCreationExpr expected = new ObjectCreationExpr(); expected.setType(kiePMMLPredicateClass); assertTrue(methodCallExprs.stream() .anyMatch(methodCallExpr -> methodCallExpr.getArguments().size() == 1 && methodCallExpr.getArguments().get(0).equals(expected))); }); }
@Test public void setCompoundPredicateConstructorAnd() { init(KIE_PMML_COMPOUND_PREDICATE_TEMPLATE_JAVA, KIE_PMML_COMPOUND_PREDICATE_TEMPLATE); String generatedClassName = "GENERATEDCLASSNAME"; String predicateName = "PREDICATENAME"; Set<String> predicatesClasses = new HashSet<>(Arrays.asList("PREDICATE_A", "PREDICATE_B")); BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.AND; KiePMMLPredicateFactory.setCompoundPredicateConstructor(generatedClassName, predicateName, constructorDeclaration, booleanOperator, predicatesClasses); Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>(); superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", predicateName))); superInvocationExpressionsMap.put(2, new NameExpr(booleanOperator.getClass().getCanonicalName() + "." + booleanOperator.name())); Map<String, Expression> assignExpressionMap = new HashMap<>(); Expression expression = parseExpression("(aBoolean, aBoolean2) -> aBoolean != null ? aBoolean && aBoolean2 : aBoolean2"); assignExpressionMap.put("operatorFunction", expression); ClassOrInterfaceType kiePMMLSegmentClass = parseClassOrInterfaceType(ArrayList.class.getName()); ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr(); objectCreationExpr.setType(kiePMMLSegmentClass); assignExpressionMap.put("kiePMMLPredicates", objectCreationExpr); assertTrue(commonEvaluateConstructor(constructorDeclaration, generatedClassName, superInvocationExpressionsMap, assignExpressionMap)); }
@Test(expected = IllegalArgumentException.class) public void setCompoundPredicateUnsupportedConstructor() { String generatedClassName = "GENERATEDCLASSNAME"; String predicateName = "PREDICATENAME"; BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.SURROGATE; Set<String> predicatesClasses = new HashSet<>(Arrays.asList("PREDICATE_A", "PREDICATE_B")); KiePMMLPredicateFactory.setCompoundPredicateConstructor(generatedClassName, predicateName, constructorDeclaration, booleanOperator, predicatesClasses); }