use of org.drools.drl.ast.descr.ExistsDescr 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.ExistsDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseExists.
@Test
public void testParseExists() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseExists.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
Object existdescr = obj.getLhs().getDescrs().get(0);
assertTrue(existdescr instanceof ExistsDescr);
Object patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
assertTrue(patternDescriptor instanceof PatternDescr);
assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Person");
Object notDescr = obj.getLhs().getDescrs().get(1);
assertEquals(notDescr.getClass().getName(), NotDescr.class.getName());
existdescr = ((NotDescr) notDescr).getDescrs().get(0);
patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
assertTrue(patternDescriptor instanceof PatternDescr);
assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Cheese");
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class CEDescrBuilderImpl method exists.
/**
* {@inheritDoc}
*/
public CEDescrBuilder<CEDescrBuilder<P, T>, ExistsDescr> exists() {
CEDescrBuilder<CEDescrBuilder<P, T>, ExistsDescr> exists = new CEDescrBuilderImpl<CEDescrBuilder<P, T>, ExistsDescr>(this, new ExistsDescr());
((ConditionalElementDescr) descr).addDescr(exists.getDescr());
return exists;
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class RuleParserTest method testPluggableOperators2.
@Test
public void testPluggableOperators2() throws Exception {
final String text = "rule \"tt\"\n" + " dialect \"mvel\"\n" + "when\n" + " exists (TelephoneCall( this finishes [1m] \"25-May-2011\" ))\n" + "then\n" + "end";
PatternDescr pattern = (PatternDescr) ((ExistsDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0)).getDescrs().get(0);
assertEquals("TelephoneCall", pattern.getObjectType());
ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("this finishes [1m] \"25-May-2011\"", constr.getText());
}
use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testExists.
@Test
public void testExists() throws Exception {
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
// Make sure we can't accessa variable bound inside the not node
RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
assertTrue(builder.hasErrors());
builder = new KnowledgeBuilderImpl();
rule = createRule(new ExistsDescr(), builder, "");
assertEquals(0, builder.getErrors().getErrors().length);
final GroupElement lhs = rule.getLhs();
assertLength(1, lhs.getChildren());
final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
assertLength(1, exists.getChildren());
final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
Aggregations