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());
}
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());
}
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());
}
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;
}
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());
}
Aggregations