Search in sources :

Example 51 with PatternDescr

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

the class RuleParserTest method testInfinityLiteral.

@Test
public void testInfinityLiteral() throws Exception {
    final String text = "rule \"infinity\"\n" + "when\n" + "    StockTick( this after[-*,*] $another )\n" + "then\n" + "end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("StockTick", pattern.getObjectType());
    ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("this after[-*,*] $another", constr.getText());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 52 with PatternDescr

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

the class RuleParserTest method testMemberof.

@Test
public void testMemberof() throws Exception {
    final String text = "rule X when Country( $cities : city )\nPerson( city memberOf $cities )\n then end";
    AndDescr descrs = ((RuleDescr) parse("rule", text)).getLhs();
    assertEquals(2, descrs.getDescrs().size());
    PatternDescr pat = (PatternDescr) descrs.getDescrs().get(1);
    ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get(0);
    assertEquals("city memberOf $cities", fieldConstr.getExpression());
}
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 53 with PatternDescr

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

the class RuleParserTest method testAccumulate.

@Test
public void testAccumulate() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate.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("int x = 0 ;", accum.getInitCode());
    assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
    assertNull(accum.getReverseCode());
    assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
    assertFalse(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 54 with PatternDescr

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

the class RuleParserTest method testOrWithBinding.

/**
 * test basic foo : Fact() || Fact() stuff
 */
@Test
public void testOrWithBinding() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr leftPattern = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Person", leftPattern.getObjectType());
    assertEquals("foo", leftPattern.getIdentifier());
    final PatternDescr rightPattern = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Person", rightPattern.getObjectType());
    assertEquals("foo", rightPattern.getIdentifier());
    final PatternDescr cheeseDescr = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Cheese", cheeseDescr.getObjectType());
    assertEquals(null, cheeseDescr.getIdentifier());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) 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 55 with PatternDescr

use of org.drools.compiler.lang.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.compiler.lang.descr.PatternDescr) 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