Search in sources :

Example 31 with PatternDescr

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

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

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

the class RuleParserTest method testConstraintOrConnective.

@Test
public void testConstraintOrConnective() throws Exception {
    final String text = "rule X when Person( age < 42 || location==\"atlanta\") then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("age < 42 || location==\"atlanta\"", fcd.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 34 with PatternDescr

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

Example 35 with PatternDescr

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

the class RuleParserTest method testSimpleMethodCallWithFrom.

@Test
public void testSimpleMethodCallWithFrom() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_SimpleMethodCallWithFrom.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final FromDescr from = (FromDescr) pattern.getSource();
    final MVELExprDescr method = (MVELExprDescr) from.getDataSource();
    assertEquals("something.doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", method.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr) Test(org.junit.Test)

Aggregations

PatternDescr (org.drools.compiler.lang.descr.PatternDescr)150 Test (org.junit.Test)122 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)84 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)56 AndDescr (org.drools.compiler.lang.descr.AndDescr)52 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)19 Cheese (org.drools.compiler.Cheese)14 FromDescr (org.drools.compiler.lang.descr.FromDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 NotDescr (org.drools.compiler.lang.descr.NotDescr)11 OrDescr (org.drools.compiler.lang.descr.OrDescr)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 Element (org.w3c.dom.Element)9 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 InputStreamReader (java.io.InputStreamReader)7 List (java.util.List)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7