Search in sources :

Example 6 with AndDescr

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

the class RuleParserTest method testNotExistWithBrackets.

@Test
public void testNotExistWithBrackets() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertEquals(2, lhs.getDescrs().size());
    final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
    assertEquals(1, not.getDescrs().size());
    final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
    assertEquals(1, ex.getDescrs().size());
    final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
    assertEquals("Foo", exPattern.getObjectType());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) 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) Test(org.junit.Test)

Example 7 with AndDescr

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

the class RuleParserTest method testSimpleConstraint.

@Test
public void testSimpleConstraint() throws Exception {
    String source = "package com.sample  rule test  when  Cheese( type == 'stilton', price > 10 )  then  end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertEquals("com.sample", pkg.getName());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr constraint = (AndDescr) pattern.getConstraint();
    assertEquals(2, constraint.getDescrs().size());
    assertEquals("type == \"stilton\"", constraint.getDescrs().get(0).toString());
    assertEquals("price > 10", constraint.getDescrs().get(1).toString());
}
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) Test(org.junit.Test)

Example 8 with AndDescr

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

the class RuleParserTest method testMultipleRules.

@Test
public void testMultipleRules() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "multiple_rules.drl");
    final List<RuleDescr> rules = pkg.getRules();
    assertEquals(2, rules.size());
    final RuleDescr rule0 = rules.get(0);
    assertEquals("Like Stilton", rule0.getName());
    final RuleDescr rule1 = rules.get(1);
    assertEquals("Like Cheddar", rule1.getName());
    // checkout the first rule
    AndDescr lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule0.getConsequence());
    // Check first pattern
    PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.getObjectType());
    // checkout the second rule
    lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule1.getConsequence());
    // Check first pattern
    first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.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) Test(org.junit.Test)

Example 9 with AndDescr

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

Example 10 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr 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)

Aggregations

AndDescr (org.drools.compiler.lang.descr.AndDescr)79 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)54 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)52 Test (org.junit.Test)52 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)39 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)27 Cheese (org.drools.compiler.Cheese)13 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)12 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)12 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)8 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)7 NotDescr (org.drools.compiler.lang.descr.NotDescr)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 List (java.util.List)4 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)4 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)4