Search in sources :

Example 41 with PatternDescr

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

the class RuleParserTest method testRuleExtends.

@Test
public void testRuleExtends() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_Extends.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    assertTrue(rule.getParentName() != null);
    assertEquals("rule1", rule.getParentName());
    AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo", pattern.getObjectType());
    assertEquals("$foo", pattern.getIdentifier());
}
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 42 with PatternDescr

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

the class RuleParserTest method testAccumulateWithNestedFrom.

@Test
public void testAccumulateWithNestedFrom() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_with_nested_from.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 accumulate = (AccumulateDescr) out.getSource();
    PatternDescr person = (PatternDescr) accumulate.getInputPattern();
    assertEquals("Person", person.getObjectType());
    final CollectDescr collect2 = (CollectDescr) person.getSource();
    final PatternDescr people = collect2.getInputPattern();
    assertEquals("People", people.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) CollectDescr(org.drools.compiler.lang.descr.CollectDescr) 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 PatternDescr

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

the class RuleParserTest method testLhsSemicolonDelim.

@Test
public void testLhsSemicolonDelim() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "lhs_semicolon_delim.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // System.err.println( lhs.getDescrs() );
    // 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());
    // LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
    AndDescr and = (AndDescr) first.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertNotNull(fld);
    assertEquals("a==3", fld.getExpression());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    assertEquals(1, second.getDescrs().size());
    final ExprConstraintDescr fieldBindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
    assertEquals("a4:a==4", fieldBindingDescr.getExpression());
    // Check third pattern
    final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
    assertNull(third.getIdentifier());
    assertEquals("Baz", third.getObjectType());
    assertEqualsIgnoreWhitespace("if ( a == b ) { " + "  assert( foo3 );" + "} else {" + "  retract( foo4 );" + "}" + "  System.out.println( a4 );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 44 with PatternDescr

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

the class RuleParserTest method testAccumulateMnemonic.

@Test
public void testAccumulateMnemonic() throws Exception {
    String drl = "package org.drools.compiler\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     acc( Cheese( $price : price ),\n" + "          $a1 : average( $price ) )\n" + "then\n" + "end\n";
    final PackageDescr pkg = (PackageDescr) parse("compilationUnit", drl);
    assertEquals(1, pkg.getRules().size());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Object", out.getObjectType());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(1, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$a1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) Test(org.junit.Test)

Example 45 with PatternDescr

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

the class RuleParserTest method testWithPredicate.

@Test
public void testWithPredicate() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_predicate.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr and = (AndDescr) col.getConstraint();
    assertEquals(2, and.getDescrs().size());
    final ExprConstraintDescr field = (ExprConstraintDescr) col.getDescrs().get(0);
    final ExprConstraintDescr pred = (ExprConstraintDescr) and.getDescrs().get(1);
    assertEquals("$age2:age", field.getExpression());
    assertEqualsIgnoreWhitespace("$age2 == $age1+2", pred.getExpression());
}
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) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) 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