use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testDuplicateRuleNames.
@Test
public void testDuplicateRuleNames() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
RuleDescr ruleDescr;
AndDescr lhs;
PatternDescr patternDescr;
FieldConstraintDescr literalDescr;
final PackageDescr packageDescr = createBasicPackageWithOneRule(1, 1);
ruleDescr = new RuleDescr("rule-1");
ruleDescr.setLocation(42, 43);
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
ruleDescr = new RuleDescr("rule-2");
ruleDescr.setLocation(42, 43);
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
builder.addPackage(packageDescr);
assertLength(2, builder.getErrors().getErrors());
final ParserError err = (ParserError) builder.getErrors().getErrors()[0];
assertEquals(42, err.getRow());
assertEquals(43, err.getCol());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTimeWindowBehavior.
@Test
public void testTimeWindowBehavior() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
typeDeclDescr.addAnnotation("role", "event");
packageDescr.addTypeDeclaration(typeDeclDescr);
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
final BehaviorDescr windowDescr = new BehaviorDescr("window");
windowDescr.setSubType("time");
windowDescr.setParameters(Collections.singletonList("60000"));
patternDescr.addBehavior(windowDescr);
lhs.addDescr(patternDescr);
ruleDescr.setConsequence("System.out.println( $tick );");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
final RuleImpl rule = pkg.getRule("rule-1");
assertNotNull(rule);
final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
final Behavior window = pattern.getBehaviors().get(0);
assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testPredicate.
@Test
public void testPredicate() throws Exception {
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 pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
final BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
final BindingDescr fieldBindingDescr2 = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr2);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final ExprConstraintDescr predicate = new ExprConstraintDescr("eval(( ( Integer )map.get( new Integer(x) )).intValue() == y)");
pattern.addConstraint(predicate);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testDuplicateDeclaration.
// TODO we now allow bindings on declarations, so update the test for this
@Test
// TODO we now allow bindings on declarations, so update the test for this
@Ignore
public void testDuplicateDeclaration() {
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 pattern1 = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern1);
final BindingDescr fieldBindingDescr = new BindingDescr("$type", "type");
final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("==", "stilton"));
pattern1.addConstraint(fieldBindingDescr);
pattern1.addConstraint(literalDescr);
final PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern2);
pattern2.addConstraint(fieldBindingDescr);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(2, builder.getErrors().getErrors());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method createReturnValueRule.
private void createReturnValueRule(final PackageDescr packageDescr, final String expression) {
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
pattern.addConstraint(new ExprConstraintDescr("price == (" + expression + ")"));
ruleDescr.setConsequence("update(stilton);");
}
Aggregations