[jboss-svn-commits] JBL Code SVN: r18638 - in labs/jbossrules/trunk: drools-compiler/src/test/java/org/drools/integrationtests and 2 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Feb 29 09:55:02 EST 2008


Author: tirelli
Date: 2008-02-29 09:55:02 -0500 (Fri, 29 Feb 2008)
New Revision: 18638

Added:
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Serializable2.drl
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Primitives.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MarshallingTest.java
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java
Log:
JBRULES-1491: fixing serialization issue

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Primitives.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Primitives.java	2008-02-29 14:37:43 UTC (rev 18637)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/Primitives.java	2008-02-29 14:55:02 UTC (rev 18638)
@@ -1,5 +1,7 @@
 package org.drools;
 
+import java.util.Arrays;
+
 /*
  * Copyright 2005 JBoss Inc
  * 
@@ -16,7 +18,10 @@
  * limitations under the License.
  */
 
-public class Primitives {
+public class Primitives implements java.io.Serializable {
+
+    private static final long serialVersionUID = -3006488134941876318L;
+
     private boolean  booleanPrimitive;
 
     private char     charPrimitive;
@@ -155,4 +160,56 @@
         this.object = object;
     }
 
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + Arrays.hashCode( arrayAttribute );
+        result = prime * result + (booleanPrimitive ? 1231 : 1237);
+        result = prime * result + ((booleanWrapper == null) ? 0 : booleanWrapper.hashCode());
+        result = prime * result + charPrimitive;
+        result = prime * result + Float.floatToIntBits( doublePrimitive );
+        result = prime * result + Float.floatToIntBits( floatPrimitive );
+        result = prime * result + intPrimitive;
+        result = prime * result + (int) (longPrimitive ^ (longPrimitive >>> 32));
+        result = prime * result + ((object == null) ? 0 : object.hashCode());
+        result = prime * result + Arrays.hashCode( primitiveArrayAttribute );
+        result = prime * result + shortPrimitive;
+        result = prime * result + Arrays.hashCode( stringArray );
+        result = prime * result + ((stringAttribute == null) ? 0 : stringAttribute.hashCode());
+        return result;
+    }
+
+    public boolean equals(Object obj) {
+        if ( this == obj ) return true;
+        if ( obj == null ) return false;
+        if ( getClass() != obj.getClass() ) return false;
+        final Primitives other = (Primitives) obj;
+        if ( !Arrays.equals( arrayAttribute,
+                             other.arrayAttribute ) ) return false;
+        if ( booleanPrimitive != other.booleanPrimitive ) return false;
+        if ( booleanWrapper == null ) {
+            if ( other.booleanWrapper != null ) return false;
+        } else if ( !booleanWrapper.equals( other.booleanWrapper ) ) return false;
+        if ( bytePrimitive != other.bytePrimitive ) return false;
+        if ( charPrimitive != other.charPrimitive ) return false;
+        if ( Float.floatToIntBits( doublePrimitive ) != Float.floatToIntBits( other.doublePrimitive ) ) return false;
+        if ( Float.floatToIntBits( floatPrimitive ) != Float.floatToIntBits( other.floatPrimitive ) ) return false;
+        if ( intPrimitive != other.intPrimitive ) return false;
+        if ( longPrimitive != other.longPrimitive ) return false;
+        if ( object == null ) {
+            if ( other.object != null ) return false;
+        } else if ( !object.equals( other.object ) ) return false;
+        if ( !Arrays.equals( primitiveArrayAttribute,
+                             other.primitiveArrayAttribute ) ) return false;
+        if ( shortPrimitive != other.shortPrimitive ) return false;
+        if ( !Arrays.equals( stringArray,
+                             other.stringArray ) ) return false;
+        if ( stringAttribute == null ) {
+            if ( other.stringAttribute != null ) return false;
+        } else if ( !stringAttribute.equals( other.stringAttribute ) ) return false;
+        return true;
+    }
+    
+    
+
 }
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MarshallingTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MarshallingTest.java	2008-02-29 14:37:43 UTC (rev 18637)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MarshallingTest.java	2008-02-29 14:55:02 UTC (rev 18638)
@@ -16,6 +16,7 @@
 
 import org.drools.Cheese;
 import org.drools.Person;
+import org.drools.Primitives;
 import org.drools.RuleBase;
 import org.drools.RuleBaseConfiguration;
 import org.drools.RuleBaseFactory;
@@ -370,6 +371,70 @@
         
     }
 
+    public void testSerializationOfIndexedWM() throws Exception {
+        final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_Serializable2.drl" ) );
+
+        final PackageBuilder builder = new PackageBuilder();
+        builder.addPackageFromDrl( reader );
+        final Package pkg = builder.getPackage();
+
+        assertEquals( builder.getErrors().toString(),
+                      0,
+                      builder.getErrors().getErrors().length );
+
+        RuleBase ruleBase = getRuleBase();// RuleBaseFactory.newRuleBase();
+
+        ruleBase.addPackage( pkg );
+
+        Map map = new HashMap();
+        map.put( "x",
+                 ruleBase );
+        final byte[] ast = serializeOut( map );
+        map = (Map) serializeIn( ast );
+        ruleBase = (RuleBase) map.get( "x" );
+        final Rule[] rules = ruleBase.getPackages()[0].getRules();
+        assertEquals( 3,
+                      rules.length );
+
+        WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+        workingMemory.setGlobal( "list",
+                                 new ArrayList() );
+
+        final Primitives p = new Primitives( );
+        p.setBytePrimitive( (byte) 1 );
+        p.setShortPrimitive( (short) 2 );
+        p.setIntPrimitive( (int) 3 );
+        workingMemory.insert( p );
+
+        final byte[] wm = serializeOut( workingMemory );
+
+        workingMemory = ruleBase.newStatefulSession( new ByteArrayInputStream( wm ) );
+
+        assertEquals( 1,
+                      IteratorToList.convert( workingMemory.iterateObjects() ).size() );
+        assertEquals( p,
+                      IteratorToList.convert( workingMemory.iterateObjects() ).get( 0 ) );
+
+        assertEquals( 3,
+                      workingMemory.getAgenda().agendaSize() );
+
+        workingMemory.fireAllRules();
+
+        final List list = (List) workingMemory.getGlobal( "list" );
+
+        assertEquals( 3,
+                      list.size() );
+        // because of agenda-groups
+        assertEquals( "1",
+                      list.get( 0 ) );
+        assertEquals( "2",
+                      list.get( 1 ) );
+        assertEquals( "3",
+                      list.get( 2 ) );
+
+    }
+
     protected RuleBase getRuleBase() throws Exception {
 
         return RuleBaseFactory.newRuleBase( RuleBase.RETEOO,

Copied: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Serializable2.drl (from rev 18637, labs/jbossrules/branches/4.0.x/drools-compiler/src/test/resources/org/drools/integrationtests/test_Serializable2.drl)
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Serializable2.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/test_Serializable2.drl	2008-02-29 14:55:02 UTC (rev 18638)
@@ -0,0 +1,31 @@
+package org.drools;
+
+global java.util.List list
+
+rule "single index"
+	salience 10
+    when
+        Primitives( $b : bytePrimitive )
+        Primitives( bytePrimitive == $b )
+    then
+		list.add( "1" );
+end    
+
+rule "double index"
+	salience 8
+    when
+        Primitives( $b : bytePrimitive, $s : shortPrimitive )
+        Primitives( bytePrimitive == $b, shortPrimitive == $s )
+    then
+		list.add( "2" );
+end    
+
+rule "triple index"
+	salience 6
+    when
+        Primitives( $b : bytePrimitive, $s : shortPrimitive, $i : intPrimitive )
+        Primitives( bytePrimitive == $b, shortPrimitive == $s, intPrimitive == $i )
+    then
+		list.add( "3" );
+end    
+

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java	2008-02-29 14:37:43 UTC (rev 18637)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/AbstractHashTable.java	2008-02-29 14:55:02 UTC (rev 18638)
@@ -450,7 +450,10 @@
         }
     }
 
-    public static class FieldIndex {
+    public static class FieldIndex implements Serializable {
+
+        private static final long serialVersionUID = 1020010166351582645L;
+        
         FieldExtractor   extractor;
         Declaration      declaration;
         public Evaluator evaluator;
@@ -477,7 +480,7 @@
         }
     }
 
-    public static interface Index {        
+    public static interface Index extends Serializable {        
         public FieldIndex getFieldIndex(int index);
         
         public int hashCodeOf(ReteTuple tuple);
@@ -498,6 +501,8 @@
         implements
         Index {
 
+        private static final long serialVersionUID = -1022777958435032326L;
+        
         private FieldExtractor extractor;
         private Declaration    declaration;
         private Evaluator      evaluator;
@@ -575,6 +580,9 @@
     public static class DoubleCompositeIndex
         implements
         Index {
+
+        private static final long serialVersionUID = 5453765340969897686L;
+        
         private FieldIndex index0;
         private FieldIndex index1;
 
@@ -671,6 +679,9 @@
     public static class TripleCompositeIndex
         implements
         Index {
+        
+        private static final long serialVersionUID = 7743486670399440233L;
+        
         private FieldIndex index0;
         private FieldIndex index1;
         private FieldIndex index2;




More information about the jboss-svn-commits mailing list