Search in sources :

Example 1 with OrDescr

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

Example 2 with OrDescr

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

the class RuleParserTest method testOrCE.

@Test
public void testOrCE() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_ce.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals("$p", person.getIdentifier());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Cheese", cheese1.getObjectType());
    assertEquals("$c", cheese1.getIdentifier());
    final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Cheese", cheese2.getObjectType());
    assertNull(cheese2.getIdentifier());
}
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 3 with OrDescr

use of org.drools.drl.ast.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.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 4 with OrDescr

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

the class KnowledgeBuilderTest method testOr.

@Test
public void testOr() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final RuleImpl rule = createRule(new OrDescr(), builder, "update(stilton);");
    assertLength(0, builder.getErrors().getErrors());
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement or = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, or.getChildren());
    final Pattern pattern = (Pattern) or.getChildren().get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 5 with OrDescr

use of org.drools.drl.ast.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.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) 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