Search in sources :

Example 6 with OrDescr

use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.

the class RuleParserTest method testNestedCEs.

@Test
public void testNestedCEs() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "nested_conditional_elements.drl");
    assertNotNull(rule);
    final AndDescr root = rule.getLhs();
    final NotDescr not1 = (NotDescr) root.getDescrs().get(0);
    final AndDescr and1 = (AndDescr) not1.getDescrs().get(0);
    final PatternDescr state = (PatternDescr) and1.getDescrs().get(0);
    final NotDescr not2 = (NotDescr) and1.getDescrs().get(1);
    final AndDescr and2 = (AndDescr) not2.getDescrs().get(0);
    final PatternDescr person = (PatternDescr) and2.getDescrs().get(0);
    final PatternDescr cheese = (PatternDescr) and2.getDescrs().get(1);
    final PatternDescr person2 = (PatternDescr) root.getDescrs().get(1);
    final OrDescr or = (OrDescr) root.getDescrs().get(2);
    final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(0);
    final PatternDescr cheese3 = (PatternDescr) or.getDescrs().get(1);
    assertEquals(state.getObjectType(), "State");
    assertEquals(person.getObjectType(), "Person");
    assertEquals(cheese.getObjectType(), "Cheese");
    assertEquals(person2.getObjectType(), "Person");
    assertEquals(cheese2.getObjectType(), "Cheese");
    assertEquals(cheese3.getObjectType(), "Cheese");
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 7 with OrDescr

use of org.drools.compiler.lang.descr.OrDescr 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());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 8 with OrDescr

use of org.drools.compiler.lang.descr.OrDescr 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());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 9 with OrDescr

use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.

the class RuleParserTest method testRuleParseLhs.

@Test
public void testRuleParseLhs() throws Exception {
    final String text = "rule X when Person(age < 42, location==\"atlanta\") \nor\nPerson(name==\"bob\") then end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    AndDescr lhs = rule.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    assertEquals(2, ((OrDescr) lhs.getDescrs().get(0)).getDescrs().size());
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 10 with OrDescr

use of org.drools.compiler.lang.descr.OrDescr in project drools by kiegroup.

the class RuleParserTest method testOrWithBinding.

/**
 * test basic foo : Fact() || Fact() stuff
 */
@Test
public void testOrWithBinding() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr leftPattern = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", leftPattern.getObjectType());
    assertEquals("foo", leftPattern.getIdentifier());
    final PatternDescr rightPattern = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Person", rightPattern.getObjectType());
    assertEquals("foo", rightPattern.getIdentifier());
    final PatternDescr cheeseDescr = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Cheese", cheeseDescr.getObjectType());
    assertEquals(null, cheeseDescr.getIdentifier());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Aggregations

OrDescr (org.drools.compiler.lang.descr.OrDescr)24 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)14 Test (org.junit.Test)14 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)11 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)10 AndDescr (org.drools.compiler.lang.descr.AndDescr)8 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)7 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)5 NotDescr (org.drools.compiler.lang.descr.NotDescr)4 CommonToken (org.antlr.runtime.CommonToken)3 Token (org.antlr.runtime.Token)3 AccumulateDescrBuilder (org.drools.compiler.lang.api.AccumulateDescrBuilder)3 AnnotatedDescrBuilder (org.drools.compiler.lang.api.AnnotatedDescrBuilder)3 AnnotationDescrBuilder (org.drools.compiler.lang.api.AnnotationDescrBuilder)3 AttributeDescrBuilder (org.drools.compiler.lang.api.AttributeDescrBuilder)3 BehaviorDescrBuilder (org.drools.compiler.lang.api.BehaviorDescrBuilder)3 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)3 CollectDescrBuilder (org.drools.compiler.lang.api.CollectDescrBuilder)3 ConditionalBranchDescrBuilder (org.drools.compiler.lang.api.ConditionalBranchDescrBuilder)3 DeclareDescrBuilder (org.drools.compiler.lang.api.DeclareDescrBuilder)3