Search in sources :

Example 31 with RuleDescr

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

the class RuleParserTest method testEnumeration.

@Test
public void testEnumeration() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "enumeration.drl");
    assertEquals("simple_rule", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Foo", col.getObjectType());
    assertEquals(1, col.getConstraint().getDescrs().size());
    final ExprConstraintDescr fld = (ExprConstraintDescr) col.getConstraint().getDescrs().get(0);
    assertEquals("bar == Foo.BAR", fld.getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 32 with RuleDescr

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

the class RuleParserTest method testFromWithInlineListIndex.

@Test
public void testFromWithInlineListIndex() throws Exception {
    String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3][1] \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("XYZ", rule.getName());
    assertFalse(parser.hasErrors());
    PatternDescr number = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("[1, 2, 3][1]", ((FromDescr) number.getSource()).getDataSource().toString());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 33 with RuleDescr

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

the class RuleParserTest method testFromFollowedByQuery.

@Test
public void testFromFollowedByQuery() throws Exception {
    // the 'from' expression requires a ";" to disambiguate the "?"
    // prefix for queries from the ternary operator "? :"
    final String text = "rule X when Cheese() from $cheesery ?person( \"Mark\", 42; ) then end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    assertEquals("from $cheesery", pattern.getSource().getText());
    assertFalse(pattern.isQuery());
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("person", pattern.getObjectType());
    assertTrue(pattern.isQuery());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Test(org.junit.Test)

Example 34 with RuleDescr

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

Example 35 with RuleDescr

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

the class RuleParserTest method testMultipleRules.

@Test
public void testMultipleRules() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "multiple_rules.drl");
    final List<RuleDescr> rules = pkg.getRules();
    assertEquals(2, rules.size());
    final RuleDescr rule0 = rules.get(0);
    assertEquals("Like Stilton", rule0.getName());
    final RuleDescr rule1 = rules.get(1);
    assertEquals("Like Cheddar", rule1.getName());
    // checkout the first rule
    AndDescr lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule0.getConsequence());
    // Check first pattern
    PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.getObjectType());
    // checkout the second rule
    lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule1.getConsequence());
    // Check first pattern
    first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.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) Test(org.junit.Test)

Aggregations

RuleDescr (org.drools.compiler.lang.descr.RuleDescr)185 Test (org.junit.Test)145 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)104 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)103 AndDescr (org.drools.compiler.lang.descr.AndDescr)54 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)37 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)24 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)20 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)17 Cheese (org.drools.compiler.Cheese)16 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 OrDescr (org.drools.compiler.lang.descr.OrDescr)14 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)14 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)13 Pattern (org.drools.core.rule.Pattern)13 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)12 List (java.util.List)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11