Search in sources :

Example 6 with ExprConstraintDescr

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

the class RuleParserTest method testInOperator.

@Test
public void testInOperator() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "in_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 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 7 with ExprConstraintDescr

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

the class RuleParserTest method testSimpleRule.

@Test
public void testSimpleRule() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "simple_rule.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    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());
    AndDescr fieldAnd = (AndDescr) first.getConstraint();
    ExprConstraintDescr constraint = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
    assertNotNull(constraint);
    assertEquals("a==3", constraint.getExpression());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    // no constraints, only a binding
    fieldAnd = (AndDescr) second.getConstraint();
    assertEquals(1, fieldAnd.getDescrs().size());
    final ExprConstraintDescr binding = (ExprConstraintDescr) second.getConstraint().getDescrs().get(0);
    assertEquals("a4:a==4", binding.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)

Example 8 with ExprConstraintDescr

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

the class RuleParserTest method testWithPredicate.

@Test
public void testWithPredicate() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "with_predicate.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr col = (PatternDescr) rule.getLhs().getDescrs().get(0);
    AndDescr and = (AndDescr) col.getConstraint();
    assertEquals(2, and.getDescrs().size());
    final ExprConstraintDescr field = (ExprConstraintDescr) col.getDescrs().get(0);
    final ExprConstraintDescr pred = (ExprConstraintDescr) and.getDescrs().get(1);
    assertEquals("$age2:age", field.getExpression());
    assertEqualsIgnoreWhitespace("$age2 == $age1+2", pred.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) 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 9 with ExprConstraintDescr

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

the class KnowledgeBuilderTest method createRule.

private RuleImpl createRule(final ConditionalElementDescr ceDescr, final KnowledgeBuilderImpl builder, final String consequence) throws Exception {
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    lhs.addDescr((BaseDescr) ceDescr);
    final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    patternDescr.addConstraint(new ExprConstraintDescr("type == \"stilton\" "));
    ceDescr.addDescr(patternDescr);
    ruleDescr.setConsequence(consequence);
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertEquals("rule-1", rule.getName());
    return rule;
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 10 with ExprConstraintDescr

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

the class KnowledgeBuilderTest method testNumbers.

@Test
public void testNumbers() throws Exception {
    // test boolean
    createLiteralRule(new ExprConstraintDescr("booleanPrimitive == true "));
    // test boolean
    createLiteralRule(new ExprConstraintDescr("booleanPrimitive == false "));
    // test char
    createLiteralRule(new ExprConstraintDescr("charPrimitive == 'a' "));
    createLiteralRule(new ExprConstraintDescr("charPrimitive == \"a\" "));
    // test byte
    createLiteralRule(new ExprConstraintDescr("bytePrimitive == 1 "));
    createLiteralRule(new ExprConstraintDescr("bytePrimitive == 0 "));
    createLiteralRule(new ExprConstraintDescr("bytePrimitive == -1 "));
    // test short
    createLiteralRule(new ExprConstraintDescr("shortPrimitive == 1 "));
    createLiteralRule(new ExprConstraintDescr("shortPrimitive == 0 "));
    createLiteralRule(new ExprConstraintDescr("shortPrimitive == -1 "));
    // test int
    createLiteralRule(new ExprConstraintDescr("intPrimitive == 1"));
    createLiteralRule(new ExprConstraintDescr("intPrimitive == 0"));
    createLiteralRule(new ExprConstraintDescr("intPrimitive == -1"));
    // test long
    createLiteralRule(new ExprConstraintDescr("longPrimitive == 1"));
    createLiteralRule(new ExprConstraintDescr("longPrimitive == 0"));
    createLiteralRule(new ExprConstraintDescr("longPrimitive == -1"));
    // test float
    createLiteralRule(new ExprConstraintDescr("floatPrimitive == 1.1"));
    createLiteralRule(new ExprConstraintDescr("floatPrimitive == 0"));
    createLiteralRule(new ExprConstraintDescr("floatPrimitive == -1.1"));
    // test double
    createLiteralRule(new ExprConstraintDescr("doublePrimitive == 1.1"));
    createLiteralRule(new ExprConstraintDescr("doublePrimitive == 0"));
    createLiteralRule(new ExprConstraintDescr("doublePrimitive == -1.1"));
}
Also used : 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