use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testLhsSemicolonDelim.
@Test
public void testLhsSemicolonDelim() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "lhs_semicolon_delim.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
final AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(3, lhs.getDescrs().size());
// System.err.println( lhs.getDescrs() );
// 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());
// LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
AndDescr and = (AndDescr) first.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
assertNotNull(fld);
assertEquals("a==3", fld.getExpression());
// Check second pattern
final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
assertEquals("foo4", second.getIdentifier());
assertEquals("Bar", second.getObjectType());
assertEquals(1, second.getDescrs().size());
final ExprConstraintDescr fieldBindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
assertEquals("a4:a==4", fieldBindingDescr.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 testWithPredicate.
@Test
public void testWithPredicate() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_predicate.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
AndDescr and = (AndDescr) col.getConstraint();
assertEquals(2, and.getDescrs().size());
final ExprConstraintDescr field = (ExprConstraintDescr) col.getDescrs().get(0);
final ExprConstraintDescr pred = (ExprConstraintDescr) and.getDescrs().get(1);
assertEquals("$age2:age", field.getExpression());
assertEqualsIgnoreWhitespace("$age2 == $age1+2", pred.getExpression());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testUnificationBinding.
@Test
public void testUnificationBinding() throws Exception {
final String text = "rule X when $p := Person( $name := name, $loc : location ) then end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals("$p", pattern.getIdentifier());
assertTrue(pattern.isUnification());
assertEquals(2, pattern.getDescrs().size());
ExprConstraintDescr bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("$name := name", bindingDescr.getExpression());
bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get(1);
assertEquals("$loc : location", bindingDescr.getExpression());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testConstraintAndConnective.
@Test
public void testConstraintAndConnective() throws Exception {
final String text = "rule X when Person( age < 42 && location==\"atlanta\") then end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals(1, pattern.getDescrs().size());
ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("age < 42 && location==\"atlanta\"", fcd.getExpression());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testInfinityLiteral.
@Test
public void testInfinityLiteral() throws Exception {
final String text = "rule \"infinity\"\n" + "when\n" + " StockTick( this after[-*,*] $another )\n" + "then\n" + "end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals("StockTick", pattern.getObjectType());
ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("this after[-*,*] $another", constr.getText());
}
Aggregations