Search in sources :

Example 41 with RuleDescr

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

the class RuleParserTest method testForall.

@Test
public void testForall() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forall.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final ForallDescr forall = (ForallDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, forall.getDescrs().size());
    final PatternDescr pattern = forall.getBasePattern();
    assertEquals("Person", pattern.getObjectType());
    final List<BaseDescr> remaining = forall.getRemainingPatterns();
    assertEquals(1, remaining.size());
    final PatternDescr cheese = (PatternDescr) remaining.get(0);
    assertEquals("Cheese", cheese.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ForallDescr(org.drools.compiler.lang.descr.ForallDescr) Test(org.junit.Test)

Example 42 with RuleDescr

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

the class RuleParserTest method testAccumulateReverse.

@Test
public void testAccumulateReverse() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateReverse.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
    assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
    assertEqualsIgnoreWhitespace("x--;", accum.getReverseCode());
    assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
    assertFalse(accum.isExternalFunction());
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Person", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Example 43 with RuleDescr

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

the class RuleParserTest method testNotWithConstraint.

@Test
public void testNotWithConstraint() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_with_constraint.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("$likes:like", fieldBinding.getExpression());
    final NotDescr not = (NotDescr) rule.getLhs().getDescrs().get(1);
    pattern = (PatternDescr) not.getDescrs().get(0);
    final ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("type == $likes", fld.getExpression());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 44 with RuleDescr

use of org.drools.compiler.lang.descr.RuleDescr 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 45 with RuleDescr

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

RuleDescr (org.drools.compiler.lang.descr.RuleDescr)185 Test (org.junit.Test)145 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)104 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)103 AndDescr (org.drools.compiler.lang.descr.AndDescr)54 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)37 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)24 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)20 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.compiler.Cheese)16 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 OrDescr (org.drools.compiler.lang.descr.OrDescr)14 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)14 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)13 Pattern (org.drools.core.rule.Pattern)13 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)12 List (java.util.List)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11