Search in sources :

Example 21 with RuleDescr

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

the class RuleParserTest method testQualifiedClassname.

@Test
public void testQualifiedClassname() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "qualified_classname.drl");
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    final PatternDescr p = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("com.cheeseco.Cheese", p.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 22 with RuleDescr

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

the class RuleParserTest method testCalendars2.

@Test
public void testCalendars2() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_calendars_attribute2.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("simple_rule", rule.getName());
    assertEqualsIgnoreWhitespace("bar();", (String) rule.getConsequence());
    final Map<String, AttributeDescr> attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    AttributeDescr at = (AttributeDescr) attrs.get("calendars");
    assertEquals("calendars", at.getName());
    assertEquals("[ \"cal 1\", \"cal 2\", \"cal 3\" ]", at.getValue());
    at = (AttributeDescr) attrs.get("lock-on-active");
    assertEquals("lock-on-active", at.getName());
    assertEquals("true", at.getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 23 with RuleDescr

use of org.drools.drl.ast.descr.RuleDescr 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.drl.ast.descr.PatternDescr) 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 24 with RuleDescr

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

Example 25 with RuleDescr

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

the class RuleParserTest method testAttributes2.

@Test
public void testAttributes2() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "rule_attributes2.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    List<RuleDescr> rules = pkg.getRules();
    assertEquals(3, rules.size());
    RuleDescr rule = rules.get(0);
    assertEquals("rule1", rule.getName());
    Map<String, AttributeDescr> attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    AttributeDescr at = (AttributeDescr) attrs.get("salience");
    assertEquals("salience", at.getName());
    assertEquals("(42)", at.getValue());
    at = (AttributeDescr) attrs.get("agenda-group");
    assertEquals("agenda-group", at.getName());
    assertEquals("my_group", at.getValue());
    rule = rules.get(1);
    assertEquals("rule2", rule.getName());
    attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    at = (AttributeDescr) attrs.get("salience");
    assertEquals("salience", at.getName());
    assertEquals("(Integer.MIN_VALUE)", at.getValue());
    at = (AttributeDescr) attrs.get("no-loop");
    assertEquals("no-loop", at.getName());
    rule = rules.get(2);
    assertEquals("rule3", rule.getName());
    attrs = rule.getAttributes();
    assertEquals(2, attrs.size());
    at = (AttributeDescr) attrs.get("enabled");
    assertEquals("enabled", at.getName());
    assertEquals("(Boolean.TRUE)", at.getValue());
    at = (AttributeDescr) attrs.get("activation-group");
    assertEquals("activation-group", at.getName());
    assertEquals("my_activation_group", at.getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) 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