Search in sources :

Example 36 with PatternDescr

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

the class RuleParserTest method testBindingComposite.

@Test
public void testBindingComposite() throws Exception {
    final String text = "rule X when Person( $name : name == \"Bob\" || $loc : location == \"Montreal\" ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    assertFalse(pattern.isUnification());
    // assertEquals( 2,
    // pattern.getDescrs().size() );
    // BindingDescr bindingDescr = pattern.getDescrs().get( 0 );
    // assertEquals( "$name",
    // bindingDescr.getVariable() );
    // assertEquals( "name",
    // bindingDescr.getExpression() );
    // assertFalse( bindingDescr.isUnification() );
    // 
    // bindingDescr = pattern.getDescrs().get( 1 );
    // assertEquals( "$loc",
    // bindingDescr.getVariable() );
    // assertEquals( "location",
    // bindingDescr.getExpression() );
    // assertFalse( bindingDescr.isUnification() );
    // embedded bindings are extracted at compile time
    List<?> constraints = pattern.getDescrs();
    assertEquals(1, constraints.size());
    assertEquals("$name : name == \"Bob\" || $loc : location == \"Montreal\"", ((ExprConstraintDescr) constraints.get(0)).getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) Test(org.junit.Test)

Example 37 with PatternDescr

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

the class RuleParserTest method testAccumulateMnemonic2.

@Test
public void testAccumulateMnemonic2() throws Exception {
    String drl = "package org.drools.compiler\n" + "rule \"Accumulate 1\"\n" + "when\n" + "     Number() from acc( Cheese( $price : price ),\n" + "                        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("Number", 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("$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 38 with PatternDescr

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

the class RuleParserTest method testRuleParseLhs3.

@Test
public void testRuleParseLhs3() throws Exception {
    final String text = "rule X when (or\nnot Person()\n(and Cheese()\nMeat()\nWine())) then end";
    AndDescr pattern = ((RuleDescr) parse("rule", text)).getLhs();
    assertEquals(1, pattern.getDescrs().size());
    OrDescr or = (OrDescr) pattern.getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    NotDescr not = (NotDescr) or.getDescrs().get(0);
    AndDescr and = (AndDescr) or.getDescrs().get(1);
    assertEquals(1, not.getDescrs().size());
    PatternDescr person = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals(3, and.getDescrs().size());
    PatternDescr cheese = (PatternDescr) and.getDescrs().get(0);
    assertEquals("Cheese", cheese.getObjectType());
    PatternDescr meat = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Meat", meat.getObjectType());
    PatternDescr wine = (PatternDescr) and.getDescrs().get(2);
    assertEquals("Wine", wine.getObjectType());
}
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 39 with PatternDescr

use of org.drools.compiler.lang.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.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) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 40 with PatternDescr

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

the class RuleParserTest method testEntryPoint.

@Test
public void testEntryPoint() throws Exception {
    final String text = "rule X when StockTick( symbol==\"ACME\") from entry-point StreamA then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("symbol==\"ACME\"", fcd.getExpression());
    assertNotNull(pattern.getSource());
    EntryPointDescr entry = (EntryPointDescr) pattern.getSource();
    assertEquals("StreamA", entry.getEntryId());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) EntryPointDescr(org.drools.compiler.lang.descr.EntryPointDescr) 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