Search in sources :

Example 56 with RuleDescr

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

the class RuleParserTest method testEvalMultiple.

@Test
public void testEvalMultiple() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "eval_multiple.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(4, rule.getLhs().getDescrs().size());
    final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get(0);
    assertEqualsIgnoreWhitespace("abc(\"foo\") + 5", (String) eval.getContent());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Foo", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 57 with RuleDescr

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

the class RuleParserTest method testEval.

@Test
public void testEval() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "eval_parsing.drl");
    assertEquals("org.drools.compiler", pkg.getName());
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule1 = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule1.getLhs().getDescrs().size());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 58 with RuleDescr

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

the class RuleParserTest method testLineNumberInAST.

@Test
public void testLineNumberInAST() throws Exception {
    // also see testSimpleExpander to see how this works with an expander
    // (should be the same).
    final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    assertEquals(22, rule.getConsequenceLine());
    assertEquals(2, rule.getConsequencePattern());
    final AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // Check first pattern
    final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo3", first.getIdentifier());
    assertEquals("Bar", first.getObjectType());
    assertEquals(1, first.getConstraint().getDescrs().size());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
    assertEquals("Baz", third.getObjectType());
    assertEquals(19, first.getLine());
    assertEquals(20, second.getLine());
    assertEquals(21, third.getLine());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Test(org.junit.Test)

Example 59 with RuleDescr

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

the class RuleParserTest method testRuleParseLhs.

@Test
public void testRuleParseLhs() throws Exception {
    final String text = "rule X when Person(age < 42, location==\"atlanta\") \nor\nPerson(name==\"bob\") then end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    AndDescr lhs = rule.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    assertEquals(2, ((OrDescr) lhs.getDescrs().get(0)).getDescrs().size());
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 60 with RuleDescr

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

the class RuleParserTest method testMemberof.

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

Aggregations

RuleDescr (org.drools.compiler.lang.descr.RuleDescr)161 Test (org.junit.Test)143 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)100 AndDescr (org.drools.compiler.lang.descr.AndDescr)51 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)37 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)19 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)19 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)17 Cheese (org.drools.compiler.Cheese)16 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)14 OrDescr (org.drools.compiler.lang.descr.OrDescr)13 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)13 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)12 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 FromDescr (org.drools.compiler.lang.descr.FromDescr)11 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)10 NotDescr (org.drools.compiler.lang.descr.NotDescr)10