Search in sources :

Example 46 with PatternDescr

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

the class RuleParserTest method testAccumulateWithBindings.

@Test
public void testAccumulateWithBindings() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_with_bindings.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final AccumulateDescr accum = (AccumulateDescr) outPattern.getSource();
    assertEqualsIgnoreWhitespace("$counter", outPattern.getIdentifier());
    assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
    assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
    assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Person", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 47 with PatternDescr

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

the class RuleParserTest method testEndPosition.

@Test
public void testEndPosition() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_EndPosition.drl");
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(21, col.getLine());
    assertEquals(23, col.getEndLine());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 48 with PatternDescr

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

the class RuleParserTest method testRuleWithLHSNesting.

@Test
public void testRuleWithLHSNesting() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_nested_LHS.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    assertEquals("test", rule.getName());
    AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(2, lhs.getDescrs().size());
    PatternDescr a = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("A", a.getObjectType());
    OrDescr or = (OrDescr) lhs.getDescrs().get(1);
    assertEquals(3, or.getDescrs().size());
    AndDescr and1 = (AndDescr) or.getDescrs().get(0);
    assertEquals(2, and1.getDescrs().size());
    PatternDescr b = (PatternDescr) and1.getDescrs().get(0);
    PatternDescr c = (PatternDescr) and1.getDescrs().get(1);
    assertEquals("B", b.getObjectType());
    assertEquals("C", c.getObjectType());
    AndDescr and2 = (AndDescr) or.getDescrs().get(1);
    assertEquals(2, and2.getDescrs().size());
    PatternDescr d = (PatternDescr) and2.getDescrs().get(0);
    PatternDescr e = (PatternDescr) and2.getDescrs().get(1);
    assertEquals("D", d.getObjectType());
    assertEquals("E", e.getObjectType());
    AndDescr and3 = (AndDescr) or.getDescrs().get(2);
    assertEquals(2, and3.getDescrs().size());
    PatternDescr f = (PatternDescr) and3.getDescrs().get(0);
    PatternDescr g = (PatternDescr) and3.getDescrs().get(1);
    assertEquals("F", f.getObjectType());
    assertEquals("G", g.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 49 with PatternDescr

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

the class RuleParserTest method testWithEval.

@Test
public void testWithEval() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_eval.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(3, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Foo", pattern.getObjectType());
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Bar", pattern.getObjectType());
    final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(2);
    assertEqualsIgnoreWhitespace("abc(\"foo\")", (String) eval.getContent());
    assertEqualsIgnoreWhitespace("Kapow", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 50 with PatternDescr

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

the class RuleParserTest method testRestrictions.

@Test
public void testRestrictions() throws Exception {
    final String text = "rule X when Foo( bar > 1 || == 1 ) then end\n";
    AndDescr descrs = (AndDescr) ((RuleDescr) parse("rule", text)).getLhs();
    assertEquals(1, descrs.getDescrs().size());
    PatternDescr pat = (PatternDescr) descrs.getDescrs().get(0);
    ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get(0);
    assertEquals("bar > 1 || == 1", fieldConstr.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

PatternDescr (org.drools.drl.ast.descr.PatternDescr)162 Test (org.junit.Test)131 RuleDescr (org.drools.drl.ast.descr.RuleDescr)103 PackageDescr (org.drools.drl.ast.descr.PackageDescr)84 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)65 AndDescr (org.drools.drl.ast.descr.AndDescr)61 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)18 FromDescr (org.drools.drl.ast.descr.FromDescr)16 BaseDescr (org.drools.drl.ast.descr.BaseDescr)15 NotDescr (org.drools.drl.ast.descr.NotDescr)14 Cheese (org.drools.mvel.compiler.Cheese)14 OrDescr (org.drools.drl.ast.descr.OrDescr)13 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 Element (org.w3c.dom.Element)9 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 InputStreamReader (java.io.InputStreamReader)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)7 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)7