Search in sources :

Example 31 with PatternDescr

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

the class RuleParserTest method testPluggableOperators2.

@Test
public void testPluggableOperators2() throws Exception {
    final String text = "rule \"tt\"\n" + "    dialect \"mvel\"\n" + "when\n" + "    exists (TelephoneCall( this finishes [1m] \"25-May-2011\" ))\n" + "then\n" + "end";
    PatternDescr pattern = (PatternDescr) ((ExistsDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0)).getDescrs().get(0);
    assertEquals("TelephoneCall", pattern.getObjectType());
    ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("this finishes [1m] \"25-May-2011\"", constr.getText());
}
Also used : ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 32 with PatternDescr

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

the class RuleParserTest method testWithRetval.

@Test
public void testWithRetval() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_retval.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);
    assertEquals(1, col.getConstraint().getDescrs().size());
    assertEquals("Foo", col.getObjectType());
    final ExprConstraintDescr fld = (ExprConstraintDescr) col.getConstraint().getDescrs().get(0);
    assertEquals("name== (a + b)", fld.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 33 with PatternDescr

use of org.drools.drl.ast.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.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 34 with PatternDescr

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

the class RuleParserTest method testAccumulateMultipleFunctionsConstraint.

@Test
public void testAccumulateMultipleFunctionsConstraint() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateMultipleFunctionsConstraint.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());
    assertEquals(2, out.getConstraint().getDescrs().size());
    assertEquals("$a1 > 10 && $M1 <= 100", out.getConstraint().getDescrs().get(0).toString());
    assertEquals("$m1 == 5", out.getConstraint().getDescrs().get(1).toString());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(3, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$a1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    assertEquals("min", functions.get(1).getFunction());
    assertEquals("$m1", functions.get(1).getBind());
    assertEquals("$price", functions.get(1).getParams()[0]);
    assertEquals("max", functions.get(2).getFunction());
    assertEquals("$M1", functions.get(2).getBind());
    assertEquals("$price", functions.get(2).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", 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 35 with PatternDescr

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

the class RuleParserTest method testNullConstraints.

@Test
public void testNullConstraints() throws Exception {
    final String text = "rule X when Person( name == null ) 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("name == null", fcd.getExpression());
    assertEquals(0, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.NAMED, fcd.getType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) 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