Search in sources :

Example 66 with RuleDescr

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

the class RuleParserTest method testAlmostEmptyRule.

@Test
public void testAlmostEmptyRule() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "almost_empty_rule.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(pkg);
    RuleDescr rule = pkg.getRules().get(0);
    assertEquals("almost_empty", rule.getName());
    assertNotNull(rule.getLhs());
    assertEquals("", ((String) rule.getConsequence()).trim());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 67 with RuleDescr

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

the class RuleParserTest method testEmptyPattern.

@Test
public void testEmptyPattern() throws Exception {
    PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "test_EmptyPattern.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals(1, pkg.getRules().size());
    final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
    assertEquals("simple rule", ruleDescr.getName());
    assertNotNull(ruleDescr.getLhs());
    assertEquals(1, ruleDescr.getLhs().getDescrs().size());
    final PatternDescr patternDescr = (PatternDescr) ruleDescr.getLhs().getDescrs().get(0);
    assertEquals(0, // this
    patternDescr.getConstraint().getDescrs().size());
    assertEquals("Cheese", patternDescr.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 68 with RuleDescr

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

the class RuleParserTest method testSimpleConstraint.

@Test
public void testSimpleConstraint() throws Exception {
    String source = "package com.sample  rule test  when  Cheese( type == 'stilton', price > 10 )  then  end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertEquals("com.sample", pkg.getName());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr constraint = (AndDescr) pattern.getConstraint();
    assertEquals(2, constraint.getDescrs().size());
    assertEquals("type == \"stilton\"", constraint.getDescrs().get(0).toString());
    assertEquals("price > 10", constraint.getDescrs().get(1).toString());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 69 with RuleDescr

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

the class RuleParserTest method testBracketsPrecedence.

/**
 */
@Test
public void testBracketsPrecedence() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "brackets_precedence.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    final AndDescr rootAnd = (AndDescr) rule.getLhs();
    assertEquals(2, rootAnd.getDescrs().size());
    final OrDescr leftOr = (OrDescr) rootAnd.getDescrs().get(0);
    assertEquals(2, leftOr.getDescrs().size());
    final NotDescr not = (NotDescr) leftOr.getDescrs().get(0);
    final PatternDescr foo1 = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Foo", foo1.getObjectType());
    final PatternDescr foo2 = (PatternDescr) leftOr.getDescrs().get(1);
    assertEquals("Foo", foo2.getObjectType());
    final OrDescr rightOr = (OrDescr) rootAnd.getDescrs().get(1);
    assertEquals(2, rightOr.getDescrs().size());
    final PatternDescr shoes = (PatternDescr) rightOr.getDescrs().get(0);
    assertEquals("Shoes", shoes.getObjectType());
    final PatternDescr butt = (PatternDescr) rightOr.getDescrs().get(1);
    assertEquals("Butt", butt.getObjectType());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 70 with RuleDescr

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

the class RuleParserTest method testNotMemberof.

@Test
public void testNotMemberof() throws Exception {
    final String text = "rule X when Country( $cities : city )\nPerson( city not memberOf $cities ) then end\n";
    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 not memberOf $cities", fieldConstr.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.drl.ast.descr.RuleDescr)191 Test (org.junit.Test)147 PackageDescr (org.drools.drl.ast.descr.PackageDescr)105 PatternDescr (org.drools.drl.ast.descr.PatternDescr)104 AndDescr (org.drools.drl.ast.descr.AndDescr)55 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)33 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)27 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)27 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)24 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)14 OrDescr (org.drools.drl.ast.descr.OrDescr)14 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)13 Pattern (org.drools.core.rule.Pattern)13 List (java.util.List)12 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 FromDescr (org.drools.drl.ast.descr.FromDescr)11