Search in sources :

Example 56 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testBigLiterals.

@Test
public void testBigLiterals() throws Exception {
    final String text = "rule X when Primitives( bigInteger == (10I), " + "                        bigDecimal == (10B), " + "                        bigInteger < 50I, " + "                        bigDecimal < 50B ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals(4, pattern.getDescrs().size());
    ExprConstraintDescr ecd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("bigInteger == (10I)", ecd.getExpression());
    ecd = (ExprConstraintDescr) pattern.getDescrs().get(1);
    assertEquals("bigDecimal == (10B)", ecd.getExpression());
    ecd = (ExprConstraintDescr) pattern.getDescrs().get(2);
    assertEquals("bigInteger < 50I", ecd.getExpression());
    ecd = (ExprConstraintDescr) pattern.getDescrs().get(3);
    assertEquals("bigDecimal < 50B", ecd.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 57 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testInlineEval.

@Test
public void testInlineEval() throws Exception {
    final String text = "rule \"inline eval\"\n" + "when\n" + "    Person( eval( name.startsWith(\"b\") && name.finishesWith(\"b\")) )\n" + "then\n" + "end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("eval( name.startsWith(\"b\") && name.finishesWith(\"b\"))", constr.getText());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 58 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testNotInOperator.

@Test
public void testNotInOperator() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "notin_operator_test.drl");
    assertNotNull(rule);
    assertEqualsIgnoreWhitespace("consequence();", (String) rule.getConsequence());
    assertEquals("simple_rule", rule.getName());
    assertEquals(2, rule.getLhs().getDescrs().size());
    // The first pattern, with 2 restrictions on a single field (plus a
    // connective)
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("age > 30 && < 40", fld.getExpression());
    // the second col, with 2 fields, the first with 2 restrictions, the
    // second field with one
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("Vehicle", pattern.getObjectType());
    assertEquals(2, pattern.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("type not in ( \"sedan\", \"wagon\" )", fld.getExpression());
    // now the second field
    fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1);
    assertEquals("age < 3", fld.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 59 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr 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.drl.ast.descr.PatternDescr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 60 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class RuleParserTest method testIsQuery.

@Test
public void testIsQuery() throws Exception {
    final String text = "rule X when ?person( \"Mark\", 42; ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertTrue(pattern.isQuery());
    assertEquals(2, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("\"Mark\"", fcd.getExpression());
    assertEquals(0, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.POSITIONAL, fcd.getType());
    fcd = (ExprConstraintDescr) pattern.getDescrs().get(1);
    assertEquals("42", fcd.getExpression());
    assertEquals(1, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.POSITIONAL, fcd.getType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)75 PatternDescr (org.drools.drl.ast.descr.PatternDescr)65 Test (org.junit.Test)56 RuleDescr (org.drools.drl.ast.descr.RuleDescr)37 AndDescr (org.drools.drl.ast.descr.AndDescr)34 PackageDescr (org.drools.drl.ast.descr.PackageDescr)23 BaseDescr (org.drools.drl.ast.descr.BaseDescr)11 BindingDescr (org.drools.drl.ast.descr.BindingDescr)10 Cheese (org.drools.mvel.compiler.Cheese)9 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)8 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)5 NotDescr (org.drools.drl.ast.descr.NotDescr)5 Declaration (org.drools.core.rule.Declaration)4 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)4 KiePMMLOperatorValue (org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 PredicateConstraint (org.drools.core.rule.PredicateConstraint)3