@Test public void testEmptyRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "empty_rule.drl" ); assertNotNull( rule ); assertEquals( "empty", rule.getName() ); assertNotNull( rule.getLhs() ); assertNotNull( rule.getConsequence() ); }
@Test public void testQuotedStringNameRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "quoted_string_name_rule.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "quoted string name", rule.getName() ); assertNotNull( rule.getLhs() ); assertEquals( "", ((String) rule.getConsequence()).trim() ); }
@Test public void testAlmostEmptyRule() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "almost_empty_rule.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( pkg ); RuleDescr rule = pkg.getRules().get( 0 ); assertEquals( "almost_empty", rule.getName() ); assertNotNull( rule.getLhs() ); assertEquals( "", ((String) rule.getConsequence()).trim() ); }
@Test public void testComment() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "comment.drl" ); assertNotNull( pkg ); assertEquals( "foo.bar", pkg.getName() ); }
@Test public void testMultiValueAnnotationsBackwardCompatibility() throws Exception { // multiple values with no keys are parsed as a single value final String text = "rule X @ann1( val1, val2 ) @ann2( \"val1\", \"val2\" ) when then end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); AnnotationDescr ann = rule.getAnnotation( "ann1" ); assertNotNull( ann ); assertEquals( "val1, val2", ann.getValue() ); ann = rule.getAnnotation( "ann2" ); assertNotNull( ann ); assertEquals( "\"val1\", \"val2\"", ann.getValue() ); }
@Test public void testRuleParseLhs() throws Exception { final String text = "rule X when Person(age < 42, location==\"atlanta\") \nor\nPerson(name==\"bob\") then end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); AndDescr lhs = rule.getLhs(); assertEquals( 1, lhs.getDescrs().size() ); assertEquals( 2, ((OrDescr) lhs.getDescrs().get( 0 )).getDescrs().size() ); }
@Test public void testRuleParseLhsWithStringQuotes() throws Exception { final String text = "rule X when Person( location==\"atlanta\\\"\") then end\n"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); AndDescr lhs = rule.getLhs(); ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get( 0 )).getDescrs().get( 0 ); assertEquals( "location==\"atlanta\\\"\"", constr.getText() ); }
@Test public void testRuleParseLhsWithStringQuotes2() throws Exception { final String text = "rule X when Cheese( $x: type, type == \"s\\tti\\\"lto\\nn\" ) then end\n"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); AndDescr lhs = rule.getLhs(); ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get( 0 )).getDescrs().get( 1 ); assertEquals( "type == \"s\\tti\\\"lto\\nn\"", constr.getText() ); }
@Test public void testFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "from.drl" ); assertFalse( parser.getErrorMessages().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "using_from", rule.getName() ); assertEquals( 9, rule.getLhs().getDescrs().size() ); }
@Test public void testEscapedStrings() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "escaped-string.drl" ); assertNotNull( rule ); assertEquals( "test_Quotes", rule.getName() ); final String expected = "String s = \"\\\"\\n\\t\\\\\";"; assertEqualsIgnoreWhitespace( expected, (String) rule.getConsequence() ); }
@Test public void testAttributeRefract() throws Exception { final String source = "rule Test refract when Person() then end"; PackageDescr pkg = (PackageDescr) parse( "compilationUnit", source ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( "Test", rule.getName() ); Map<String, AttributeDescr> attributes = rule.getAttributes(); assertEquals( 1, attributes.size() ); AttributeDescr refract = attributes.get( "refract" ); assertNotNull( refract ); assertEquals( "true", refract.getValue() ); }
@Test public void testExpanderLineSpread() throws Exception { final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( this.getReader( "expander_spread_lines.dslr" ), this.getReader( "complex.dsl" ) ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, rule.getLhs().getDescrs().size() ); final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get( 0 ); assertEquals( 2, or.getDescrs().size() ); assertNotNull( (String) rule.getConsequence() ); }
@Test public void testEntryPoint2() throws Exception { final String text = "rule X when StockTick( symbol==\"ACME\") from entry-point \"StreamA\" 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() ); assertNotNull( pattern.getSource() ); EntryPointDescr entry = (EntryPointDescr) pattern.getSource(); assertEquals( "StreamA", entry.getEntryId() ); }
@Test public void testConsequenceWithDeclaration() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "declaration-in-consequence.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "myrule", rule.getName() ); final String expected = "int i = 0; i = 1; i / 1; i == 1; i(i); i = 'i'; i.i.i; i\\i; i<i; i>i; i=\"i\"; ++i;" + "i++; --i; i--; i += i; i -= i; i *= i; i /= i;" + "int i = 5;" + "for(int j; j<i; ++j) {" + "System.out.println(j);}" + "Object o = new String(\"Hello\");" + "String s = (String) o;"; assertEqualsIgnoreWhitespace( expected, (String) rule.getConsequence() ); assertTrue( ((String) rule.getConsequence()).indexOf( "++" ) > 0 ); assertTrue( ((String) rule.getConsequence()).indexOf( "--" ) > 0 ); assertTrue( ((String) rule.getConsequence()).indexOf( "+=" ) > 0 ); assertTrue( ((String) rule.getConsequence()).indexOf( "==" ) > 0 ); // System.out.println(( String ) rule.getConsequence()); // note, need to assert that "i++" is preserved as is, no extra spaces. }
@Test public void testRuleFlowGroup() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "ruleflowgroup.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "rule1", rule.getName() ); final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( "ruleflow-group" ); assertEquals( "a group", att.getValue() ); assertEquals( "ruleflow-group", att.getName() ); }
@Test public void testNoLoop() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "no-loop.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "rule1", rule.getName() ); final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( "no-loop" ); assertEquals( "false", att.getValue() ); assertEquals( "no-loop", att.getName() ); }
@Test public void testAutofocus() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "autofocus.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "rule1", rule.getName() ); final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( "auto-focus" ); assertEquals( "true", att.getValue() ); assertEquals( "auto-focus", att.getName() ); }
@Test public void testEmptyPattern() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "test_EmptyPattern.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertEquals( 1, pkg.getRules().size() ); final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( "simple rule", ruleDescr.getName() ); assertNotNull( ruleDescr.getLhs() ); assertEquals( 1, ruleDescr.getLhs().getDescrs().size() ); final PatternDescr patternDescr = (PatternDescr) ruleDescr.getLhs().getDescrs().get( 0 ); assertEquals( 0, patternDescr.getConstraint().getDescrs().size() ); // this assertEquals( "Cheese", patternDescr.getObjectType() ); }
@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() ); }
@Test public void testExpanderMultipleConstraintsFlush() throws Exception { final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); // this is similar to the other test, but it requires a flush to add the // constraints final PackageDescr pkg = parser.parse( this.getReader( "expander_multiple_constraints_flush.dslr" ), this.getReader( "multiple_constraints.dsl" ) ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, rule.getLhs().getDescrs().size() ); final 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() ); assertNotNull( (String) rule.getConsequence() ); }