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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Mar 2 03:41:29 EST 2009


Author: Rikkola
Date: 2009-03-02 03:41:29 -0500 (Mon, 02 Mar 2009)
New Revision: 25468

Modified:
   labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandler.java
   labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandlerTest.java
Log:
GUVNOR-110: category rules not inherited

Modified: labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandler.java	2009-03-01 18:18:47 UTC (rev 25467)
+++ labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandler.java	2009-03-02 08:41:29 UTC (rev 25468)
@@ -1,4 +1,5 @@
 package org.drools.guvnor.server.contenthandler;
+
 /*
  * Copyright 2005 JBoss Inc
  *
@@ -15,8 +16,6 @@
  * limitations under the License.
  */
 
-
-
 import java.io.IOException;
 import java.io.StringReader;
 import java.util.StringTokenizer;
@@ -26,53 +25,71 @@
 import org.drools.guvnor.server.builder.ContentPackageAssembler;
 import org.drools.repository.AssetItem;
 
-public class DRLFileContentHandler extends PlainTextContentHandler implements IRuleAsset {
+public class DRLFileContentHandler extends PlainTextContentHandler
+    implements
+    IRuleAsset {
 
-    public void compile(BRMSPackageBuilder builder, AssetItem asset, ContentPackageAssembler.ErrorLogger logger) throws DroolsParserException, IOException {
-        builder.addPackageFromDrl( new StringReader(getContent( asset )) );
+    public void compile(BRMSPackageBuilder builder,
+                        AssetItem asset,
+                        ContentPackageAssembler.ErrorLogger logger) throws DroolsParserException,
+                                                                   IOException {
+        builder.addPackageFromDrl( new StringReader( getContent( asset ) ) );
     }
 
     private String getContent(AssetItem asset) {
         String content = asset.getContent();
-        if (isStandAloneRule( content )) {
-            content = wrapRuleDeclaration( asset.getName(), content );
+        if ( isStandAloneRule( content ) ) {
+
+            String parentName = this.parentNameFromCategory( asset,
+                                                             "" );
+            content = wrapRuleDeclaration( asset.getName(),
+                                           parentName,
+                                           content );
         }
         return content;
     }
 
-    String wrapRuleDeclaration(String name, String content) {
-        return "rule '" + name + "'\n"  + getContent(content) + "\nend";
+    String wrapRuleDeclaration(String name,
+                               String parentName,
+                               String content) {
+        if ( parentName == null || "".equals( parentName ) ) {
+            return "rule '" + name + "'\n" + getContent( content ) + "\nend";
+        } else {
+            return "rule '" + name + "' extends " + parentName + "\n" + getContent( content ) + "\nend";
+        }
     }
 
     String getContent(String content) {
-		if (content != null && content.indexOf("dialect") == -1) {
-			return "dialect 'mvel'\n" + content;
-		}
-		return content;
-	}
+        if ( content != null && content.indexOf( "dialect" ) == -1 ) {
+            return "dialect 'mvel'\n" + content;
+        }
+        return content;
+    }
 
-	/**
+    /**
      * This will try and sniff ouf if its a stand alone rule which
      * will use the asset name as the rule name, or if it should be treated as a package
      * (in the latter case, the content is passed as it to the compiler).
      */
     public static boolean isStandAloneRule(String content) {
-        if (content == null || "".equals( content.trim() )) {
+        if ( content == null || "".equals( content.trim() ) ) {
             return false;
         }
-        StringTokenizer st = new StringTokenizer(content, "\n\r");
-        while (st.hasMoreTokens()) {
+        StringTokenizer st = new StringTokenizer( content,
+                                                  "\n\r" );
+        while ( st.hasMoreTokens() ) {
             String tok = st.nextToken().trim();
-            if (tok.startsWith("when")) {
-            	//well obviously it is stand alone...
-            	return true;
+            if ( tok.startsWith( "when" ) ) {
+                //well obviously it is stand alone...
+                return true;
             }
             //otherwise sniff for a suitable keyword at the start of a line
-            if (startsWithWord( "package", tok ) ||
-                    startsWithWord( "rule", tok ) ||
-                    startsWithWord( "end", tok ) ||
-                    startsWithWord( "function", tok ) ||
-                    startsWithWord( "query", tok )) {
+            if ( startsWithWord( "package",
+                                 tok ) || startsWithWord( "rule",
+                                                          tok ) || startsWithWord( "end",
+                                                                                   tok ) || startsWithWord( "function",
+                                                                                                            tok ) || startsWithWord( "query",
+                                                                                                                                     tok ) ) {
                 return false;
             }
         }
@@ -80,20 +97,27 @@
 
     }
 
-    static boolean startsWithWord(String word, String sentence) {
-    	String[] words = sentence.trim().split("\\s");
-    	if (words.length > 0) {
-    		return words[0].equals(word);
-    	} else {
-    		return false;
-    	}
+    static boolean startsWithWord(String word,
+                                  String sentence) {
+        String[] words = sentence.trim().split( "\\s" );
+        if ( words.length > 0 ) {
+            return words[0].equals( word );
+        } else {
+            return false;
+        }
     }
 
-    public void assembleDRL(BRMSPackageBuilder builder, AssetItem asset, StringBuffer buf) {
+    public void assembleDRL(BRMSPackageBuilder builder,
+                            AssetItem asset,
+                            StringBuffer buf) {
         String content = asset.getContent();
         boolean standAlone = isStandAloneRule( content );
-        if (standAlone) {
-            buf.append( wrapRuleDeclaration( asset.getName(), content ) );
+        if ( standAlone ) {
+            String parentName = this.parentNameFromCategory( asset,
+                                                             "" );
+            buf.append( wrapRuleDeclaration( asset.getName(),
+                                             parentName,
+                                             content ) );
         } else {
             buf.append( content );
         }

Modified: labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandlerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandlerTest.java	2009-03-01 18:18:47 UTC (rev 25467)
+++ labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/contenthandler/DRLFileContentHandlerTest.java	2009-03-02 08:41:29 UTC (rev 25468)
@@ -20,97 +20,101 @@
 
 public class DRLFileContentHandlerTest extends TestCase {
 
-	public void testSniffDRLType() throws Exception {
+    public void testSniffDRLType() throws Exception {
 
-		DRLFileContentHandler h = new DRLFileContentHandler();
+        DRLFileContentHandler h = new DRLFileContentHandler();
 
-		// in this case we have package, and N rules
-		String classic = "package foobar \n rule boo \n when \n then\n end \n rule boo2 \n when \n then\n end";
+        // in this case we have package, and N rules
+        String classic = "package foobar \n rule boo \n when \n then\n end \n rule boo2 \n when \n then\n end";
 
-		// in this case we just have rules
-		String moreRuleClassic = "\nrule bar \n when \n then \n end\nrule x \n when \n then \n end ";
+        // in this case we just have rules
+        String moreRuleClassic = "\nrule bar \n when \n then \n end\nrule x \n when \n then \n end ";
 
-		// in this case we just have a single rule
-		String newRule = "agenda-group 'x' \n when \n then \n";
+        // in this case we just have a single rule
+        String newRule = "agenda-group 'x' \n when \n then \n";
 
-		String moreSingle = "rule foo when then end";
+        String moreSingle = "rule foo when then end";
 
-		String query = "query FooBar\nend";
+        String query = "query FooBar\nend";
 
-		String moreNewRule = "agenda-group 'x' \n when end.bar \n then rule.end.bar";
+        String moreNewRule = "agenda-group 'x' \n when end.bar \n then rule.end.bar";
 
-		String emptyRule = "";
+        String emptyRule = "";
 
-		String complex = "#some comments about the rule \n#here\n when \n #goo \n foo.bar \n then \n #goo \n end.bar";
+        String complex = "#some comments about the rule \n#here\n when \n #goo \n foo.bar \n then \n #goo \n end.bar";
 
-		assertTrue(h.isStandAloneRule(newRule));
-		assertFalse(h.isStandAloneRule(moreRuleClassic));
-		assertFalse(h.isStandAloneRule(classic));
-		assertFalse(h.isStandAloneRule(moreSingle));
-		assertFalse(h.isStandAloneRule(null));
-		assertFalse(h.isStandAloneRule(emptyRule));
-		assertTrue(h.isStandAloneRule(moreNewRule));
-		assertTrue(h.isStandAloneRule(complex));
-		assertFalse(h.isStandAloneRule(query));
+        assertTrue( h.isStandAloneRule( newRule ) );
+        assertFalse( h.isStandAloneRule( moreRuleClassic ) );
+        assertFalse( h.isStandAloneRule( classic ) );
+        assertFalse( h.isStandAloneRule( moreSingle ) );
+        assertFalse( h.isStandAloneRule( null ) );
+        assertFalse( h.isStandAloneRule( emptyRule ) );
+        assertTrue( h.isStandAloneRule( moreNewRule ) );
+        assertTrue( h.isStandAloneRule( complex ) );
+        assertFalse( h.isStandAloneRule( query ) );
 
-	}
+    }
 
     public void testRuleWithDialect() {
-       String rule = "rule \"DemoRule\" \n "+
-                    "    salience 10 \n" +
-                    "    dialect \"mvel\" \n " +
-                    " when \n" +
-                    " Driver( age > 65 ) \n" +
-                    " then \n" +
-                    " insert(new Rejection(\" too old \"));" +
-                    "end ";
-       DRLFileContentHandler h = new DRLFileContentHandler();
-       assertFalse(h.isStandAloneRule( rule ));
+        String rule = "rule \"DemoRule\" \n " + "    salience 10 \n" + "    dialect \"mvel\" \n " + " when \n" + " Driver( age > 65 ) \n" + " then \n" + " insert(new Rejection(\" too old \"));" + "end ";
+        DRLFileContentHandler h = new DRLFileContentHandler();
+        assertFalse( h.isStandAloneRule( rule ) );
 
-       assertFalse(h.isStandAloneRule( "" ));
+        assertFalse( h.isStandAloneRule( "" ) );
 
     }
 
     public void testStandAlone() throws Exception {
-    	String rule = "when \nFoo()\nthen\n\tbar()";
+        String rule = "when \nFoo()\nthen\n\tbar()";
         DRLFileContentHandler h = new DRLFileContentHandler();
-        assertTrue(h.isStandAloneRule( rule ));
+        assertTrue( h.isStandAloneRule( rule ) );
 
-        String r = h.wrapRuleDeclaration("whee", rule);
-        assertTrue(r.indexOf("rule 'whee'") > -1);
-        assertTrue(r.indexOf("dialect 'mvel'") > -1);
+        String r = h.wrapRuleDeclaration( "whee",
+                                          "",
+                                          rule );
+        assertTrue( r.indexOf( "rule 'whee'" ) > -1 );
+        assertTrue( r.indexOf( "dialect 'mvel'" ) > -1 );
 
         rule = "dialect 'java'\nwhen \nFoo()\nthen\n\tbar()";
-        r = h.wrapRuleDeclaration("whee", rule);
-        assertTrue(r.indexOf("rule 'whee'") > -1);
-        assertEquals(-1, r.indexOf("dialect 'mvel'"));
-        assertTrue(r.indexOf("dialect 'java'") > -1);
+        r = h.wrapRuleDeclaration( "whee",
+                                   null,
+                                   rule );
+        assertTrue( r.indexOf( "rule 'whee'" ) > -1 );
+        assertEquals( -1,
+                      r.indexOf( "dialect 'mvel'" ) );
+        assertTrue( r.indexOf( "dialect 'java'" ) > -1 );
 
+    }
 
+    /**
+     * Tests a rule that inherits another.
+     * @throws Exception 
+     */
+    public void testStandAloneWithExtends() throws Exception {
+        String rule = "when \nFoo()\nthen\n\tbar()";
+        DRLFileContentHandler h = new DRLFileContentHandler();
+        assertTrue( h.isStandAloneRule( rule ) );
 
+        String r = h.wrapRuleDeclaration( "whee",
+                                          "parentRule",
+                                          rule );
+        assertTrue( r.indexOf( "extends parentRule" ) > -1 );
 
+        rule = "dialect 'java'\nwhen \nFoo()\nthen\n\tbar()";
+        r = h.wrapRuleDeclaration( "whee",
+                                   "",
+                                   rule );
+        assertTrue( r.indexOf( "extends" ) == -1 );
+
     }
 
     public void testRuleWithRuleFlowGroup() {
-        String rule = "rule \"DemoRule\" \n "+
-        "    ruleflow-group \"name-of-ruleflow\"  \n" +
-        "    dialect \"mvel\" \n " +
-        " when \n" +
-        " Driver( age > 65 ) \n" +
-        " then \n" +
-        " insert(new Rejection(\" too old \"));" +
-        "end ";
-		DRLFileContentHandler h = new DRLFileContentHandler();
-		assertFalse(h.isStandAloneRule( rule ));
+        String rule = "rule \"DemoRule\" \n " + "    ruleflow-group \"name-of-ruleflow\"  \n" + "    dialect \"mvel\" \n " + " when \n" + " Driver( age > 65 ) \n" + " then \n" + " insert(new Rejection(\" too old \"));" + "end ";
+        DRLFileContentHandler h = new DRLFileContentHandler();
+        assertFalse( h.isStandAloneRule( rule ) );
 
-		rule =
-        "    ruleflow-group \"name-of-ruleflow\"  \n" +
-        "    dialect \"mvel\" \n " +
-        " when \n" +
-        " Driver( age > 65 ) \n" +
-        " then \n" +
-        " insert(new Rejection(\" too old \"));";
-		assertTrue(h.isStandAloneRule( rule ));
+        rule = "    ruleflow-group \"name-of-ruleflow\"  \n" + "    dialect \"mvel\" \n " + " when \n" + " Driver( age > 65 ) \n" + " then \n" + " insert(new Rejection(\" too old \"));";
+        assertTrue( h.isStandAloneRule( rule ) );
     }
 
 }
\ No newline at end of file




More information about the jboss-svn-commits mailing list