use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testBindingComposite.
@Test
public void testBindingComposite() throws Exception {
final String text = "rule X when Person( $name : name == \"Bob\" || $loc : location == \"Montreal\" ) then end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertFalse(pattern.isUnification());
// assertEquals( 2,
// pattern.getDescrs().size() );
// BindingDescr bindingDescr = pattern.getDescrs().get( 0 );
// assertEquals( "$name",
// bindingDescr.getVariable() );
// assertEquals( "name",
// bindingDescr.getExpression() );
// assertFalse( bindingDescr.isUnification() );
//
// bindingDescr = pattern.getDescrs().get( 1 );
// assertEquals( "$loc",
// bindingDescr.getVariable() );
// assertEquals( "location",
// bindingDescr.getExpression() );
// assertFalse( bindingDescr.isUnification() );
// embedded bindings are extracted at compile time
List<?> constraints = pattern.getDescrs();
assertEquals(1, constraints.size());
assertEquals("$name : name == \"Bob\" || $loc : location == \"Montreal\"", ((ExprConstraintDescr) constraints.get(0)).getExpression());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testAccumulateMnemonic2.
@Test
public void testAccumulateMnemonic2() throws Exception {
String drl = "package org.drools.compiler\n" + "rule \"Accumulate 1\"\n" + "when\n" + " Number() from acc( Cheese( $price : price ),\n" + " average( $price ) )\n" + "then\n" + "end\n";
final PackageDescr pkg = (PackageDescr) parse("compilationUnit", drl);
assertEquals(1, pkg.getRules().size());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Number", out.getObjectType());
AccumulateDescr accum = (AccumulateDescr) out.getSource();
assertTrue(accum.isExternalFunction());
List<AccumulateFunctionCallDescr> functions = accum.getFunctions();
assertEquals(1, functions.size());
assertEquals("average", functions.get(0).getFunction());
assertEquals("$price", functions.get(0).getParams()[0]);
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Cheese", pattern.getObjectType());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testRuleParseLhs3.
@Test
public void testRuleParseLhs3() throws Exception {
final String text = "rule X when (or\nnot Person()\n(and Cheese()\nMeat()\nWine())) then end";
AndDescr pattern = ((RuleDescr) parse("rule", text)).getLhs();
assertEquals(1, pattern.getDescrs().size());
OrDescr or = (OrDescr) pattern.getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
NotDescr not = (NotDescr) or.getDescrs().get(0);
AndDescr and = (AndDescr) or.getDescrs().get(1);
assertEquals(1, not.getDescrs().size());
PatternDescr person = (PatternDescr) not.getDescrs().get(0);
assertEquals("Person", person.getObjectType());
assertEquals(3, and.getDescrs().size());
PatternDescr cheese = (PatternDescr) and.getDescrs().get(0);
assertEquals("Cheese", cheese.getObjectType());
PatternDescr meat = (PatternDescr) and.getDescrs().get(1);
assertEquals("Meat", meat.getObjectType());
PatternDescr wine = (PatternDescr) and.getDescrs().get(2);
assertEquals("Wine", wine.getObjectType());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testRuleWithLHSNesting.
@Test
public void testRuleWithLHSNesting() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_nested_LHS.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
assertEquals("test", rule.getName());
AndDescr lhs = rule.getLhs();
assertNotNull(lhs);
assertEquals(2, lhs.getDescrs().size());
PatternDescr a = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("A", a.getObjectType());
OrDescr or = (OrDescr) lhs.getDescrs().get(1);
assertEquals(3, or.getDescrs().size());
AndDescr and1 = (AndDescr) or.getDescrs().get(0);
assertEquals(2, and1.getDescrs().size());
PatternDescr b = (PatternDescr) and1.getDescrs().get(0);
PatternDescr c = (PatternDescr) and1.getDescrs().get(1);
assertEquals("B", b.getObjectType());
assertEquals("C", c.getObjectType());
AndDescr and2 = (AndDescr) or.getDescrs().get(1);
assertEquals(2, and2.getDescrs().size());
PatternDescr d = (PatternDescr) and2.getDescrs().get(0);
PatternDescr e = (PatternDescr) and2.getDescrs().get(1);
assertEquals("D", d.getObjectType());
assertEquals("E", e.getObjectType());
AndDescr and3 = (AndDescr) or.getDescrs().get(2);
assertEquals(2, and3.getDescrs().size());
PatternDescr f = (PatternDescr) and3.getDescrs().get(0);
PatternDescr g = (PatternDescr) and3.getDescrs().get(1);
assertEquals("F", f.getObjectType());
assertEquals("G", g.getObjectType());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testEntryPoint.
@Test
public void testEntryPoint() 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());
}
Aggregations