[jboss-svn-commits] JBL Code SVN: r15129 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/compiler and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Sep 14 16:25:36 EDT 2007


Author: fmeyer
Date: 2007-09-14 16:25:36 -0400 (Fri, 14 Sep 2007)
New Revision: 15129

Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/DrlParser.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/DrlParserTest.java
Log:
JBRULES-1118 Added support for custom resolver

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/DrlParser.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/DrlParser.java	2007-09-14 19:42:53 UTC (rev 15128)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/DrlParser.java	2007-09-14 20:25:36 UTC (rev 15129)
@@ -73,7 +73,7 @@
     //        //return parse( text.toString() );
     //    }
 
-    /** 
+	/** 
      * Parse and build a rule package from a DRL source with a 
      * domain specific language.
      */
@@ -94,13 +94,9 @@
      */
     public PackageDescr parse(final String source,
                               final Reader dsl) throws DroolsParserException {
-        DefaultExpanderResolver resolver;
-        try {
-            resolver = new DefaultExpanderResolver( dsl );
-        } catch ( final IOException e ) {
-            throw new DroolsParserException( "Error parsing the DSL.",
-                                             e );
-        }
+    	
+    	DefaultExpanderResolver resolver = getDefaultResolver(dsl);
+    	
         final Expander expander = resolver.get( "*",
                                           null );
         final String expanded = expander.expand( source );
@@ -118,14 +114,20 @@
      * @throws DroolsParserException If unable to expand in any way.
      */
     public String getExpandedDRL(final String source, final Reader dsl) throws DroolsParserException {
-        DefaultExpanderResolver resolver;
-        try {
-            resolver = new DefaultExpanderResolver( dsl );
-        } catch ( final IOException e ) {
-            throw new DroolsParserException( "Error parsing the DSL.",
-                                             e );
-        }
-        final Expander expander = resolver.get( "*",
+    	DefaultExpanderResolver resolver = getDefaultResolver(dsl);
+        return getExpandedDRL(source, resolver);
+    }
+    
+    /**
+     * This will expand the DRL using the given expander resolver.
+     * useful for debugging.
+     * @param source - the source which use a DSL
+     * @param resolver - the DSL expander resolver itself.
+     * @throws DroolsParserException If unable to expand in any way.
+     */
+    public String getExpandedDRL(final String source, final DefaultExpanderResolver resolver ) throws DroolsParserException {
+
+    	final Expander expander = resolver.get( "*",
                                           null );
         final String expanded = expander.expand( source );
         
@@ -141,6 +143,7 @@
         return expanded;
     }
     
+    
     private StringBuffer getDRLText(final Reader reader) throws IOException {
         final StringBuffer text = new StringBuffer();
 
@@ -211,4 +214,15 @@
     public Location getLocation() {
         return this.location; 
     }
+    
+    public DefaultExpanderResolver getDefaultResolver(final Reader dsl) throws DroolsParserException {
+        DefaultExpanderResolver resolver;
+        try {
+        		resolver = new DefaultExpanderResolver( dsl );
+        } catch ( final IOException e ) {
+            throw new DroolsParserException( "Error parsing the DSL.",
+                                             e );
+        }
+        return resolver;
+	}
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/DrlParserTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/DrlParserTest.java	2007-09-14 19:42:53 UTC (rev 15128)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/DrlParserTest.java	2007-09-14 20:25:36 UTC (rev 15129)
@@ -2,6 +2,12 @@
 
 import java.io.StringReader;
 
+import org.drools.RuntimeDroolsException;
+import org.drools.lang.Expander;
+import org.drools.lang.dsl.DSLMappingFile;
+import org.drools.lang.dsl.DefaultExpander;
+import org.drools.lang.dsl.DefaultExpanderResolver;
+
 import junit.framework.TestCase;
 
 public class DrlParserTest extends TestCase {
@@ -15,7 +21,28 @@
         assertEqualsIgnoreWhitespace( "rule 'foo' \n when \n Something() \n then \n another(); \nend", result );
     }
     
-    
+    public void testExpandDRLUsingInjectedExpander() throws Exception {
+        String dsl = "[condition]Something=Something()\n[then]another=another();";
+        String drl = "rule 'foo' \n when \n Something \n then \n another \nend";
+        
+        
+        DefaultExpanderResolver resolver = new DefaultExpanderResolver(new StringReader(dsl));
+        
+        
+        final DSLMappingFile file = new DSLMappingFile();
+        if ( file.parseAndLoad( new StringReader(dsl) ) ) {
+            final Expander expander = new DefaultExpander();
+            expander.addDSLMapping( file.getMapping() );
+            resolver.addExpander("*", expander);
+        } else {
+            throw new RuntimeDroolsException( "Error parsing and loading DSL file." + file.getErrors() );
+        }        
+
+        DrlParser parser = new DrlParser();
+        String result = parser.getExpandedDRL( drl, resolver);
+        assertEqualsIgnoreWhitespace( "rule 'foo' \n when \n Something() \n then \n another(); \nend", result );
+    }
+
     private void assertEqualsIgnoreWhitespace(final String expected,
                                               final String actual) {
         final String cleanExpected = expected.replaceAll( "\\s+",




More information about the jboss-svn-commits mailing list