Search in sources :

Example 6 with AndDescr

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

the class RuleParserTest method testRuleExtends.

@Test
public void testRuleExtends() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_Extends.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    assertTrue(rule.getParentName() != null);
    assertEquals("rule1", rule.getParentName());
    AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo", pattern.getObjectType());
    assertEquals("$foo", pattern.getIdentifier());
}
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) Test(org.junit.Test)

Example 7 with AndDescr

use of org.drools.drl.ast.descr.AndDescr 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 AndDescr

use of org.drools.drl.ast.descr.AndDescr 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 AndDescr

use of org.drools.drl.ast.descr.AndDescr 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 AndDescr

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

the class KnowledgeBuilderTest method testNull.

@Test
public void testNull() {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) Cheese(org.drools.mvel.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

AndDescr (org.drools.drl.ast.descr.AndDescr)88 Test (org.junit.Test)61 PatternDescr (org.drools.drl.ast.descr.PatternDescr)60 RuleDescr (org.drools.drl.ast.descr.RuleDescr)54 PackageDescr (org.drools.drl.ast.descr.PackageDescr)39 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)34 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 BaseDescr (org.drools.drl.ast.descr.BaseDescr)18 Cheese (org.drools.mvel.compiler.Cheese)13 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)11 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)10 OrDescr (org.drools.drl.ast.descr.OrDescr)10 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 BindingDescr (org.drools.drl.ast.descr.BindingDescr)8 NotDescr (org.drools.drl.ast.descr.NotDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 QueryDescr (org.drools.drl.ast.descr.QueryDescr)6 RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)5 FieldConstraintDescr (org.drools.drl.ast.descr.FieldConstraintDescr)4 LiteralRestrictionDescr (org.drools.drl.ast.descr.LiteralRestrictionDescr)4