use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testOrWithBinding.
/**
* test basic foo : Fact() || Fact() stuff
*/
@Test
public void testOrWithBinding() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_binding.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, or.getDescrs().size());
final PatternDescr leftPattern = (PatternDescr) or.getDescrs().get(0);
assertEquals("Person", leftPattern.getObjectType());
assertEquals("foo", leftPattern.getIdentifier());
final PatternDescr rightPattern = (PatternDescr) or.getDescrs().get(1);
assertEquals("Person", rightPattern.getObjectType());
assertEquals("foo", rightPattern.getIdentifier());
final PatternDescr cheeseDescr = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Cheese", cheeseDescr.getObjectType());
assertEquals(null, cheeseDescr.getIdentifier());
assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" + bar );", (String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.PatternDescr 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.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testBindingComposite.
@Test
public void testBindingComposite() throws Exception {
final String text = "rule X when Person( $name : name == \"Bob\" || $loc : location == \"Montreal\" ) then end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertFalse(pattern.isUnification());
// assertEquals( 2,
// pattern.getDescrs().size() );
// BindingDescr bindingDescr = pattern.getDescrs().get( 0 );
// assertEquals( "$name",
// bindingDescr.getVariable() );
// assertEquals( "name",
// bindingDescr.getExpression() );
// assertFalse( bindingDescr.isUnification() );
//
// bindingDescr = pattern.getDescrs().get( 1 );
// assertEquals( "$loc",
// bindingDescr.getVariable() );
// assertEquals( "location",
// bindingDescr.getExpression() );
// assertFalse( bindingDescr.isUnification() );
// embedded bindings are extracted at compile time
List<?> constraints = pattern.getDescrs();
assertEquals(1, constraints.size());
assertEquals("$name : name == \"Bob\" || $loc : location == \"Montreal\"", ((ExprConstraintDescr) constraints.get(0)).getExpression());
}
use of org.drools.drl.ast.descr.PatternDescr 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.PatternDescr 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