Search in sources :

Example 21 with OrDescr

use of org.drools.drl.ast.descr.OrDescr in project drools by kiegroup.

the class RuleParserTest method testOrBindingWithBrackets.

@Test
public void testOrBindingWithBrackets() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding_with_brackets.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    assertEquals(1, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    // first fact
    final PatternDescr firstFact = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", firstFact.getObjectType());
    assertEquals("foo", firstFact.getIdentifier());
    // second "option"
    final PatternDescr secondFact = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", secondFact.getObjectType());
    assertEquals("foo", secondFact.getIdentifier());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 22 with OrDescr

use of org.drools.drl.ast.descr.OrDescr in project drools by kiegroup.

the class RuleParserTest method testOrWithSpecialBind.

@Test
public void testOrWithSpecialBind() throws Exception {
    String source = "rule \"A and (B or C or D)\" \n" + "    when \n" + "        pdo1 : ParametricDataObject( paramID == 101, stringValue == \"1000\" ) and \n" + "        pdo2 :(ParametricDataObject( paramID == 101, stringValue == \"1001\" ) or \n" + "               ParametricDataObject( paramID == 101, stringValue == \"1002\" ) or \n" + "               ParametricDataObject( paramID == 101, stringValue == \"1003\" )) \n" + "    then \n" + "        System.out.println( \"Rule: A and (B or C or D) Fired. pdo1: \" + pdo1 +  \" pdo2: \"+ pdo2); \n" + "end\n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    AndDescr lhs = rule.getLhs();
    assertEquals(2, lhs.getDescrs().size());
    PatternDescr pdo1 = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("pdo1", pdo1.getIdentifier());
    OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(3, or.getDescrs().size());
    for (BaseDescr pdo2 : or.getDescrs()) {
        assertEquals("pdo2", ((PatternDescr) pdo2).getIdentifier());
    }
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 23 with OrDescr

use of org.drools.drl.ast.descr.OrDescr in project drools by kiegroup.

the class RuleParserTest method testOrBindingComplex.

/**
 * test basic foo : Fact() || Fact() stuff binding to an "or"
 */
@Test
public void testOrBindingComplex() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding_complex.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    assertEquals(1, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    // first fact
    final PatternDescr firstFact = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", firstFact.getObjectType());
    assertEquals("foo", firstFact.getIdentifier());
    // second "option"
    final PatternDescr secondFact = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Person", secondFact.getObjectType());
    assertEquals(1, secondFact.getConstraint().getDescrs().size());
    assertEquals("foo", secondFact.getIdentifier());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 24 with OrDescr

use of org.drools.drl.ast.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.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 25 with OrDescr

use of org.drools.drl.ast.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.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Aggregations

OrDescr (org.drools.drl.ast.descr.OrDescr)25 Test (org.junit.Test)16 RuleDescr (org.drools.drl.ast.descr.RuleDescr)14 PatternDescr (org.drools.drl.ast.descr.PatternDescr)13 AndDescr (org.drools.drl.ast.descr.AndDescr)10 PackageDescr (org.drools.drl.ast.descr.PackageDescr)10 BaseDescr (org.drools.drl.ast.descr.BaseDescr)8 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)5 NotDescr (org.drools.drl.ast.descr.NotDescr)4 CommonToken (org.antlr.runtime.CommonToken)3 Token (org.antlr.runtime.Token)3 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)3 AccumulateDescrBuilder (org.drools.drl.ast.dsl.AccumulateDescrBuilder)3 AnnotatedDescrBuilder (org.drools.drl.ast.dsl.AnnotatedDescrBuilder)3 AnnotationDescrBuilder (org.drools.drl.ast.dsl.AnnotationDescrBuilder)3 AttributeDescrBuilder (org.drools.drl.ast.dsl.AttributeDescrBuilder)3 BehaviorDescrBuilder (org.drools.drl.ast.dsl.BehaviorDescrBuilder)3 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)3 CollectDescrBuilder (org.drools.drl.ast.dsl.CollectDescrBuilder)3 ConditionalBranchDescrBuilder (org.drools.drl.ast.dsl.ConditionalBranchDescrBuilder)3