[jboss-svn-commits] JBL Code SVN: r17872 - in labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools: reteoo and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jan 15 13:11:43 EST 2008


Author: tirelli
Date: 2008-01-15 13:11:43 -0500 (Tue, 15 Jan 2008)
New Revision: 17872

Added:
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/ConcurrentNodeMemories.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/NodeMemories.java
Modified:
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
   labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java
Log:
JBRULES-1390: adding support to multiple node memory strategies

Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2008-01-15 17:10:24 UTC (rev 17871)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -79,7 +79,6 @@
 import org.drools.temporal.SessionClock;
 import org.drools.util.JavaIteratorAdapter;
 import org.drools.util.ObjectHashMap;
-import org.drools.util.PrimitiveLongMap;
 import org.drools.util.AbstractHashTable.HashTableIterator;
 
 /**
@@ -98,6 +97,7 @@
     // Constants
     // ------------------------------------------------------------
     protected static final Class[]                       ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES = new Class[]{PropertyChangeListener.class};
+    private static final int                             NODE_MEMORIES_ARRAY_GROWTH                    = 32;
 
     // ------------------------------------------------------------
     // Instance members
@@ -108,8 +108,8 @@
     protected final Object[]                             addRemovePropertyChangeListenerArgs           = new Object[]{this};
 
     /** The actual memory for the <code>JoinNode</code>s. */
-    protected final PrimitiveLongMap                     nodeMemories                                  = new PrimitiveLongMap( 32,
-                                                                                                                               8 );
+    protected final NodeMemories                         nodeMemories;
+
     /** Object-to-handle mapping. */
     private final ObjectHashMap                          assertMap;
     private final ObjectHashMap                          identityMap;
@@ -190,6 +190,8 @@
         this.globalResolver = new MapGlobalResolver();
         this.maintainTms = this.ruleBase.getConfiguration().isMaintainTms();
         this.sequential = this.ruleBase.getConfiguration().isSequential();
+        
+        this.nodeMemories = new ConcurrentNodeMemories( this.ruleBase );
 
         if ( this.maintainTms ) {
             this.tms = new TruthMaintenanceSystem( this );
@@ -227,6 +229,7 @@
 
     public void setRuleBase(final InternalRuleBase ruleBase) {
         this.ruleBase = ruleBase;
+        this.nodeMemories.setRuleBaseReference( this.ruleBase );
     }
 
     public void setWorkingMemoryEventSupport(WorkingMemoryEventSupport workingMemoryEventSupport) {
@@ -684,18 +687,18 @@
      * @see WorkingMemory
      */
     public FactHandle insert(final Object object) throws FactException {
-        return insert( object,  /* Not-Dynamic */
-        			   0,
+        return insert( object, /* Not-Dynamic */
+                       0,
                        false,
                        false,
                        null,
                        null );
     }
-    
-    public FactHandle insert(final Object object, 
-    						 final long duration) throws FactException {
+
+    public FactHandle insert(final Object object,
+                             final long duration) throws FactException {
         return insert( object, /* Not-Dynamic */
-        			   duration,
+                       duration,
                        false,
                        false,
                        null,
@@ -706,65 +709,65 @@
      * @see WorkingMemory
      */
     public FactHandle insertLogical(final Object object) throws FactException {
-        return insert( object,  //Not-Dynamic 
-        			   0,
+        return insert( object, //Not-Dynamic 
+                       0,
                        false,
                        true,
                        null,
                        null );
     }
-    
-    public FactHandle insertLogical(final Object object, 
-    								final long duration) throws FactException {
+
+    public FactHandle insertLogical(final Object object,
+                                    final long duration) throws FactException {
         return insert( object, /* Not-Dynamic */
-        			   duration,
+                       duration,
                        false,
                        true,
                        null,
                        null );
     }
 
-   public FactHandle insert(final Object object,
+    public FactHandle insert(final Object object,
                              final boolean dynamic) throws FactException {
         return insert( object,
-        			   0,
+                       0,
                        dynamic,
                        false,
                        null,
                        null );
     }
-    
+
     public FactHandle insert(final Object object,
-    						 final long duration,
-            				 final boolean dynamic) throws FactException {
-	return insert( object,
-				   duration,
-				   dynamic,
-				   false,
-				   null,
-				   null );
-	}
+                             final long duration,
+                             final boolean dynamic) throws FactException {
+        return insert( object,
+                       duration,
+                       dynamic,
+                       false,
+                       null,
+                       null );
+    }
 
     public FactHandle insertLogical(final Object object,
                                     final boolean dynamic) throws FactException {
         return insert( object,
-        			   0,
+                       0,
                        dynamic,
                        true,
                        null,
                        null );
     }
-    
+
     public FactHandle insertLogical(final Object object,
-    								final long duration,
-            						final boolean dynamic) throws FactException {
-		return insert( object,
-					   duration,
-					   dynamic,
-					   true,
-					   null,
-					   null );
-	}
+                                    final long duration,
+                                    final boolean dynamic) throws FactException {
+        return insert( object,
+                       duration,
+                       dynamic,
+                       true,
+                       null,
+                       null );
+    }
 
     public FactHandle insert(final Object object,
                              final boolean dynamic,
@@ -780,37 +783,37 @@
                             activation );
 
     }
-    
+
     public FactHandle insert(final Object object,
-    						final long duration,
-				            final boolean dynamic,
-				            boolean logical,
-				            final Rule rule,
-				            final Activation activation) throws FactException {
-    	return this.insert( EntryPoint.DEFAULT,
-    						object,
-    						duration,
-    						dynamic,
-    						logical,
-    						rule,
-    						activation );
+                             final long duration,
+                             final boolean dynamic,
+                             boolean logical,
+                             final Rule rule,
+                             final Activation activation) throws FactException {
+        return this.insert( EntryPoint.DEFAULT,
+                            object,
+                            duration,
+                            dynamic,
+                            logical,
+                            rule,
+                            activation );
     }
 
     protected FactHandle insert(final EntryPoint entryPoint,
-            final Object object,
-            final boolean dynamic,
-            boolean logical,
-            final Rule rule,
-            final Activation activation) throws FactException {
-    	return this.insert(entryPoint, 
-    					   object,
-    					   0,
-                           dynamic,
-                           logical,
-                           rule,
-                           activation);
+                                final Object object,
+                                final boolean dynamic,
+                                boolean logical,
+                                final Rule rule,
+                                final Activation activation) throws FactException {
+        return this.insert( entryPoint,
+                            object,
+                            0,
+                            dynamic,
+                            logical,
+                            rule,
+                            activation );
     }
-    
+
     protected FactHandle insert(final EntryPoint entryPoint,
                                 final Object object,
                                 final long duration,
@@ -1516,20 +1519,11 @@
      * @return The node's memory.
      */
     public Object getNodeMemory(final NodeMemory node) {
-        Object memory = this.nodeMemories.get( node.getId() );
-
-        if ( memory == null ) {
-            memory = node.createMemory( this.ruleBase.getConfiguration() );
-
-            this.nodeMemories.put( node.getId(),
-                                   memory );
-        }
-
-        return memory;
+        return this.nodeMemories.getNodeMemory( node );
     }
 
     public void clearNodeMemory(final NodeMemory node) {
-        this.nodeMemories.remove( node.getId() );
+        this.nodeMemories.clearNodeMemory( node );
     }
 
     public WorkingMemoryEventSupport getWorkingMemoryEventSupport() {

Added: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/ConcurrentNodeMemories.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/ConcurrentNodeMemories.java	                        (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/ConcurrentNodeMemories.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2008 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jan 15, 2008
+ */
+
+package org.drools.common;
+
+import java.util.concurrent.atomic.AtomicReferenceArray;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * A concurrent implementation for the node memories interface
+ * 
+ * @author etirelli
+ */
+public class ConcurrentNodeMemories
+    implements
+    NodeMemories {
+
+    private static final long serialVersionUID = -2032997426288974117L;
+    
+    private AtomicReferenceArray<Object> memories;
+    private Lock                         lock;
+    private transient InternalRuleBase   rulebase;
+
+    public ConcurrentNodeMemories(InternalRuleBase rulebase) {
+        this.rulebase = rulebase;
+        this.memories = new AtomicReferenceArray<Object>( this.rulebase.getNodeCount() );
+        this.lock = new ReentrantLock();
+    }
+
+    /**
+     * @inheritDoc
+     *
+     * @see org.drools.common.NodeMemories#clearNodeMemory(org.drools.common.NodeMemory)
+     */
+    public void clearNodeMemory(NodeMemory node) {
+        this.memories.set( node.getId(), null );
+    }
+
+    /**
+     * @inheritDoc
+     *
+     * @see org.drools.common.NodeMemories#getNodeMemory(org.drools.common.NodeMemory)
+     */
+    public Object getNodeMemory(NodeMemory node) {
+        if ( node.getId() >= this.memories.length() ) {
+            resize( node );
+        }
+        Object memory = this.memories.get( node.getId() );
+
+        if ( memory == null ) {
+            memory = node.createMemory( this.rulebase.getConfiguration() );
+
+            if( !this.memories.compareAndSet( node.getId(), null, memory ) ) {
+                memory = this.memories.get( node.getId() );
+            }
+        }
+
+        return memory;
+    }
+
+    /**
+     * @param node
+     */
+    private void resize(NodeMemory node) {
+        this.lock.lock();
+        try {
+            if( node.getId() >= this.memories.length() ) {
+                int size = Math.max( this.rulebase.getNodeCount(),
+                                     node.getId() + 1 );
+                AtomicReferenceArray<Object> newMem = new AtomicReferenceArray<Object>( size );
+                for( int i = 0; i < this.memories.length(); i++ ) {
+                    newMem.set( i, this.memories.get( i ) );
+                }
+                this.memories = newMem;
+            }
+        } finally {
+            this.lock.unlock();
+        }
+    }
+
+    public void setRuleBaseReference(InternalRuleBase ruleBase) {
+        this.rulebase = ruleBase;
+    }
+
+}

Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/InternalRuleBase.java	2008-01-15 17:10:24 UTC (rev 17871)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/InternalRuleBase.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -114,4 +114,6 @@
     public boolean isEvent( Class clazz );
 
 	public Objenesis getObjenesis();
+	
+	public int getNodeCount();
 }

Added: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/NodeMemories.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/NodeMemories.java	                        (rev 0)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/common/NodeMemories.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2008 JBoss Inc
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Created on Jan 15, 2008
+ */
+
+package org.drools.common;
+
+import java.io.Serializable;
+
+/**
+ * An interface for node memories implementation
+ * 
+ * @author etirelli
+ */
+public interface NodeMemories extends Serializable {
+    
+    public Object getNodeMemory( NodeMemory node );
+    
+    public void clearNodeMemory( NodeMemory node );
+
+    public void setRuleBaseReference(InternalRuleBase ruleBase);
+
+}

Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2008-01-15 17:10:24 UTC (rev 17871)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooBuilder.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -269,6 +269,10 @@
             this.recycledIds.add( new Integer(id) );
         }
 
+        public int getLastId() {
+            return this.nextId-1;
+        }
+
     }
 
 }

Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java	2008-01-15 17:10:24 UTC (rev 17871)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -311,6 +311,10 @@
     protected synchronized void removeRule(final Rule rule) {
         this.reteooBuilder.removeRule( rule );
     }
+    
+    public int getNodeCount() {
+        return this.reteooBuilder.getIdGenerator().getLastId();
+    }
 
     public static class InitialFactHandleDummyObject
         implements

Modified: labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java
===================================================================
--- labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java	2008-01-15 17:10:24 UTC (rev 17871)
+++ labs/jbossrules/branches/temporal_rete/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java	2008-01-15 18:11:43 UTC (rev 17872)
@@ -119,10 +119,12 @@
             }
             list = Collections.EMPTY_LIST;
         } else {
-            list = (List) this.nodeMemories.remove( node.getId() );
+            list = (List) this.getNodeMemory( node );
             
             if ( list == null ) {
                 list = Collections.EMPTY_LIST;
+            } else {
+                this.clearNodeMemory( node );
             }
             queryObj = (Query) node.getRule();
             




More information about the jboss-svn-commits mailing list