[jboss-svn-commits] JBL Code SVN: r21075 - in labs/jbossrules/trunk: drools-core/src/main/java/org/drools/common and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jul 16 23:47:27 EDT 2008


Author: mark.proctor at jboss.com
Date: 2008-07-16 23:47:26 -0400 (Wed, 16 Jul 2008)
New Revision: 21075

Modified:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaDialect.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeData.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeRegistry.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/JavaDialectRuntimeData.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MVELDialectRuntimeData.java
Log:
JBRULES-1684 Package merging is incorrect for dialects

Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaDialect.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaDialect.java	2008-07-17 02:43:54 UTC (rev 21074)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/rule/builder/dialect/java/JavaDialect.java	2008-07-17 03:47:26 UTC (rev 21075)
@@ -31,6 +31,7 @@
 import org.drools.compiler.PackageBuilder.ProcessInvokerErrorHandler;
 import org.drools.compiler.PackageBuilder.RuleErrorHandler;
 import org.drools.compiler.PackageBuilder.RuleInvokerErrorHandler;
+import org.drools.compiler.PackageBuilder.SrcErrorHandler;
 import org.drools.lang.descr.AccumulateDescr;
 import org.drools.lang.descr.AndDescr;
 import org.drools.lang.descr.BaseDescr;
@@ -128,7 +129,7 @@
     private Map                                      errorHandlers;
     private List                                     results;
     private PackageBuilder                           packageBuilder;
-    
+
     private PackageRegistry                          packageRegistry;
 
     private ClassFieldAccessorCache                  classFieldExtractorCache;
@@ -139,7 +140,7 @@
         this.packageBuilder = builder;
         this.pkg = pkg;
         this.packageRegistry = pkgRegistry;
-        
+
         this.configuration = (JavaDialectConfiguration) builder.getPackageBuilderConfiguration().getDialectConfiguration( "java" );
         this.classFieldExtractorCache = builder.getClassFieldExtractorCache();
 
@@ -389,13 +390,13 @@
                            dumpDir );
         }
 
-        //        for (String clazz : classes) {
-        //            System.out.println("---" + clazz + "----");
-        //            for (byte b : src.getBytes(clazz)) {
-        //                System.out.print((char) b);
-        //            }
-        //            System.out.println("--- END: " + clazz + "----");
-        //        }
+//        for ( String clazz : classes ) {
+//            System.out.println( "---" + clazz + "----" );
+//            for ( byte b : src.getBytes( clazz ) ) {
+//                System.out.print( (char) b );
+//            }
+//            System.out.println( "--- END: " + clazz + "----" );
+//        }
 
         final CompilationResult result = this.compiler.compile( classes,
                                                                 this.src,
@@ -640,6 +641,19 @@
         this.packageRegistry.addStaticImport( functionClassName + "." + functionDescr.getName() );
     }
 
+    public void addSrc(String resourceName,
+                       byte[] content) {
+
+        src.add( resourceName,
+                 content );
+
+        this.errorHandlers.put( resourceName,
+                                new SrcErrorHandler( "Src compile error" ) );
+
+        addClassName( resourceName );
+
+    }
+
     /**
      * This adds a compile "task" for when the compiler of
      * semantics (JCI) is called later on with compileAll()\

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java	2008-07-17 02:43:54 UTC (rev 21074)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java	2008-07-17 03:47:26 UTC (rev 21075)
@@ -428,56 +428,47 @@
 
             this.eventSupport.fireBeforePackageAdded( newPkg );
 
+            // create new base package if it doesn't exist, as we always merge the newPkg into the existing one, 
+            // to isolate the base package from further possible changes to newPkg.
             if ( pkg == null ) {
+                // create a new package, use the same parent classloader as the incoming new package
                 pkg = new Package( newPkg.getName(),
-                                   newPkg.getPackageScopeClassLoader() );
+                                   new MapBackedClassLoader( newPkg.getPackageScopeClassLoader().getParent() ) );
+                                   //newPkg.getPackageScopeClassLoader() );
                 pkgs.put( pkg.getName(),
                           pkg );
+                // add the dialect registry composite classloader (which uses the above classloader as it's parent)
                 this.packageClassLoader.addClassLoader( pkg.getDialectRuntimeRegistry().getClassLoader() );
-            } else {
-                pkg.getPackageScopeClassLoader().getStore().putAll( newPkg.getPackageScopeClassLoader().getStore() );
-                this.packageClassLoader.addClassLoader( newPkg.getDialectRuntimeRegistry().getClassLoader() );
-            }
+            } 
+//            else {
+//
+//                this.packageClassLoader.addClassLoader( newPkg.getDialectRuntimeRegistry().getClassLoader() );
+//            }
+            
+            // now merge the new package into the existing one
+            mergePackage( pkg, newPkg );
 
-            final Map<String, Class> newGlobals = newPkg.getGlobals();
-
-            if ( newGlobals != null ) {
-                // Check that the global data is valid, we cannot change the type
-                // of an already declared global variable
-                for ( final Map.Entry<String, Class> entry : newGlobals.entrySet() ) {
-                    final String identifier = entry.getKey();
-                    final Class type = entry.getValue();
-                    if ( this.globals.containsKey( identifier ) && !this.globals.get( identifier ).equals( type ) ) {
-                        throw new PackageIntegrationException( pkg );
-                    } else {
-                        this.globals.put( identifier,
-                                          type );
-                    }
-                }
-            }
-
-            mergePackage( pkg,
-                          newPkg );
-
+            // Add the type declarations to the RuleBase
             if ( newPkg.getTypeDeclarations() != null ) {
-                // Add type declarations
+                // add type declarations
                 for ( TypeDeclaration type : newPkg.getTypeDeclarations().values() ) {
-                    // should we allow overrides?
+                    // @TODO should we allow overrides? only if the class is not in use.
                     if ( !this.classTypeDeclaration.containsKey( type.getTypeClass() ) ) {
+                        // add to rulebase list of type declarations                        
                         this.classTypeDeclaration.put( type.getTypeClass(),
                                                        type );
                     }
                 }
-            }
+            } 
 
+            // add the rules to the RuleBase
             final Rule[] rules = newPkg.getRules();
-
             for ( int i = 0; i < rules.length; ++i ) {
                 addRule( newPkg,
                          rules[i] );
             }
-
-            //and now the rule flows
+            
+            // add the flows to the RuleBase
             if ( newPkg.getRuleFlows() != null ) {
                 final Map flows = newPkg.getRuleFlows();
                 for ( final Object object : newPkg.getRuleFlows().entrySet() ) {
@@ -504,45 +495,43 @@
      * and the actual Rule objects into the package).
      */
     private void mergePackage(final Package pkg,
-                              final Package newPkg) {
-        final Map<String, ImportDeclaration> imports = pkg.getImports();
-
+                              final Package newPkg) {        
         // Merge imports
+        final Map<String, ImportDeclaration> imports = pkg.getImports();
         imports.putAll( newPkg.getImports() );
 
-        if ( newPkg.getGlobals() != null ) {
-            final Map<String, Class> globals = pkg.getGlobals();
+        // merge globals
+        if ( newPkg.getGlobals() != null || newPkg.getGlobals() != Collections.EMPTY_MAP ) {
+            Map<String, Class> globals = pkg.getGlobals();
             // Add globals
             for ( final Map.Entry<String, Class> entry : newPkg.getGlobals().entrySet() ) {
                 final String identifier = entry.getKey();
                 final Class type = entry.getValue();
                 if ( globals.containsKey( identifier ) && !globals.get( identifier ).equals( type ) ) {
                     throw new PackageIntegrationException( pkg );
-                } else if ( globals == Collections.EMPTY_MAP ) {
+                } else {
                     pkg.addGlobal( identifier,
                                    type );
-                } else {
-                    globals.put( identifier,
-                                 type );
-                }
+                } 
             }
         }
-
+        
+        // merge the type declarations
         if ( newPkg.getTypeDeclarations() != null ) {
             // add type declarations
             for ( TypeDeclaration type : newPkg.getTypeDeclarations().values() ) {
-                // should we allow overrides?
-                if ( !this.classTypeDeclaration.containsKey( type.getTypeClass() ) ) {
-                    this.classTypeDeclaration.put( type.getTypeClass(),
-                                                   type );
-                }
+                // @TODO should we allow overrides? only if the class is not in use.
                 if ( !pkg.getTypeDeclarations().containsKey( type.getTypeName() ) ) {
+                    // add to package list of type declarations
                     pkg.addTypeDeclaration( type );
                 }
             }
-        }
-
-        //Add rules into the RuleBase package
+        }              
+        
+        // merge the contents of the MapBackedClassloader, that is the root of the dialect registry's composite classloader.
+        pkg.getPackageScopeClassLoader().getStore().putAll( newPkg.getPackageScopeClassLoader().getStore() );        
+                    
+        //Merge rules into the RuleBase package
         //as this is needed for individual rule removal later on
         final Rule[] newRules = newPkg.getRules();
         for ( int i = 0; i < newRules.length; i++ ) {
@@ -555,16 +544,16 @@
             }
 
             pkg.addRule( newRule );
-        }
-
-        //and now the rule flows
+        }           
+        
+        //Merge The Rule Flows
         if ( newPkg.getRuleFlows() != null ) {
             final Map flows = newPkg.getRuleFlows();
             for ( final Iterator iter = flows.values().iterator(); iter.hasNext(); ) {
                 final Process flow = (Process) iter.next();
                 pkg.addProcess( flow );
             }
-        }
+        }            
 
         pkg.getDialectRuntimeRegistry().merge( newPkg.getDialectRuntimeRegistry() );
 
@@ -574,6 +563,8 @@
             }
         }
 
+        // this handles re-wiring any dirty Packages, it's done lazily to allow incremental 
+        // additions without incurring the repeated cost.
         if ( this.reloadPackageCompilationData == null ) {
             this.reloadPackageCompilationData = new ReloadPackageCompilationData();
         }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeData.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeData.java	2008-07-17 02:43:54 UTC (rev 21074)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeData.java	2008-07-17 03:47:26 UTC (rev 21075)
@@ -5,7 +5,7 @@
 
     public void removeFunction(Package pkg, Function function);
 
-    public void merge(DialectRuntimeData newData);
+    public void merge(DialectRuntimeRegistry registry, DialectRuntimeData newData);
 
     public boolean isDirty();
 
@@ -13,5 +13,5 @@
 
     public void reload();
 
-    public DialectRuntimeData clone();
+    public DialectRuntimeData clone(DialectRuntimeRegistry registry);
 }

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeRegistry.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeRegistry.java	2008-07-17 02:43:54 UTC (rev 21074)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DialectRuntimeRegistry.java	2008-07-17 03:47:26 UTC (rev 21075)
@@ -94,12 +94,12 @@
         for ( Entry<String, DialectRuntimeData> entry : newDatas.dialects.entrySet() ) {
             DialectRuntimeData data = this.dialects.get( entry.getKey() );
             if ( data == null ) {
-                DialectRuntimeData dialectData = entry.getValue().clone();
+                DialectRuntimeData dialectData = entry.getValue().clone( this );
                 //dialectData.setDialectRuntimeRegistry( this );
                 this.dialects.put( entry.getKey(),
                                    dialectData );
             } else {
-                data.merge( entry.getValue() );
+                data.merge( null, entry.getValue() );
             }
         }
 

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/JavaDialectRuntimeData.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/JavaDialectRuntimeData.java	2008-07-17 02:43:54 UTC (rev 21074)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/JavaDialectRuntimeData.java	2008-07-17 03:47:26 UTC (rev 21075)
@@ -91,10 +91,10 @@
         this.dirty = false;
     }
 
-    public DialectRuntimeData clone() {
-        DialectRuntimeData cloneOne = new JavaDialectRuntimeData();
+    public DialectRuntimeData clone(DialectRuntimeRegistry registry) {
+        DialectRuntimeData cloneOne = new JavaDialectRuntimeData(registry);
 
-        cloneOne.merge(this);
+        cloneOne.merge(registry, this);
         return cloneOne;
     }
 
@@ -168,17 +168,9 @@
         remove( pkg.getName() + "." + StringUtils.ucFirst( function.getName() ) );
     }
 
-    public void merge(DialectRuntimeData newData) {
-        JavaDialectRuntimeData newJavaData = (JavaDialectRuntimeData) newData;
+    public void merge(DialectRuntimeRegistry registry, DialectRuntimeData newData) {
+        JavaDialectRuntimeData newJavaData = (JavaDialectRuntimeData) newData;        
 
-        this.datas = newJavaData.datas;
-        
-        this.dirty = newData.isDirty();
-        if (this.classLoader == null) {
-            this.classLoader    = new PackageClassLoader(newJavaData.getClassLoader().getParent(), this);
-            this.dirty = true;
-        }
-
         // First update the binary files
         // @todo: this probably has issues if you add classes in the incorrect order - functions, rules, invokers.
         for ( String file : newJavaData.list()) {

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MVELDialectRuntimeData.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MVELDialectRuntimeData.java	2008-07-17 02:43:54 UTC (rev 21074)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MVELDialectRuntimeData.java	2008-07-17 03:47:26 UTC (rev 21075)
@@ -26,9 +26,9 @@
 		this.functionFactory = new MapFunctionResolverFactory();
 	}
 
-    public DialectRuntimeData clone() {
-        DialectRuntimeData clone = new MVELDialectRuntimeData();
-        clone.merge(this);
+    public DialectRuntimeData clone(DialectRuntimeRegistry registry) {
+        DialectRuntimeData clone = new MVELDialectRuntimeData(registry);
+        clone.merge(registry, this);
         return clone;
     }
 
@@ -55,7 +55,7 @@
     public void setDirty(boolean dirty) {
     }
 
-    public void merge(DialectRuntimeData newData) {
+    public void merge(DialectRuntimeRegistry registry, DialectRuntimeData newData) {
 	}
 
 	public void reload() {




More information about the jboss-svn-commits mailing list