Search in sources :

Example 21 with RuleDescr

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

the class RuleParserTest method testFromWithTernaryFollowedByQuery.

@Test
public void testFromWithTernaryFollowedByQuery() 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 (isFull ? $cheesery : $market) ?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 (isFull ? $cheesery : $market)", 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 22 with RuleDescr

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

the class RuleParserTest method testOrCE.

@Test
public void testOrCE() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_ce.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals("$p", person.getIdentifier());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Cheese", cheese1.getObjectType());
    assertEquals("$c", cheese1.getIdentifier());
    final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Cheese", cheese2.getObjectType());
    assertNull(cheese2.getIdentifier());
}
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 23 with RuleDescr

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

the class RuleParserTest method testRuleTwoLines.

@Test
public void testRuleTwoLines() throws Exception {
    final String text = "rule \"another test\" salience 10 when eval( true ) then System.out.println(1);\n end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    assertEquals("another test", rule.getName());
    assertEquals("System.out.println(1);\n ", rule.getConsequence());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Test(org.junit.Test)

Example 24 with RuleDescr

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

the class RuleParserTest method testMultiValueAnnotationsBackwardCompatibility.

@Test
public void testMultiValueAnnotationsBackwardCompatibility() throws Exception {
    // multiple values with no keys are parsed as a single value
    final String text = "rule X @ann1( val1, val2 ) @ann2( \"val1\", \"val2\" ) when then end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    AnnotationDescr ann = rule.getAnnotation("ann1");
    assertNotNull(ann);
    assertEquals("val1, val2", ann.getValue());
    ann = rule.getAnnotation("ann2");
    assertNotNull(ann);
    assertEquals("\"val1\", \"val2\"", ann.getValue());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Test(org.junit.Test)

Example 25 with RuleDescr

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

the class RuleParserTest method testSlidingWindow.

@Test
public void testSlidingWindow() throws Exception {
    final String text = "rule X when StockTick( symbol==\"ACME\") over window:length(10) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("symbol==\"ACME\"", fcd.getExpression());
    List<BehaviorDescr> behaviors = pattern.getBehaviors();
    assertNotNull(behaviors);
    assertEquals(1, behaviors.size());
    BehaviorDescr descr = behaviors.get(0);
    assertEquals("window", descr.getType());
    assertEquals("length", descr.getSubType());
    assertEquals("10", descr.getParameters().get(0));
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) 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