Search in sources :

Example 21 with OrDescr

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

the class RuleParserTest method testBracketsPrecedence.

/**
 */
@Test
public void testBracketsPrecedence() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "brackets_precedence.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    final AndDescr rootAnd = (AndDescr) rule.getLhs();
    assertEquals(2, rootAnd.getDescrs().size());
    final OrDescr leftOr = (OrDescr) rootAnd.getDescrs().get(0);
    assertEquals(2, leftOr.getDescrs().size());
    final NotDescr not = (NotDescr) leftOr.getDescrs().get(0);
    final PatternDescr foo1 = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Foo", foo1.getObjectType());
    final PatternDescr foo2 = (PatternDescr) leftOr.getDescrs().get(1);
    assertEquals("Foo", foo2.getObjectType());
    final OrDescr rightOr = (OrDescr) rootAnd.getDescrs().get(1);
    assertEquals(2, rightOr.getDescrs().size());
    final PatternDescr shoes = (PatternDescr) rightOr.getDescrs().get(0);
    assertEquals("Shoes", shoes.getObjectType());
    final PatternDescr butt = (PatternDescr) rightOr.getDescrs().get(1);
    assertEquals("Butt", butt.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) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 22 with OrDescr

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

the class RuleParserTest method testExpanderLineSpread.

@Test
public void testExpanderLineSpread() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkg = parser.parse(this.getReader("expander_spread_lines.dslr"), this.getReader("complex.dsl"));
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    assertNotNull((String) rule.getConsequence());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 23 with OrDescr

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

the class RuleParserTest method testOrNesting.

@Test
public void testOrNesting() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_nesting.drl");
    assertNotNull(pkg);
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("simple_rule", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr first = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", first.getObjectType());
    final AndDescr and = (AndDescr) or.getDescrs().get(1);
    assertEquals(2, and.getDescrs().size());
    final PatternDescr left = (PatternDescr) and.getDescrs().get(0);
    assertEquals("Person", left.getObjectType());
    final PatternDescr right = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Cheese", right.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 24 with OrDescr

use of org.drools.compiler.lang.descr.OrDescr 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());
}
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) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) 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