KiePMMLDescrLhsFactory { protected void declareConstraintsXor(final List<KiePMMLFieldOperatorValue> xorConstraints) { if (xorConstraints.size() != 2) { throw new KiePMMLException("Expecting two fields for XOR constraints, retrieved " + xorConstraints.size()); } final String[] keys = new String[xorConstraints.size()]; final String[] values = new String[xorConstraints.size()]; for (int i = 0; i < keys.length; i++) { keys[i] = xorConstraints.get(i).getName(); values[i] = xorConstraints.get(i).getConstraintsAsString(); } final CEDescrBuilder<CEDescrBuilder<RuleDescrBuilder, AndDescr>, AndDescr> andBuilder = builder.and(); final CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<RuleDescrBuilder, AndDescr>, AndDescr>, NotDescr>, AndDescr> notBuilder = andBuilder.not().and(); commonDeclarePatternWithConstraint(notBuilder, keys[0], values[0]); commonDeclarePatternWithConstraint(notBuilder, keys[1], values[1]); final CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<RuleDescrBuilder, AndDescr>, AndDescr>, ExistsDescr>, OrDescr> existsBuilder = andBuilder.exists().or(); commonDeclarePatternWithConstraint(existsBuilder, keys[0], values[0]); commonDeclarePatternWithConstraint(existsBuilder.or(), keys[1], values[1]); } private  KiePMMLDescrLhsFactory(final CEDescrBuilder<RuleDescrBuilder, AndDescr> builder); static KiePMMLDescrLhsFactory factory(final CEDescrBuilder<RuleDescrBuilder, AndDescr> builder); void declareLhs(final KiePMMLDroolsRule rule);  }
@Test public void declareConstraintsXor() { String temperatureField = "TEMPERATURE"; final List<KiePMMLFieldOperatorValue> xorConstraints = Arrays .asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null)); KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsXor(xorConstraints); assertNotNull(lhsBuilder.getDescr()); assertNotNull(lhsBuilder.getDescr().getDescrs()); assertEquals(1, lhsBuilder.getDescr().getDescrs().size()); assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr); AndDescr rootAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0); assertEquals(2, rootAndDescr.getDescrs().size()); assertTrue(rootAndDescr.getDescrs().get(0) instanceof NotDescr); assertTrue(rootAndDescr.getDescrs().get(1) instanceof ExistsDescr); NotDescr notDescr = (NotDescr) rootAndDescr.getDescrs().get(0); assertEquals(1, notDescr.getDescrs().size()); assertTrue(notDescr.getDescrs().get(0) instanceof AndDescr); AndDescr notAndDescr = (AndDescr) notDescr.getDescrs().get(0); assertTrue(notAndDescr.getDescrs().get(0) instanceof PatternDescr); assertTrue(notAndDescr.getDescrs().get(1) instanceof PatternDescr); PatternDescr patternDescr = (PatternDescr) notAndDescr.getDescrs().get(0); assertEquals(temperatureField, patternDescr.getObjectType()); assertNull(patternDescr.getIdentifier()); assertTrue(patternDescr.getConstraint() instanceof AndDescr); AndDescr andDescr = (AndDescr) patternDescr.getConstraint(); assertEquals(1, andDescr.getDescrs().size()); assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr); ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0); assertFalse(exprConstraintDescr.isNegated()); assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType()); String expected = "value < 35"; assertEquals(expected, exprConstraintDescr.getExpression()); patternDescr = (PatternDescr) notAndDescr.getDescrs().get(1); assertEquals(temperatureField, patternDescr.getObjectType()); assertNull(patternDescr.getIdentifier()); assertTrue(patternDescr.getConstraint() instanceof AndDescr); andDescr = (AndDescr) patternDescr.getConstraint(); assertEquals(1, andDescr.getDescrs().size()); assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr); exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0); assertFalse(exprConstraintDescr.isNegated()); assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType()); expected = "value > 85"; assertEquals(expected, exprConstraintDescr.getExpression()); ExistsDescr existsDescr = (ExistsDescr) rootAndDescr.getDescrs().get(1); assertEquals(1, existsDescr.getDescrs().size()); assertTrue(existsDescr.getDescrs().get(0) instanceof OrDescr); OrDescr existsOrDescr = (OrDescr) existsDescr.getDescrs().get(0); assertEquals(2, existsOrDescr.getDescrs().size()); assertTrue(existsOrDescr.getDescrs().get(0) instanceof PatternDescr); assertTrue(existsOrDescr.getDescrs().get(1) instanceof OrDescr); patternDescr = (PatternDescr) existsOrDescr.getDescrs().get(0); assertEquals(temperatureField, patternDescr.getObjectType()); assertNull(patternDescr.getIdentifier()); assertTrue(patternDescr.getConstraint() instanceof AndDescr); andDescr = (AndDescr) patternDescr.getConstraint(); assertEquals(1, andDescr.getDescrs().size()); assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr); exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0); assertFalse(exprConstraintDescr.isNegated()); assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType()); expected = "value < 35"; assertEquals(expected, exprConstraintDescr.getExpression()); OrDescr nestedOrDescr = (OrDescr) existsOrDescr.getDescrs().get(1); assertEquals(1, nestedOrDescr.getDescrs().size()); assertTrue(nestedOrDescr.getDescrs().get(0) instanceof PatternDescr); patternDescr = (PatternDescr) nestedOrDescr.getDescrs().get(0); assertEquals(temperatureField, patternDescr.getObjectType()); assertNull(patternDescr.getIdentifier()); assertTrue(patternDescr.getConstraint() instanceof AndDescr); andDescr = (AndDescr) patternDescr.getConstraint(); assertEquals(1, andDescr.getDescrs().size()); assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr); exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0); assertFalse(exprConstraintDescr.isNegated()); assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType()); expected = "value > 85"; assertEquals(expected, exprConstraintDescr.getExpression()); }