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 testEvalWithSemicolon() throws Exception { DRLParser parser = parseResource( "eval_with_semicolon.drl" ); parser.compilationUnit(); assertTrue( parser.hasErrors() ); assertEquals(1, parser.getErrorMessages().size()); assertEquals("ERR 102", parser.getErrors().get(0).getErrorCode()); }
@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 testFromComplexAcessor() throws Exception { String source = "rule \"Invalid customer id\" ruleflow-group \"validate\" lock-on-active true \n" + " when \n" + " o: Order( ) \n" + " not( Customer( ) from customerService.getCustomer(o.getCustomerId()) ) \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.getErrorMessages().toString(), parser.hasErrors() ); RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( "Invalid customer id", rule.getName() ); assertEquals( 2, rule.getLhs().getDescrs().size() ); NotDescr not = (NotDescr) rule.getLhs().getDescrs().get( 1 ); PatternDescr customer = (PatternDescr) not.getDescrs().get( 0 ); assertEquals( "Customer", customer.getObjectType() ); assertEquals( "customerService.getCustomer(o.getCustomerId())", ((FromDescr) customer.getSource()).getDataSource().getText() ); }