use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsXor.
@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);
// "Not" construct
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());
// "Exists" construct
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());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintNotIn.
@Test
public void declareConstraintNotIn() {
List<Object> values = Arrays.asList("3", "8.5");
String patternType = "INPUT2";
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintNotIn(patternType, values);
final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
assertNotNull(descrs);
assertEquals(1, descrs.size());
assertTrue(descrs.get(0) instanceof NotDescr);
NotDescr notDescr = (NotDescr) descrs.get(0);
assertEquals(1, notDescr.getDescrs().size());
assertTrue(notDescr.getDescrs().get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
assertEquals(patternType, 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 in (3, 8.5)";
assertEquals(expected, exprConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsOr.
@Test
public void declareConstraintsOr() {
String temperatureField = "TEMPERATURE";
String humidityField = "HUMIDITY";
List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.or());
assertNotNull(lhsBuilder.getDescr());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof OrDescr);
OrDescr baseOrDescr = (OrDescr) lhsBuilder.getDescr().getDescrs().get(0);
final List<BaseDescr> descrs = baseOrDescr.getDescrs();
assertNotNull(descrs);
assertEquals(2, descrs.size());
// First KiePMMLFieldOperatorValue
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.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());
// Second KiePMMLFieldOperatorValue
assertTrue(descrs.get(1) instanceof PatternDescr);
patternDescr = (PatternDescr) descrs.get(1);
assertEquals(humidityField, 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());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintIn.
@Test
public void declareConstraintIn() {
List<Object> values = Arrays.asList("-5", "0.5", "1", "10");
String patternType = "INPUT1";
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintIn(patternType, values);
final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
assertNotNull(descrs);
assertEquals(1, descrs.size());
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.get(0);
assertEquals(patternType, 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 in (-5, 0.5, 1, 10)";
assertEquals(expected, exprConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class SolversTest method testNotAnd.
/**
* <pre>
* when
* Foo( r && r2 )
* and
* not Foo( r3 && r4 )
* </pre>
*
* result:<br>
* r && r2<br>
* r3 && r4
*/
@Test
public void testNotAnd() {
PackageDescr descr = new PackageDescr("testPackage");
RulePackage rulePackage = new RulePackage(descr);
rulePackage.setName("testPackage");
VerifierRule rule = new VerifierRule(descr, rulePackage, new HashMap<String, Object>());
rule.setName("testRule");
Pattern pattern = new Pattern(new PatternDescr(), rule);
Restriction r = LiteralRestriction.createRestriction(pattern, "");
Restriction r2 = LiteralRestriction.createRestriction(pattern, "");
Restriction r3 = LiteralRestriction.createRestriction(pattern, "");
Restriction r4 = LiteralRestriction.createRestriction(pattern, "");
Solvers solvers = new Solvers();
solvers.startRuleSolver(rule);
solvers.startOperator(OperatorDescrType.AND);
solvers.startPatternSolver(pattern);
solvers.startOperator(OperatorDescrType.AND);
solvers.addPatternComponent(r);
solvers.addPatternComponent(r2);
solvers.endOperator();
solvers.endPatternSolver();
solvers.startNot();
solvers.startPatternSolver(pattern);
solvers.startOperator(OperatorDescrType.AND);
solvers.addPatternComponent(r3);
solvers.addPatternComponent(r4);
solvers.endOperator();
solvers.endPatternSolver();
solvers.endNot();
solvers.endOperator();
solvers.endRuleSolver();
List<SubRule> list = solvers.getRulePossibilities();
assertEquals(1, list.size());
assertEquals(2, list.get(0).getItems().size());
List<Restriction> result = new ArrayList<Restriction>();
result.add(r);
result.add(r2);
List<Restriction> result2 = new ArrayList<Restriction>();
result2.add(r3);
result2.add(r4);
Object[] possibilies = list.get(0).getItems().toArray();
SubPattern p1 = (SubPattern) possibilies[0];
SubPattern p2 = (SubPattern) possibilies[1];
/*
* Order may change but it doesn't matter.
*/
if (p1.getItems().containsAll(result)) {
assertTrue(p2.getItems().containsAll(result2));
} else if (p1.getItems().containsAll(result2)) {
assertTrue(p2.getItems().containsAll(result));
} else {
fail("No items found.");
}
}
Aggregations