use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testLiteralBoolAndNegativeNumbersRule.
@Test
public void testLiteralBoolAndNegativeNumbersRule() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "literal_bool_and_negative.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
assertNotNull(rule.getLhs());
assertEqualsIgnoreWhitespace("cons();", (String) rule.getConsequence());
final AndDescr lhs = rule.getLhs();
assertEquals(3, lhs.getDescrs().size());
PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
assertEquals(1, pattern.getConstraint().getDescrs().size());
AndDescr fieldAnd = (AndDescr) pattern.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertEquals("bar == false", fld.getExpression());
pattern = (PatternDescr) lhs.getDescrs().get(1);
assertEquals(1, pattern.getConstraint().getDescrs().size());
fieldAnd = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertEquals("boo > -42", fld.getText());
pattern = (PatternDescr) lhs.getDescrs().get(2);
assertEquals(1, pattern.getConstraint().getDescrs().size());
fieldAnd = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertEquals("boo > -42.42", fld.getText());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testEntryPoint2.
@Test
public void testEntryPoint2() throws Exception {
final String text = "rule X when StockTick( symbol==\"ACME\") from entry-point \"StreamA\" then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals(1, pattern.getDescrs().size());
ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("symbol==\"ACME\"", fcd.getExpression());
assertNotNull(pattern.getSource());
EntryPointDescr entry = (EntryPointDescr) pattern.getSource();
assertEquals("StreamA", entry.getEntryId());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testSimpleRule.
@Test
public void testSimpleRule() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
assertEquals(22, rule.getConsequenceLine());
assertEquals(2, rule.getConsequencePattern());
final AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(3, lhs.getDescrs().size());
// Check first pattern
final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("foo3", first.getIdentifier());
assertEquals("Bar", first.getObjectType());
assertEquals(1, first.getConstraint().getDescrs().size());
AndDescr fieldAnd = (AndDescr) first.getConstraint();
ExprConstraintDescr constraint = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertNotNull(constraint);
assertEquals("a==3", constraint.getExpression());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
// no constraints, only a binding
fieldAnd = (AndDescr) second.getConstraint();
assertEquals(1, fieldAnd.getDescrs().size());
final ExprConstraintDescr binding = (ExprConstraintDescr) second.getConstraint().getDescrs().get(0);
assertEquals("a4:a==4", binding.getExpression());
// Check third pattern
final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
assertNull(third.getIdentifier());
assertEquals("Baz", third.getObjectType());
assertEqualsIgnoreWhitespace("if ( a == b ) { " + " assert( foo3 );" + "} else {" + " retract( foo4 );" + "}" + " System.out.println( a4 );", (String) rule.getConsequence());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testRestrictionsMultiple.
@Test
public void testRestrictionsMultiple() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "restrictions_test.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEqualsIgnoreWhitespace("consequence();", (String) rule.getConsequence());
assertEquals("simple_rule", rule.getName());
assertEquals(2, rule.getLhs().getDescrs().size());
// The first pattern, with 2 restrictions on a single field (plus a
// connective)
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(1, pattern.getConstraint().getDescrs().size());
AndDescr and = (AndDescr) pattern.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertEquals("age > 30 && < 40", fld.getExpression());
// the second col, with 2 fields, the first with 2 restrictions, the
// second field with one
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Vehicle", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
and = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertEquals("type == \"sedan\" || == \"wagon\"", fld.getExpression());
// now the second field
fld = (ExprConstraintDescr) and.getDescrs().get(1);
assertEquals("age < 3", fld.getExpression());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testAndOrRules.
/**
* Test that explicit "&&", "||" works as expected
*/
@Test
public void testAndOrRules() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "and_or_rule.drl");
assertNotNull(pkg);
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("simple_rule", rule.getName());
// we will have 3 children under the main And node
final AndDescr and = rule.getLhs();
assertEquals(3, and.getDescrs().size());
PatternDescr left = (PatternDescr) and.getDescrs().get(0);
PatternDescr right = (PatternDescr) and.getDescrs().get(1);
assertEquals("Person", left.getObjectType());
assertEquals("Cheese", right.getObjectType());
assertEquals(1, left.getConstraint().getDescrs().size());
ExprConstraintDescr fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
assertEquals("name == \"mark\"", fld.getExpression());
assertEquals(1, right.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
assertEquals("type == \"stilton\"", fld.getExpression());
// now the "||" part
final OrDescr or = (OrDescr) and.getDescrs().get(2);
assertEquals(2, or.getDescrs().size());
left = (PatternDescr) or.getDescrs().get(0);
right = (PatternDescr) or.getDescrs().get(1);
assertEquals("Person", left.getObjectType());
assertEquals("Cheese", right.getObjectType());
assertEquals(1, left.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
assertEquals("name == \"mark\"", fld.getExpression());
assertEquals(1, right.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
assertEquals("type == \"stilton\"", fld.getExpression());
assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" );", (String) rule.getConsequence());
}
Aggregations