use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testSlidingWindow.
@Test
public void testSlidingWindow() throws Exception {
final String text = "rule X when StockTick( symbol==\"ACME\") over window:length(10) then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals(1, pattern.getDescrs().size());
ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
assertEquals("symbol==\"ACME\"", fcd.getExpression());
List<BehaviorDescr> behaviors = pattern.getBehaviors();
assertNotNull(behaviors);
assertEquals(1, behaviors.size());
BehaviorDescr descr = behaviors.get(0);
assertEquals("window", descr.getType());
assertEquals("length", descr.getSubType());
assertEquals("10", descr.getParameters().get(0));
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testFromWithInlineList.
@Test
public void testFromWithInlineList() throws Exception {
String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " not( Number( ) from [1, 2, 3] ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("XYZ", rule.getName());
PatternDescr number = (PatternDescr) ((NotDescr) rule.getLhs().getDescrs().get(1)).getDescrs().get(0);
assertEquals("[1, 2, 3]", ((FromDescr) number.getSource()).getDataSource().toString());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testPluggableOperators.
@Test
public void testPluggableOperators() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "pluggable_operators.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(5, rule.getLhs().getDescrs().size());
final PatternDescr eventA = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("$a", eventA.getIdentifier());
assertEquals("EventA", eventA.getObjectType());
final PatternDescr eventB = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("$b", eventB.getIdentifier());
assertEquals("EventB", eventB.getObjectType());
assertEquals(1, eventB.getConstraint().getDescrs().size());
assertEquals(1, eventB.getConstraint().getDescrs().size());
final ExprConstraintDescr fcdB = (ExprConstraintDescr) eventB.getConstraint().getDescrs().get(0);
assertEquals("this after[1,10] $a || this not after[15,20] $a", fcdB.getExpression());
final PatternDescr eventC = (PatternDescr) rule.getLhs().getDescrs().get(2);
assertEquals("$c", eventC.getIdentifier());
assertEquals("EventC", eventC.getObjectType());
assertEquals(1, eventC.getConstraint().getDescrs().size());
final ExprConstraintDescr fcdC = (ExprConstraintDescr) eventC.getConstraint().getDescrs().get(0);
assertEquals("this finishes $b", fcdC.getExpression());
final PatternDescr eventD = (PatternDescr) rule.getLhs().getDescrs().get(3);
assertEquals("$d", eventD.getIdentifier());
assertEquals("EventD", eventD.getObjectType());
assertEquals(1, eventD.getConstraint().getDescrs().size());
final ExprConstraintDescr fcdD = (ExprConstraintDescr) eventD.getConstraint().getDescrs().get(0);
assertEquals("this not starts $a", fcdD.getExpression());
final PatternDescr eventE = (PatternDescr) rule.getLhs().getDescrs().get(4);
assertEquals("$e", eventE.getIdentifier());
assertEquals("EventE", eventE.getObjectType());
assertEquals(1, eventE.getConstraint().getDescrs().size());
ExprConstraintDescr fcdE = (ExprConstraintDescr) eventE.getConstraint().getDescrs().get(0);
assertEquals("this not before[1, 10] $b || after[1, 10] $c && this after[1, 5] $d", fcdE.getExpression());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testExpanderMultipleConstraints.
@Test
public void testExpanderMultipleConstraints() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints.dslr"), this.getReader("multiple_constraints.dsl"));
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
assertEquals("age < 42", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0)).getExpression());
assertEquals("location==atlanta", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1)).getExpression());
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Bar", pattern.getObjectType());
assertNotNull((String) rule.getConsequence());
}
use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testNotExistWithBrackets.
@Test
public void testNotExistWithBrackets() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
final AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
assertEquals(1, not.getDescrs().size());
final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
assertEquals("Cheese", pattern.getObjectType());
final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
assertEquals(1, ex.getDescrs().size());
final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
assertEquals("Foo", exPattern.getObjectType());
}
Aggregations