Search in sources :

Example 46 with PatternDescr

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

the class RuleParserTest method testAccumulateExternalFunction.

@Test
public void testAccumulateExternalFunction() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateExternalFunction.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("$age", accum.getFunctions().get(0).getParams()[0]);
    assertEqualsIgnoreWhitespace("average", accum.getFunctions().get(0).getFunction());
    assertTrue(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 47 with PatternDescr

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

the class RuleParserTest method testEvalMultiple.

@Test
public void testEvalMultiple() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "eval_multiple.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(4, rule.getLhs().getDescrs().size());
    final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(0);
    assertEqualsIgnoreWhitespace("abc(\"foo\") + 5", (String) eval.getContent());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Foo", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 48 with PatternDescr

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

the class RuleParserTest method testUnificationBinding.

@Test
public void testUnificationBinding() throws Exception {
    final String text = "rule X when $p := Person( $name := name, $loc : location ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("$p", pattern.getIdentifier());
    assertTrue(pattern.isUnification());
    assertEquals(2, pattern.getDescrs().size());
    ExprConstraintDescr bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("$name := name", bindingDescr.getExpression());
    bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get(1);
    assertEquals("$loc : location", bindingDescr.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 49 with PatternDescr

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

the class RuleParserTest method testConstraintAndConnective.

@Test
public void testConstraintAndConnective() 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 50 with PatternDescr

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

the class RuleParserTest method testLineNumberInAST.

@Test
public void testLineNumberInAST() throws Exception {
    // also see testSimpleExpander to see how this works with an expander
    // (should be the same).
    final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    assertEquals(22, rule.getConsequenceLine());
    assertEquals(2, rule.getConsequencePattern());
    final AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // Check first pattern
    final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo3", first.getIdentifier());
    assertEquals("Bar", first.getObjectType());
    assertEquals(1, first.getConstraint().getDescrs().size());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
    assertEquals("Baz", third.getObjectType());
    assertEquals(19, first.getLine());
    assertEquals(20, second.getLine());
    assertEquals(21, third.getLine());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) 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