@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 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 testLineNumberIncludingCommentsInRHS() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "test_CommentLineNumbersInConsequence.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final String rhs = (String) ((RuleDescr) pkg.getRules().get( 0 )).getConsequence(); String expected = "\\s*//woot$\\s*first$\\s*$\\s*//$\\s*$\\s*/\\* lala$\\s*$\\s*\\*/$\\s*second$\\s*"; assertTrue( Pattern.compile( expected, Pattern.DOTALL | Pattern.MULTILINE ).matcher( rhs ).matches() ); }
@Test public void testComment() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "comment.drl" ); assertNotNull( pkg ); assertEquals( "foo.bar", pkg.getName() ); }
@Test public void testRuleTwoLines() throws Exception { final String text = "rule \"another test\" salience 10 when eval( true ) then System.out.println(1);\n end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertEquals( "another test", rule.getName() ); assertEquals( "System.out.println(1);\n ", rule.getConsequence() ); }
@Test public void testTernaryExpression() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "ternary_expression.drl" ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, pkg.getRules().size() ); assertEqualsIgnoreWhitespace( "if (speed > speedLimit ? true : false;) pullEmOver();", (String) rule.getConsequence() ); }
@Test public void testTypeWithMetaData() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "type_with_meta.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations(); assertEquals( 3, declarations.size() ); }
@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 testUnificationBinding() throws Exception { final String text = "rule X when $p := Person( $name := name, $loc : location ) then end"; PatternDescr pattern = (PatternDescr) ((RuleDescr) parse( "rule", text )).getLhs().getDescrs().get( 0 ); assertEquals( "$p", pattern.getIdentifier() ); assertTrue( pattern.isUnification() ); assertEquals( 2, pattern.getDescrs().size() ); ExprConstraintDescr bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get( 0 ); assertEquals( "$name := name", bindingDescr.getExpression() ); bindingDescr = (ExprConstraintDescr) pattern.getDescrs().get( 1 ); assertEquals( "$loc : location", bindingDescr.getExpression() ); }
@Test public void testFromWithTernaryFollowedByQuery() throws Exception { // the 'from' expression requires a ";" to disambiguate the "?" // prefix for queries from the ternary operator "? :" final String text = "rule X when Cheese() from (isFull ? $cheesery : $market) ?person( \"Mark\", 42; ) then end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); assertEquals( "Cheese", pattern.getObjectType() ); assertEquals( "from (isFull ? $cheesery : $market)", pattern.getSource().getText() ); assertFalse( pattern.isQuery() ); pattern = (PatternDescr) rule.getLhs().getDescrs().get( 1 ); assertEquals( "person", pattern.getObjectType() ); assertTrue( pattern.isQuery() ); }
@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 testAccumulateMultipleFunctions() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "accumulateMultipleFunctions.drl" ); assertEquals( 1, pkg.getRules().size() ); assertEquals( 1, rule.getLhs().getDescrs().size() ); assertEquals( "Object", out.getObjectType() ); AccumulateDescr accum = (AccumulateDescr) out.getSource(); assertTrue( accum.isExternalFunction() ); assertEquals( 3, functions.size() ); assertEquals( "average", functions.get( 0 ).getFunction() ); assertEquals( "$a1", functions.get( 0 ).getBind() ); assertEquals( "$price", functions.get( 0 ).getParams()[0] ); assertEquals( "min", functions.get( 1 ).getFunction() ); assertEquals( "$m1", functions.get( 1 ).getBind() ); assertEquals( "$price",
@Test public void testChunkWithParensAndQuotedString() throws Exception { String input = "( fnord( \"cheese\" ) )"; createParser( new ANTLRStringStream( input ) ); String returnData = parser.chunk( DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1 ); assertEquals( "fnord( \"cheese\" )", returnData ); }
@Test public void testFromWithInlineListIndex() throws Exception { String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3][1] \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() ); assertFalse( parser.hasErrors() ); PatternDescr number = (PatternDescr) rule.getLhs().getDescrs().get( 1 ); assertEquals( "[1, 2, 3][1]", ((FromDescr) number.getSource()).getDataSource().toString() ); }
@Test public void testOrCE() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "or_ce.drl" ); assertEquals( 1, pkg.getRules().size() ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 2, rule.getLhs().getDescrs().size() ); final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); assertEquals( "Person", person.getObjectType() ); assertEquals( "$p", person.getIdentifier() ); final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get( 1 ); assertEquals( 2, or.getDescrs().size() ); final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get( 0 ); assertEquals( "Cheese", cheese1.getObjectType() ); assertEquals( "$c", cheese1.getIdentifier() ); final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get( 1 ); assertEquals( "Cheese", cheese2.getObjectType() ); assertNull( cheese2.getIdentifier() ); }
@Test public void testEval() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "eval_parsing.drl" ); assertEquals( "org.drools.compiler", pkg.getName() ); assertEquals( 1, pkg.getRules().size() ); final RuleDescr rule1 = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, rule1.getLhs().getDescrs().size() ); }
@Test public void testPackageWithError() throws Exception { final String source = "package 12 foo.bar.baz"; final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( true, new StringReader( source ) ); assertTrue( parser.hasErrors() ); assertEquals( "foo.bar.baz", pkg.getName() ); }
@Test public void testRuleWithoutEnd() throws Exception { String source = "rule \"Invalid customer id\" \n" + " when \n" + " o: Order( ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n"; parse( "compilationUnit", source ); assertTrue( parser.hasErrors() ); }
public Object execParser( String testRuleName, CharStream charStream ) { try { createParser( charStream ); /** Use Reflection to get rule method from parser */ Method ruleName = null; Object[] params = null; for ( Method method : DRL6Parser.class.getMethods() ) { if ( method.getName().equals( testRuleName ) ) { ruleName = method; Class< ? >[] parameterTypes = method.getParameterTypes(); params = new Object[parameterTypes.length]; } } /** Invoke grammar rule, and get the return value */ Object ruleReturn = ruleName.invoke( parser, params ); if ( parser.hasErrors() ) { System.out.println( parser.getErrorMessages() ); } return ruleReturn; } catch ( Exception e ) { e.printStackTrace(); fail( e.getMessage() ); } return null; }
@Test public void testPackage() throws Exception { final String source = "package foo.bar.baz"; final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( new StringReader( source ) ); assertFalse( parser.hasErrors() ); assertEquals( "foo.bar.baz", pkg.getName() ); }