Search in sources :

Example 51 with ExprConstraintDescr

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

the class RuleParserTest method testLiteralBoolAndNegativeNumbersRule.

@Test
public void testLiteralBoolAndNegativeNumbersRule() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "literal_bool_and_negative.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    assertNotNull(rule.getLhs());
    assertEqualsIgnoreWhitespace("cons();", (String) rule.getConsequence());
    final AndDescr lhs = rule.getLhs();
    assertEquals(3, lhs.getDescrs().size());
    PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    AndDescr fieldAnd = (AndDescr) pattern.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertEquals("bar == false", fld.getExpression());
    pattern = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    fieldAnd = (AndDescr) pattern.getConstraint();
    fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertEquals("boo > -42", fld.getText());
    pattern = (PatternDescr) lhs.getDescrs().get(2);
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    fieldAnd = (AndDescr) pattern.getConstraint();
    fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertEquals("boo > -42.42", fld.getText());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 52 with ExprConstraintDescr

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

the class RuleParserTest method testPluggableOperators.

@Test
public void testPluggableOperators() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "pluggable_operators.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(5, rule.getLhs().getDescrs().size());
    final PatternDescr eventA = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("$a", eventA.getIdentifier());
    assertEquals("EventA", eventA.getObjectType());
    final PatternDescr eventB = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("$b", eventB.getIdentifier());
    assertEquals("EventB", eventB.getObjectType());
    assertEquals(1, eventB.getConstraint().getDescrs().size());
    assertEquals(1, eventB.getConstraint().getDescrs().size());
    final ExprConstraintDescr fcdB = (ExprConstraintDescr) eventB.getConstraint().getDescrs().get(0);
    assertEquals("this after[1,10] $a || this not after[15,20] $a", fcdB.getExpression());
    final PatternDescr eventC = (PatternDescr) rule.getLhs().getDescrs().get(2);
    assertEquals("$c", eventC.getIdentifier());
    assertEquals("EventC", eventC.getObjectType());
    assertEquals(1, eventC.getConstraint().getDescrs().size());
    final ExprConstraintDescr fcdC = (ExprConstraintDescr) eventC.getConstraint().getDescrs().get(0);
    assertEquals("this finishes $b", fcdC.getExpression());
    final PatternDescr eventD = (PatternDescr) rule.getLhs().getDescrs().get(3);
    assertEquals("$d", eventD.getIdentifier());
    assertEquals("EventD", eventD.getObjectType());
    assertEquals(1, eventD.getConstraint().getDescrs().size());
    final ExprConstraintDescr fcdD = (ExprConstraintDescr) eventD.getConstraint().getDescrs().get(0);
    assertEquals("this not starts $a", fcdD.getExpression());
    final PatternDescr eventE = (PatternDescr) rule.getLhs().getDescrs().get(4);
    assertEquals("$e", eventE.getIdentifier());
    assertEquals("EventE", eventE.getObjectType());
    assertEquals(1, eventE.getConstraint().getDescrs().size());
    ExprConstraintDescr fcdE = (ExprConstraintDescr) eventE.getConstraint().getDescrs().get(0);
    assertEquals("this not before[1, 10] $b || after[1, 10] $c && this after[1, 5] $d", fcdE.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) 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 53 with ExprConstraintDescr

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

the class RuleParserTest method testNotWithConstraint.

@Test
public void testNotWithConstraint() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_with_constraint.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("$likes:like", fieldBinding.getExpression());
    final NotDescr not = (NotDescr) rule.getLhs().getDescrs().get(1);
    pattern = (PatternDescr) not.getDescrs().get(0);
    final ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("type == $likes", fld.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) 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 54 with ExprConstraintDescr

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

the class RuleParserTest method testRestrictionsMultiple.

@Test
public void testRestrictionsMultiple() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "restrictions_test.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    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());
    AndDescr and = (AndDescr) pattern.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) and.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());
    and = (AndDescr) pattern.getConstraint();
    fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertEquals("type == \"sedan\" || == \"wagon\"", fld.getExpression());
    // now the second field
    fld = (ExprConstraintDescr) and.getDescrs().get(1);
    assertEquals("age < 3", fld.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 55 with ExprConstraintDescr

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

the class RuleParserTest method testLhsSemicolonDelim.

@Test
public void testLhsSemicolonDelim() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "lhs_semicolon_delim.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // System.err.println( lhs.getDescrs() );
    // 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());
    // LiteralDescr constraint = (LiteralDescr) first.getDescrs().get( 0 );
    AndDescr and = (AndDescr) first.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertNotNull(fld);
    assertEquals("a==3", fld.getExpression());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    assertEquals(1, second.getDescrs().size());
    final ExprConstraintDescr fieldBindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
    assertEquals("a4:a==4", fieldBindingDescr.getExpression());
    // Check third pattern
    final PatternDescr third = (PatternDescr) lhs.getDescrs().get(2);
    assertNull(third.getIdentifier());
    assertEquals("Baz", third.getObjectType());
    assertEqualsIgnoreWhitespace("if ( a == b ) { " + "  assert( foo3 );" + "} else {" + "  retract( foo4 );" + "}" + "  System.out.println( a4 );", (String) rule.getConsequence());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) 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