KiePMMLPredicateFactory { static void setSimplePredicateConstructor(final String generatedClassName, final String predicateName, final ConstructorDeclaration constructorDeclaration, final OPERATOR operator, final Object value) { if (operator.equals(OPERATOR.IS_MISSING) || operator.equals(OPERATOR.IS_NOT_MISSING) ) { throw new IllegalArgumentException(operator + " 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, "operator", operator.getClass().getCanonicalName() + "." + operator.name()); Expression expression = value instanceof String ? new StringLiteralExpr((String) value) : new NameExpr(value.toString()); CommonCodegenUtils.setAssignExpressionValue(body, "value", expression); } 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 setSimplePredicateConstructorGreaterOrEqual() { init(KIE_PMML_SIMPLE_PREDICATE_TEMPLATE_JAVA, KIE_PMML_SIMPLE_PREDICATE_TEMPLATE); AssignExpr valueAssignExpr = assignExprs.stream().filter(assignExpr -> assignExpr.getTarget().asNameExpr().getNameAsString().equals("value")) .findFirst() .orElseThrow(() -> new RuntimeException("Missing \"value\" assign variable")); assertTrue(valueAssignExpr.getValue() instanceof NullLiteralExpr); String generatedClassName = "GENERATEDCLASSNAME"; String predicateName = "PREDICATENAME"; OPERATOR operator = OPERATOR.GREATER_OR_EQUAL; Object value = 24; KiePMMLPredicateFactory.setSimplePredicateConstructor(generatedClassName, predicateName, constructorDeclaration, operator, value); Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>(); superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", predicateName))); superInvocationExpressionsMap.put(2, new NameExpr(operator.getClass().getCanonicalName() + "." + operator.name())); Map<String, Expression> assignExpressionMap = new HashMap<>(); assignExpressionMap.put("value", valueAssignExpr.getValue().asNameExpr()); assertTrue(commonEvaluateConstructor(constructorDeclaration, generatedClassName, superInvocationExpressionsMap, assignExpressionMap)); assertEquals("24", valueAssignExpr.getValue().asNameExpr().toString()); }
@Test(expected = IllegalArgumentException.class) public void setSimplePredicateConstructorIsMissing() { init(KIE_PMML_SIMPLE_PREDICATE_TEMPLATE_JAVA, KIE_PMML_SIMPLE_PREDICATE_TEMPLATE); AssignExpr valueAssignExpr = assignExprs.stream().filter(assignExpr -> assignExpr.getTarget().asNameExpr().getNameAsString().equals("value")) .findFirst() .orElseThrow(() -> new RuntimeException("Missing \"value\" assign variable")); assertTrue(valueAssignExpr.getValue() instanceof NullLiteralExpr); String generatedClassName = "GENERATEDCLASSNAME"; String predicateName = "PREDICATENAME"; OPERATOR operator = OPERATOR.IS_MISSING; Object value = "VALUE"; KiePMMLPredicateFactory.setSimplePredicateConstructor(generatedClassName, predicateName, constructorDeclaration, operator, value); }