Search in sources :

Example 36 with RuleDescr

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

the class RuleParserTest method testFromWithInlineListMethod.

@Test
public void testFromWithInlineListMethod() throws Exception {
    String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3].sublist(1, 2) \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].sublist(1, 2)", ((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 37 with RuleDescr

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

the class RuleParserTest method testRuleMetadata.

@Test
public void testRuleMetadata() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_Metadata.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    // @fooAttribute(barValue)
    // @fooAtt2(barVal2)
    RuleDescr rule = pkg.getRules().get(0);
    assertTrue(rule.getAnnotationNames().contains("fooMeta1"));
    assertEquals("barVal1", rule.getAnnotation("fooMeta1").getValue());
    assertTrue(rule.getAnnotationNames().contains("fooMeta2"));
    assertEquals("barVal2", rule.getAnnotation("fooMeta2").getValue());
    assertEqualsIgnoreWhitespace("System.out.println(\"Consequence\");", (String) rule.getConsequence());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 38 with RuleDescr

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

Example 39 with RuleDescr

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

the class RuleParserTest method testQueryRuleMixed.

@Test
public void testQueryRuleMixed() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "query_and_rule.drl");
    assertEquals(4, // as queries are rules
    pkg.getRules().size());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("bar", rule.getName());
    QueryDescr query = (QueryDescr) pkg.getRules().get(1);
    assertEquals("simple_query", query.getName());
    rule = (RuleDescr) pkg.getRules().get(2);
    assertEquals("bar2", rule.getName());
    query = (QueryDescr) pkg.getRules().get(3);
    assertEquals("simple_query2", query.getName());
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 40 with RuleDescr

use of org.drools.compiler.lang.descr.RuleDescr 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<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.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AccumulateFunctionCallDescr(org.drools.compiler.lang.descr.AccumulateDescr.AccumulateFunctionCallDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) 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