Search in sources :

Example 66 with RuleDescr

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

the class RuleParserTest method testNotMemberof.

@Test
public void testNotMemberof() throws Exception {
    final String text = "rule X when Country( $cities : city )\nPerson( city not memberOf $cities ) then end\n";
    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 not 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)

Example 67 with RuleDescr

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

the class RuleParserTest method testExpanderMultipleConstraintsFlush.

@Test
public void testExpanderMultipleConstraintsFlush() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    // this is similar to the other test, but it requires a flush to add the
    // constraints
    final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints_flush.dslr"), this.getReader("multiple_constraints.dsl"));
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    assertEquals(2, pattern.getConstraint().getDescrs().size());
    assertEquals("age < 42", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0)).getExpression());
    assertEquals("location==atlanta", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1)).getExpression());
    assertNotNull((String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 68 with RuleDescr

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

the class RuleParserTest method testRuleOldSyntax1.

@Test
public void testRuleOldSyntax1() throws Exception {
    final String source = "rule \"Test\" when ( not $r :LiteralRestriction( operator == Operator.EQUAL ) ) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("Test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    assertEquals(1, ((NotDescr) rule.getLhs().getDescrs().get(0)).getDescrs().size());
    NotDescr notDescr = (NotDescr) rule.getLhs().getDescrs().get(0);
    PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
    assertEquals("$r", patternDescr.getIdentifier());
    assertEquals(1, patternDescr.getDescrs().size());
    ExprConstraintDescr fieldConstraintDescr = (ExprConstraintDescr) patternDescr.getDescrs().get(0);
    assertEquals("operator == Operator.EQUAL", fieldConstraintDescr.getExpression());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) 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)

Example 69 with RuleDescr

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

the class RuleParserTest method testRuleSingleLine.

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

Example 70 with RuleDescr

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

the class Misc2Test method testEvalConstraintWithMvelOperator.

@Test
public void testEvalConstraintWithMvelOperator() {
    String drl = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Foo( eval( field soundslike \"water\" ) )" + " then " + "end";
    DrlParser drlParser = new DrlParser();
    PackageDescr packageDescr;
    try {
        packageDescr = drlParser.parse(true, drl);
    } catch (DroolsParserException e) {
        throw new RuntimeException(e);
    }
    RuleDescr r = packageDescr.getRules().get(0);
    PatternDescr pd = (PatternDescr) r.getLhs().getDescrs().get(0);
    assertEquals(1, pd.getConstraint().getDescrs().size());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) 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