[jboss-svn-commits] JBL Code SVN: r19998 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/base and 7 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat May 17 23:11:26 EDT 2008
Author: mark.proctor at jboss.com
Date: 2008-05-17 23:11:26 -0400 (Sat, 17 May 2008)
New Revision: 19998
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ClassPlaceholderResolverStrategyAcceptor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/IdentityPlaceholderResolverStrategy.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Marshaller.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterHelper.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyAcceptor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandleDummyObject.java
Removed:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/FactHandle.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseEventManager.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEventManager.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/MapGlobalResolver.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractFactHandleFactory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultFactHandle.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EqualityKey.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EventFactHandle.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TruthMaintenanceSystem.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/WorkingMemoryAction.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/CollectNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EvalConditionNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/FromNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandle.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/LeftTuple.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/PropagationQueuingNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooFactHandleFactory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooTemporalSession.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightInputAdapterNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTuple.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTupleMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FactHandleFactory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Entry.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/LeftTupleList.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashMap.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashSet.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleIndexHashTable.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleList.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java
Log:
JBRULES-1598 Efficient WorkingMemory serialization with binary protocol
-First working version, ready for the world to see :) Still needs Accumulate, Collect and From to be done.
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/FactHandle.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/FactHandle.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/FactHandle.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -26,9 +26,7 @@
import java.io.Externalizable;
-public interface FactHandle
- extends
- Externalizable {
+public interface FactHandle {
/**
* Convert this handle to an external representation.
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBase.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBase.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -19,7 +19,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Externalizable;
+import java.io.OutputStream;
+import org.drools.marshalling.Marshaller;
import org.drools.rule.Package;
/**
@@ -77,25 +79,6 @@
StatefulSession newStatefulSession(boolean keepReference);
/**
- * RuleBases handle the returning of a Serialized WorkingMemory
- * pass as an InputStream. If the reference is a byte[] then
- * wrap with new ByteArrayInputStream. By default the RuleBase retains a
- * weak reference to returned WorkingMemory.
- *
- * <p>
- * The created <code>WorkingMemory</code> uses the default conflict
- * resolution strategy.
- * </p>
- *
- * @see WorkingMemory
- * @see org.drools.conflict.DefaultConflictResolver
- *
- * @return A serialised initialized <code>WorkingMemory</code>.
- */
- StatefulSession newStatefulSession(InputStream stream) throws IOException,
- ClassNotFoundException;
-
- /**
* Creates a new temporal session using the defined clock type.
*
* @param clockType
@@ -110,8 +93,13 @@
* @param clockType
* @return
*/
- TemporalSession newTemporalSession(boolean keepReference, ClockType clockType);
+ TemporalSession newTemporalSession(boolean keepReference,
+ ClockType clockType);
+ StatefulSession readStatefulSession(InputStream stream,
+ Marshaller marshaller) throws IOException,
+ ClassNotFoundException;
+
/**
* RuleBases handle the returning of a Serialized WorkingMemory
* pass as an InputStream. If the reference is a byte[] then
@@ -128,10 +116,15 @@
*
* @return A serialised initialized <code>WorkingMemory</code>.
*/
- StatefulSession newStatefulSession(InputStream stream,
- boolean keepReference) throws IOException,
- ClassNotFoundException;
+ StatefulSession readStatefulSession(InputStream stream,
+ boolean keepReference,
+ Marshaller marshaller) throws IOException,
+ ClassNotFoundException;
+ public void writeStatefulSession(StatefulSession session,
+ OutputStream stream,
+ Marshaller marshaller) throws IOException;
+
Package[] getPackages();
Package getPackage(String name);
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -45,6 +45,7 @@
import org.drools.temporal.SessionClock;
import org.drools.temporal.SessionPseudoClock;
import org.drools.util.ChainedProperties;
+import org.drools.util.ClassUtils;
import org.drools.util.ConfFileUtils;
import org.drools.workflow.core.Node;
import org.drools.workflow.instance.impl.NodeInstanceFactory;
@@ -778,36 +779,38 @@
if ( this.shadowProxyExcludes == null ) {
this.shadowProxyExcludes = new HashMap();
}
+
+ ClassUtils.addImportStylePatterns( this.shadowProxyExcludes, excludes );
- String[] items = excludes.split( " " );
- for ( int i = 0; i < items.length; i++ ) {
- String qualifiedNamespace = items[i].substring( 0,
- items[i].lastIndexOf( '.' ) ).trim();
- String name = items[i].substring( items[i].lastIndexOf( '.' ) + 1 ).trim();
- Object object = this.shadowProxyExcludes.get( qualifiedNamespace );
- if ( object == null ) {
- if ( STAR.equals( name ) ) {
- this.shadowProxyExcludes.put( qualifiedNamespace,
- STAR );
- } else {
- // create a new list and add it
- List list = new ArrayList();
- list.add( name );
- this.shadowProxyExcludes.put( qualifiedNamespace,
- list );
- }
- } else if ( name.equals( STAR ) ) {
- // if its a STAR now add it anyway, we don't care if it was a STAR or a List before
- this.shadowProxyExcludes.put( qualifiedNamespace,
- STAR );
- } else {
- // its a list so add it if it doesn't already exist
- List list = (List) object;
- if ( !list.contains( object ) ) {
- list.add( name );
- }
- }
- }
+// String[] items = excludes.split( " " );
+// for ( int i = 0; i < items.length; i++ ) {
+// String qualifiedNamespace = items[i].substring( 0,
+// items[i].lastIndexOf( '.' ) ).trim();
+// String name = items[i].substring( items[i].lastIndexOf( '.' ) + 1 ).trim();
+// Object object = this.shadowProxyExcludes.get( qualifiedNamespace );
+// if ( object == null ) {
+// if ( STAR.equals( name ) ) {
+// this.shadowProxyExcludes.put( qualifiedNamespace,
+// STAR );
+// } else {
+// // create a new list and add it
+// List list = new ArrayList();
+// list.add( name );
+// this.shadowProxyExcludes.put( qualifiedNamespace,
+// list );
+// }
+// } else if ( name.equals( STAR ) ) {
+// // if its a STAR now add it anyway, we don't care if it was a STAR or a List before
+// this.shadowProxyExcludes.put( qualifiedNamespace,
+// STAR );
+// } else {
+// // its a list so add it if it doesn't already exist
+// List list = (List) object;
+// if ( !list.contains( object ) ) {
+// list.add( name );
+// }
+// }
+// }
}
public boolean isShadowed(String className) {
@@ -815,18 +818,7 @@
return true;
}
- String qualifiedNamespace = className.substring( 0,
- className.lastIndexOf( '.' ) ).trim();
- String name = className.substring( className.lastIndexOf( '.' ) + 1 ).trim();
- Object object = this.shadowProxyExcludes.get( qualifiedNamespace );
- if ( object == null ) {
- return true;
- } else if ( STAR.equals( object ) ) {
- return false;
- } else {
- List list = (List) object;
- return !list.contains( name );
- }
+ return ClassUtils.isMatched( this.shadowProxyExcludes, className );
}
private static ConsequenceExceptionHandler determineConsequenceExceptionHandler(String className) {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseEventManager.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseEventManager.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseEventManager.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -25,9 +25,7 @@
* The RuleBaseEventManager class is implemented by classes wishing to add,remove and get the various RuleBase EventListeners.
*
*/
-public interface RuleBaseEventManager
- extends
- Externalizable {
+public interface RuleBaseEventManager {
/**
* Add an event listener.
*
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -37,7 +37,7 @@
* in WorkingMemories from an InputStream.
*
*/
-public interface WorkingMemory extends WorkingMemoryEventManager, WorkingMemoryEntryPoint, Externalizable {
+public interface WorkingMemory extends WorkingMemoryEventManager, WorkingMemoryEntryPoint {
/**
* Returns the Agenda for this WorkingMemory. While the WorkingMemory interface is considered public, the Agenda interface
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -25,9 +25,7 @@
*
* @author etirelli
*/
-public interface WorkingMemoryEntryPoint
- extends
- Serializable {
+public interface WorkingMemoryEntryPoint {
/**
* Assert a fact.
*
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEventManager.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEventManager.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEventManager.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -29,8 +29,7 @@
*/
public interface WorkingMemoryEventManager
extends
- RuleBaseEventManager,
- Externalizable {
+ RuleBaseEventManager {
/**
* Add an event listener.
*
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/MapGlobalResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/MapGlobalResolver.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/MapGlobalResolver.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -45,5 +45,14 @@
public Entry[] getGlobals() {
return (Entry[]) this.map.entrySet().toArray(new Entry[this.map.size()]);
}
+
+ public GlobalResolver clone() {
+ Map clone = new HashMap();
+
+ for ( Entry entry : getGlobals() ) {
+ clone.put( entry.getKey(), entry.getValue() );
+ }
+ return new MapGlobalResolver( clone );
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractFactHandleFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractFactHandleFactory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractFactHandleFactory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -24,12 +24,14 @@
import java.util.concurrent.atomic.AtomicLong;
import org.drools.WorkingMemory;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.reteoo.ObjectTypeConf;
import org.drools.spi.FactHandleFactory;
public abstract class AbstractFactHandleFactory
implements
- FactHandleFactory, Externalizable {
+ FactHandleFactory {
/**
*
@@ -48,18 +50,12 @@
this.id = new AtomicInteger(-1);
this.counter = new AtomicLong(-1);
}
+
+ public AbstractFactHandleFactory(int id, long counter) {
+ this.id = new AtomicInteger( id );
+ this.counter = new AtomicLong( counter );
+ }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- this.id = new AtomicInteger( in.readInt() );
- this.counter = new AtomicLong( in.readLong() );
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeInt(id.get());
- out.writeLong(counter.get());
- }
-
/* (non-Javadoc)
* @see org.drools.reteoo.FactHandleFactory#newFactHandle()
*/
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-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -23,6 +23,7 @@
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
+import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -41,8 +42,16 @@
import org.drools.concurrent.ExecutorService;
import org.drools.event.RuleBaseEventListener;
import org.drools.event.RuleBaseEventSupport;
+import org.drools.marshalling.Marshaller;
+import org.drools.marshalling.OutputPersister;
+import org.drools.marshalling.PlaceholderResolverStrategyFactory;
+import org.drools.marshalling.RuleBaseNodes;
+import org.drools.marshalling.SerializablePlaceholderResolverStrategy;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.objenesis.Objenesis;
import org.drools.process.core.Process;
+import org.drools.reteoo.ReteooStatefulSession;
import org.drools.rule.CompositePackageClassLoader;
import org.drools.rule.DialectDatas;
import org.drools.rule.Function;
@@ -197,7 +206,7 @@
droolsStream.writeInt( workingMemoryCounter );
droolsStream.writeObject( this.processes );
droolsStream.writeObject( this.agendaGroupRuleTotals );
- droolsStream.writeObject( this.factHandleFactory );
+ droolsStream.writeUTF( this.factHandleFactory.getClass().getName() );
droolsStream.writeObject( this.globals );
droolsStream.writeObject( reloadPackageCompilationData );
@@ -255,7 +264,17 @@
this.processes = (Map) droolsStream.readObject();
this.agendaGroupRuleTotals = (Map) droolsStream.readObject();
- this.factHandleFactory = (FactHandleFactory) droolsStream.readObject();
+ Class cls = null;
+ try {
+ cls = droolsStream.getClassLoader().loadClass( droolsStream.readUTF() );
+ this.factHandleFactory = (FactHandleFactory) cls.newInstance();
+ } catch ( InstantiationException e ) {
+ DroolsObjectInputStream.newInvalidClassException( cls,
+ e );
+ } catch ( IllegalAccessException e ) {
+ DroolsObjectInputStream.newInvalidClassException( cls,
+ e );
+ }
this.globals = (Map) droolsStream.readObject();
this.reloadPackageCompilationData = (ReloadPackageCompilationData) droolsStream.readObject();
@@ -276,17 +295,17 @@
this.populateTypeDeclarationMaps();
}
- /**
- * Creates Objenesis instance for the RuleBase.
- * @return a standart Objenesis instanse with caching turned on.
- */
- protected Objenesis createObjenesis() {
- if( this.config.isUseStaticObjenesis() ) {
- return ObjenesisFactory.getStaticObjenesis();
- } else {
- return ObjenesisFactory.getDefaultObjenesis();
- }
- }
+ /**
+ * Creates Objenesis instance for the RuleBase.
+ * @return a standart Objenesis instanse with caching turned on.
+ */
+ protected Objenesis createObjenesis() {
+ if ( this.config.isUseStaticObjenesis() ) {
+ return ObjenesisFactory.getStaticObjenesis();
+ } else {
+ return ObjenesisFactory.getDefaultObjenesis();
+ }
+ }
private void populateTypeDeclarationMaps() {
this.classTypeDeclaration = new HashMap<Class< ? >, TypeDeclaration>();
@@ -312,17 +331,20 @@
return newStatefulSession( true );
}
- /**
- * @see RuleBase
- */
- abstract public StatefulSession newStatefulSession(boolean keepReference);
-
public synchronized void disposeStatefulSession(final StatefulSession statefulSession) {
this.statefulSessions.remove( statefulSession );
for ( Iterator it = statefulSession.getRuleBaseUpdateListeners().iterator(); it.hasNext(); ) {
this.removeEventListener( (RuleBaseEventListener) it.next() );
}
}
+
+ public StatefulSession readStatefulSession(final InputStream stream,
+ Marshaller marshaller) throws IOException,
+ ClassNotFoundException {
+ return readStatefulSession( stream,
+ true,
+ marshaller );
+ }
/**
* @see RuleBase
@@ -335,6 +357,12 @@
return this.factHandleFactory.newInstance();
}
+ public FactHandleFactory newFactHandleFactory(int id,
+ long counter) {
+ return this.factHandleFactory.newInstance( id,
+ counter );
+ }
+
public Process[] getProcesses() {
return (Process[]) this.processes.values().toArray( new Process[this.processes.size()] );
}
@@ -427,29 +455,28 @@
this.eventSupport.fireBeforePackageAdded( newPkg );
if ( pkg == null ) {
- pkg = new Package(newPkg.getName(),
- newPkg.getDialectDatas().getParentClassLoader());
- pkgs.put(pkg.getName(), pkg);
- this.packageClassLoader.addClassLoader( pkg.getDialectDatas().getClassLoader());
- }
- else {
+ pkg = new Package( newPkg.getName(),
+ newPkg.getDialectDatas().getParentClassLoader() );
+ pkgs.put( pkg.getName(),
+ pkg );
+ this.packageClassLoader.addClassLoader( pkg.getDialectDatas().getClassLoader() );
+ } else {
this.packageClassLoader.addClassLoader( newPkg.getDialectDatas().getClassLoader() );
}
final Map<String, Class> newGlobals = newPkg.getGlobals();
- if (newGlobals != null) {
+ 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 );
+ 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);
+ this.globals.put( identifier,
+ type );
}
}
}
@@ -457,7 +484,7 @@
mergePackage( pkg,
newPkg );
- if (newPkg.getTypeDeclarations() != null) {
+ if ( newPkg.getTypeDeclarations() != null ) {
// Add type declarations
for ( TypeDeclaration type : newPkg.getTypeDeclarations().values() ) {
// should we allow overrides?
@@ -508,25 +535,25 @@
// Merge imports
imports.putAll( newPkg.getImports() );
- if (newPkg.getGlobals() != null) {
+ if ( newPkg.getGlobals() != null ) {
final 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) {
- pkg.addGlobal(identifier, type);
+ final Class type = entry.getValue();
+ if ( globals.containsKey( identifier ) && !globals.get( identifier ).equals( type ) ) {
+ throw new PackageIntegrationException( pkg );
+ } else if ( globals == Collections.EMPTY_MAP ) {
+ pkg.addGlobal( identifier,
+ type );
} else {
- globals.put(identifier,
- type);
+ globals.put( identifier,
+ type );
}
}
}
- if (newPkg.getTypeDeclarations() != null) {
+ if ( newPkg.getTypeDeclarations() != null ) {
// add type declarations
for ( TypeDeclaration type : newPkg.getTypeDeclarations().values() ) {
// should we allow overrides?
@@ -566,9 +593,9 @@
pkg.getDialectDatas().merge( newPkg.getDialectDatas() );
- if ( newPkg.getFunctions() != null) {
- for (Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet()) {
- pkg.addFunction(entry.getValue());
+ if ( newPkg.getFunctions() != null ) {
+ for ( Map.Entry<String, Function> entry : newPkg.getFunctions().entrySet() ) {
+ pkg.addFunction( entry.getValue() );
}
}
@@ -780,46 +807,6 @@
return this.config;
}
- public StatefulSession newStatefulSession(final InputStream stream) throws IOException,
- ClassNotFoundException {
- return newStatefulSession( stream,
- true );
- }
-
- public StatefulSession newStatefulSession(final InputStream stream,
- final boolean keepReference) throws IOException,
- ClassNotFoundException {
-
- if ( this.config.isSequential() ) {
- throw new RuntimeException( "Cannot have a stateful rule session, with sequential configuration set to true" );
- }
-
- final DroolsObjectInputStream streamWithLoader = new DroolsObjectInputStream( stream,
- this.packageClassLoader );
- streamWithLoader.setRuleBase( this );
-
- final StatefulSession session = (StatefulSession) streamWithLoader.readObject();
-
- synchronized ( this.pkgs ) {
- ((InternalWorkingMemory) session).setRuleBase( this );
- ((InternalWorkingMemory) session).setId( (nextWorkingMemoryCounter()) );
-
-
- ExecutorService executor = ExecutorServiceFactory.createExecutorService( this.config.getExecutorService() );
- executor.setCommandExecutor( new CommandExecutor( session ) );
- ((InternalWorkingMemory) session).setExecutorService( executor );
-
- if ( keepReference ) {
- addStatefulSession( session );
- for ( Iterator it = session.getRuleBaseUpdateListeners().iterator(); it.hasNext(); ) {
- addEventListener( (RuleBaseEventListener) it.next() );
- }
- }
-
- return session;
- }
- }
-
public void addClass(final String className,
final byte[] bytes) {
this.classLoader.addClass( className,
@@ -887,8 +874,7 @@
if ( this.set == null ) {
this.set = new HashSet<DialectDatas>();
}
- if (!this.set.contains(dialectDatas))
- this.set.add( dialectDatas );
+ if ( !this.set.contains( dialectDatas ) ) this.set.add( dialectDatas );
}
public void execute(final InternalRuleBase ruleBase) {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -62,6 +62,12 @@
import org.drools.event.RuleFlowEventSupport;
import org.drools.event.WorkingMemoryEventListener;
import org.drools.event.WorkingMemoryEventSupport;
+import org.drools.marshalling.InputPersister;
+import org.drools.marshalling.OutputPersister;
+import org.drools.marshalling.PersisterEnums;
+import org.drools.marshalling.PersisterHelper;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.process.core.Process;
import org.drools.process.core.context.variable.Variable;
import org.drools.process.core.context.variable.VariableScope;
@@ -71,9 +77,12 @@
import org.drools.process.instance.context.variable.VariableScopeInstance;
import org.drools.process.instance.timer.TimerManager;
import org.drools.reteoo.EntryPointNode;
+import org.drools.reteoo.InitialFactHandle;
+import org.drools.reteoo.InitialFactHandleDummyObject;
import org.drools.reteoo.LIANodePropagation;
import org.drools.reteoo.LeftTuple;
import org.drools.reteoo.ObjectTypeConf;
+import org.drools.reteoo.ReteooFactHandleFactory;
import org.drools.rule.Declaration;
import org.drools.rule.EntryPoint;
import org.drools.rule.Rule;
@@ -99,8 +108,7 @@
implements
InternalWorkingMemoryActions,
EventSupport,
- PropertyChangeListener,
- Externalizable {
+ PropertyChangeListener {
// ------------------------------------------------------------
// Constants
// ------------------------------------------------------------
@@ -113,26 +121,26 @@
protected long id;
/** The arguments used when adding/removing a property change listener. */
- protected Object[] addRemovePropertyChangeListenerArgs = new Object[]{this};
+ protected Object[] addRemovePropertyChangeListenerArgs;
/** The actual memory for the <code>JoinNode</code>s. */
protected NodeMemories nodeMemories;
protected ObjectStore objectStore;
- protected Map queryResults = Collections.EMPTY_MAP;
+ protected Map queryResults;
/** Global values which are associated with this memory. */
protected GlobalResolver globalResolver;
/** The eventSupport */
- protected WorkingMemoryEventSupport workingMemoryEventSupport = new WorkingMemoryEventSupport();
+ protected WorkingMemoryEventSupport workingMemoryEventSupport;
- protected AgendaEventSupport agendaEventSupport = new AgendaEventSupport();
+ protected AgendaEventSupport agendaEventSupport;
- protected RuleFlowEventSupport workflowEventSupport = new RuleFlowEventSupport();
+ protected RuleFlowEventSupport workflowEventSupport;
- protected List __ruleBaseEventListeners = new LinkedList();
+ protected List __ruleBaseEventListeners;
/** The <code>RuleBase</code> with which this memory is associated. */
protected transient InternalRuleBase ruleBase;
@@ -144,11 +152,11 @@
/** Rule-firing agenda. */
protected DefaultAgenda agenda;
- protected Queue<WorkingMemoryAction> actionQueue = new LinkedList<WorkingMemoryAction>();
+ protected Queue<WorkingMemoryAction> actionQueue;
protected volatile boolean evaluatingActionQueue;
- protected ReentrantLock lock = new ReentrantLock();
+ protected ReentrantLock lock;
protected boolean discardOnLogicalOverride;
@@ -161,14 +169,14 @@
private boolean maintainTms;
private boolean sequential;
- private List liaPropagations = Collections.EMPTY_LIST;
+ private List liaPropagations;
/** Flag to determine if a rule is currently being fired. */
protected volatile boolean firing;
protected volatile boolean halt;
- private Map processInstances = new HashMap();
+ private Map processInstances;
private int processCounter;
@@ -176,9 +184,9 @@
private TimerManager timerManager;
- private Map<String, ProcessInstanceFactory> processInstanceFactories = new HashMap();
+ private Map<String, ProcessInstanceFactory> processInstanceFactories;
- private TimeMachine timeMachine = new TimeMachine();
+ private TimeMachine timeMachine;
protected transient ObjectTypeConfigurationRegistry typeConfReg;
@@ -187,6 +195,8 @@
protected Map<String, WorkingMemoryEntryPoint> entryPoints;
+ protected InternalFactHandle initialFactHandle;
+
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
@@ -203,6 +213,18 @@
public AbstractWorkingMemory(final int id,
final InternalRuleBase ruleBase,
final FactHandleFactory handleFactory) {
+ this( id,
+ ruleBase,
+ handleFactory,
+ null,
+ 0 );
+ }
+
+ public AbstractWorkingMemory(int id,
+ InternalRuleBase ruleBase,
+ FactHandleFactory handleFactory,
+ InitialFactHandle initialFactHandle,
+ long propagationContext) {
this.id = id;
this.ruleBase = ruleBase;
this.handleFactory = handleFactory;
@@ -210,6 +232,28 @@
this.maintainTms = this.ruleBase.getConfiguration().isMaintainTms();
this.sequential = this.ruleBase.getConfiguration().isSequential();
+ if ( initialFactHandle == null ) {
+ this.initialFactHandle = new InitialFactHandle( handleFactory.newFactHandle( new InitialFactHandleDummyObject(),
+ null,
+ this ) );
+ } else {
+ this.initialFactHandle = initialFactHandle;
+ }
+
+ this.actionQueue = new LinkedList<WorkingMemoryAction>();
+
+ this.addRemovePropertyChangeListenerArgs = new Object[]{this};
+ this.queryResults = Collections.EMPTY_MAP;
+ this.workingMemoryEventSupport = new WorkingMemoryEventSupport();
+ this.agendaEventSupport = new AgendaEventSupport();
+ this.workflowEventSupport = new RuleFlowEventSupport();
+ this.__ruleBaseEventListeners = new LinkedList();
+ this.lock = new ReentrantLock();
+ this.liaPropagations = Collections.EMPTY_LIST;
+ this.processInstances = new HashMap();
+ this.processInstanceFactories = new HashMap();
+ this.timeMachine = new TimeMachine();
+
this.nodeMemories = new ConcurrentNodeMemories( this.ruleBase );
if ( this.maintainTms ) {
@@ -220,7 +264,7 @@
final RuleBaseConfiguration conf = this.ruleBase.getConfiguration();
- this.propagationIdCounter = new AtomicLong();
+ this.propagationIdCounter = new AtomicLong( propagationContext );
this.objectStore = new SingleThreadedObjectStore( conf,
this.lock );
@@ -242,78 +286,6 @@
initTransient();
}
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- id = in.readLong();
- evaluatingActionQueue = in.readBoolean();
- discardOnLogicalOverride = in.readBoolean();
- propagationIdCounter = (AtomicLong) in.readObject();
- maintainTms = in.readBoolean();
- sequential = in.readBoolean();
- firing = in.readBoolean();
- halt = in.readBoolean();
- processCounter = in.readInt();
- addRemovePropertyChangeListenerArgs = (Object[]) in.readObject();
- nodeMemories = (NodeMemories) in.readObject();
- objectStore = (ObjectStore) in.readObject();
- queryResults = (Map) in.readObject();
- globalResolver = (GlobalResolver) in.readObject();
- workingMemoryEventSupport = (WorkingMemoryEventSupport) in.readObject();
- agendaEventSupport = (AgendaEventSupport) in.readObject();
- workflowEventSupport = (RuleFlowEventSupport) in.readObject();
- __ruleBaseEventListeners = (List) in.readObject();
- ruleBase = (InternalRuleBase) in.readObject();
- handleFactory = (FactHandleFactory) in.readObject();
- tms = (TruthMaintenanceSystem) in.readObject();
- agenda = (DefaultAgenda) in.readObject();
- lock = (ReentrantLock) in.readObject();
- actionQueue = (Queue<WorkingMemoryAction>) in.readObject();
- liaPropagations = (List) in.readObject();
- processInstances = (Map) in.readObject();
- workItemManager = (WorkItemManager) in.readObject();
- processInstanceFactories = (Map<String, ProcessInstanceFactory>) in.readObject();
- timeMachine = (TimeMachine) in.readObject();
- entryPoint = (EntryPoint) in.readObject();
- entryPointNode = (EntryPointNode) in.readObject();
- entryPoints = (Map<String, WorkingMemoryEntryPoint>) in.readObject();
- initTransient();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeLong( id );
- out.writeBoolean( evaluatingActionQueue );
- out.writeBoolean( discardOnLogicalOverride );
- out.writeObject( propagationIdCounter );
- out.writeBoolean( maintainTms );
- out.writeBoolean( sequential );
- out.writeBoolean( firing );
- out.writeBoolean( halt );
- out.writeInt( processCounter );
- out.writeObject( addRemovePropertyChangeListenerArgs );
- out.writeObject( nodeMemories );
- out.writeObject( objectStore );
- out.writeObject( queryResults );
- out.writeObject( globalResolver );
- out.writeObject( workingMemoryEventSupport );
- out.writeObject( agendaEventSupport );
- out.writeObject( workflowEventSupport );
- out.writeObject( __ruleBaseEventListeners );
- out.writeObject( ruleBase );
- out.writeObject( handleFactory );
- out.writeObject( tms );
- out.writeObject( agenda );
- out.writeObject( lock );
- out.writeObject( actionQueue );
- out.writeObject( liaPropagations );
- out.writeObject( processInstances );
- out.writeObject( workItemManager );
- out.writeObject( processInstanceFactories );
- out.writeObject( timeMachine );
- out.writeObject( entryPoint );
- out.writeObject( entryPointNode );
- out.writeObject( entryPoints );
- }
-
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
@@ -1095,7 +1067,7 @@
this.objectStore.removeHandle( handle );
this.handleFactory.destroyFactHandle( handle );
-
+
executeQueuedActions();
} finally {
this.lock.unlock();
@@ -1236,6 +1208,7 @@
propagationContext.clearRetractedTuples();
executeQueuedActions();
+
} finally {
this.lock.unlock();
}
@@ -1362,7 +1335,7 @@
if ( !this.actionQueue.isEmpty() && !evaluatingActionQueue ) {
evaluatingActionQueue = true;
WorkingMemoryAction action = null;
-
+
while ( (action = actionQueue.poll()) != null ) {
action.execute( this );
}
@@ -1371,6 +1344,10 @@
}
}
+ public Queue<WorkingMemoryAction> getActionQueue() {
+ return this.actionQueue;
+ }
+
public void queueWorkingMemoryAction(final WorkingMemoryAction action) {
synchronized ( this.actionQueue ) {
this.actionQueue.add( action );
@@ -1451,6 +1428,10 @@
return this.propagationIdCounter.incrementAndGet();
}
+ public long getPropagationIdCounter() {
+ return this.propagationIdCounter.get();
+ }
+
public Lock getLock() {
return this.lock;
}
@@ -1636,78 +1617,44 @@
return wmEntryPoint;
}
- // protected static class EntryPointInterfaceImpl
- // implements
- // EntryPointInterface {
+ public ObjectTypeConfigurationRegistry getObjectTypeConfigurationRegistry() {
+ return this.typeConfReg;
+ }
+
+ public InternalFactHandle getInitialFactHandle() {
+ return this.initialFactHandle;
+ }
+
+ public void setInitialFactHandle(InternalFactHandle initialFactHandle) {
+ this.initialFactHandle = initialFactHandle;
+ }
+
+ // public static class FactHandleInvalidation implements WorkingMemoryAction {
+ // private final InternalFactHandle handle;
+ //
+ // public FactHandleInvalidation(InternalFactHandle handle) {
+ // this.handle = handle;
+ // }
//
- // private static final long serialVersionUID = 2917871170743358801L;
+ // public void execute(InternalWorkingMemory workingMemory) {
+ // workingMemory.getFactHandleFactory().destroyFactHandle( handle );
+ // }
//
- // private final EntryPoint entryPoint;
- // private final AbstractWorkingMemory wm;
+ // public void write(WMSerialisationOutContext context) throws IOException {
+ // context.writeInt( handle.getId() );
+ // }
//
- // public EntryPointInterfaceImpl(EntryPoint entryPoint,
- // AbstractWorkingMemory wm) {
- // this.entryPoint = entryPoint;
- // this.wm = wm;
- // }
+ // public void readExternal(ObjectInput in) throws IOException,
+ // ClassNotFoundException {
+ // // TODO Auto-generated method stub
+ //
+ // }
//
- // public FactHandle insert(Object object) throws FactException {
- // return wm.insert( this.entryPoint,
- // object, /* Not-Dynamic */
- // false,
- // false,
- // null,
- // null );
- // }
- //
- // public FactHandle insert(Object object,
- // boolean dynamic) throws FactException {
- // return wm.insert( this.entryPoint,
- // object, /* Not-Dynamic */
- // dynamic,
- // false,
- // null,
- // null );
- // }
- //
- // public void modifyInsert(FactHandle factHandle,
- // Object object) {
- // wm.modifyInsert( this.entryPoint,
- // factHandle,
- // object,
- // null,
- // null );
- // }
- //
- // public void modifyRetract(FactHandle factHandle) {
- // wm.modifyRetract( this.entryPoint,
- // factHandle,
- // null,
- // null );
- // }
- //
- // public void retract(FactHandle handle) throws FactException {
- // wm.retract( this.entryPoint,
- // handle,
- // true,
- // true,
- // null,
- // null );
- // }
- //
- // public void update(FactHandle handle,
- // Object object) throws FactException {
- // wm.update( this.entryPoint,
- // handle,
- // object,
- // null,
- // null );
- // }
- //
- // }
+ // public void writeExternal(ObjectOutput out) throws IOException {
+ // // TODO Auto-generated method stub
+ //
+ // }
+ //
+ // }
- public ObjectTypeConfigurationRegistry getObjectTypeConfigurationRegistry() {
- return this.typeConfReg;
- }
-
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -17,6 +17,8 @@
*/
import org.drools.conflict.DepthConflictResolver;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.spi.Activation;
import org.drools.spi.AgendaGroup;
import org.drools.spi.ConflictResolver;
@@ -67,7 +69,8 @@
this.name = name;
this.queue = new BinaryHeapQueue( ruleBase.getConfiguration().getConflictResolver() );
}
-
+
+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = (String)in.readObject();
queue = (BinaryHeapQueue)in.readObject();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -23,16 +23,22 @@
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.ListIterator;
import java.util.Map;
+import java.util.Map.Entry;
import org.drools.WorkingMemory;
import org.drools.base.DefaultKnowledgeHelper;
import org.drools.base.SequentialKnowledgeHelper;
import org.drools.common.RuleFlowGroupImpl.DeactivateCallback;
+import org.drools.marshalling.PersisterEnums;
+import org.drools.marshalling.WMSerialisationOutContext;
+import org.drools.marshalling.OutputPersister.HandleSorter;
import org.drools.spi.Activation;
import org.drools.spi.ActivationGroup;
import org.drools.spi.AgendaFilter;
@@ -72,36 +78,36 @@
/**
*
*/
- private static final long serialVersionUID = 400L;
+ private static final long serialVersionUID = 400L;
/** Working memory of this Agenda. */
- private InternalWorkingMemory workingMemory;
+ private InternalWorkingMemory workingMemory;
- private org.drools.util.LinkedList scheduledActivations;
+ private org.drools.util.LinkedList scheduledActivations;
/** Items time-delayed. */
- private Map agendaGroups;
+ private Map<String, AgendaGroup> agendaGroups;
- private Map activationGroups;
+ private Map<String, ActivationGroup> activationGroups;
- private Map ruleFlowGroups;
+ private Map<String, RuleFlowGroup> ruleFlowGroups;
- private LinkedList focusStack;
+ private LinkedList<AgendaGroup> focusStack;
- private AgendaGroup currentModule;
+ private AgendaGroup currentModule;
- private AgendaGroup main;
+ private AgendaGroup main;
- private AgendaGroupFactory agendaGroupFactory;
+ private AgendaGroupFactory agendaGroupFactory;
- private KnowledgeHelper knowledgeHelper;
+ private KnowledgeHelper knowledgeHelper;
- public int activeActivations;
+ public int activeActivations;
- public int dormantActivations;
+ public int dormantActivations;
- private ConsequenceExceptionHandler consequenceExceptionHandler;
+ private ConsequenceExceptionHandler consequenceExceptionHandler;
// ------------------------------------------------------------
// Constructors
@@ -117,64 +123,134 @@
* @param conflictResolver
* The conflict resolver.
*/
- public DefaultAgenda(final InternalWorkingMemory workingMemory) {
+ public DefaultAgenda(InternalRuleBase rb) {
+ this(rb, true);
+ }
+
+ /**
+ * Construct.
+ *
+ * @param workingMemory
+ * The <code>WorkingMemory</code> of this agenda.
+ * @param conflictResolver
+ * The conflict resolver.
+ */
+ public DefaultAgenda(InternalRuleBase rb, boolean initMain) {
+
+ this.agendaGroups = new HashMap<String, AgendaGroup>();
+ this.activationGroups = new HashMap<String, ActivationGroup>();
+ this.ruleFlowGroups = new HashMap<String, RuleFlowGroup>();
+ this.focusStack = new LinkedList<AgendaGroup>();
+
+ this.agendaGroupFactory = rb.getConfiguration().getAgendaGroupFactory();
+
+ if ( initMain ) {
+ // MAIN should always be the first AgendaGroup and can never be removed
+ this.main = agendaGroupFactory.createAgendaGroup( AgendaGroup.MAIN,
+ rb );
+
+ this.agendaGroups.put( AgendaGroup.MAIN,
+ this.main );
+
+ this.focusStack.add( this.main );
+ }
+
+ this.consequenceExceptionHandler = rb.getConfiguration().getConsequenceExceptionHandler();
+ }
+
+ public void setWorkingMemory(final InternalWorkingMemory workingMemory) {
this.workingMemory = workingMemory;
if ( ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().isSequential() ) {
this.knowledgeHelper = new SequentialKnowledgeHelper( this.workingMemory );
} else {
this.knowledgeHelper = new DefaultKnowledgeHelper( this.workingMemory );
}
- this.agendaGroups = new HashMap();
- this.activationGroups = new HashMap();
- this.ruleFlowGroups = new HashMap();
- this.focusStack = new LinkedList();
+ }
- this.agendaGroupFactory = ((InternalRuleBase) this.workingMemory.getRuleBase()).getConfiguration().getAgendaGroupFactory();
+// public void write(WMSerialisationOutContext context) throws IOException {
+// BinaryHeapQueueAgendaGroup[] agendaGroups = (BinaryHeapQueueAgendaGroup[]) this.agendaGroups.values().toArray( new AgendaGroup[this.agendaGroups.size()] );
+// Arrays.sort( agendaGroups,
+// AgendaGroupSorter.instance );
+//
+// for ( BinaryHeapQueueAgendaGroup group : agendaGroups ) {
+// context.writeInt( PersisterEnums.AGENDA_GROUP );
+// group.write( context );
+// }
+// context.writeInt( PersisterEnums.END );
+//
+// for ( ListIterator it = this.focusStack.listIterator( this.focusStack.size() - 1 ); it.hasPrevious(); ) {
+// AgendaGroup group = (AgendaGroup) it.previous();
+// context.writeInt( PersisterEnums.AGENDA_GROUP );
+// context.writeUTF( group.getName() );
+// }
+// context.writeInt( PersisterEnums.END );
+//
+// RuleFlowGroupImpl[] ruleFlowGroups = (RuleFlowGroupImpl[]) this.ruleFlowGroups.values().toArray( new RuleFlowGroupImpl[this.ruleFlowGroups.size()] );
+// Arrays.sort( agendaGroups,
+// AgendaGroupSorter.instance );
+//
+// for ( BinaryHeapQueueAgendaGroup group : agendaGroups ) {
+// context.writeInt( PersisterEnums.RULE_FLOW_GROUP );
+// group.write( context );
+// }
+// context.writeInt( PersisterEnums.END );
+// }
- // MAIN should always be the first AgendaGroup and can never be removed
- this.main = agendaGroupFactory.createAgendaGroup( AgendaGroup.MAIN,
- ((InternalRuleBase) this.workingMemory.getRuleBase()) );
+// public static class AgendaGroupSorter
+// implements
+// Comparator<AgendaGroup> {
+// public static final AgendaGroupSorter instance = new AgendaGroupSorter();
+//
+// public int compare(AgendaGroup group1,
+// AgendaGroup group2) {
+// return group1.getName().compareTo( group2.getName() );
+// }
+// }
+//
+// public static class RuleFlowGroupSorter
+// implements
+// Comparator<AgendaGroup> {
+// public static final AgendaGroupSorter instance = new AgendaGroupSorter();
+//
+// public int compare(AgendaGroup group1,
+// AgendaGroup group2) {
+// return group1.getName().compareTo( group2.getName() );
+// }
+// }
- this.agendaGroups.put( AgendaGroup.MAIN,
- this.main );
-
- this.focusStack.add( this.main );
-
- this.consequenceExceptionHandler = ((InternalRuleBase) workingMemory.getRuleBase()).getConfiguration().getConsequenceExceptionHandler();
-
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ workingMemory = (InternalWorkingMemory) in.readObject();
+ scheduledActivations = (org.drools.util.LinkedList) in.readObject();
+ agendaGroups = (Map) in.readObject();
+ activationGroups = (Map) in.readObject();
+ ruleFlowGroups = (Map) in.readObject();
+ focusStack = (LinkedList) in.readObject();
+ currentModule = (AgendaGroup) in.readObject();
+ main = (AgendaGroup) in.readObject();
+ agendaGroupFactory = (AgendaGroupFactory) in.readObject();
+ knowledgeHelper = (KnowledgeHelper) in.readObject();
+ activeActivations = in.readInt();
+ dormantActivations = in.readInt();
+ consequenceExceptionHandler = (ConsequenceExceptionHandler) in.readObject();
}
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- workingMemory = (InternalWorkingMemory)in.readObject();
- scheduledActivations = (org.drools.util.LinkedList)in.readObject();
- agendaGroups = (Map)in.readObject();
- activationGroups = (Map)in.readObject();
- ruleFlowGroups = (Map)in.readObject();
- focusStack = (LinkedList)in.readObject();
- currentModule = (AgendaGroup)in.readObject();
- main = (AgendaGroup)in.readObject();
- agendaGroupFactory = (AgendaGroupFactory)in.readObject();
- knowledgeHelper = (KnowledgeHelper)in.readObject();
- activeActivations = in.readInt();
- dormantActivations = in.readInt();
- consequenceExceptionHandler = (ConsequenceExceptionHandler)in.readObject();
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject( workingMemory );
+ out.writeObject( scheduledActivations );
+ out.writeObject( agendaGroups );
+ out.writeObject( activationGroups );
+ out.writeObject( ruleFlowGroups );
+ out.writeObject( focusStack );
+ out.writeObject( currentModule );
+ out.writeObject( main );
+ out.writeObject( agendaGroupFactory );
+ out.writeObject( knowledgeHelper );
+ out.writeInt( activeActivations );
+ out.writeInt( dormantActivations );
+ out.writeObject( consequenceExceptionHandler );
}
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject(workingMemory);
- out.writeObject(scheduledActivations);
- out.writeObject(agendaGroups);
- out.writeObject(activationGroups);
- out.writeObject(ruleFlowGroups);
- out.writeObject(focusStack);
- out.writeObject(currentModule);
- out.writeObject(main);
- out.writeObject(agendaGroupFactory);
- out.writeObject(knowledgeHelper);
- out.writeInt(activeActivations);
- out.writeInt(dormantActivations);
- out.writeObject(consequenceExceptionHandler);
- }
/* (non-Javadoc)
* @see org.drools.common.AgendaI#getWorkingMemory()
*/
@@ -201,7 +277,7 @@
public void removeScheduleItem(final ScheduledAgendaItem item) {
this.scheduledActivations.remove( item );
item.cancel();
- }
+ }
public void addAgendaGroup(final AgendaGroup agendaGroup) {
this.agendaGroups.put( agendaGroup.getName(),
@@ -218,7 +294,8 @@
this.focusStack.add( agendaGroup );
((InternalAgendaGroup) agendaGroup).setActive( true );
final EventSupport eventsupport = (EventSupport) this.workingMemory;
- eventsupport.getAgendaEventSupport().fireAgendaGroupPushed( agendaGroup, this.workingMemory );
+ eventsupport.getAgendaEventSupport().fireAgendaGroupPushed( agendaGroup,
+ this.workingMemory );
return true;
} else {
return false;
@@ -256,7 +333,8 @@
agendaGroup.setActive( false );
this.focusStack.removeLast();
final EventSupport eventsupport = (EventSupport) this.workingMemory;
- eventsupport.getAgendaEventSupport().fireAgendaGroupPopped( agendaGroup, this.workingMemory );
+ eventsupport.getAgendaEventSupport().fireAgendaGroupPopped( agendaGroup,
+ this.workingMemory );
} else {
agendaGroup = (empty) ? null : agendaGroup;
break;
@@ -305,12 +383,28 @@
return (AgendaGroup[]) this.agendaGroups.values().toArray( new AgendaGroup[this.agendaGroups.size()] );
}
+ public Map<String , AgendaGroup> getAgendaGroupsMap() {
+ return this.agendaGroups;
+ }
+
/* (non-Javadoc)
* @see org.drools.common.AgendaI#getStack()
*/
public AgendaGroup[] getStack() {
return (AgendaGroup[]) this.focusStack.toArray( new AgendaGroup[this.focusStack.size()] );
+ }
+
+ public LinkedList<AgendaGroup> getStackList() {
+ return this.focusStack;
+ }
+
+ public Map<String, RuleFlowGroup> getRuleFlowGroupsMap() {
+ return this.ruleFlowGroups;
}
+
+ public Map<String, ActivationGroup> getActivationGroupsMap() {
+ return this.activationGroups;
+ }
/* (non-Javadoc)
* @see org.drools.common.AgendaI#getActivationGroup(java.lang.String)
@@ -390,7 +484,7 @@
}
return (Activation[]) list.toArray( new Activation[list.size()] );
}
-
+
public org.drools.util.LinkedList getScheduledActivationsLinkedList() {
return this.scheduledActivations;
}
@@ -503,10 +597,9 @@
public void clearRuleFlowGroup(final RuleFlowGroup ruleFlowGroup) {
final EventSupport eventsupport = (EventSupport) this.workingMemory;
-
for ( Iterator it = ruleFlowGroup.iterator(); it.hasNext(); ) {
- RuleFlowGroupNode node = ( RuleFlowGroupNode ) it.next();
- AgendaItem item = ( AgendaItem ) node.getActivation();
+ RuleFlowGroupNode node = (RuleFlowGroupNode) it.next();
+ AgendaItem item = (AgendaItem) node.getActivation();
if ( item != null ) {
item.setActivated( false );
item.remove();
@@ -516,8 +609,6 @@
}
}
-
-
eventsupport.getAgendaEventSupport().fireActivationCancelled( item,
this.workingMemory );
}
@@ -525,9 +616,9 @@
((InternalRuleFlowGroup) ruleFlowGroup).clear();
if ( ruleFlowGroup.isActive() && ruleFlowGroup.isAutoDeactivate() ) {
- // deactivate callback
- WorkingMemoryAction action = new DeactivateCallback( (InternalRuleFlowGroup) ruleFlowGroup );
- this.workingMemory.queueWorkingMemoryAction( action );
+ // deactivate callback
+ WorkingMemoryAction action = new DeactivateCallback( (InternalRuleFlowGroup) ruleFlowGroup );
+ this.workingMemory.queueWorkingMemoryAction( action );
}
}
@@ -591,7 +682,9 @@
this.workingMemory );
this.knowledgeHelper.reset();
} catch ( final Exception e ) {
- this.consequenceExceptionHandler.handleException( activation, this.workingMemory, e );
+ this.consequenceExceptionHandler.handleException( activation,
+ this.workingMemory,
+ e );
}
if ( activation.getRuleFlowGroupNode() != null ) {
@@ -599,7 +692,8 @@
ruleFlowGroup.removeActivation( activation );
}
- eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation, this.workingMemory );
+ eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation,
+ this.workingMemory );
}
public void increaseActiveActivations() {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultFactHandle.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultFactHandle.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DefaultFactHandle.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -50,23 +50,6 @@
private RightTuple rightTuple;
private LeftTuple leftTuple;
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeInt(id);
- out.writeLong(recency);
- out.writeObject(object);
- out.writeObject(key);
- out.writeInt(objectHashCode);
- out.writeBoolean(shadowFact);
- }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- id = in.readInt();
- recency = in.readLong();
- object = in.readObject();
- key = (EqualityKey)in.readObject();
- objectHashCode = in.readInt();
- shadowFact = in.readBoolean();
- }
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/DroolsObjectInputStream.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -428,7 +428,7 @@
objectsByHandle.put(handle, object);
}
- private static InvalidClassException newInvalidClassException(Class clazz, Throwable cause) {
+ public static InvalidClassException newInvalidClassException(Class clazz, Throwable cause) {
InvalidClassException exception = new InvalidClassException(clazz.getName());
exception.initCause(cause);
return exception;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EqualityKey.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EqualityKey.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EqualityKey.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -44,7 +44,7 @@
private InternalFactHandle handle;
/** this is always lazily maintainned and deleted when empty to minimise memory consumption */
- private List instances;
+ private List<InternalFactHandle> instances;
/** This is cached in the constructor from the first added Object */
private int hashCode;
@@ -86,13 +86,13 @@
return this.handle;
}
- public List getOtherFactHandle() {
+ public List<InternalFactHandle> getOtherFactHandle() {
return this.instances;
}
public void addFactHandle(final InternalFactHandle handle) {
if ( this.instances == null ) {
- this.instances = new ArrayList();
+ this.instances = new ArrayList<InternalFactHandle>();
}
this.instances.add( handle );
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EventFactHandle.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EventFactHandle.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/EventFactHandle.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -68,20 +68,6 @@
this.duration = duration;
}
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- super.readExternal(in);
- startTimestamp = in.readLong();
- duration = in.readLong();
-
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- super.writeExternal(out);
- out.writeLong(startTimestamp);
- out.writeLong(duration);
- }
-
-
/**
* @see FactHandle
*/
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -16,6 +16,7 @@
* limitations under the License.
*/
+import java.io.IOException;
import java.util.Map;
import org.drools.FactException;
@@ -23,6 +24,8 @@
import org.drools.RuleBase;
import org.drools.RuleBaseConfiguration;
import org.drools.StatefulSession;
+import org.drools.audit.WorkingMemoryInMemoryLogger;
+import org.drools.marshalling.WMSerialisationInContext;
import org.drools.objenesis.Objenesis;
import org.drools.process.core.Process;
import org.drools.reteoo.Rete;
@@ -46,6 +49,8 @@
public int nextWorkingMemoryCounter();
public FactHandleFactory newFactHandleFactory();
+
+ public FactHandleFactory newFactHandleFactory(int id, long counter) throws IOException ;
public Map getGlobals();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -96,4 +96,5 @@
public ObjectTypeConfigurationRegistry getObjectTypeConfigurationRegistry();
+ public InternalFactHandle getInitialFactHandle();
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -18,10 +18,13 @@
import java.io.IOException;
import java.io.ObjectInput;
+import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
-
+import org.drools.marshalling.PersisterEnums;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.spi.Activation;
import org.drools.util.Iterator;
import org.drools.util.LinkedList;
@@ -40,20 +43,23 @@
* @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
*
*/
-public class RuleFlowGroupImpl implements InternalRuleFlowGroup {
+public class RuleFlowGroupImpl
+ implements
+ InternalRuleFlowGroup {
- private static final long serialVersionUID = 400L;
+ private static final long serialVersionUID = 400L;
- private InternalWorkingMemory workingMemory;
- private String name;
- private boolean active = false;
- private LinkedList list;
- private boolean autoDeactivate = true;
+ private InternalWorkingMemory workingMemory;
+ private String name;
+ private boolean active = false;
+ private boolean autoDeactivate = true;
+ private LinkedList list;
private List<RuleFlowGroupListener> listeners;
public RuleFlowGroupImpl() {
}
+
/**
* Construct a <code>RuleFlowGroupImpl</code> with the given name.
*
@@ -64,23 +70,31 @@
this.name = name;
this.list = new LinkedList();
}
+
+ public RuleFlowGroupImpl(final String name, final boolean active, final boolean autoDeactivate) {
+ this.name = name;
+ this.active = active;
+ this.autoDeactivate = autoDeactivate;
+ this.list = new LinkedList();
+ }
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- workingMemory = (InternalWorkingMemory)in.readObject();
- name = (String)in.readObject();
- active = in.readBoolean();
- list = (LinkedList)in.readObject();
- autoDeactivate = in.readBoolean();
- listeners = (List<RuleFlowGroupListener>)in.readObject();
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ workingMemory = (InternalWorkingMemory) in.readObject();
+ name = (String) in.readObject();
+ active = in.readBoolean();
+ list = (LinkedList) in.readObject();
+ autoDeactivate = in.readBoolean();
+ listeners = (List<RuleFlowGroupListener>) in.readObject();
}
public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject(workingMemory);
- out.writeObject(name);
- out.writeBoolean(active);
- out.writeObject(list);
- out.writeBoolean(autoDeactivate);
- out.writeObject(listeners);
+ out.writeObject( workingMemory );
+ out.writeObject( name );
+ out.writeBoolean( active );
+ out.writeObject( list );
+ out.writeBoolean( autoDeactivate );
+ out.writeObject( listeners );
}
public String getName() {
@@ -101,8 +115,8 @@
}
this.active = active;
if ( active ) {
- ((EventSupport) this.workingMemory).getRuleFlowEventSupport()
- .fireBeforeRuleFlowGroupActivated(this, this.workingMemory);
+ ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireBeforeRuleFlowGroupActivated( this,
+ this.workingMemory );
if ( this.list.isEmpty() ) {
if ( this.autoDeactivate ) {
// if the list of activations is empty and
@@ -113,11 +127,11 @@
} else {
triggerActivations();
}
- ((EventSupport) this.workingMemory).getRuleFlowEventSupport()
- .fireAfterRuleFlowGroupActivated(this, this.workingMemory);
+ ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireAfterRuleFlowGroupActivated( this,
+ this.workingMemory );
} else {
- ((EventSupport) this.workingMemory).getRuleFlowEventSupport()
- .fireBeforeRuleFlowGroupDeactivated(this, this.workingMemory);
+ ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireBeforeRuleFlowGroupDeactivated( this,
+ this.workingMemory );
final Iterator it = this.list.iterator();
for ( RuleFlowGroupNode node = (RuleFlowGroupNode) it.next(); node != null; node = (RuleFlowGroupNode) it.next() ) {
final Activation activation = node.getActivation();
@@ -127,8 +141,8 @@
}
}
notifyRuleFlowGroupListeners();
- ((EventSupport) this.workingMemory).getRuleFlowEventSupport()
- .fireAfterRuleFlowGroupDeactivated(this, this.workingMemory);
+ ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireAfterRuleFlowGroupDeactivated( this,
+ this.workingMemory );
}
}
@@ -189,10 +203,10 @@
}
public void addRuleFlowGroupListener(RuleFlowGroupListener listener) {
- if (listeners == null) {
+ if ( listeners == null ) {
listeners = new CopyOnWriteArrayList<RuleFlowGroupListener>();
}
- listeners.add(listener);
+ listeners.add( listener );
}
public void removeRuleFlowGroupListener(RuleFlowGroupListener listener) {
@@ -202,8 +216,8 @@
}
public void notifyRuleFlowGroupListeners() {
- if (listeners != null) {
- for (java.util.Iterator<RuleFlowGroupListener> iterator = listeners.iterator(); iterator.hasNext(); ) {
+ if ( listeners != null ) {
+ for ( java.util.Iterator<RuleFlowGroupListener> iterator = listeners.iterator(); iterator.hasNext(); ) {
iterator.next().ruleFlowGroupDeactivated();
}
}
@@ -237,29 +251,40 @@
return this.name.hashCode();
}
- public static class DeactivateCallback implements WorkingMemoryAction {
- private static final long serialVersionUID = 400L;
+ public static class DeactivateCallback
+ implements
+ WorkingMemoryAction {
+ private static final long serialVersionUID = 400L;
private InternalRuleFlowGroup ruleFlowGroup;
public DeactivateCallback() {
}
+
public DeactivateCallback(InternalRuleFlowGroup ruleFlowGroup) {
this.ruleFlowGroup = ruleFlowGroup;
}
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- ruleFlowGroup = (InternalRuleFlowGroup)in.readObject();
+ public DeactivateCallback(WMSerialisationInContext context) throws IOException {
+
}
+ public void write(WMSerialisationOutContext context) throws IOException {
+ }
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ ruleFlowGroup = (InternalRuleFlowGroup) in.readObject();
+ }
+
public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject(ruleFlowGroup);
+ out.writeObject( ruleFlowGroup );
}
public void execute(InternalWorkingMemory workingMemory) {
// check whether ruleflow group is still empty first
- if (this.ruleFlowGroup.isEmpty()) {
+ if ( this.ruleFlowGroup.isEmpty() ) {
// deactivate ruleflow group
- this.ruleFlowGroup.setActive(false);
+ this.ruleFlowGroup.setActive( false );
}
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TruthMaintenanceSystem.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TruthMaintenanceSystem.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/TruthMaintenanceSystem.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -16,20 +16,30 @@
* limitations under the License.
*/
+import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.Externalizable;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.drools.FactException;
+import org.drools.marshalling.PersisterEnums;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
import org.drools.spi.PropagationContext;
import org.drools.util.ObjectHashMap;
import org.drools.util.PrimitiveLongMap;
+import org.drools.util.ObjectHashMap.ObjectEntry;
import org.w3c.dom.views.AbstractView;
/**
@@ -42,15 +52,13 @@
* @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
*
*/
-public class TruthMaintenanceSystem
- implements
- Externalizable {
+public class TruthMaintenanceSystem {
- private static final long serialVersionUID = 400L;
+ private static final long serialVersionUID = 400L;
private AbstractWorkingMemory workingMemory;
- private PrimitiveLongMap justifiedMap;
+ private ObjectHashMap justifiedMap;
private ObjectHashMap assertMap;
@@ -60,25 +68,81 @@
public TruthMaintenanceSystem(final AbstractWorkingMemory workingMemory) {
this.workingMemory = workingMemory;
- this.justifiedMap = new PrimitiveLongMap( 8,
- 32 );
+ this.justifiedMap = new ObjectHashMap();
this.assertMap = new ObjectHashMap();
this.assertMap.setComparator( EqualityKeyComparator.getInstance() );
}
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- workingMemory = (AbstractWorkingMemory)in.readObject();
- justifiedMap = (PrimitiveLongMap)in.readObject();
- assertMap = (ObjectHashMap)in.readObject();
- }
+ // public void write(WMSerialisationOutContext context) throws IOException {
+ // ObjectOutputStream stream = context.stream;
+ //
+ // EqualityKey[] keys = new EqualityKey[ this.assertMap.size() ];
+ // org.drools.util.Iterator it = this.assertMap.iterator();
+ // int i = 0;
+ // for ( ObjectEntry entry = ( ObjectEntry ) it.next(); entry != null; entry = ( ObjectEntry ) it.next() ) {
+ // EqualityKey key = ( EqualityKey ) entry.getKey();
+ // keys[i++] = key;
+ // }
+ //
+ // Arrays.sort( keys, EqualityKeySorter.instance );
+ //
+ // // write the assert map of Equality keys
+ // for ( EqualityKey key : keys ) {
+ // stream.writeInt( PersisterEnums.EQUALITY_KEY );
+ // stream.writeInt( key.getStatus() );
+ // InternalFactHandle handle = key.getFactHandle();
+ // stream.writeInt( handle.getId() );
+ // context.out.println( "EqualityKey int:" + key.getStatus() + " int:" + handle.getId() );
+ // if ( key.getOtherFactHandle() != null && !key.getOtherFactHandle().isEmpty() ) {
+ // for ( InternalFactHandle handle2 : key.getOtherFactHandle() ) {
+ // stream.writeInt( PersisterEnums.FACT_HANDLE );
+ // stream.writeInt( handle2.getId() );
+ // context.out.println( "OtherHandle int:" + handle2.getId() );
+ // }
+ // }
+ // stream.writeInt( PersisterEnums.END );
+ // }
+ // stream.writeInt( PersisterEnums.END );
+ // }
+ //
+ // public static class EqualityKeySorter implements Comparator<EqualityKey> {
+ // public static final EqualityKeySorter instance = new EqualityKeySorter();
+ // public int compare(EqualityKey key1,
+ // EqualityKey key2) {
+ // return key1.getFactHandle().getId() - key2.getFactHandle().getId();
+ // }
+ // }
+ //
+ // public void read(WMSerialisationInContext context) throws IOException {
+ // ObjectInputStream stream = context.stream;
+ //
+ // while ( stream.readInt() == PersisterEnums.EQUALITY_KEY ) {
+ // int status = stream.readInt();
+ // InternalFactHandle handle = ( InternalFactHandle ) context.handles.get( stream.readInt() );
+ // EqualityKey key = new EqualityKey(handle, status);
+ // handle.setEqualityKey( key );
+ // while ( stream.readInt() == PersisterEnums.FACT_HANDLE ) {
+ // handle = ( InternalFactHandle ) context.wm.getFactHandle( stream.readInt() );
+ // key.addFactHandle( handle );
+ // handle.setEqualityKey( key );
+ // }
+ // put( key );
+ // }
+ // }
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject(workingMemory);
- out.writeObject(justifiedMap);
- out.writeObject(assertMap);
- }
+ // public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ // workingMemory = (AbstractWorkingMemory)in.readObject();
+ // justifiedMap = (PrimitiveLongMap)in.readObject();
+ // assertMap = (ObjectHashMap)in.readObject();
+ // }
+ //
+ // public void writeExternal(ObjectOutput out) throws IOException {
+ // out.writeObject(workingMemory);
+ // out.writeObject(justifiedMap);
+ // out.writeObject(assertMap);
+ // }
- public PrimitiveLongMap getJustifiedMap() {
+ public ObjectHashMap getJustifiedMap() {
return this.justifiedMap;
}
@@ -151,6 +215,7 @@
public LogicalRetractCallback() {
}
+
public LogicalRetractCallback(final TruthMaintenanceSystem tms,
final LogicalDependency node,
final Set set,
@@ -164,37 +229,61 @@
this.context = context;
}
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- tms = (TruthMaintenanceSystem)in.readObject();
- node = (LogicalDependency)in.readObject();
- set = (Set)in.readObject();
- handle = (InternalFactHandle)in.readObject();
- context = (PropagationContext)in.readObject();
- activation = ( Activation ) in.readObject();
+ public LogicalRetractCallback(WMSerialisationInContext context) throws IOException {
+ this.tms = context.wm.getTruthMaintenanceSystem();
+
+ this.handle = context.handles.get( context.readInt() );
+ this.context = context.propagationContexts.get( context.readLong() );
+ this.activation = context.terminalTupleMap.get( context.readInt() ).getActivation();
+
+ this.set = ( Set ) this.tms.getJustifiedMap().get( handle.getId() );
+
+ for ( Iterator it = this.set.iterator(); it.hasNext(); ) {
+ LogicalDependency node = ( LogicalDependency ) it.next();
+ if ( node.getJustifier() == this.activation ) {
+ this.node = node;
+ break;
+ }
+ }
}
+ public void write(WMSerialisationOutContext context) throws IOException {
+ context.writeInt( WorkingMemoryAction.LogicalRetractCallback );
+
+ context.writeInt( this.handle.getId() );
+ context.writeLong( this.context.getPropagationNumber() );
+ context.writeInt( context.terminalTupleMap.get( this.activation.getTuple() ) );
+ }
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ tms = (TruthMaintenanceSystem) in.readObject();
+ node = (LogicalDependency) in.readObject();
+ set = (Set) in.readObject();
+ handle = (InternalFactHandle) in.readObject();
+ context = (PropagationContext) in.readObject();
+ activation = (Activation) in.readObject();
+ }
+
public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject(tms);
- out.writeObject(node);
- out.writeObject(set);
- out.writeObject(handle);
- out.writeObject(context);
+ out.writeObject( tms );
+ out.writeObject( node );
+ out.writeObject( set );
+ out.writeObject( handle );
+ out.writeObject( context );
out.writeObject( activation );
}
public void execute(InternalWorkingMemory workingMemory) {
-
if ( set.isEmpty() ) {
- if ( set.isEmpty() ) {
- this.tms.getJustifiedMap().remove( handle.getId() );
- // this needs to be scheduled so we don't upset the current
- // working memory operation
- workingMemory.retract( this.handle,
- false,
- true,
- context.getRuleOrigin(),
- this.activation );
- }
+ this.tms.getJustifiedMap().remove( handle.getId() );
+ // this needs to be scheduled so we don't upset the current
+ // working memory operation
+ workingMemory.retract( this.handle,
+ false,
+ true,
+ context.getRuleOrigin(),
+ this.activation );
}
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/WorkingMemoryAction.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/WorkingMemoryAction.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/WorkingMemoryAction.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -4,7 +4,18 @@
package org.drools.common;
import java.io.Externalizable;
+import java.io.IOException;
+import org.drools.marshalling.WMSerialisationOutContext;
+
public interface WorkingMemoryAction extends Externalizable {
+ public static final int WorkingMemoryReteAssertAction = 1;
+ public static final int DeactivateCallback = 2;
+ public static final int PropagateAction = 3;
+ public static final int LogicalRetractCallback = 4;
+
+
public void execute(InternalWorkingMemory workingMemory);
+
+ public void write(WMSerialisationOutContext context) throws IOException;
}
\ No newline at end of file
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling (from rev 19976, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister)
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ClassPlaceholderResolverStrategyAcceptor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ClassPlaceholderResolverStrategyAcceptor.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ClassPlaceholderResolverStrategyAcceptor.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,29 @@
+package org.drools.marshalling;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.util.ClassUtils;
+
+public class ClassPlaceholderResolverStrategyAcceptor implements PlaceholderResolverStrategyAcceptor {
+ private final Map<String, Object> patterns;
+
+ public ClassPlaceholderResolverStrategyAcceptor(String str) {
+ this.patterns = new HashMap<String, Object>();
+ addPatterns(str);
+ }
+
+ public ClassPlaceholderResolverStrategyAcceptor() {
+ this.patterns = new HashMap<String, Object>();
+ }
+
+ public void addPatterns(String str) {
+
+ ClassUtils.addImportStylePatterns( this.patterns, str );
+ }
+
+ public boolean accept(Object object) {
+ return ClassUtils.isMatched( this.patterns, object.getClass().getName() );
+ }
+
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/IdentityPlaceholderResolverStrategy.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/IdentityPlaceholderResolverStrategy.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/IdentityPlaceholderResolverStrategy.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,48 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.IdentityHashMap;
+import java.util.Map;
+
+public class IdentityPlaceholderResolverStrategy
+ implements
+ PlaceholderResolverStrategy {
+
+ private int index;
+
+ private Map<Integer, Object> map;
+
+ private PlaceholderResolverStrategyAcceptor acceptor;
+
+ public IdentityPlaceholderResolverStrategy(PlaceholderResolverStrategyAcceptor acceptor) {
+ this.acceptor = acceptor;
+ this.map = new IdentityHashMap<Integer, Object>();
+ }
+
+ public int getIndex() {
+ return this.index;
+ }
+
+ public void setIndex(int index) {
+ this.index = index;
+ }
+
+ public ObjectPlaceholder read(ObjectInputStream os) throws IOException,
+ ClassNotFoundException {
+ int id = os.readInt();
+ return new SerializablePlaceholder( map.get( id ));
+ }
+
+ public void write(ObjectOutputStream os,
+ Object object) throws IOException {
+ Integer id = map.size();
+ map.put( id, object );
+ os.writeInt( id );
+ }
+
+ public boolean accept(Object object) {
+ return this.acceptor.accept( object );
+ }
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/InputPersister.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,353 +0,0 @@
-package org.drools.persister;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.util.Map;
-
-import org.drools.base.ClassObjectType;
-import org.drools.common.AgendaItem;
-import org.drools.common.BaseNode;
-import org.drools.common.BinaryHeapQueueAgendaGroup;
-import org.drools.common.DefaultAgenda;
-import org.drools.common.DefaultFactHandle;
-import org.drools.common.InternalAgendaGroup;
-import org.drools.common.InternalFactHandle;
-import org.drools.common.InternalRuleBase;
-import org.drools.common.InternalWorkingMemory;
-import org.drools.common.PropagationContextImpl;
-import org.drools.reteoo.BetaMemory;
-import org.drools.reteoo.BetaNode;
-import org.drools.reteoo.EntryPointNode;
-import org.drools.reteoo.JoinNode;
-import org.drools.reteoo.LeftInputAdapterNode;
-import org.drools.reteoo.LeftTuple;
-import org.drools.reteoo.LeftTupleSink;
-import org.drools.reteoo.LeftTupleSource;
-import org.drools.reteoo.NotNode;
-import org.drools.reteoo.ObjectSink;
-import org.drools.reteoo.ObjectTypeNode;
-import org.drools.reteoo.ReteooFactHandleFactory;
-import org.drools.reteoo.ReteooWorkingMemory;
-import org.drools.reteoo.RightTuple;
-import org.drools.reteoo.RightTupleSink;
-import org.drools.reteoo.RuleTerminalNode;
-import org.drools.reteoo.RuleTerminalNode.TerminalNodeMemory;
-import org.drools.rule.EntryPoint;
-import org.drools.rule.GroupElement;
-import org.drools.rule.Package;
-import org.drools.rule.Rule;
-import org.drools.spi.Activation;
-import org.drools.spi.AgendaGroup;
-import org.drools.spi.ObjectType;
-import org.drools.spi.PropagationContext;
-import org.drools.util.BinaryHeapQueue;
-import org.drools.util.ObjectHashMap;
-import org.drools.util.ObjectHashSet;
-
-public class InputPersister {
- public WMSerialisationInContext context;
-
- public InputPersister(InternalRuleBase ruleBase,
- ObjectInputStream stream,
- PlaceholderResolverStrategyFactory resolverStrategyFactory) {
- context = new WMSerialisationInContext( stream,
- ruleBase,
- RuleBaseNodes.getNodeMap( ruleBase ),
- resolverStrategyFactory );
- }
-
- public InternalWorkingMemory read() throws IOException,
- ClassNotFoundException {
- readFactHandles( context );
- context.stream.close();
- return context.wm;
- }
-
- public static void readFactHandles(WMSerialisationInContext context) throws IOException,
- ClassNotFoundException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
-
- PlaceholderResolverStrategyFactory resolverStrategyFactory = context.resolverStrategyFactory;
-
- ReteooFactHandleFactory factHandleFactory = new ReteooFactHandleFactory();
- factHandleFactory.readExternal( stream );
- context.wm = new ReteooWorkingMemory( 0,
- ruleBase,
- factHandleFactory );
-
- int size = stream.readInt();
-
- if ( size == 0 ) {
- return;
- }
-
- // load the handles
- InternalFactHandle[] handles = new InternalFactHandle[size];
- for ( int i = 0; i < size; i++ ) {
- int id = stream.readInt();
- long recency = stream.readLong();
- PlaceholderResolverStrategy strategy = resolverStrategyFactory.get( null );
- ObjectPlaceholder placeHolder = strategy.read( stream );
-
- Object object = placeHolder.resolveObject();
-
- InternalFactHandle handle = new DefaultFactHandle( id,
- object,
- recency );
- context.handles.put( id,
- handle );
- handles[i] = handle;
-
- context.wm.getObjectStore().addHandle( handle,
- object );
-
- readRightTuples( handle,
- context );
- }
-
- EntryPointNode node = ruleBase.getRete().getEntryPointNode( EntryPoint.DEFAULT );
- Map<ObjectType, ObjectTypeNode> objectTypeNodes = node.getObjectTypeNodes();
-
- // add handles to object type nodes
- for ( InternalFactHandle handle : handles ) {
- Object object = handle.getObject();
- ClassObjectType objectType = new ClassObjectType( object.getClass() );
- ObjectTypeNode objectTypeNode = objectTypeNodes.get( objectType );
- ObjectHashSet set = (ObjectHashSet) context.wm.getNodeMemory( objectTypeNode );
- set.add( handle,
- false );
- }
-
- readLeftTuples( context );
-
- readPropagationContexts( context );
-
- readActivations( context );
- }
-
- public static void readRightTuples(InternalFactHandle factHandle,
- WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
- while ( stream.readInt() == PersisterEnums.RIGHT_TUPLE ) {
- readRightTuple( context,
- factHandle );
- }
- }
-
- public static void readRightTuple(WMSerialisationInContext context,
- InternalFactHandle factHandle) throws IOException {
- ObjectInputStream stream = context.stream;
-
- RightTupleSink sink = (RightTupleSink) context.sinks.get( stream.readInt() );
-
- BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
-
- RightTuple rightTuple = new RightTuple( factHandle,
- sink );
- context.rightTuples.put( new RightTupleKey( factHandle.getId(),
- sink ),
- rightTuple );
-
- memory.getRightTupleMemory().add( rightTuple );
- }
-
- public static void readLeftTuples(WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
-
- while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
- LeftTupleSink sink = (LeftTupleSink) context.sinks.get( stream.readInt() );
- int factHandleId = stream.readInt();
- LeftTuple leftTuple = new LeftTuple( context.handles.get( factHandleId ),
- sink,
- true );
- readLeftTuple( leftTuple,
- context );
- }
- }
-
- public static void readLeftTuple(LeftTuple parentLeftTuple,
- WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
- Map<Integer, BaseNode> sinks = context.sinks;
-
- LeftTupleSink sink = parentLeftTuple.getLeftTupleSink();
-
- if ( sink instanceof JoinNode ) {
- BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
- memory.getLeftTupleMemory().add( parentLeftTuple );
-
- while ( stream.readInt() == PersisterEnums.RIGHT_TUPLE ) {
- LeftTupleSink childSink = (LeftTupleSink) sinks.get( stream.readInt() );
- int factHandleId = stream.readInt();
- RightTupleKey key = new RightTupleKey( factHandleId,
- sink );
- RightTuple rightTuple = context.rightTuples.get( key );
- LeftTuple childLeftTuple = new LeftTuple( parentLeftTuple,
- rightTuple,
- childSink,
- true );
- readLeftTuple( childLeftTuple,
- context );
- }
-
- } else if ( sink instanceof NotNode ) {
- BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
- int type = stream.readInt();
- if ( type == PersisterEnums.LEFT_TUPLE_NOT_BLOCKED ) {
- memory.getLeftTupleMemory().add( parentLeftTuple );
-
- while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
- LeftTupleSink childSink = (LeftTupleSink) sinks.get( stream.readInt() );
- LeftTuple childLeftTuple = new LeftTuple( parentLeftTuple,
- childSink,
- true );
- readLeftTuple( childLeftTuple,
- context );
- }
-
- } else {
- int factHandleId = stream.readInt();
- RightTupleKey key = new RightTupleKey( factHandleId,
- sink );
- RightTuple rightTuple = context.rightTuples.get( key );
-
- LeftTuple blockedPrevious = rightTuple.getBlocked();
- if ( blockedPrevious != null ) {
- parentLeftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( parentLeftTuple );
- }
- rightTuple.setBlocked( parentLeftTuple );
- }
- } else if ( sink instanceof RuleTerminalNode ) {
- RuleTerminalNode ruleTerminalNode = (RuleTerminalNode) sink;
- TerminalNodeMemory memory = (TerminalNodeMemory) wm.getNodeMemory( ruleTerminalNode );
- memory.getTupleMemory().add( parentLeftTuple );
-
- int pos = context.terminalTupleMap.size();
- context.terminalTupleMap.put( pos,
- parentLeftTuple );
- }
- }
-
- public static void readActivations(WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
-
- while ( stream.readInt() == PersisterEnums.ACTIVATION ) {
- readActivation( context );
- }
- }
-
- public static Activation readActivation(WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
-
- long activationNumber = stream.readLong();
-
- LeftTuple leftTuple = context.terminalTupleMap.get( stream.readInt() );
-
- int salience = stream.readInt();
-
- //PropagationContext context,
- String pkgName = stream.readUTF();
- String ruleName = stream.readUTF();
- Package pkg = ruleBase.getPackage( pkgName );
- Rule rule = pkg.getRule( ruleName );
-
- RuleTerminalNode ruleTerminalNode = (RuleTerminalNode) leftTuple.getLeftTupleSink();
- GroupElement subRule = ruleTerminalNode.getSubRule();
-
- PropagationContext pc = context.propagationContexts.get( stream.readLong() );
-
- AgendaItem activation = new AgendaItem( activationNumber,
- leftTuple,
- salience,
- pc,
- rule,
- subRule );
-
- leftTuple.setActivation( activation );
-
- boolean activated = stream.readBoolean();
- activation.setActivated( activated );
- if ( activated ) {
- InternalAgendaGroup agendaGroup;
- if ( rule.getAgendaGroup() == null || rule.getAgendaGroup().equals( "" ) || rule.getAgendaGroup().equals( AgendaGroup.MAIN ) ) {
- // Is the Rule AgendaGroup undefined? If it is use MAIN,
- // which is added to the Agenda by default
- agendaGroup = (InternalAgendaGroup) wm.getAgenda().getAgendaGroup( AgendaGroup.MAIN );
- } else {
- // AgendaGroup is defined, so try and get the AgendaGroup
- // from the Agenda
- agendaGroup = (InternalAgendaGroup) wm.getAgenda().getAgendaGroup( rule.getAgendaGroup() );
- }
-
- agendaGroup.add( activation );
- }
-
- return activation;
- }
-
- public static void readPropagationContexts(WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
-
- while ( stream.readInt() == PersisterEnums.PROPAGATION_CONTEXT ) {
- readPropagationContext( context );
- }
-
- }
-
- public static void readPropagationContext(WMSerialisationInContext context) throws IOException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
-
- int type = stream.readInt();
-
- Rule rule = null;
- if ( stream.readBoolean() ) {
- String pkgName = stream.readUTF();
- String ruleName = stream.readUTF();
- Package pkg = ruleBase.getPackage( pkgName );
- rule = pkg.getRule( ruleName );
- }
-
- LeftTuple leftTuple = null;
- if ( stream.readBoolean() ) {
- int tuplePos = stream.readInt();
- leftTuple = (LeftTuple) context.terminalTupleMap.get( tuplePos );
- }
-
- long propagationNumber = stream.readLong();
-
- int factHandleId = stream.readInt();
- InternalFactHandle factHandle = context.handles.get( factHandleId );
-
-
- int activeActivations = stream.readInt();
- int dormantActivations = stream.readInt();
- String entryPointId = stream.readUTF();
-
- EntryPoint entryPoint = context.entryPoints.get( entryPointId );
- if ( entryPoint == null ) {
- entryPoint = new EntryPoint( entryPointId );
- context.entryPoints.put( entryPointId,
- entryPoint );
- }
-
- PropagationContext pc = new PropagationContextImpl( propagationNumber,
- type,
- rule,
- leftTuple,
- factHandle,
- activeActivations,
- dormantActivations,
- entryPoint );
- context.propagationContexts.put( propagationNumber,
- pc );
- }
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/InputPersister.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/InputPersister.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,513 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Queue;
+
+import org.drools.base.ClassObjectType;
+import org.drools.common.AgendaItem;
+import org.drools.common.BaseNode;
+import org.drools.common.BinaryHeapQueueAgendaGroup;
+import org.drools.common.DefaultAgenda;
+import org.drools.common.DefaultFactHandle;
+import org.drools.common.EqualityKey;
+import org.drools.common.InternalAgendaGroup;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalRuleFlowGroup;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.PropagationContextImpl;
+import org.drools.common.RuleFlowGroupImpl;
+import org.drools.common.TruthMaintenanceSystem;
+import org.drools.concurrent.ExecutorService;
+import org.drools.reteoo.BetaMemory;
+import org.drools.reteoo.BetaNode;
+import org.drools.reteoo.EntryPointNode;
+import org.drools.reteoo.EvalConditionNode;
+import org.drools.reteoo.ExistsNode;
+import org.drools.reteoo.InitialFactHandle;
+import org.drools.reteoo.InitialFactHandleDummyObject;
+import org.drools.reteoo.JoinNode;
+import org.drools.reteoo.LeftInputAdapterNode;
+import org.drools.reteoo.LeftTuple;
+import org.drools.reteoo.LeftTupleSink;
+import org.drools.reteoo.LeftTupleSource;
+import org.drools.reteoo.NotNode;
+import org.drools.reteoo.ObjectSink;
+import org.drools.reteoo.ObjectTypeNode;
+import org.drools.reteoo.ReteooFactHandleFactory;
+import org.drools.reteoo.ReteooStatefulSession;
+import org.drools.reteoo.ReteooWorkingMemory;
+import org.drools.reteoo.RightTuple;
+import org.drools.reteoo.RightTupleSink;
+import org.drools.reteoo.RuleTerminalNode;
+import org.drools.reteoo.EvalConditionNode.EvalMemory;
+import org.drools.reteoo.RuleTerminalNode.TerminalNodeMemory;
+import org.drools.rule.EntryPoint;
+import org.drools.rule.GroupElement;
+import org.drools.rule.Package;
+import org.drools.rule.Rule;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.FactHandleFactory;
+import org.drools.spi.ObjectType;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.RuleFlowGroup;
+import org.drools.util.BinaryHeapQueue;
+import org.drools.util.Iterator;
+import org.drools.util.ObjectHashMap;
+import org.drools.util.ObjectHashSet;
+import org.drools.util.ObjectHashSet.ObjectEntry;
+
+public class InputPersister {
+ public static ReteooStatefulSession readSession(WMSerialisationInContext context,
+ int id,
+ ExecutorService executor) throws IOException,
+ ClassNotFoundException {
+ FactHandleFactory handleFactory = context.ruleBase.newFactHandleFactory( context.readInt(),
+ context.readLong() );
+
+ InitialFactHandle initialFactHandle = new InitialFactHandle( new DefaultFactHandle( context.readInt(), //id
+ new InitialFactHandleDummyObject(),
+ context.readLong() ) ); //recency
+ context.handles.put( initialFactHandle.getId(),
+ initialFactHandle );
+
+ long propagationCounter = context.readLong();
+
+ DefaultAgenda agenda = new DefaultAgenda( context.ruleBase, false );
+ readAgenda( context, agenda );
+ ReteooStatefulSession session = new ReteooStatefulSession( id,
+ context.ruleBase,
+ executor,
+ handleFactory,
+ initialFactHandle,
+ propagationCounter,
+ agenda );
+
+ // RuleFlowGroups need to reference the session
+ for ( RuleFlowGroup group : agenda.getRuleFlowGroupsMap().values() ) {
+ ((RuleFlowGroupImpl) group).setWorkingMemory( session );
+ }
+ context.wm = session;
+
+ readFactHandles( context );
+
+ readActionQueue( context );
+
+ if ( context.readBoolean() ) {
+ readTruthMaintenanceSystem( context );
+ }
+
+ return session;
+ }
+
+ public static void readAgenda(WMSerialisationInContext context, DefaultAgenda agenda) throws IOException {
+ ObjectInputStream stream = context.stream;
+ while ( stream.readInt() == PersisterEnums.AGENDA_GROUP ) {
+ BinaryHeapQueueAgendaGroup group = new BinaryHeapQueueAgendaGroup( stream.readUTF(), context.ruleBase );
+ group.setActive( stream.readBoolean() );
+ agenda.getAgendaGroupsMap().put( group.getName(), group );
+ }
+
+ while ( stream.readInt() == PersisterEnums.AGENDA_GROUP ) {
+ String agendaGroupName = stream.readUTF();
+ agenda.getStackList().add( agenda.getAgendaGroup( agendaGroupName ) );
+ }
+
+ while ( stream.readInt() == PersisterEnums.RULE_FLOW_GROUP ) {
+ String rfgName = stream.readUTF();
+ boolean active = stream.readBoolean();
+ boolean autoDeactivate = stream.readBoolean();
+ RuleFlowGroup rfg = new RuleFlowGroupImpl( rfgName, active, autoDeactivate );
+ agenda.getRuleFlowGroupsMap().put( rfgName, rfg );
+ }
+
+ }
+
+ public static void readActionQueue(WMSerialisationInContext context) throws IOException {
+ ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm;
+ Queue actionQueue = wm.getActionQueue();
+ while ( context.readInt() == PersisterEnums.WORKING_MEMORY_ACTION ) {
+ actionQueue.offer( PersisterHelper.readWorkingMemoryAction( context ) );
+ }
+ }
+
+ public static void readTruthMaintenanceSystem(WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+
+ TruthMaintenanceSystem tms = context.wm.getTruthMaintenanceSystem();
+ while ( stream.readInt() == PersisterEnums.EQUALITY_KEY ) {
+ int status = stream.readInt();
+ int factHandleId = stream.readInt();
+ InternalFactHandle handle = (InternalFactHandle) context.handles.get( factHandleId );
+ EqualityKey key = new EqualityKey( handle,
+ status );
+ handle.setEqualityKey( key );
+ while ( stream.readInt() == PersisterEnums.FACT_HANDLE ) {
+ factHandleId = stream.readInt();
+ handle = (InternalFactHandle) context.handles.get( factHandleId );
+ key.addFactHandle( handle );
+ handle.setEqualityKey( key );
+ }
+ tms.put( key );
+ }
+ }
+
+ public static void readFactHandles(WMSerialisationInContext context) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream stream = context.stream;
+ InternalRuleBase ruleBase = context.ruleBase;
+ PlaceholderResolverStrategyFactory resolverStrategyFactory = context.resolverStrategyFactory;
+ InternalWorkingMemory wm = context.wm;
+
+ if ( stream.readBoolean() ) {
+ InternalFactHandle initialFactHandle = wm.getInitialFactHandle();
+ int sinkId = stream.readInt();
+ ObjectTypeNode initialFactNode = (ObjectTypeNode) context.sinks.get( sinkId );
+ ObjectHashSet initialFactMemory = (ObjectHashSet) context.wm.getNodeMemory( initialFactNode );
+
+ initialFactMemory.add( initialFactHandle );
+ readRightTuples( initialFactHandle,
+ context );
+ }
+
+ int size = stream.readInt();
+
+ // load the handles
+ InternalFactHandle[] handles = new InternalFactHandle[size];
+ for ( int i = 0; i < size; i++ ) {
+ int id = stream.readInt();
+ long recency = stream.readLong();
+
+ int strategyIndex = stream.readInt();
+ PlaceholderResolverStrategy strategy = resolverStrategyFactory.getStrategy( strategyIndex );
+ ObjectPlaceholder placeHolder = strategy.read( stream );
+
+ Object object = placeHolder.resolveObject();
+
+ InternalFactHandle handle = new DefaultFactHandle( id,
+ object,
+ recency );
+ context.handles.put( id,
+ handle );
+ handles[i] = handle;
+
+ context.wm.getObjectStore().addHandle( handle,
+ object );
+
+ readRightTuples( handle,
+ context );
+ }
+
+ EntryPointNode node = ruleBase.getRete().getEntryPointNode( EntryPoint.DEFAULT );
+ Map<ObjectType, ObjectTypeNode> objectTypeNodes = node.getObjectTypeNodes();
+
+ // add handles to object type nodes
+ for ( InternalFactHandle handle : handles ) {
+ Object object = handle.getObject();
+ ClassObjectType objectType = new ClassObjectType( object.getClass() );
+ ObjectTypeNode objectTypeNode = objectTypeNodes.get( objectType );
+ ObjectHashSet set = (ObjectHashSet) context.wm.getNodeMemory( objectTypeNode );
+ set.add( handle,
+ false );
+ }
+
+ InternalFactHandle handle = wm.getInitialFactHandle();
+ while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
+ LeftTupleSink sink = (LeftTupleSink) context.sinks.get( stream.readInt() );
+ LeftTuple leftTuple = new LeftTuple( handle,
+ sink,
+ true );
+ readLeftTuple( leftTuple,
+ context );
+ }
+
+ readLeftTuples( context );
+
+ readPropagationContexts( context );
+
+ readActivations( context );
+ }
+
+ public static void readRightTuples(InternalFactHandle factHandle,
+ WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+ while ( stream.readInt() == PersisterEnums.RIGHT_TUPLE ) {
+ readRightTuple( context,
+ factHandle );
+ }
+ }
+
+ public static void readRightTuple(WMSerialisationInContext context,
+ InternalFactHandle factHandle) throws IOException {
+ ObjectInputStream stream = context.stream;
+
+ RightTupleSink sink = (RightTupleSink) context.sinks.get( stream.readInt() );
+
+ BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
+
+ RightTuple rightTuple = new RightTuple( factHandle,
+ sink );
+ context.rightTuples.put( new RightTupleKey( factHandle.getId(),
+ sink ),
+ rightTuple );
+
+ memory.getRightTupleMemory().add( rightTuple );
+ }
+
+ public static void readLeftTuples(WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+
+ while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
+ LeftTupleSink sink = (LeftTupleSink) context.sinks.get( stream.readInt() );
+ int factHandleId = stream.readInt();
+ LeftTuple leftTuple = new LeftTuple( context.handles.get( factHandleId ),
+ sink,
+ true );
+ readLeftTuple( leftTuple,
+ context );
+ }
+ }
+
+ public static void readLeftTuple(LeftTuple parentLeftTuple,
+ WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+ InternalRuleBase ruleBase = context.ruleBase;
+ InternalWorkingMemory wm = context.wm;
+ Map<Integer, BaseNode> sinks = context.sinks;
+
+ LeftTupleSink sink = parentLeftTuple.getLeftTupleSink();
+
+ if ( sink instanceof JoinNode ) {
+ BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
+ memory.getLeftTupleMemory().add( parentLeftTuple );
+
+ while ( stream.readInt() == PersisterEnums.RIGHT_TUPLE ) {
+ LeftTupleSink childSink = (LeftTupleSink) sinks.get( stream.readInt() );
+ int factHandleId = stream.readInt();
+ RightTupleKey key = new RightTupleKey( factHandleId,
+ sink );
+ RightTuple rightTuple = context.rightTuples.get( key );
+ LeftTuple childLeftTuple = new LeftTuple( parentLeftTuple,
+ rightTuple,
+ childSink,
+ true );
+ readLeftTuple( childLeftTuple,
+ context );
+ }
+
+ } else if ( sink instanceof EvalConditionNode ) {
+ final EvalMemory memory = (EvalMemory) context.wm.getNodeMemory( (EvalConditionNode) sink );
+ memory.tupleMemory.add( parentLeftTuple );
+ while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
+ LeftTupleSink childSink = (LeftTupleSink) sinks.get( stream.readInt() );
+ LeftTuple childLeftTuple = new LeftTuple( parentLeftTuple,
+ childSink,
+ true );
+ readLeftTuple( childLeftTuple,
+ context );
+ }
+ } else if ( sink instanceof NotNode ) {
+ BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
+ int type = stream.readInt();
+ if ( type == PersisterEnums.LEFT_TUPLE_NOT_BLOCKED ) {
+ memory.getLeftTupleMemory().add( parentLeftTuple );
+
+ while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
+ LeftTupleSink childSink = (LeftTupleSink) sinks.get( stream.readInt() );
+ LeftTuple childLeftTuple = new LeftTuple( parentLeftTuple,
+ childSink,
+ true );
+ readLeftTuple( childLeftTuple,
+ context );
+ }
+
+ } else {
+ int factHandleId = stream.readInt();
+ RightTupleKey key = new RightTupleKey( factHandleId,
+ sink );
+ RightTuple rightTuple = context.rightTuples.get( key );
+
+ parentLeftTuple.setBlocker( rightTuple );
+ rightTuple.setBlocked( parentLeftTuple );
+ }
+ } else if ( sink instanceof ExistsNode ) {
+ BetaMemory memory = (BetaMemory) context.wm.getNodeMemory( (BetaNode) sink );
+ int type = stream.readInt();
+ if ( type == PersisterEnums.LEFT_TUPLE_NOT_BLOCKED ) {
+ memory.getLeftTupleMemory().add( parentLeftTuple );
+ } else {
+ int factHandleId = stream.readInt();
+ RightTupleKey key = new RightTupleKey( factHandleId,
+ sink );
+ RightTuple rightTuple = context.rightTuples.get( key );
+
+ parentLeftTuple.setBlocker( rightTuple );
+ rightTuple.setBlocked( parentLeftTuple );
+
+ while ( stream.readInt() == PersisterEnums.LEFT_TUPLE ) {
+ LeftTupleSink childSink = (LeftTupleSink) sinks.get( stream.readInt() );
+ LeftTuple childLeftTuple = new LeftTuple( parentLeftTuple,
+ childSink,
+ true );
+ readLeftTuple( childLeftTuple,
+ context );
+ }
+ }
+ } else if ( sink instanceof RuleTerminalNode ) {
+ RuleTerminalNode ruleTerminalNode = (RuleTerminalNode) sink;
+ TerminalNodeMemory memory = (TerminalNodeMemory) wm.getNodeMemory( ruleTerminalNode );
+ memory.getTupleMemory().add( parentLeftTuple );
+
+ int pos = context.terminalTupleMap.size();
+ context.terminalTupleMap.put( pos,
+ parentLeftTuple );
+ }
+ }
+
+ public static void readActivations(WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+
+ while ( stream.readInt() == PersisterEnums.ACTIVATION ) {
+ readActivation( context );
+ }
+ }
+
+ public static Activation readActivation(WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+ InternalRuleBase ruleBase = context.ruleBase;
+ InternalWorkingMemory wm = context.wm;
+
+ long activationNumber = stream.readLong();
+
+ int pos = stream.readInt();
+ LeftTuple leftTuple = context.terminalTupleMap.get( pos );
+
+ int salience = stream.readInt();
+
+ //PropagationContext context,
+ String pkgName = stream.readUTF();
+ String ruleName = stream.readUTF();
+ Package pkg = ruleBase.getPackage( pkgName );
+ Rule rule = pkg.getRule( ruleName );
+
+ RuleTerminalNode ruleTerminalNode = (RuleTerminalNode) leftTuple.getLeftTupleSink();
+ GroupElement subRule = ruleTerminalNode.getSubRule();
+
+ PropagationContext pc = context.propagationContexts.get( stream.readLong() );
+
+ AgendaItem activation = new AgendaItem( activationNumber,
+ leftTuple,
+ salience,
+ pc,
+ rule,
+ subRule );
+
+ leftTuple.setActivation( activation );
+
+ if ( stream.readBoolean() ) {
+ String activationGroupName = stream.readUTF();
+ wm.getAgenda().getActivationGroup( activationGroupName ).addActivation( activation );
+ }
+
+ boolean activated = stream.readBoolean();
+ activation.setActivated( activated );
+
+ InternalAgendaGroup agendaGroup;
+ if ( rule.getAgendaGroup() == null || rule.getAgendaGroup().equals( "" ) || rule.getAgendaGroup().equals( AgendaGroup.MAIN ) ) {
+ // Is the Rule AgendaGroup undefined? If it is use MAIN,
+ // which is added to the Agenda by default
+ agendaGroup = (InternalAgendaGroup) wm.getAgenda().getAgendaGroup( AgendaGroup.MAIN );
+ } else {
+ // AgendaGroup is defined, so try and get the AgendaGroup
+ // from the Agenda
+ agendaGroup = (InternalAgendaGroup) wm.getAgenda().getAgendaGroup( rule.getAgendaGroup() );
+ }
+
+ activation.setAgendaGroup( agendaGroup );
+
+ if ( activated ) {
+ if ( rule.getRuleFlowGroup() == null ) {
+ agendaGroup.add( activation );
+ } else {
+ InternalRuleFlowGroup rfg = (InternalRuleFlowGroup) wm.getAgenda().getRuleFlowGroup( rule.getRuleFlowGroup() );
+ rfg.addActivation( activation );
+ }
+ }
+
+ TruthMaintenanceSystem tms = context.wm.getTruthMaintenanceSystem();
+ while ( stream.readInt() == PersisterEnums.LOGICAL_DEPENDENCY ) {
+ int factHandleId = stream.readInt();
+ InternalFactHandle handle = (InternalFactHandle) context.handles.get( factHandleId );
+ tms.addLogicalDependency( handle,
+ activation,
+ pc,
+ rule );
+ }
+
+ return activation;
+ }
+
+ public static void readPropagationContexts(WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+
+ while ( stream.readInt() == PersisterEnums.PROPAGATION_CONTEXT ) {
+ readPropagationContext( context );
+ }
+
+ }
+
+ public static void readPropagationContext(WMSerialisationInContext context) throws IOException {
+ ObjectInputStream stream = context.stream;
+ InternalRuleBase ruleBase = context.ruleBase;
+ InternalWorkingMemory wm = context.wm;
+
+ int type = stream.readInt();
+
+ Rule rule = null;
+ if ( stream.readBoolean() ) {
+ String pkgName = stream.readUTF();
+ String ruleName = stream.readUTF();
+ Package pkg = ruleBase.getPackage( pkgName );
+ rule = pkg.getRule( ruleName );
+ }
+
+ LeftTuple leftTuple = null;
+ if ( stream.readBoolean() ) {
+ int tuplePos = stream.readInt();
+ leftTuple = (LeftTuple) context.terminalTupleMap.get( tuplePos );
+ }
+
+ long propagationNumber = stream.readLong();
+
+ int factHandleId = stream.readInt();
+ InternalFactHandle factHandle = context.handles.get( factHandleId );
+
+ int activeActivations = stream.readInt();
+ int dormantActivations = stream.readInt();
+ String entryPointId = stream.readUTF();
+
+ EntryPoint entryPoint = context.entryPoints.get( entryPointId );
+ if ( entryPoint == null ) {
+ entryPoint = new EntryPoint( entryPointId );
+ context.entryPoints.put( entryPointId,
+ entryPoint );
+ }
+
+ PropagationContext pc = new PropagationContextImpl( propagationNumber,
+ type,
+ rule,
+ leftTuple,
+ factHandle,
+ activeActivations,
+ dormantActivations,
+ entryPoint );
+ context.propagationContexts.put( propagationNumber,
+ pc );
+ }
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Marshaller.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Marshaller.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Marshaller.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,72 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.drools.RuleBaseConfiguration;
+import org.drools.StatefulSession;
+import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.concurrent.ExecutorService;
+import org.drools.reteoo.ReteooStatefulSession;
+import org.drools.spi.GlobalResolver;
+
+public class Marshaller {
+ GlobalResolver globalResolver;
+ private RuleBaseConfiguration config;
+ PlaceholderResolverStrategyFactory factory;
+
+ public Marshaller() {
+ this( null );
+ }
+
+ public Marshaller(RuleBaseConfiguration config) {
+ this( config, null);
+ }
+
+ public Marshaller(RuleBaseConfiguration config,
+ PlaceholderResolverStrategyFactory factory) {
+ this.config = (config != null) ? config : new RuleBaseConfiguration();
+
+ if ( factory == null ) {
+ this.factory = new PlaceholderResolverStrategyFactory();
+ ClassPlaceholderResolverStrategyAcceptor acceptor = new ClassPlaceholderResolverStrategyAcceptor( "*.*" );
+ IdentityPlaceholderResolverStrategy strategy = new IdentityPlaceholderResolverStrategy( acceptor );
+ this.factory.addStrategy( strategy );
+ } else {
+ this.factory = factory;
+ }
+ }
+
+ public ReteooStatefulSession read(final InputStream stream,
+ final InternalRuleBase ruleBase,
+ final int id,
+ final ExecutorService executor) throws IOException,
+ ClassNotFoundException {
+ WMSerialisationInContext context = new WMSerialisationInContext( stream,
+ ruleBase,
+ RuleBaseNodes.getNodeMap( ruleBase ),
+ factory );
+
+ ReteooStatefulSession session = InputPersister.readSession( context,
+ id,
+ executor );
+ context.close();
+ return session;
+
+ }
+
+ public void write(final OutputStream stream,
+ final InternalRuleBase ruleBase,
+ final StatefulSession session) throws IOException {
+ WMSerialisationOutContext context = new WMSerialisationOutContext( stream,
+ ruleBase,
+ (InternalWorkingMemory) session,
+ RuleBaseNodes.getNodeMap( ruleBase ),
+ this.factory );
+ OutputPersister.writeSession( context );
+ context.close();
+ }
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/ObjectPlaceholder.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,8 +0,0 @@
-package org.drools.persister;
-
-public interface ObjectPlaceholder {
-
- public Object resolveObject();
-
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/ObjectPlaceholder.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/ObjectPlaceholder.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,8 @@
+package org.drools.marshalling;
+
+public interface ObjectPlaceholder {
+
+ public Object resolveObject();
+
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/OutputPersister.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,268 +0,0 @@
-package org.drools.persister;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.util.HashMap;
-import java.util.IdentityHashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import org.drools.base.ShadowProxy;
-import org.drools.common.AbstractFactHandleFactory;
-import org.drools.common.AgendaItem;
-import org.drools.common.BinaryHeapQueueAgendaGroup;
-import org.drools.common.InternalFactHandle;
-import org.drools.common.InternalRuleBase;
-import org.drools.common.InternalWorkingMemory;
-import org.drools.common.PropagationContextImpl;
-import org.drools.persister.Placeholders.PlaceholderEntry;
-import org.drools.reteoo.BetaMemory;
-import org.drools.reteoo.BetaNode;
-import org.drools.reteoo.JoinNode;
-import org.drools.reteoo.LeftInputAdapterNode;
-import org.drools.reteoo.LeftTuple;
-import org.drools.reteoo.LeftTupleSink;
-import org.drools.reteoo.NotNode;
-import org.drools.reteoo.ReteooFactHandleFactory;
-import org.drools.reteoo.RightTuple;
-import org.drools.reteoo.RightTupleSink;
-import org.drools.reteoo.RuleTerminalNode;
-import org.drools.rule.Rule;
-import org.drools.spi.Activation;
-import org.drools.spi.FactHandleFactory;
-import org.drools.spi.PropagationContext;
-
-public class OutputPersister {
- public WMSerialisationOutContext context;
-
- public OutputPersister(InternalRuleBase ruleBase,
- InternalWorkingMemory wm,
- ObjectOutputStream stream,
- PlaceholderResolverStrategyFactory resolverStrategyFactory) {
- context = new WMSerialisationOutContext( stream,
- ruleBase,
- wm,
- RuleBaseNodes.getNodeMap( ruleBase ),
- resolverStrategyFactory );
- }
-
- public void write() throws IOException {
- writeFactHandles( context );
- context.stream.close();
- }
-
- public static void writeFactHandles(WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
- InternalWorkingMemory wm = context.wm;
- PlaceholderResolverStrategyFactory resolverStrategyFactory = context.resolverStrategyFactory;
-
- ((AbstractFactHandleFactory) wm.getFactHandleFactory()).writeExternal( stream );
-
- stream.writeInt( wm.getObjectStore().size() );
-
- // Write out FactHandles
- for ( Iterator it = wm.getObjectStore().iterateFactHandles(); it.hasNext(); ) {
- //stream.writeInt( PersisterEnums.FACT_HANDLE );
- InternalFactHandle handle = (InternalFactHandle) it.next();
- stream.writeInt( handle.getId() );
- stream.writeLong( handle.getRecency() );
-
- PlaceholderResolverStrategy strategy = resolverStrategyFactory.get( handle.getObject() );
- //stream.writeInt( strategy.getId() );
-
- Object object = handle.getObject();
- if ( object instanceof ShadowProxy ) {
- object = ((ShadowProxy) object).getShadowedObject();
- }
- strategy.write( stream,
- object );
-
- writeRightTuples( handle,
- context );
- }
-
- writeLeftTuples( context );
-
- writePropagationContexts( context );
-
- writeActivations( context );
-
- stream.writeInt( PersisterEnums.END );
- }
-
- public static void writeRightTuples(InternalFactHandle handle,
- WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
- for ( RightTuple rightTuple = handle.getRightTuple(); rightTuple != null; rightTuple = (RightTuple) rightTuple.getHandleNext() ) {
- stream.writeInt( PersisterEnums.RIGHT_TUPLE );
- writeRightTuple( rightTuple,
- context );
- }
- stream.writeInt( PersisterEnums.END );
- }
-
- public static void writeRightTuple(RightTuple rightTuple,
- WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
- InternalWorkingMemory wm = context.wm;
- stream.writeInt( rightTuple.getRightTupleSink().getId() );
- }
-
- public static void writeLeftTuples(WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
- InternalWorkingMemory wm = context.wm;
-
- // Write out LeftTuples
- for ( Iterator it = wm.getObjectStore().iterateFactHandles(); it.hasNext(); ) {
- InternalFactHandle handle = (InternalFactHandle) it.next();
-
- for ( LeftTuple leftTuple = handle.getLeftTuple(); leftTuple != null; leftTuple = (LeftTuple) leftTuple.getLeftParentNext() ) {
- stream.writeInt( PersisterEnums.LEFT_TUPLE );
-
- stream.writeInt( leftTuple.getLeftTupleSink().getId() );
- stream.writeInt( leftTuple.getLastHandle().getId() );
-
- writeLeftTuple( leftTuple,
- context );
- }
- }
- stream.writeInt( PersisterEnums.END );
- }
-
- public static void writeLeftTuple(LeftTuple leftTuple,
- WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
-
- LeftTupleSink sink = leftTuple.getLeftTupleSink();
-
- if ( sink instanceof JoinNode ) {
- for ( LeftTuple childLeftTuple = leftTuple.getBetaChildren(); leftTuple != null; childLeftTuple = (LeftTuple) leftTuple.getLeftParentNext() ) {
- stream.writeInt( PersisterEnums.RIGHT_TUPLE );
- stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
- stream.writeInt( childLeftTuple.getRightParent().getFactHandle().getId() );
- writeLeftTuple( childLeftTuple,
- context );
- }
- stream.writeInt( PersisterEnums.END );
- } else if ( sink instanceof NotNode ) {
- if ( leftTuple.getBlocker() == null ) {
- // is blocked so has children
- stream.writeInt( PersisterEnums.LEFT_TUPLE_NOT_BLOCKED );
-
- for ( LeftTuple childLeftTuple = leftTuple.getBetaChildren(); leftTuple != null; childLeftTuple = (LeftTuple) leftTuple.getLeftParentNext() ) {
- stream.writeInt( PersisterEnums.LEFT_TUPLE );
- stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
- writeLeftTuple( childLeftTuple,
- context );
- }
- stream.writeInt( PersisterEnums.END );
-
- } else {
- stream.writeInt( PersisterEnums.LEFT_TUPLE_BLOCKED );
- stream.writeInt( leftTuple.getBlocker().getFactHandle().getId() );
- }
- } else if ( sink instanceof RuleTerminalNode ) {
- int pos = context.terminalTupleMap.size();
- context.terminalTupleMap.put( leftTuple,
- pos );
- }
-
- }
-
- public static void writeActivations(WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
-
- Map<LeftTuple, Integer> tuples = context.terminalTupleMap;
- if ( !tuples.isEmpty() ) {
- for ( LeftTuple leftTuple : tuples.keySet() ) {
- stream.writeInt( PersisterEnums.ACTIVATION );
- writeActivation( context,
- leftTuple,
- (AgendaItem) leftTuple.getActivation(),
- (RuleTerminalNode) leftTuple.getLeftTupleSink() );
- }
- }
- stream.writeInt( PersisterEnums.END );
- }
-
- public static void writeActivation(WMSerialisationOutContext context,
- LeftTuple leftTuple,
- AgendaItem agendaItem,
- RuleTerminalNode ruleTerminalNode) throws IOException {
- ObjectOutputStream stream = context.stream;
-
- stream.writeLong( agendaItem.getActivationNumber() );
-
- stream.writeInt( context.terminalTupleMap.get( leftTuple ) );
-
- stream.writeInt( agendaItem.getSalience() );
-
- Rule rule = agendaItem.getRule();
- stream.writeUTF( rule.getPackage() );
- stream.writeUTF( rule.getName() );
-
- stream.writeLong( agendaItem.getPropagationContext().getPropagationNumber() );
-
- stream.writeBoolean( agendaItem.isActivated() );
- }
-
- public static void writePropagationContexts(WMSerialisationOutContext context) throws IOException {
- ObjectOutputStream stream = context.stream;
-
- Map<LeftTuple, Integer> tuples = context.terminalTupleMap;
- if ( !tuples.isEmpty() ) {
- Map<Long, PropagationContext> pcMap = new HashMap<Long, PropagationContext>();
-
- for ( LeftTuple leftTuple : tuples.keySet() ) {
- PropagationContext pc = leftTuple.getActivation().getPropagationContext();
- if ( !pcMap.containsKey( pc.getPropagationNumber() ) ) {
- stream.writeInt( PersisterEnums.PROPAGATION_CONTEXT );
- writePropagationContext( context,
- pc );
- pcMap.put( pc.getPropagationNumber(),
- pc );
- }
- }
- }
-
- stream.writeInt( PersisterEnums.END );
- }
-
- public static void writePropagationContext(WMSerialisationOutContext context,
- PropagationContext pc) throws IOException {
- ObjectOutputStream stream = context.stream;
- Map<LeftTuple, Integer> tuples = context.terminalTupleMap;
-
- stream.writeInt( pc.getType() );
-
- Rule ruleOrigin = pc.getRuleOrigin();
- if ( ruleOrigin != null ) {
- stream.writeBoolean( true );
- stream.writeUTF( ruleOrigin.getPackage() );
- stream.writeUTF( ruleOrigin.getName() );
- } else {
- stream.writeBoolean( false );
- }
-
- LeftTuple tupleOrigin = pc.getLeftTupleOrigin();
- if ( tupleOrigin != null ) {
- stream.writeBoolean( true );
- stream.writeInt( tuples.get( tupleOrigin ) );
- } else {
- stream.writeBoolean( false );
- }
-
- stream.writeLong( pc.getPropagationNumber() );
- stream.writeInt( pc.getFactHandleOrigin().getId() );
-
- stream.writeInt( pc.getActiveActivations() );
- stream.writeInt( pc.getDormantActivations() );
-
- stream.writeUTF( pc.getEntryPoint().getEntryPointId() );
- }
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/OutputPersister.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/OutputPersister.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,587 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.drools.InitialFact;
+import org.drools.base.ClassObjectType;
+import org.drools.base.ShadowProxy;
+import org.drools.common.AbstractFactHandleFactory;
+import org.drools.common.AgendaItem;
+import org.drools.common.BinaryHeapQueueAgendaGroup;
+import org.drools.common.DefaultAgenda;
+import org.drools.common.EqualityKey;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.common.LogicalDependency;
+import org.drools.common.ObjectStore;
+import org.drools.common.PropagationContextImpl;
+import org.drools.common.RuleFlowGroupImpl;
+import org.drools.common.WorkingMemoryAction;
+import org.drools.marshalling.Placeholders.PlaceholderEntry;
+import org.drools.reteoo.BetaMemory;
+import org.drools.reteoo.BetaNode;
+import org.drools.reteoo.EvalConditionNode;
+import org.drools.reteoo.ExistsNode;
+import org.drools.reteoo.JoinNode;
+import org.drools.reteoo.LeftInputAdapterNode;
+import org.drools.reteoo.LeftTuple;
+import org.drools.reteoo.LeftTupleSink;
+import org.drools.reteoo.NotNode;
+import org.drools.reteoo.ObjectTypeNode;
+import org.drools.reteoo.ReteooFactHandleFactory;
+import org.drools.reteoo.ReteooWorkingMemory;
+import org.drools.reteoo.RightTuple;
+import org.drools.reteoo.RightTupleSink;
+import org.drools.reteoo.RuleTerminalNode;
+import org.drools.reteoo.EvalConditionNode.EvalMemory;
+import org.drools.rule.EntryPoint;
+import org.drools.rule.Rule;
+import org.drools.spi.Activation;
+import org.drools.spi.ActivationGroup;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.FactHandleFactory;
+import org.drools.spi.PropagationContext;
+import org.drools.spi.RuleFlowGroup;
+import org.drools.util.ObjectHashMap;
+import org.drools.util.ObjectHashSet;
+import org.drools.util.ObjectHashSet.ObjectEntry;
+
+public class OutputPersister {
+ public static void writeSession(WMSerialisationOutContext context) throws IOException {
+ ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm;
+
+ context.writeInt( wm.getFactHandleFactory().getId() );
+ context.writeLong( wm.getFactHandleFactory().getRecency() );
+ System.out.println( "FactHandleFactory int:" + wm.getFactHandleFactory().getId() + " long:" + wm.getFactHandleFactory().getRecency() );
+
+ InternalFactHandle handle = context.wm.getInitialFactHandle();
+ context.writeInt( handle.getId() );
+ context.writeLong( handle.getRecency() );
+ context.out.println( "InitialFact int:" + handle.getId() + " long:" + handle.getRecency() );
+
+ context.writeLong( wm.getPropagationIdCounter() );
+ System.out.println( "PropagationCounter long:" + wm.getPropagationIdCounter() );
+
+ writeAgenda( context );
+
+ writeFactHandles( context );
+
+ writeActionQueue( context );
+
+ if ( wm.getTruthMaintenanceSystem() != null ) {
+ context.writeBoolean( true );
+ writeTruthMaintenanceSystem( context );
+ } else {
+ context.writeBoolean( false );
+ }
+ }
+
+ public static void writeAgenda(WMSerialisationOutContext context) throws IOException {
+ InternalWorkingMemory wm = context.wm;
+ DefaultAgenda agenda = (DefaultAgenda) wm.getAgenda();
+
+ Map<String, ActivationGroup> activationGroups = agenda.getActivationGroupsMap();
+
+ AgendaGroup[] agendaGroups = (AgendaGroup[]) agenda.getAgendaGroupsMap().values().toArray( new AgendaGroup[agenda.getAgendaGroupsMap().size()] );
+ Arrays.sort( agendaGroups,
+ AgendaGroupSorter.instance );
+
+ for ( AgendaGroup group : agendaGroups ) {
+ context.writeInt( PersisterEnums.AGENDA_GROUP );
+ context.writeUTF( group.getName() );
+ context.writeBoolean( group.isActive() );
+ }
+ context.writeInt( PersisterEnums.END );
+
+ LinkedList<AgendaGroup> focusStack = agenda.getStackList();
+ for ( Iterator<AgendaGroup> it = focusStack.iterator(); it.hasNext(); ) {
+ AgendaGroup group = it.next();
+ context.writeInt( PersisterEnums.AGENDA_GROUP );
+ context.writeUTF( group.getName() );
+ }
+ context.writeInt( PersisterEnums.END );
+
+ RuleFlowGroupImpl[] ruleFlowGroups = (RuleFlowGroupImpl[]) agenda.getRuleFlowGroupsMap().values().toArray( new RuleFlowGroupImpl[agenda.getRuleFlowGroupsMap().size()] );
+ Arrays.sort( ruleFlowGroups,
+ RuleFlowGroupSorter.instance );
+
+ for ( RuleFlowGroupImpl group : ruleFlowGroups ) {
+ context.writeInt( PersisterEnums.RULE_FLOW_GROUP );
+ //group.write( context );
+ context.writeUTF( group.getName() );
+ context.writeBoolean( group.isActive() );
+ context.writeBoolean( group.isAutoDeactivate() );
+ }
+ context.writeInt( PersisterEnums.END );
+ }
+
+ public static class AgendaGroupSorter
+ implements
+ Comparator<AgendaGroup> {
+ public static final AgendaGroupSorter instance = new AgendaGroupSorter();
+
+ public int compare(AgendaGroup group1,
+ AgendaGroup group2) {
+ return group1.getName().compareTo( group2.getName() );
+ }
+ }
+
+ public static class RuleFlowGroupSorter
+ implements
+ Comparator<RuleFlowGroup> {
+ public static final RuleFlowGroupSorter instance = new RuleFlowGroupSorter();
+
+ public int compare(RuleFlowGroup group1,
+ RuleFlowGroup group2) {
+ return group1.getName().compareTo( group2.getName() );
+ }
+ }
+
+ public static void writeActionQueue(WMSerialisationOutContext context) throws IOException {
+ ReteooWorkingMemory wm = (ReteooWorkingMemory) context.wm;
+
+ WorkingMemoryAction[] queue = wm.getActionQueue().toArray( new WorkingMemoryAction[wm.getActionQueue().size()] );
+ for ( int i = queue.length - 1; i >= 0; i-- ) {
+ context.writeInt( PersisterEnums.WORKING_MEMORY_ACTION );
+ queue[i].write( context );
+ }
+ context.writeInt( PersisterEnums.END );
+ }
+
+ public static void writeTruthMaintenanceSystem(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+
+ ObjectHashMap assertMap = context.wm.getTruthMaintenanceSystem().getAssertMap();
+
+ EqualityKey[] keys = new EqualityKey[assertMap.size()];
+ org.drools.util.Iterator it = assertMap.iterator();
+ int i = 0;
+ for ( org.drools.util.ObjectHashMap.ObjectEntry entry = (org.drools.util.ObjectHashMap.ObjectEntry) it.next(); entry != null; entry = (org.drools.util.ObjectHashMap.ObjectEntry) it.next() ) {
+ EqualityKey key = (EqualityKey) entry.getKey();
+ keys[i++] = key;
+ }
+
+ Arrays.sort( keys,
+ EqualityKeySorter.instance );
+
+ // write the assert map of Equality keys
+ for ( EqualityKey key : keys ) {
+ stream.writeInt( PersisterEnums.EQUALITY_KEY );
+ stream.writeInt( key.getStatus() );
+ InternalFactHandle handle = key.getFactHandle();
+ stream.writeInt( handle.getId() );
+ context.out.println( "EqualityKey int:" + key.getStatus() + " int:" + handle.getId() );
+ if ( key.getOtherFactHandle() != null && !key.getOtherFactHandle().isEmpty() ) {
+ for ( InternalFactHandle handle2 : key.getOtherFactHandle() ) {
+ stream.writeInt( PersisterEnums.FACT_HANDLE );
+ stream.writeInt( handle2.getId() );
+ context.out.println( "OtherHandle int:" + handle2.getId() );
+ }
+ }
+ stream.writeInt( PersisterEnums.END );
+ }
+ stream.writeInt( PersisterEnums.END );
+ }
+
+ public static class EqualityKeySorter
+ implements
+ Comparator<EqualityKey> {
+ public static final EqualityKeySorter instance = new EqualityKeySorter();
+
+ public int compare(EqualityKey key1,
+ EqualityKey key2) {
+ return key1.getFactHandle().getId() - key2.getFactHandle().getId();
+ }
+ }
+
+ public static void writeFactHandles(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ InternalWorkingMemory wm = context.wm;
+ PlaceholderResolverStrategyFactory resolverStrategyFactory = context.resolverStrategyFactory;
+
+ writeInitialFactHandleRightTuples( context );
+
+ stream.writeInt( wm.getObjectStore().size() );
+
+ // Write out FactHandles
+ for ( InternalFactHandle handle : orderFacts( wm.getObjectStore() ) ) {
+ //stream.writeInt( PersisterEnums.FACT_HANDLE );
+ //InternalFactHandle handle = (InternalFactHandle) it.next();
+ stream.writeInt( handle.getId() );
+ stream.writeLong( handle.getRecency() );
+
+ context.out.println( "Object : int:" + handle.getId() + " long:" + handle.getRecency() );
+ context.out.println( handle.getObject() );
+
+ Object object = handle.getObject();
+ if ( object instanceof ShadowProxy ) {
+ object = ((ShadowProxy) object).getShadowedObject();
+ }
+
+ PlaceholderResolverStrategy strategy = resolverStrategyFactory.getStrategy( handle.getObject() );
+
+ stream.writeInt( strategy.getIndex() );
+
+ strategy.write( stream,
+ object );
+
+ writeRightTuples( handle,
+ context );
+ }
+
+ writeInitialFactHandleLeftTuples( context );
+
+ writeLeftTuples( context );
+
+ writePropagationContexts( context );
+
+ writeActivations( context );
+ }
+
+ public static InternalFactHandle[] orderFacts(ObjectStore objectStore) {
+ // this method is just needed for testing purposes, to allow round tripping
+ int size = objectStore.size();
+ InternalFactHandle[] handles = new InternalFactHandle[size];
+ int i = 0;
+ for ( Iterator it = objectStore.iterateFactHandles(); it.hasNext(); ) {
+ handles[i++] = (InternalFactHandle) it.next();
+ }
+
+ Arrays.sort( handles,
+ new HandleSorter() );
+
+ return handles;
+ }
+
+ public static class HandleSorter
+ implements
+ Comparator<InternalFactHandle> {
+ public int compare(InternalFactHandle h1,
+ InternalFactHandle h2) {
+ return h1.getId() - h2.getId();
+ }
+ }
+
+ public static void writeInitialFactHandleRightTuples(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ InternalRuleBase ruleBase = context.ruleBase;
+
+ ObjectTypeNode initialFactNode = ruleBase.getRete().getEntryPointNode( EntryPoint.DEFAULT ).getObjectTypeNodes().get( new ClassObjectType( InitialFact.class ) );
+
+ // do we write the fact to the objecttypenode memory
+ if ( initialFactNode != null ) {
+ ObjectHashSet initialFactMemory = (ObjectHashSet) context.wm.getNodeMemory( initialFactNode );
+ if ( initialFactMemory != null && !initialFactMemory.isEmpty() ) {
+ context.out.println( "InitialFactMemory true int:" + initialFactNode.getId() );
+ stream.writeBoolean( true );
+ stream.writeInt( initialFactNode.getId() );
+
+ context.out.println( "InitialFact RightTuples" );
+ writeRightTuples( context.wm.getInitialFactHandle(),
+ context );
+ } else {
+ context.out.println( "InitialFactMemory false " );
+ stream.writeBoolean( false );
+ }
+ } else {
+ context.out.println( "InitialFactMemory false " );
+ stream.writeBoolean( false );
+ }
+ }
+
+ public static void writeInitialFactHandleLeftTuples(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+
+ context.out.println( "InitialFact LeftTuples Start" );
+ InternalFactHandle handle = context.wm.getInitialFactHandle();
+ for ( LeftTuple leftTuple = getLeftTuple( handle.getLeftTuple() ); leftTuple != null; leftTuple = (LeftTuple) leftTuple.getLeftParentPrevious() ) {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE );
+
+ stream.writeInt( leftTuple.getLeftTupleSink().getId() );
+ context.out.println( "LeftTuple int:" + leftTuple.getLeftTupleSink().getId() );
+ writeLeftTuple( leftTuple,
+ context );
+ }
+ stream.writeInt( PersisterEnums.END );
+ context.out.println( "InitialFact LeftTuples End" );
+ }
+
+ public static void writeRightTuples(InternalFactHandle handle,
+ WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ context.out.println( "RightTuples Start" );
+
+ RightTuple rightTuple = handle.getRightTuple();
+ for ( RightTuple tempRightTuple = rightTuple; tempRightTuple != null; tempRightTuple = (RightTuple) tempRightTuple.getHandleNext() ) {
+ rightTuple = tempRightTuple;
+ }
+ for ( ; rightTuple != null; rightTuple = (RightTuple) rightTuple.getHandlePrevious() ) {
+ stream.writeInt( PersisterEnums.RIGHT_TUPLE );
+ writeRightTuple( rightTuple,
+ context );
+ }
+ stream.writeInt( PersisterEnums.END );
+ context.out.println( "RightTuples END" );
+ }
+
+ public static void writeRightTuple(RightTuple rightTuple,
+ WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ InternalWorkingMemory wm = context.wm;
+ stream.writeInt( rightTuple.getRightTupleSink().getId() );
+ context.out.println( "RightTuple int:" + rightTuple.getRightTupleSink().getId() );
+ }
+
+ public static void writeLeftTuples(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ InternalWorkingMemory wm = context.wm;
+
+ // Write out LeftTuples
+ context.out.println( "LeftTuples Start" );
+ for ( InternalFactHandle handle : orderFacts( wm.getObjectStore() ) ) {
+ //InternalFactHandle handle = (InternalFactHandle) it.next();
+
+ for ( LeftTuple leftTuple = getLeftTuple( handle.getLeftTuple() ); leftTuple != null; leftTuple = (LeftTuple) leftTuple.getLeftParentPrevious() ) {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE );
+
+ stream.writeInt( leftTuple.getLeftTupleSink().getId() );
+ stream.writeInt( handle.getId() );
+
+ context.out.println( "LeftTuple int:" + leftTuple.getLeftTupleSink().getId() + " int:" + handle.getId() );
+ writeLeftTuple( leftTuple,
+ context );
+ }
+ }
+ stream.writeInt( PersisterEnums.END );
+ context.out.println( "LeftTuples End" );
+ }
+
+ public static void writeLeftTuple(LeftTuple leftTuple,
+ WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ InternalRuleBase ruleBase = context.ruleBase;
+ InternalWorkingMemory wm = context.wm;
+
+ LeftTupleSink sink = leftTuple.getLeftTupleSink();
+
+ if ( sink instanceof JoinNode ) {
+ context.out.println( "JoinNode" );
+
+ for ( LeftTuple childLeftTuple = getLeftTuple( leftTuple.getBetaChildren() ); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentPrevious() ) {
+ stream.writeInt( PersisterEnums.RIGHT_TUPLE );
+ stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
+ stream.writeInt( childLeftTuple.getRightParent().getFactHandle().getId() );
+ context.out.println( "RightTuple int:" + childLeftTuple.getLeftTupleSink().getId() + " int:" + childLeftTuple.getRightParent().getFactHandle().getId() );
+ writeLeftTuple( childLeftTuple,
+ context );
+ }
+ stream.writeInt( PersisterEnums.END );
+ } else if ( sink instanceof EvalConditionNode ) {
+ context.out.println( "EvalConditionNode" );
+ for ( LeftTuple childLeftTuple = getLeftTuple( leftTuple.getBetaChildren() ); childLeftTuple != null; childLeftTuple = (LeftTuple) childLeftTuple.getLeftParentPrevious() ) {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE );
+ stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
+ writeLeftTuple( childLeftTuple,
+ context );
+ }
+ stream.writeInt( PersisterEnums.END );
+ } else if ( sink instanceof NotNode ) {
+ if ( leftTuple.getBlocker() == null ) {
+ // is not blocked so has children
+ stream.writeInt( PersisterEnums.LEFT_TUPLE_NOT_BLOCKED );
+
+ for ( LeftTuple childLeftTuple = getLeftTuple( leftTuple.getBetaChildren() ); childLeftTuple != null; childLeftTuple = (LeftTuple) leftTuple.getLeftParentPrevious() ) {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE );
+ stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
+ writeLeftTuple( childLeftTuple,
+ context );
+ }
+ stream.writeInt( PersisterEnums.END );
+
+ } else {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE_BLOCKED );
+ stream.writeInt( leftTuple.getBlocker().getFactHandle().getId() );
+ }
+ } else if ( sink instanceof ExistsNode ) {
+ if ( leftTuple.getBlocker() == null ) {
+ // is blocked so has children
+ stream.writeInt( PersisterEnums.LEFT_TUPLE_NOT_BLOCKED );
+ } else {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE_BLOCKED );
+ stream.writeInt( leftTuple.getBlocker().getFactHandle().getId() );
+
+ for ( LeftTuple childLeftTuple = getLeftTuple( leftTuple.getBetaChildren() ); childLeftTuple != null; childLeftTuple = (LeftTuple) leftTuple.getLeftParentPrevious() ) {
+ stream.writeInt( PersisterEnums.LEFT_TUPLE );
+ stream.writeInt( childLeftTuple.getLeftTupleSink().getId() );
+ writeLeftTuple( childLeftTuple,
+ context );
+ }
+ stream.writeInt( PersisterEnums.END );
+ }
+ } else if ( sink instanceof RuleTerminalNode ) {
+ context.out.println( "RuleTerminalNode" );
+ int pos = context.terminalTupleMap.size();
+ context.terminalTupleMap.put( leftTuple,
+ pos );
+ }
+ }
+
+ public static LeftTuple getLeftTuple(LeftTuple leftTuple) {
+ for ( LeftTuple tempLeftTuple = leftTuple; tempLeftTuple != null; tempLeftTuple = (LeftTuple) tempLeftTuple.getLeftParentNext() ) {
+ leftTuple = tempLeftTuple;
+ }
+ return leftTuple;
+ }
+
+ public static void writeActivations(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+
+ Entry<LeftTuple, Integer>[] entries = context.terminalTupleMap.entrySet().toArray( new Entry[ context.terminalTupleMap.size() ] );
+ Arrays.sort( entries, TupleSorter.instance );
+
+ //Map<LeftTuple, Integer> tuples = context.terminalTupleMap;
+ if ( entries.length != 0 ) {
+ for ( Entry<LeftTuple, Integer> entry : entries ) {
+ LeftTuple leftTuple = entry.getKey();
+ stream.writeInt( PersisterEnums.ACTIVATION );
+ writeActivation( context,
+ leftTuple,
+ (AgendaItem) leftTuple.getActivation(),
+ (RuleTerminalNode) leftTuple.getLeftTupleSink() );
+ }
+ }
+ stream.writeInt( PersisterEnums.END );
+ }
+
+ public static class TupleSorter
+ implements
+ Comparator<Entry<LeftTuple, Integer>> {
+ public static final TupleSorter instance = new TupleSorter();
+ public int compare(Entry<LeftTuple, Integer> e1,
+ Entry<LeftTuple, Integer> e2) {
+ return e1.getValue() - e2.getValue();
+ }
+ }
+
+ public static void writeActivation(WMSerialisationOutContext context,
+ LeftTuple leftTuple,
+ AgendaItem agendaItem,
+ RuleTerminalNode ruleTerminalNode) throws IOException {
+ ObjectOutputStream stream = context.stream;
+
+ stream.writeLong( agendaItem.getActivationNumber() );
+
+ stream.writeInt( context.terminalTupleMap.get( leftTuple ) );
+
+ stream.writeInt( agendaItem.getSalience() );
+
+ Rule rule = agendaItem.getRule();
+ stream.writeUTF( rule.getPackage() );
+ stream.writeUTF( rule.getName() );
+
+ context.out.println( "Rule " + rule.getPackage() + "." + rule.getName() );
+
+ context.out.println( "AgendaItem long:" + agendaItem.getPropagationContext().getPropagationNumber() );
+ stream.writeLong( agendaItem.getPropagationContext().getPropagationNumber() );
+
+ if ( agendaItem.getActivationGroupNode() != null ) {
+ stream.writeBoolean( true );
+ context.out.println( "ActivationGroup bool:" + true );
+ stream.writeUTF( agendaItem.getActivationGroupNode().getActivationGroup().getName() );
+ context.out.println( "ActivationGroup string:" + agendaItem.getActivationGroupNode().getActivationGroup().getName() );
+ } else {
+ stream.writeBoolean( false );
+ context.out.println( "ActivationGroup bool:" + false );
+ }
+
+ stream.writeBoolean( agendaItem.isActivated() );
+ context.out.println( "AgendaItem bool:" + agendaItem.isActivated() );
+
+ org.drools.util.LinkedList list = agendaItem.getLogicalDependencies();
+ if ( list != null && !list.isEmpty() ) {
+ for ( LogicalDependency node = (LogicalDependency) list.getFirst(); node != null; node = (LogicalDependency) node.getNext() ) {
+ stream.writeInt( PersisterEnums.LOGICAL_DEPENDENCY );
+ stream.writeInt( ((InternalFactHandle) node.getFactHandle()).getId() );
+ context.out.println( "Logical Depenency : int " + ((InternalFactHandle) node.getFactHandle()).getId() );
+ }
+ }
+ stream.writeInt( PersisterEnums.END );
+ }
+
+ public static void writePropagationContexts(WMSerialisationOutContext context) throws IOException {
+ ObjectOutputStream stream = context.stream;
+
+ Entry<LeftTuple, Integer>[] entries = context.terminalTupleMap.entrySet().toArray( new Entry[ context.terminalTupleMap.size() ] );
+ Arrays.sort( entries, TupleSorter.instance );
+
+ //Map<LeftTuple, Integer> tuples = context.terminalTupleMap;
+ if ( entries.length != 0 ) {
+ Map<Long, PropagationContext> pcMap = new HashMap<Long, PropagationContext>();
+ for ( Entry<LeftTuple, Integer> entry : entries ) {
+ LeftTuple leftTuple = entry.getKey();
+ PropagationContext pc = leftTuple.getActivation().getPropagationContext();
+ if ( !pcMap.containsKey( pc.getPropagationNumber() ) ) {
+ stream.writeInt( PersisterEnums.PROPAGATION_CONTEXT );
+ writePropagationContext( context,
+ pc );
+ pcMap.put( pc.getPropagationNumber(),
+ pc );
+ }
+ }
+ }
+
+ stream.writeInt( PersisterEnums.END );
+ }
+
+ public static void writePropagationContext(WMSerialisationOutContext context,
+ PropagationContext pc) throws IOException {
+ ObjectOutputStream stream = context.stream;
+ Map<LeftTuple, Integer> tuples = context.terminalTupleMap;
+
+ stream.writeInt( pc.getType() );
+
+ Rule ruleOrigin = pc.getRuleOrigin();
+ if ( ruleOrigin != null ) {
+ stream.writeBoolean( true );
+ stream.writeUTF( ruleOrigin.getPackage() );
+ stream.writeUTF( ruleOrigin.getName() );
+ } else {
+ stream.writeBoolean( false );
+ }
+
+ LeftTuple tupleOrigin = pc.getLeftTupleOrigin();
+ if ( tupleOrigin != null ) {
+ stream.writeBoolean( true );
+ stream.writeInt( tuples.get( tupleOrigin ) );
+ } else {
+ stream.writeBoolean( false );
+ }
+
+ stream.writeLong( pc.getPropagationNumber() );
+ if ( pc.getFactHandleOrigin() != null ) {
+ stream.writeInt( pc.getFactHandleOrigin().getId() );
+ } else {
+ stream.writeInt( -1 );
+ }
+
+ stream.writeInt( pc.getActiveActivations() );
+ stream.writeInt( pc.getDormantActivations() );
+
+ stream.writeUTF( pc.getEntryPoint().getEntryPointId() );
+ }
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PersisterEnums.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,15 +0,0 @@
-package org.drools.persister;
-
-public class PersisterEnums {
- public static final int REPEAT = 0;
- public static final int END = 1;
- public static final int FACT_HANDLE = 2;
- public static final int LEFT_TUPLE = 3;
- public static final int RIGHT_TUPLE = 4;
-
- public static final int LEFT_TUPLE_BLOCKED = 5;
- public static final int LEFT_TUPLE_NOT_BLOCKED = 6;
-
- public static final int ACTIVATION = 7;
- public static final int PROPAGATION_CONTEXT = 8;
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PersisterEnums.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterEnums.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,28 @@
+package org.drools.marshalling;
+
+public class PersisterEnums {
+ public static final int REPEAT = 0;
+ public static final int END = 1;
+ public static final int FACT_HANDLE = 2;
+ public static final int LEFT_TUPLE = 3;
+ public static final int RIGHT_TUPLE = 4;
+
+ public static final int INITIAL_FACT_NODE = 5;
+
+ public static final int LEFT_TUPLE_BLOCKED = 6;
+ public static final int LEFT_TUPLE_NOT_BLOCKED = 7;
+
+ public static final int ACTIVATION = 8;
+ public static final int PROPAGATION_CONTEXT = 9;
+
+ public static final int WORKING_MEMORY_ACTION = 10;
+
+ public static final int EQUALITY_KEY = 11;
+ public static final int LOGICAL_DEPENDENCY = 12;
+
+ public static final int AGENDA_GROUP = 13;
+ public static final int ACTIVATION_GROUP = 14;
+
+ public static final int RULE_FLOW_GROUP = 15;
+ public static final int RULE_FLOW_NODE = 16;
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterHelper.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterHelper.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,37 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+
+import org.drools.common.WorkingMemoryAction;
+import org.drools.common.RuleFlowGroupImpl.DeactivateCallback;
+import org.drools.common.TruthMaintenanceSystem.LogicalRetractCallback;
+import org.drools.reteoo.PropagationQueuingNode.PropagateAction;
+import org.drools.reteoo.ReteooWorkingMemory.WorkingMemoryReteAssertAction;
+import org.drools.reteoo.PropagationQueuingNode.PropagateAction;
+import org.drools.common.RuleFlowGroupImpl.DeactivateCallback;
+import org.drools.common.TruthMaintenanceSystem;
+
+public class PersisterHelper {
+ public static WorkingMemoryAction readWorkingMemoryAction(WMSerialisationInContext context) throws IOException {
+ int type = context.readInt();
+ switch(type) {
+ case WorkingMemoryAction.WorkingMemoryReteAssertAction : {
+ return new WorkingMemoryReteAssertAction(context);
+ }
+ case WorkingMemoryAction.DeactivateCallback : {
+ return new DeactivateCallback(context);
+ }
+ case WorkingMemoryAction.PropagateAction : {
+ return new PropagateAction(context);
+ }
+ case WorkingMemoryAction.LogicalRetractCallback : {
+ return new LogicalRetractCallback(context);
+ }
+ }
+ return null;
+ }
+
+ public void write(WMSerialisationOutContext context) throws IOException {
+
+ }
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PersisterKey.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,7 +0,0 @@
-package org.drools.persister;
-
-public interface PersisterKey {
- public boolean equal(Object object1, Object object2);
-
- public int hashCode(Object object);
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PersisterKey.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PersisterKey.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,7 @@
+package org.drools.marshalling;
+
+public interface PersisterKey {
+ public boolean equal(Object object1, Object object2);
+
+ public int hashCode(Object object);
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PlaceholderResolverStrategy.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,19 +0,0 @@
-package org.drools.persister;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
-
-public interface PlaceholderResolverStrategy {
-
- public void setId(int id);
-
- public int getId();
-
- public void write(ObjectOutputStream os,
- Object object) throws IOException;
-
- public ObjectPlaceholder read(ObjectInputStream os) throws IOException, ClassNotFoundException;
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PlaceholderResolverStrategy.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategy.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,21 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+
+public interface PlaceholderResolverStrategy {
+
+ public void setIndex(int id);
+
+ public int getIndex();
+
+ public boolean accept(Object object);
+
+ public void write(ObjectOutputStream os,
+ Object object) throws IOException;
+
+ public ObjectPlaceholder read(ObjectInputStream os) throws IOException, ClassNotFoundException;
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyAcceptor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyAcceptor.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyAcceptor.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,5 @@
+package org.drools.marshalling;
+
+public interface PlaceholderResolverStrategyAcceptor {
+ public boolean accept(Object object);
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PlaceholderResolverStrategyFactory.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,28 +0,0 @@
-package org.drools.persister;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-public class PlaceholderResolverStrategyFactory {
- List<PlaceholderResolverStrategy> strategiesList;
- Map<Object, PlaceholderResolverStrategy> strategiesMap = new HashMap<Object, PlaceholderResolverStrategy>();
-
- public PlaceholderResolverStrategyFactory() {
- this.strategiesList = new ArrayList<PlaceholderResolverStrategy>();
- }
-
- public void addPlaceholderResolverStrategy(PlaceholderResolverStrategy strategy) {
- this.strategiesList.add( strategy );
- }
-
- public PlaceholderResolverStrategy get(int i) {
- return this.strategiesList.get( i );
- }
-
- public PlaceholderResolverStrategy get(Object object) {
- return this.strategiesList.get( 0 );
- //return this.strategiesMap.get( object );
- }
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/PlaceholderResolverStrategyFactory.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/PlaceholderResolverStrategyFactory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,32 @@
+package org.drools.marshalling;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class PlaceholderResolverStrategyFactory {
+ List<PlaceholderResolverStrategy> strategiesList;
+
+ public PlaceholderResolverStrategyFactory() {
+ this.strategiesList = new ArrayList<PlaceholderResolverStrategy>();
+ }
+
+ public void addStrategy(PlaceholderResolverStrategy strategy) {
+ strategy.setIndex( this.strategiesList.size() );
+ this.strategiesList.add( strategy );
+ }
+
+ public PlaceholderResolverStrategy getStrategy(int index) {
+ return this.strategiesList.get( index );
+ }
+
+ public PlaceholderResolverStrategy getStrategy(Object object) {
+ for ( PlaceholderResolverStrategy strategy: strategiesList ) {
+ if ( strategy.accept( object ) ) {
+ return strategy;
+ }
+ }
+ throw new RuntimeException( "Unable to info PlaceholderResolverStrategy for class : " + object.getClass() + " object : " + object );
+ }
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/Placeholders.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,143 +0,0 @@
-package org.drools.persister;
-
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.drools.util.AbstractHashTable;
-import org.drools.util.Entry;
-
-public class Placeholders extends AbstractHashTable {
- private List<Object> ids;
-
- public Placeholders() {
- super();
- this.ids = new ArrayList<Object>();
- }
-
- public PlaceholderEntry lookupPlaceholder(Object object, PersisterKey key) {
- final int hashCode = key.hashCode( object );
- final int index = indexOf( hashCode,
- this.table.length );
-
- PlaceholderEntry current = (PlaceholderEntry) this.table[index];
- while ( current != null ) {
- if ( hashCode == current.hashCode && current.key == key && key.equal( object,
- current.object ) ) {
- return current;
- }
- current = (PlaceholderEntry) current.getNext();
- }
- return null;
- }
-
- public PlaceholderEntry assignPlaceholder(Object object,
- PersisterKey key) {
- final int hashCode = key.hashCode( object );
- final int index = indexOf( hashCode,
- this.table.length );
-
- int id = ids.size();
- this.ids.add( object );
- PlaceholderEntry entry = new PlaceholderEntry( object,
- id,
- key );
-
- entry.next = this.table[index];
- this.table[index] = entry;
-
- if ( this.size++ >= this.threshold ) {
- resize( 2 * this.table.length );
- }
-
- return entry;
- }
-
- public Object lookupObject(int id) {
- return this.ids.get( id );
- }
-
- public static class PlaceholderEntry
- implements
- Entry {
-
- private static final long serialVersionUID = 400L;
-
- public Object object;
-
- public int id;
-
- public PersisterKey key;
-
- public int hashCode;
-
- public Entry next;
-
- // private LinkedList list;
-
- public PlaceholderEntry(final Object object,
- final int id,
- final PersisterKey key) {
- this.object = object;
- this.id = id;
- this.key = key;
- this.hashCode = key.hashCode( object );
- }
-
- // public PlaceholderEntry(final InternalFactHandle handle,
- // final int hashCode) {
- // this.handle = handle;
- // this.hashCode = hashCode;
- // // this.list = new LinkedList();
- // }
-
- public Entry getNext() {
- return this.next;
- }
-
- public void setNext(final Entry next) {
- this.next = next;
- }
-
- public int hashCode() {
- return this.hashCode;
- }
-
- public String toString() {
- return "Placeholder( object=" + this.object + " id = " + this.id + " hashcode=" + this.hashCode + " next=" + this.next + " )";
- }
-
- public Object getObject() {
- return object;
- }
-
- public int getId() {
- return id;
- }
-
- public PersisterKey getKey() {
- return key;
- }
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- // TODO Auto-generated method stub
-
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- // TODO Auto-generated method stub
-
- }
-
- }
-
- @Override
- public Entry getBucket(Object object) {
- // TODO Auto-generated method stub
- return null;
- }
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/Placeholders.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/Placeholders.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,132 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.util.AbstractHashTable;
+import org.drools.util.Entry;
+
+public class Placeholders extends AbstractHashTable {
+ private List<Object> ids;
+
+ public Placeholders() {
+ super();
+ this.ids = new ArrayList<Object>();
+ }
+
+ public PlaceholderEntry lookupPlaceholder(Object object, PersisterKey key) {
+ final int hashCode = key.hashCode( object );
+ final int index = indexOf( hashCode,
+ this.table.length );
+
+ PlaceholderEntry current = (PlaceholderEntry) this.table[index];
+ while ( current != null ) {
+ if ( hashCode == current.hashCode && current.key == key && key.equal( object,
+ current.object ) ) {
+ return current;
+ }
+ current = (PlaceholderEntry) current.getNext();
+ }
+ return null;
+ }
+
+ public PlaceholderEntry assignPlaceholder(Object object,
+ PersisterKey key) {
+ final int hashCode = key.hashCode( object );
+ final int index = indexOf( hashCode,
+ this.table.length );
+
+ int id = ids.size();
+ this.ids.add( object );
+ PlaceholderEntry entry = new PlaceholderEntry( object,
+ id,
+ key );
+
+ entry.next = this.table[index];
+ this.table[index] = entry;
+
+ if ( this.size++ >= this.threshold ) {
+ resize( 2 * this.table.length );
+ }
+
+ return entry;
+ }
+
+ public Object lookupObject(int id) {
+ return this.ids.get( id );
+ }
+
+ public static class PlaceholderEntry
+ implements
+ Entry {
+
+ private static final long serialVersionUID = 400L;
+
+ public Object object;
+
+ public int id;
+
+ public PersisterKey key;
+
+ public int hashCode;
+
+ public Entry next;
+
+ // private LinkedList list;
+
+ public PlaceholderEntry(final Object object,
+ final int id,
+ final PersisterKey key) {
+ this.object = object;
+ this.id = id;
+ this.key = key;
+ this.hashCode = key.hashCode( object );
+ }
+
+ // public PlaceholderEntry(final InternalFactHandle handle,
+ // final int hashCode) {
+ // this.handle = handle;
+ // this.hashCode = hashCode;
+ // // this.list = new LinkedList();
+ // }
+
+ public Entry getNext() {
+ return this.next;
+ }
+
+ public void setNext(final Entry next) {
+ this.next = next;
+ }
+
+ public int hashCode() {
+ return this.hashCode;
+ }
+
+ public String toString() {
+ return "Placeholder( object=" + this.object + " id = " + this.id + " hashcode=" + this.hashCode + " next=" + this.next + " )";
+ }
+
+ public Object getObject() {
+ return object;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public PersisterKey getKey() {
+ return key;
+ }
+
+ }
+
+ @Override
+ public Entry getBucket(Object object) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/RightTupleKey.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,48 +0,0 @@
-/**
- *
- */
-package org.drools.persister;
-
-import org.drools.reteoo.Sink;
-
-public class RightTupleKey {
- private final int id;
- private final Sink sink;
-
- public RightTupleKey(int id,
- Sink sink) {
- super();
- this.id = id;
- this.sink = sink;
- }
-
- public int getId() {
- return id;
- }
-
- public Sink getSink() {
- return sink;
- }
-
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + id;
- result = prime * result + sink.getId();
- return result;
- }
-
- public boolean equals(Object obj) {
- if ( this == obj ) return true;
- if ( obj == null ) return false;
- if ( getClass() != obj.getClass() ) return false;
-
- final RightTupleKey other = (RightTupleKey) obj;
- if ( id != other.id ) return false;
- if ( sink == null ) {
- if ( other.sink != null ) return false;
- } else if ( sink.getId() != other.sink.getId() ) return false;
- return true;
- }
-
-}
\ No newline at end of file
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/RightTupleKey.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RightTupleKey.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,48 @@
+/**
+ *
+ */
+package org.drools.marshalling;
+
+import org.drools.reteoo.Sink;
+
+public class RightTupleKey {
+ private final int id;
+ private final Sink sink;
+
+ public RightTupleKey(int id,
+ Sink sink) {
+ super();
+ this.id = id;
+ this.sink = sink;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public Sink getSink() {
+ return sink;
+ }
+
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + id;
+ result = prime * result + sink.getId();
+ return result;
+ }
+
+ public boolean equals(Object obj) {
+ if ( this == obj ) return true;
+ if ( obj == null ) return false;
+ if ( getClass() != obj.getClass() ) return false;
+
+ final RightTupleKey other = (RightTupleKey) obj;
+ if ( id != other.id ) return false;
+ if ( sink == null ) {
+ if ( other.sink != null ) return false;
+ } else if ( sink.getId() != other.sink.getId() ) return false;
+ return true;
+ }
+
+}
\ No newline at end of file
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/RuleBaseNodes.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,69 +0,0 @@
-package org.drools.persister;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.drools.common.BaseNode;
-import org.drools.common.InternalRuleBase;
-import org.drools.reteoo.BetaNode;
-import org.drools.reteoo.LeftTupleSink;
-import org.drools.reteoo.LeftTupleSource;
-import org.drools.reteoo.ObjectSink;
-import org.drools.reteoo.ObjectSource;
-import org.drools.reteoo.ObjectTypeNode;
-import org.drools.reteoo.RuleTerminalNode;
-
-public class RuleBaseNodes {
- public static Map<Integer, BaseNode> getNodeMap(InternalRuleBase ruleBase) {
- Map<Integer, BaseNode> nodes = new HashMap<Integer, BaseNode>();
- buildNodeMap( ruleBase, nodes );
- return nodes;
- }
-
- private static void buildNodeMap(InternalRuleBase ruleBase,
- Map<Integer, BaseNode> nodes) {
- for ( ObjectTypeNode sink : ruleBase.getRete().getObjectTypeNodes() ) {
- nodes.put( sink.getId(),
- sink );
- addObjectSink( ruleBase,
- sink,
- nodes );
- }
- }
-
- private static void addObjectSink(InternalRuleBase ruleBase,
- ObjectSink sink,
- Map<Integer, BaseNode> nodes) {
- // we don't need to store alpha nodes, as they have no state to serialise
- if ( sink instanceof LeftTupleSource ) {
- LeftTupleSource node = (LeftTupleSource) sink;
- for ( LeftTupleSink leftTupleSink : node.getSinkPropagator().getSinks() ) {
- addLeftTupleSink( ruleBase,
- leftTupleSink,
- nodes );
- }
- } else {
- ObjectSource node = ( ObjectSource ) sink;
- for ( ObjectSink objectSink : node.getSinkPropagator().getSinks() ) {
- addObjectSink( ruleBase,
- objectSink,
- nodes );
- }
- }
- }
-
- private static void addLeftTupleSink(InternalRuleBase ruleBase,
- LeftTupleSink sink,
- Map<Integer, BaseNode> nodes) {
- if ( sink instanceof LeftTupleSource ) {
- for ( LeftTupleSink leftTupleSink : ((LeftTupleSource) sink).getSinkPropagator().getSinks() ) {
- addLeftTupleSink( ruleBase,
- leftTupleSink,
- nodes );
- }
- } else if ( sink instanceof RuleTerminalNode ) {
- nodes.put( sink.getId(),
- (RuleTerminalNode) sink );
- }
- }
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/RuleBaseNodes.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/RuleBaseNodes.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,71 @@
+package org.drools.marshalling;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.common.BaseNode;
+import org.drools.common.InternalRuleBase;
+import org.drools.reteoo.BetaNode;
+import org.drools.reteoo.LeftTupleSink;
+import org.drools.reteoo.LeftTupleSource;
+import org.drools.reteoo.ObjectSink;
+import org.drools.reteoo.ObjectSource;
+import org.drools.reteoo.ObjectTypeNode;
+import org.drools.reteoo.RuleTerminalNode;
+
+public class RuleBaseNodes {
+ public static Map<Integer, BaseNode> getNodeMap(InternalRuleBase ruleBase) {
+ Map<Integer, BaseNode> nodes = new HashMap<Integer, BaseNode>();
+ buildNodeMap( ruleBase, nodes );
+ return nodes;
+ }
+
+ private static void buildNodeMap(InternalRuleBase ruleBase,
+ Map<Integer, BaseNode> nodes) {
+ for ( ObjectTypeNode sink : ruleBase.getRete().getObjectTypeNodes() ) {
+ nodes.put( sink.getId(),
+ sink );
+ addObjectSink( ruleBase,
+ sink,
+ nodes );
+ }
+ }
+
+ private static void addObjectSink(InternalRuleBase ruleBase,
+ ObjectSink sink,
+ Map<Integer, BaseNode> nodes) {
+ // we don't need to store alpha nodes, as they have no state to serialise
+ if ( sink instanceof LeftTupleSource ) {
+ LeftTupleSource node = (LeftTupleSource) sink;
+ for ( LeftTupleSink leftTupleSink : node.getSinkPropagator().getSinks() ) {
+ addLeftTupleSink( ruleBase,
+ leftTupleSink,
+ nodes );
+ }
+ } else {
+ ObjectSource node = ( ObjectSource ) sink;
+ for ( ObjectSink objectSink : node.getSinkPropagator().getSinks() ) {
+ addObjectSink( ruleBase,
+ objectSink,
+ nodes );
+ }
+ }
+ }
+
+ private static void addLeftTupleSink(InternalRuleBase ruleBase,
+ LeftTupleSink sink,
+ Map<Integer, BaseNode> nodes) {
+ if ( sink instanceof LeftTupleSource ) {
+ nodes.put( sink.getId(),
+ (LeftTupleSource) sink );
+ for ( LeftTupleSink leftTupleSink : ((LeftTupleSource) sink).getSinkPropagator().getSinks() ) {
+ addLeftTupleSink( ruleBase,
+ leftTupleSink,
+ nodes );
+ }
+ } else if ( sink instanceof RuleTerminalNode ) {
+ nodes.put( sink.getId(),
+ (RuleTerminalNode) sink );
+ }
+ }
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/SerializablePlaceholder.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,14 +0,0 @@
-package org.drools.persister;
-
-public class SerializablePlaceholder implements ObjectPlaceholder {
- private final Object object;
-
- public SerializablePlaceholder(Object object) {
- this.object = object;
- }
-
- public Object resolveObject() {
- return object;
- }
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/SerializablePlaceholder.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholder.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,14 @@
+package org.drools.marshalling;
+
+public class SerializablePlaceholder implements ObjectPlaceholder {
+ private final Object object;
+
+ public SerializablePlaceholder(Object object) {
+ this.object = object;
+ }
+
+ public Object resolveObject() {
+ return object;
+ }
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/SerializablePlaceholderResolverStrategy.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,34 +0,0 @@
-package org.drools.persister;
-
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
-public class SerializablePlaceholderResolverStrategy
- implements
- PlaceholderResolverStrategy {
- private int id;
-
-// public SerializablePlaceholderResolverStrategy(int id) {
-// this.id = id;
-// }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public int getId() {
- return id;
- }
-
- public ObjectPlaceholder read(ObjectInputStream os) throws IOException,
- ClassNotFoundException {
- return new SerializablePlaceholder( os.readObject() );
- }
-
- public void write(ObjectOutputStream os,
- Object object) throws IOException {
- os.writeObject( object );
- }
-
-}
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/SerializablePlaceholderResolverStrategy.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/SerializablePlaceholderResolverStrategy.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,41 @@
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+public class SerializablePlaceholderResolverStrategy
+ implements
+ PlaceholderResolverStrategy {
+
+ private int index;
+
+ private PlaceholderResolverStrategyAcceptor acceptor;
+
+ public SerializablePlaceholderResolverStrategy(PlaceholderResolverStrategyAcceptor acceptor) {
+ this.acceptor = acceptor;
+ }
+
+ public int getIndex() {
+ return this.index;
+ }
+
+ public void setIndex(int index) {
+ this.index = index;
+ }
+
+ public ObjectPlaceholder read(ObjectInputStream os) throws IOException,
+ ClassNotFoundException {
+ return new SerializablePlaceholder( os.readObject() );
+ }
+
+ public void write(ObjectOutputStream os,
+ Object object) throws IOException {
+ os.writeObject( object );
+ }
+
+ public boolean accept(Object object) {
+ return this.accept( object );
+ }
+
+}
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/WMSerialisationInContext.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,50 +0,0 @@
-/**
- *
- */
-package org.drools.persister;
-
-import java.io.ObjectInputStream;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.drools.common.BaseNode;
-import org.drools.common.InternalFactHandle;
-import org.drools.common.InternalRuleBase;
-import org.drools.common.InternalWorkingMemory;
-import org.drools.reteoo.LeftTuple;
-import org.drools.reteoo.RightTuple;
-import org.drools.rule.EntryPoint;
-import org.drools.spi.PropagationContext;
-
-public class WMSerialisationInContext {
- public final ObjectInputStream stream;
- public final InternalRuleBase ruleBase;
- public InternalWorkingMemory wm;
- public final Map<Integer, BaseNode> sinks;
-
- public Map<Integer, InternalFactHandle> handles;
-
- public final Map<RightTupleKey, RightTuple> rightTuples;
- public final Map<Integer, LeftTuple> terminalTupleMap;
-
- public final PlaceholderResolverStrategyFactory resolverStrategyFactory;
- public final Map<String, EntryPoint> entryPoints;
-
- public final Map<Long, PropagationContext> propagationContexts;
-
- public WMSerialisationInContext(ObjectInputStream stream,
- InternalRuleBase ruleBase,
- Map<Integer, BaseNode> sinks,
- PlaceholderResolverStrategyFactory resolverStrategyFactory) {
- super();
- this.stream = stream;
- this.ruleBase = ruleBase;
- this.sinks = sinks;
- handles = new HashMap<Integer, InternalFactHandle>();
- this.rightTuples = new HashMap<RightTupleKey, RightTuple>();
- this.terminalTupleMap = new HashMap<Integer, LeftTuple>();
- this.entryPoints = new HashMap<String, EntryPoint>();
- this.propagationContexts = new HashMap<Long, PropagationContext>();
- this.resolverStrategyFactory = resolverStrategyFactory;
- }
-}
\ No newline at end of file
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/WMSerialisationInContext.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationInContext.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,52 @@
+/**
+ *
+ */
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.common.BaseNode;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.reteoo.LeftTuple;
+import org.drools.reteoo.RightTuple;
+import org.drools.rule.EntryPoint;
+import org.drools.spi.PropagationContext;
+
+public class WMSerialisationInContext extends ObjectInputStream {
+ public final WMSerialisationInContext stream;
+ public final InternalRuleBase ruleBase;
+ public InternalWorkingMemory wm;
+ public final Map<Integer, BaseNode> sinks;
+
+ public Map<Integer, InternalFactHandle> handles;
+
+ public final Map<RightTupleKey, RightTuple> rightTuples;
+ public final Map<Integer, LeftTuple> terminalTupleMap;
+
+ public final PlaceholderResolverStrategyFactory resolverStrategyFactory;
+ public final Map<String, EntryPoint> entryPoints;
+
+ public final Map<Long, PropagationContext> propagationContexts;
+
+ public WMSerialisationInContext(InputStream stream,
+ InternalRuleBase ruleBase,
+ Map<Integer, BaseNode> sinks,
+ PlaceholderResolverStrategyFactory resolverStrategyFactory) throws IOException {
+ super( stream );
+ this.stream = this;
+ this.ruleBase = ruleBase;
+ this.sinks = sinks;
+ this.handles = new HashMap<Integer, InternalFactHandle>();
+ this.rightTuples = new HashMap<RightTupleKey, RightTuple>();
+ this.terminalTupleMap = new HashMap<Integer, LeftTuple>();
+ this.entryPoints = new HashMap<String, EntryPoint>();
+ this.propagationContexts = new HashMap<Long, PropagationContext>();
+ this.resolverStrategyFactory = resolverStrategyFactory;
+ }
+}
\ No newline at end of file
Deleted: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/WMSerialisationOutContext.java 2008-05-17 00:52:52 UTC (rev 19976)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,43 +0,0 @@
-/**
- *
- */
-package org.drools.persister;
-
-import java.io.ObjectOutputStream;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.drools.common.BaseNode;
-import org.drools.common.InternalRuleBase;
-import org.drools.common.InternalWorkingMemory;
-import org.drools.reteoo.LeftTuple;
-
-public class WMSerialisationOutContext {
- public final ObjectOutputStream stream;
- public final InternalRuleBase ruleBase;
- public final InternalWorkingMemory wm;
- public final Map<Integer, BaseNode> sinks;
-
- public final PlaceholderResolverStrategyFactory resolverStrategyFactory;
-// public final Placeholders placeholders;
-
- public final Map<LeftTuple, Integer> terminalTupleMap;
-
- public WMSerialisationOutContext(ObjectOutputStream stream,
- InternalRuleBase ruleBase,
- InternalWorkingMemory wm,
- Map<Integer, BaseNode> sinks,
- PlaceholderResolverStrategyFactory resolverStrategyFactory) {
- //Placeholders placeholders) {
- super();
- this.stream = stream;
- this.ruleBase = ruleBase;
- this.wm = wm;
- this.sinks = sinks;
-
- this.resolverStrategyFactory = resolverStrategyFactory;
-// this.placeholders = placeholders;
-
- this.terminalTupleMap = new HashMap<LeftTuple, Integer>();
- }
-}
\ No newline at end of file
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java (from rev 19996, labs/jbossrules/trunk/drools-core/src/main/java/org/drools/persister/WMSerialisationOutContext.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/WMSerialisationOutContext.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,50 @@
+/**
+ *
+ */
+package org.drools.marshalling;
+
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.Map;
+
+import org.drools.common.BaseNode;
+import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalWorkingMemory;
+import org.drools.reteoo.LeftTuple;
+
+public class WMSerialisationOutContext extends ObjectOutputStream {
+ public final WMSerialisationOutContext stream;
+ public final InternalRuleBase ruleBase;
+ public final InternalWorkingMemory wm;
+ public final Map<Integer, BaseNode> sinks;
+
+ public final PrintStream out = System.out;
+
+ public final PlaceholderResolverStrategyFactory resolverStrategyFactory;
+// public final Placeholders placeholders;
+
+ public final Map<LeftTuple, Integer> terminalTupleMap;
+
+ public WMSerialisationOutContext(OutputStream stream,
+ InternalRuleBase ruleBase,
+ InternalWorkingMemory wm,
+ Map<Integer, BaseNode> sinks,
+ PlaceholderResolverStrategyFactory resolverStrategyFactory) throws IOException {
+ //Placeholders placeholders) {
+ super( stream );
+ this.stream = this;
+ this.ruleBase = ruleBase;
+ this.wm = wm;
+ this.sinks = sinks;
+
+ this.resolverStrategyFactory = resolverStrategyFactory;
+// this.placeholders = placeholders;
+
+ this.terminalTupleMap = new IdentityHashMap<LeftTuple, Integer>();
+ }
+}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/AccumulateNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -36,6 +36,7 @@
import org.drools.util.ArrayUtils;
import org.drools.util.Entry;
import org.drools.util.Iterator;
+import org.drools.util.ObjectHashMap.ObjectEntry;
/**
* AccumulateNode
@@ -534,6 +535,15 @@
}
}
}
+
+ protected void doRemove(final InternalWorkingMemory workingMemory,
+ final AccumulateMemory memory) {
+ Iterator it = memory.betaMemory.getCreatedHandles().iterator();
+ for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
+ AccumulateContext ctx = ( AccumulateContext ) entry.getValue();
+ workingMemory.getFactHandleFactory().destroyFactHandle( ctx.result.getFactHandle() );
+ }
+ }
/* (non-Javadoc)
* @see org.drools.reteoo.BaseNode#hashCode()
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaMemory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaMemory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -8,9 +8,7 @@
import org.drools.rule.ContextEntry;
import org.drools.util.ObjectHashMap;
-public class BetaMemory
- implements
- Externalizable {
+public class BetaMemory {
private static final long serialVersionUID = 400L;
@@ -30,21 +28,6 @@
this.context = context;
}
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- leftTupleMemory = (LeftTupleMemory) in.readObject();
- rightTupleMemory = (RightTupleMemory) in.readObject();
- createdHandles = (ObjectHashMap) in.readObject();
- context = (ContextEntry[]) in.readObject();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject( leftTupleMemory );
- out.writeObject( rightTupleMemory );
- out.writeObject( createdHandles );
- out.writeObject( context );
- }
-
public RightTupleMemory getRightTupleMemory() {
return this.rightTupleMemory;
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/BetaNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -28,10 +28,13 @@
import org.drools.common.InternalWorkingMemory;
import org.drools.common.NodeMemory;
import org.drools.common.PropagationContextImpl;
+import org.drools.reteoo.AccumulateNode.AccumulateMemory;
+import org.drools.reteoo.CollectNode.CollectMemory;
import org.drools.rule.Behavior;
import org.drools.rule.BehaviorManager;
import org.drools.spi.BetaNodeFieldConstraint;
import org.drools.spi.PropagationContext;
+import org.drools.util.Iterator;
import org.drools.util.LinkedList;
import org.drools.util.LinkedListEntry;
@@ -218,6 +221,48 @@
}
if ( !this.isInUse() ) {
for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
+ BetaMemory memory = null;
+ Object object = workingMemories[i].getNodeMemory( this );
+
+ // handle special cases for Collect and Accumulate to make sure they tidy up their specific data
+ // like destroying the local FactHandles
+ if ( object instanceof CollectMemory ) {
+ ((CollectNode) this).doRemove( workingMemories[i], ( CollectMemory ) object );
+ memory = (( CollectMemory )object).betaMemory;
+ } else if ( object instanceof AccumulateMemory ) {
+ ((AccumulateNode) this).doRemove( workingMemories[i], ( AccumulateMemory ) object );
+ memory = (( AccumulateMemory )object).betaMemory;
+ } else {
+ memory = ( BetaMemory ) object;
+ }
+
+ Iterator it = memory.getLeftTupleMemory().iterator();
+ for ( LeftTuple leftTuple = (LeftTuple) it.next(); leftTuple != null; leftTuple = (LeftTuple) it.next() ) {
+ leftTuple.unlinkFromLeftParent();
+ leftTuple.unlinkFromRightParent();
+ }
+
+ it = memory.getRightTupleMemory().iterator();
+ for ( RightTuple rightTuple = (RightTuple) it.next(); rightTuple != null; rightTuple = (RightTuple) it.next() ) {
+ if ( rightTuple.getBlocked() != null ) {
+ // special case for a not, so unlink left tuple from here, as they aren't in the left memory
+ for ( LeftTuple leftTuple = (LeftTuple)rightTuple.getBlocked(); leftTuple != null; ) {
+ LeftTuple temp = leftTuple.getBlockedNext();
+
+ leftTuple.setBlocker( null );
+ leftTuple.setBlockedPrevious( null );
+ leftTuple.setBlockedNext( null );
+ leftTuple.unlinkFromLeftParent();
+ leftTuple = temp;
+ }
+ }
+
+ if ( rightTuple.getRightTupleSink() == null ) {
+ // special case for FromNode
+ workingMemories[i].getFactHandleFactory().destroyFactHandle( rightTuple.getFactHandle() );
+ }
+ rightTuple.unlinkFromRightParent();
+ }
workingMemories[i].clearNodeMemory( this );
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/CollectNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/CollectNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/CollectNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -24,6 +24,7 @@
import java.util.Collection;
import org.drools.RuleBaseConfiguration;
+import org.drools.common.BaseNode;
import org.drools.common.BetaConstraints;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
@@ -36,6 +37,7 @@
import org.drools.util.ArrayUtils;
import org.drools.util.Entry;
import org.drools.util.Iterator;
+import org.drools.util.ObjectHashMap.ObjectEntry;
/**
* @author etirelli
@@ -447,6 +449,15 @@
}
}
}
+
+ protected void doRemove(final InternalWorkingMemory workingMemory,
+ final CollectMemory memory) {
+ Iterator it = memory.betaMemory.getCreatedHandles().iterator();
+ for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
+ CollectContext ctx = ( CollectContext ) entry.getValue();
+ workingMemory.getFactHandleFactory().destroyFactHandle( ctx.resultTuple.getFactHandle() );
+ }
+ }
/* (non-Javadoc)
* @see org.drools.reteoo.BaseNode#hashCode()
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EvalConditionNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EvalConditionNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EvalConditionNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -259,11 +259,19 @@
if ( !node.isInUse() ) {
removeTupleSink( (LeftTupleSink) node );
}
+
if ( !this.isInUse() ) {
for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
+ BetaMemory memory = ( BetaMemory ) workingMemories[i].getNodeMemory( this );
+ Iterator it = memory.getLeftTupleMemory().iterator();
+ for ( LeftTuple leftTuple = (LeftTuple) it.next(); leftTuple != null; leftTuple = (LeftTuple) it.next() ) {
+ leftTuple.unlinkFromLeftParent();
+ leftTuple.unlinkFromRightParent();
+ }
workingMemories[i].clearNodeMemory( this );
}
}
+
if ( !context.alreadyVisited( this.tupleSource ) ) {
this.tupleSource.remove( context,
builder,
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ExistsNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -106,12 +106,6 @@
rightTuple.getFactHandle() ) ) {
leftTuple.setBlocker( rightTuple );
-
- LeftTuple blockedPrevious = rightTuple.getBlocked();
- if ( blockedPrevious != null ) {
- leftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( leftTuple );
- }
rightTuple.setBlocked( leftTuple );
break;
@@ -193,12 +187,6 @@
if ( this.constraints.isAllowedCachedRight( memory.getContext(),
leftTuple ) ) {
leftTuple.setBlocker( rightTuple );
-
- LeftTuple blockedPrevious = rightTuple.getBlocked();
- if ( blockedPrevious != null ) {
- leftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( leftTuple );
- }
rightTuple.setBlocked( leftTuple );
if ( this.tupleMemoryEnabled ) {
@@ -303,12 +291,6 @@
if ( this.constraints.isAllowedCachedLeft( memory.getContext(),
newBlocker.getFactHandle() ) ) {
leftTuple.setBlocker( newBlocker );
-
- LeftTuple blockedPrevious = newBlocker.getBlocked();
- if ( blockedPrevious != null ) {
- leftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( leftTuple );
- }
newBlocker.setBlocked( leftTuple );
break;
@@ -364,22 +346,7 @@
context,
workingMemory );
- LeftTuple previous = (LeftTuple) leftTuple.getBlockedPrevious();
- LeftTuple next = (LeftTuple) leftTuple.getBlockedNext();
- if ( previous != null && next != null ) {
- //remove from middle
- previous.setBlockedNext( next );
- next.setBlockedPrevious( previous );
- } else if ( next != null ) {
- //remove from first
- blocker.setBlocked( next );
- next.setBlockedPrevious( null );
- } else if ( previous != null ) {
- //remove from end
- previous.setBlockedNext( null );
- } else {
- blocker.setBlocked( null );
- }
+ blocker.removeBlocked( leftTuple );
} else {
memory.getLeftTupleMemory().remove( leftTuple );
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/FromNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/FromNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/FromNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -207,11 +207,23 @@
if ( !node.isInUse() ) {
removeTupleSink( (LeftTupleSink) node );
}
+
if ( !this.isInUse() ) {
for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
+ FromMemory memory = ( FromMemory ) workingMemories[i].getNodeMemory( this );
+ Iterator it = memory.betaMemory.getLeftTupleMemory().iterator();
+ for ( LeftTuple leftTuple = (LeftTuple) it.next(); leftTuple != null; leftTuple = (LeftTuple) it.next() ) {
+ leftTuple.unlinkFromLeftParent();
+ leftTuple.unlinkFromRightParent();
+ }
+
+ // RightTuple is already disconnected via the child LeftTuple in the sink nodes
+ // special case exists in the BetaNode to handle destroying of the FactHandle
+
workingMemories[i].clearNodeMemory( this );
}
}
+
if ( !context.alreadyVisited( this.tupleSource ) ) {
this.tupleSource.remove( context,
builder,
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandle.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandle.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandle.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -46,19 +46,7 @@
// ----------------------------------------------------------------------
// Instance members
// ----------------------------------------------------------------------
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- super.readExternal( in );
- delegate = (InternalFactHandle) in.readObject();
- object = in.readObject();
- }
- public void writeExternal(ObjectOutput out) throws IOException {
- super.writeExternal( out );
- out.writeObject( delegate );
- out.writeObject( object );
- }
-
/**
* @see Object
*/
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandleDummyObject.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandleDummyObject.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/InitialFactHandleDummyObject.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -0,0 +1,22 @@
+/**
+ *
+ */
+package org.drools.reteoo;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+public class InitialFactHandleDummyObject
+ implements
+ Externalizable {
+ private static final long serialVersionUID = 400L;
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ }
+}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/LeftTuple.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/LeftTuple.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/LeftTuple.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -143,14 +143,23 @@
this.leftParentPrevious.leftParentNext = this.leftParentNext;
this.leftParentNext.leftParentPrevious = this.leftParentPrevious;
} else if ( next != null ) {
- //remove from first
- this.leftParent.children = this.leftParentNext;
+ if ( this.leftParent != null ) {
+ //remove from first
+ this.leftParent.children = this.leftParentNext;
+ } else {
+ this.handle.setLeftTuple( this.leftParentNext );
+ }
+
this.leftParentNext.leftParentPrevious = null;
} else if ( previous != null ) {
//remove from end
this.leftParentPrevious.leftParentNext = null;
} else {
- this.leftParent.children = null;
+ if ( this.leftParent != null ) {
+ this.leftParent.children = null;
+ } else {
+ this.handle.setLeftTuple( null );
+ }
}
//this.parent = null;
@@ -163,6 +172,10 @@
}
public void unlinkFromRightParent() {
+ if ( this.rightParent == null ) {
+ // no right parent;
+ return;
+ }
LeftTuple previous = (LeftTuple) this.rightParentPrevious;
LeftTuple next = (LeftTuple) this.rightParentNext;
@@ -445,57 +458,6 @@
return objects;
}
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- index = in.readInt();
- handle = (InternalFactHandle) in.readObject();
- parent = (LeftTuple) in.readObject();
- activation = (Activation) in.readObject();
- recency = in.readLong();
- hashCode = in.readInt();
- blocker = (RightTuple) in.readObject();
- blockedPrevious = (LeftTuple) in.readObject();
- blockedNext = (LeftTuple) in.readObject();
- leftParent = (LeftTuple) in.readObject();
- leftParentPrevious = (LeftTuple) in.readObject();
- leftParentNext = (LeftTuple) in.readObject();
- rightParent = (RightTuple) in.readObject();
- rightParentPrevious = (LeftTuple) in.readObject();
- rightParentNext = (LeftTuple) in.readObject();
- memory = (LeftTupleList) in.readObject();
- next = (Entry) in.readObject();
- previous = (Entry) in.readObject();
- children = (LeftTuple) in.readObject();
-
- // @todo should not serialise out part of the rete network
- sink = (LeftTupleSink) in.readObject();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeInt( index );
- out.writeObject( handle );
- out.writeObject( parent );
- out.writeObject( activation );
- out.writeLong( recency );
- out.writeInt( hashCode );
- out.writeObject( blocker );
- out.writeObject( blockedPrevious );
- out.writeObject( blockedNext );
- out.writeObject( leftParent );
- out.writeObject( leftParentPrevious );
- out.writeObject( leftParentNext );
- out.writeObject( rightParent );
- out.writeObject( rightParentPrevious );
- out.writeObject( rightParentNext );
- out.writeObject( memory );
- out.writeObject( next );
- out.writeObject( previous );
- out.writeObject( children );
-
- // @todo should not serialise in part of the rete network
- out.writeObject( sink );
- }
-
public LeftTuple getParent() {
return parent;
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/NotNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -105,11 +105,6 @@
leftTuple.setBlocker( rightTuple );
if ( this.tupleMemoryEnabled ) {
- LeftTuple blockedPrevious = rightTuple.getBlocked();
- if ( blockedPrevious != null ) {
- leftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( leftTuple );
- }
rightTuple.setBlocked( leftTuple );
}
@@ -169,12 +164,6 @@
if ( this.constraints.isAllowedCachedRight( memory.getContext(),
leftTuple ) ) {
leftTuple.setBlocker( rightTuple );
-
- LeftTuple blockedPrevious = rightTuple.getBlocked();
- if ( blockedPrevious != null ) {
- leftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( leftTuple );
- }
rightTuple.setBlocked( leftTuple );
// this is now blocked so remove from memory
@@ -233,12 +222,6 @@
if ( this.constraints.isAllowedCachedLeft( memory.getContext(),
newBlocker.getFactHandle() ) ) {
leftTuple.setBlocker( newBlocker );
-
- LeftTuple blockedPrevious = newBlocker.getBlocked();
- if ( blockedPrevious != null ) {
- leftTuple.setBlockedNext( blockedPrevious );
- blockedPrevious.setBlockedPrevious( leftTuple );
- }
newBlocker.setBlocked( leftTuple );
break;
@@ -283,22 +266,7 @@
context,
workingMemory );
} else {
- LeftTuple previous = (LeftTuple) leftTuple.getBlockedPrevious();
- LeftTuple next = (LeftTuple) leftTuple.getBlockedNext();
- if ( previous != null && next != null ) {
- //remove from middle
- previous.setBlockedNext( next );
- next.setBlockedPrevious( previous );
- } else if ( next != null ) {
- //remove from first
- blocker.setBlocked( next );
- next.setBlockedPrevious( null );
- } else if ( previous != null ) {
- //remove from end
- previous.setBlockedNext( null );
- } else {
- blocker.setBlocked( null );
- }
+ blocker.removeBlocked( leftTuple );
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -212,8 +212,8 @@
for ( LeftTuple leftTuple = factHandle.getLeftTuple(); leftTuple != null; leftTuple = (LeftTuple) leftTuple.getLeftParentNext() ) {
leftTuple.getLeftTupleSink().retractLeftTuple( leftTuple,
- context,
- workingMemory );
+ context,
+ workingMemory );
}
factHandle.setLeftTuple( null );
}
@@ -223,8 +223,10 @@
final InternalWorkingMemory workingMemory) {
final ObjectHashSet memory = (ObjectHashSet) workingMemory.getNodeMemory( this );
Iterator it = memory.iterator();
- for ( ObjectEntry entry = ( ObjectEntry ) it.next(); entry != null; entry = ( ObjectEntry ) it.next() ) {
- sink.assertObject( (InternalFactHandle) entry.getValue(), context, workingMemory );
+ for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
+ sink.assertObject( (InternalFactHandle) entry.getValue(),
+ context,
+ workingMemory );
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/PropagationQueuingNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/PropagationQueuingNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/PropagationQueuingNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -29,6 +29,8 @@
import org.drools.common.InternalWorkingMemory;
import org.drools.common.NodeMemory;
import org.drools.common.WorkingMemoryAction;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.reteoo.builder.BuildContext;
import org.drools.spi.PropagationContext;
@@ -418,6 +420,14 @@
public PropagateAction(PropagationQueuingNode node) {
this.node = node;
}
+
+ public PropagateAction(WMSerialisationInContext context) throws IOException {
+ this.node = ( PropagationQueuingNode ) context.sinks.get( context.readInt() );
+ }
+
+ public void write(WMSerialisationOutContext context) throws IOException {
+ context.write( node.getId() );
+ }
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooFactHandleFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooFactHandleFactory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooFactHandleFactory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -19,6 +19,8 @@
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
import org.drools.TemporalSession;
import org.drools.common.AbstractFactHandleFactory;
@@ -26,6 +28,8 @@
import org.drools.common.EventFactHandle;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.rule.TypeDeclaration;
import org.drools.spi.FactHandleFactory;
@@ -36,16 +40,11 @@
public ReteooFactHandleFactory() {
super();
}
+
+ public ReteooFactHandleFactory(int id, long counter) {
+ super( id, counter );
+ }
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- super.readExternal( in );
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- super.writeExternal( out );
- }
-
/* (non-Javadoc)
* @see org.drools.reteoo.FactHandleFactory#newFactHandle(long)
*/
@@ -80,6 +79,10 @@
public FactHandleFactory newInstance() {
return new ReteooFactHandleFactory();
}
+
+ public FactHandleFactory newInstance(int id, long counter) {
+ return new ReteooFactHandleFactory(id, counter);
+ }
public Class getFactHandleType() {
return DefaultFactHandle.class;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -17,10 +17,11 @@
*/
import java.io.IOException;
+import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
+import java.io.OutputStream;
import java.io.Serializable;
-import java.io.Externalizable;
import java.util.Iterator;
import org.drools.ClockType;
@@ -31,6 +32,8 @@
import org.drools.StatefulSession;
import org.drools.StatelessSession;
import org.drools.TemporalSession;
+import org.drools.WorkingMemory;
+import org.drools.audit.WorkingMemoryInMemoryLogger;
import org.drools.common.AbstractRuleBase;
import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalFactHandle;
@@ -38,6 +41,14 @@
import org.drools.concurrent.CommandExecutor;
import org.drools.concurrent.ExecutorService;
import org.drools.event.RuleBaseEventListener;
+import org.drools.marshalling.InputPersister;
+import org.drools.marshalling.Marshaller;
+import org.drools.marshalling.OutputPersister;
+import org.drools.marshalling.PlaceholderResolverStrategyFactory;
+import org.drools.marshalling.RuleBaseNodes;
+import org.drools.marshalling.SerializablePlaceholderResolverStrategy;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.reteoo.ReteooWorkingMemory.WorkingMemoryReteAssertAction;
import org.drools.reteoo.builder.BuildContext;
import org.drools.rule.EntryPoint;
@@ -274,14 +285,8 @@
addEventListener( (RuleBaseEventListener) it.next() );
}
}
-
- final InitialFactHandleDummyObject initialFact = new InitialFactHandleDummyObject();
- final InitialFactHandle handle = new InitialFactHandle( session.getFactHandleFactory().newFactHandle( initialFact,
- null,
- session ) );
-
- session.queueWorkingMemoryAction( new WorkingMemoryReteAssertAction( handle,
+ session.queueWorkingMemoryAction( new WorkingMemoryReteAssertAction( session.getInitialFactHandle(),
false,
true,
null,
@@ -291,6 +296,93 @@
return session;
}
+ public StatefulSession readStatefulSession(final InputStream stream,
+ final boolean keepReference,
+ Marshaller marshaller) throws IOException,
+ ClassNotFoundException {
+
+ if ( this.config.isSequential() ) {
+ throw new RuntimeException( "Cannot have a stateful rule session, with sequential configuration set to true" );
+ }
+
+// PlaceholderResolverStrategyFactory factory = new PlaceholderResolverStrategyFactory();
+// factory.addPlaceholderResolverStrategy( new SerializablePlaceholderResolverStrategy() );
+
+ StatefulSession session = null;
+ synchronized ( this.pkgs ) {
+ ExecutorService executor = ExecutorServiceFactory.createExecutorService( this.config.getExecutorService() );
+
+ session = marshaller.read( stream, this, nextWorkingMemoryCounter(), executor );
+
+// WMSerialisationInContext context = new WMSerialisationInContext( stream,
+// this,
+// RuleBaseNodes.getNodeMap( this ),
+// factory );
+//
+// session = InputPersister.readSession( context, nextWorkingMemoryCounter(), executor );
+
+ executor.setCommandExecutor( new CommandExecutor( session ) );
+
+
+ if ( keepReference ) {
+ super.addStatefulSession( session );
+ for ( Iterator it = session.getRuleBaseUpdateListeners().iterator(); it.hasNext(); ) {
+ addEventListener( (RuleBaseEventListener) it.next() );
+ }
+ }
+ }
+ return session;
+
+ // final DroolsObjectInputStream streamWithLoader = new DroolsObjectInputStream( stream,
+ // this.packageClassLoader );
+ // streamWithLoader.setRuleBase( this );
+ //
+ // final StatefulSession session = (StatefulSession) streamWithLoader.readObject();
+ //
+ // synchronized ( this.pkgs ) {
+ // ((InternalWorkingMemory) session).setRuleBase( this );
+ // ((InternalWorkingMemory) session).setId( (nextWorkingMemoryCounter()) );
+ //
+ // ExecutorService executor = ExecutorServiceFactory.createExecutorService( this.config.getExecutorService() );
+ // executor.setCommandExecutor( new CommandExecutor( session ) );
+ // ((InternalWorkingMemory) session).setExecutorService( executor );
+ //
+ // if ( keepReference ) {
+ // addStatefulSession( session );
+ // for ( Iterator it = session.getRuleBaseUpdateListeners().iterator(); it.hasNext(); ) {
+ // addEventListener( (RuleBaseEventListener) it.next() );
+ // }
+ // }
+ //
+ // return session;
+ // }
+ }
+
+ public void writeStatefulSession(final StatefulSession session,
+ final OutputStream stream,
+ Marshaller marshaller) throws IOException {
+
+// PlaceholderResolverStrategyFactory factory = new PlaceholderResolverStrategyFactory();
+// factory.addPlaceholderResolverStrategy( new SerializablePlaceholderResolverStrategy() );
+//
+// WMSerialisationOutContext context = new WMSerialisationOutContext( stream,
+// this,
+// (InternalWorkingMemory) session,
+// RuleBaseNodes.getNodeMap( this ),
+// factory );
+// OutputPersister.writeSession( context );
+ marshaller.write( stream, this, session );
+ //((ReteooStatefulSession) session).write( context );
+
+
+ // ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // OutputPersister op = new OutputPersister( this,
+ // (InternalWorkingMemory) wm,
+ // baos,
+ // factory );
+ // op.write();
+ }
+
public StatelessSession newStatelessSession() {
//orders the rules
@@ -318,23 +410,10 @@
}
public synchronized void addPackage(final Package newPkg) {
- super.addPackage(newPkg);
+ super.addPackage( newPkg );
if ( this.config.isSequential() ) {
- this.reteooBuilder.setOrdered(false);
+ this.reteooBuilder.setOrdered( false );
}
}
-
- public static class InitialFactHandleDummyObject
- implements
- Externalizable {
- private static final long serialVersionUID = 400L;
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- }
- }
-
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,5 +1,6 @@
package org.drools.reteoo;
+import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
@@ -7,6 +8,7 @@
import org.drools.FactHandle;
import org.drools.StatefulSession;
+import org.drools.common.DefaultAgenda;
import org.drools.common.InternalRuleBase;
import org.drools.concurrent.AssertObject;
import org.drools.concurrent.AssertObjects;
@@ -16,7 +18,10 @@
import org.drools.concurrent.RetractObject;
import org.drools.concurrent.UpdateObject;
import org.drools.event.RuleBaseEventListener;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.spi.AgendaFilter;
+import org.drools.spi.FactHandleFactory;
import org.drools.spi.RuleBaseUpdateListener;
import org.drools.spi.RuleBaseUpdateListenerFactory;
@@ -29,10 +34,6 @@
private transient List ruleBaseListeners;
- public ReteooStatefulSession() {
-
- }
-
public ReteooStatefulSession(final int id,
final InternalRuleBase ruleBase,
final ExecutorService executorService) {
@@ -41,6 +42,22 @@
this.executor = executorService;
}
+ public ReteooStatefulSession(final int id,
+ final InternalRuleBase ruleBase,
+ final ExecutorService executorService,
+ final FactHandleFactory handleFactory,
+ final InitialFactHandle initialFactHandle,
+ final long propagationContext,
+ final DefaultAgenda agenda) {
+ super( id,
+ ruleBase,
+ handleFactory,
+ initialFactHandle,
+ propagationContext,
+ agenda );
+ this.executor = executorService;
+ }
+
public Future asyncInsert(final Object object) {
final AssertObject assertObject = new AssertObject( object );
this.executor.submit( assertObject );
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatelessSession.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -25,7 +25,6 @@
import org.drools.event.RuleFlowEventSupport;
import org.drools.event.WorkingMemoryEventListener;
import org.drools.event.WorkingMemoryEventSupport;
-import org.drools.reteoo.ReteooRuleBase.InitialFactHandleDummyObject;
import org.drools.reteoo.ReteooWorkingMemory.WorkingMemoryReteAssertAction;
import org.drools.rule.EntryPoint;
import org.drools.spi.AgendaFilter;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooTemporalSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooTemporalSession.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooTemporalSession.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -24,6 +24,8 @@
import org.drools.TemporalSession;
import org.drools.common.InternalRuleBase;
import org.drools.concurrent.ExecutorService;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.temporal.SessionClock;
/**
@@ -39,9 +41,6 @@
private static final long serialVersionUID = -2129661675928809928L;
private T sessionClock;
- public ReteooTemporalSession() {
- }
-
public ReteooTemporalSession(int id,
InternalRuleBase ruleBase,
ExecutorService executorService,
@@ -53,17 +52,6 @@
this.sessionClock.setSession( this );
}
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- super.readExternal( in );
- sessionClock = (T) in.readObject();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- super.writeExternal( out );
- out.writeObject( sessionClock );
- }
-
public T getSessionClock() {
return this.sessionClock;
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -37,7 +37,12 @@
import org.drools.common.PropagationContextImpl;
import org.drools.common.WorkingMemoryAction;
import org.drools.event.RuleBaseEventListener;
+import org.drools.marshalling.InputPersister;
+import org.drools.marshalling.OutputPersister;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.rule.EntryPoint;
+import org.drools.rule.Package;
import org.drools.rule.Query;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
@@ -51,19 +56,13 @@
* @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
* @author <a href="mailto:simon at redhillconsulting.com.au">Simon Harris</a>
*/
-public class ReteooWorkingMemory extends AbstractWorkingMemory
- implements
- Externalizable {
+public class ReteooWorkingMemory extends AbstractWorkingMemory {
/**
*
*/
- private static final long serialVersionUID = 400L;
+ private static final long serialVersionUID = 400L;
- public ReteooWorkingMemory() {
-
- }
-
/**
* Construct.
*
@@ -75,18 +74,38 @@
super( id,
ruleBase,
ruleBase.newFactHandleFactory() );
- this.agenda = new DefaultAgenda( this );
+ this.agenda = new DefaultAgenda( ruleBase );
+ this.agenda.setWorkingMemory( this );
}
public ReteooWorkingMemory(final int id,
final InternalRuleBase ruleBase,
- final FactHandleFactory factory) {
+ final FactHandleFactory handleFactory,
+ final InitialFactHandle initialFactHandle,
+ final long propagationContext,
+ final DefaultAgenda agenda) {
super( id,
ruleBase,
- factory );
- this.agenda = new DefaultAgenda( this );
- }
+ handleFactory,
+ initialFactHandle,
+ //ruleBase.newFactHandleFactory(context),
+ propagationContext );
+ this.agenda = agenda;
+ this.agenda.setWorkingMemory( this );
+// InputPersister.readFactHandles( context );
+// super.read( context );
+ }
+// public void write(WMSerialisationOutContext context) throws IOException, ClassNotFoundException {
+// this.handleFactory.write( context );
+//
+//// context.writeInt( this.initialFactHandle.getId() );
+//// context.writeLong( this.initialFactHandle.getRecency() );
+//
+// OutputPersister.writeFactHandles( context );
+// super.write( context );
+// }
+
public QueryResults getQueryResults(final String query) {
return getQueryResults( query,
null );
@@ -170,10 +189,6 @@
private LeftTuple leftTuple;
- public WorkingMemoryReteAssertAction() {
-
- }
-
public WorkingMemoryReteAssertAction(final InternalFactHandle factHandle,
final boolean removeLogical,
final boolean updateEqualsMap,
@@ -185,6 +200,46 @@
this.ruleOrigin = ruleOrigin;
this.leftTuple = leftTuple;
}
+
+ public WorkingMemoryReteAssertAction(WMSerialisationInContext context) throws IOException {
+ this.factHandle = context.handles.get( context.readInt() );
+ this.removeLogical = context.readBoolean();
+ this.updateEqualsMap = context.readBoolean();
+
+ if ( context.readBoolean() ) {
+ String pkgName = context.readUTF();
+ String ruleName = context.readUTF();
+ Package pkg = context.ruleBase.getPackage( pkgName );
+ this.ruleOrigin = pkg.getRule( ruleName );
+ }
+ if ( context.readBoolean() ) {
+ this.leftTuple = context.terminalTupleMap.get( context.readInt() );
+ }
+ }
+
+ public void write(WMSerialisationOutContext context) throws IOException {
+ context.writeInt( WorkingMemoryAction.WorkingMemoryReteAssertAction );
+
+ context.writeInt( this.factHandle.getId() );
+ context.writeBoolean( this.removeLogical );
+ context.writeBoolean( this.updateEqualsMap );
+
+ if ( this.ruleOrigin != null ) {
+ context.writeBoolean( true );
+ context.writeUTF( ruleOrigin.getPackage() );
+ context.writeUTF( ruleOrigin.getName() );
+ } else {
+ context.writeBoolean( false );
+ }
+
+ if ( this.leftTuple != null ) {
+ context.writeBoolean( true );
+ context.writeInt( context.terminalTupleMap.get( this.leftTuple ) );
+ } else {
+ context.writeBoolean( false );
+ }
+
+ }
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightInputAdapterNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightInputAdapterNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightInputAdapterNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -214,6 +214,23 @@
if ( !node.isInUse() ) {
removeObjectSink( (ObjectSink) node );
}
+
+ if ( !this.isInUse() ) {
+ for ( int i = 0, length = workingMemories.length; i < length; i++ ) {
+ ObjectHashMap memory = ( ObjectHashMap ) workingMemories[i].getNodeMemory( this );
+
+ Iterator it = memory.iterator();
+ for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
+ LeftTuple leftTuple = ( LeftTuple ) entry.getKey();
+ leftTuple.unlinkFromLeftParent();
+ leftTuple.unlinkFromRightParent();
+
+ InternalFactHandle handle = ( InternalFactHandle ) entry.getValue();
+ workingMemories[i].getFactHandleFactory().destroyFactHandle( handle);
+ }
+ }
+ }
+
if ( !context.alreadyVisited( this.tupleSource ) ) {
this.tupleSource.remove( context,
builder,
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTuple.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTuple.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTuple.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -80,12 +80,35 @@
}
public LeftTuple getBlocked() {
- return blocked;
- }
+ return this.blocked;
+ }
- public void setBlocked(LeftTuple blocked) {
- this.blocked = blocked;
+ public void setBlocked(LeftTuple leftTuple) {
+ if ( this.blocked != null && leftTuple != null ) {
+ leftTuple.setBlockedNext( this.blocked );
+ this.blocked.setBlockedPrevious( leftTuple );
+ }
+ this.blocked = leftTuple;
}
+
+ public void removeBlocked(LeftTuple leftTuple) {
+ LeftTuple previous = (LeftTuple) leftTuple.getBlockedPrevious();
+ LeftTuple next = (LeftTuple) leftTuple.getBlockedNext();
+ if ( previous != null && next != null ) {
+ //remove from middle
+ previous.setBlockedNext( next );
+ next.setBlockedPrevious( previous );
+ } else if ( next != null ) {
+ //remove from first
+ this.blocked = next ;
+ next.setBlockedPrevious( null );
+ } else if ( previous != null ) {
+ //remove from end
+ previous.setBlockedNext( null );
+ } else {
+ this.blocked = null;
+ }
+ }
public RightTupleList getMemory() {
return memory;
@@ -168,32 +191,4 @@
public boolean equals(Object object) {
return equals( (RightTuple) object );
}
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- this.handle = (InternalFactHandle) in.readObject();
- this.handlePrevious = (RightTuple) in.readObject();
- this.handleNext = (RightTuple) in.readObject();
- this.memory = (RightTupleList) in.readObject();
- this.previous = (RightTuple) in.readObject();
- this.next = (RightTuple) in.readObject();
- this.betaChildren = (LeftTuple) in.readObject();
- this.blocked = (LeftTuple) in.readObject();
- this.sink = (RightTupleSink) in.readObject();
- this.hashCode = in.readInt();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject( this.handle );
- out.writeObject( this.handlePrevious );
- out.writeObject( this.handleNext );
- out.writeObject( this.memory );
- out.writeObject( this.previous );
- out.writeObject( this.next );
- out.writeObject( this.betaChildren );
- out.writeObject( this.blocked );
- out.writeObject( this.sink );
- out.writeInt( this.hashCode );
- }
-
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTupleMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTupleMemory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RightTupleMemory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -14,6 +14,8 @@
public void remove(RightTuple rightTuple);
public boolean contains(RightTuple rightTuple);
+
+ public Iterator iterator();
public boolean isIndexed();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/RuleTerminalNode.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -165,7 +165,7 @@
public void assertLeftTuple(final LeftTuple tuple,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
- assertTuple( tuple,
+ assertLeftTuple( tuple,
context,
workingMemory,
true );
@@ -182,7 +182,7 @@
* @throws AssertionException
* If an error occurs while asserting.
*/
- public void assertTuple(final LeftTuple tuple,
+ public void assertLeftTuple(final LeftTuple tuple,
final PropagationContext context,
final InternalWorkingMemory workingMemory,
final boolean fireActivationCreated) {
@@ -475,8 +475,8 @@
final TerminalNodeMemory memory = (TerminalNodeMemory) workingMemory.getNodeMemory( this );
final Iterator it = memory.getTupleMemory().iterator();
- for ( LeftTuple tuple = (LeftTuple) it.next(); tuple != null; tuple = (LeftTuple) it.next() ) {
- final Activation activation = tuple.getActivation();
+ for ( LeftTuple leftTuple = (LeftTuple) it.next(); leftTuple != null; leftTuple = (LeftTuple) it.next() ) {
+ final Activation activation = leftTuple.getActivation();
if ( activation.isActivated() ) {
activation.remove();
@@ -492,6 +492,8 @@
workingMemory.getTruthMaintenanceSystem().removeLogicalDependencies( activation,
propagationContext,
this.rule );
+ leftTuple.unlinkFromLeftParent();
+ leftTuple.unlinkFromRightParent();
}
workingMemory.executeQueuedActions();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FactHandleFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FactHandleFactory.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/FactHandleFactory.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -16,11 +16,14 @@
* limitations under the License.
*/
+import java.io.IOException;
import java.io.Serializable;
import org.drools.FactHandle;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
+import org.drools.marshalling.WMSerialisationInContext;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.reteoo.ObjectTypeConf;
/**
@@ -31,9 +34,7 @@
* @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
* @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
*/
-public interface FactHandleFactory
- extends
- Serializable {
+public interface FactHandleFactory {
/**
* Construct a handle with a new id.
*
@@ -55,6 +56,8 @@
* @return a fresh instance of the fact handle factory, with any IDs reset etc.
*/
public FactHandleFactory newInstance();
+
+ public FactHandleFactory newInstance(int id, long counter);
public Class getFactHandleType();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/temporal/SessionPseudoClock.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -28,6 +28,7 @@
import org.drools.common.DroolsObjectInputStream;
import org.drools.common.InternalWorkingMemory;
import org.drools.common.WorkingMemoryAction;
+import org.drools.marshalling.WMSerialisationOutContext;
import org.drools.reteoo.ReteooTemporalSession;
import org.drools.rule.Behavior;
@@ -202,6 +203,11 @@
public String toString() {
return "ScheduledItem( timestamp="+timestamp+", behavior="+behavior+" )";
}
+
+ public void write(WMSerialisationOutContext context) throws IOException {
+ // TODO Auto-generated method stub
+
+ }
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -1,13 +1,17 @@
package org.drools.util;
import java.io.File;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
public final class ClassUtils {
- private static Map classes = Collections.synchronizedMap( new HashMap() );
+ private static Map classes = Collections.synchronizedMap( new HashMap() );
+ private static final String STAR = "*";
+
/**
* Please do not use - internal
* org/my/Class.xxx -> org.my.Class
@@ -133,7 +137,7 @@
//swallow
}
}
-
+
if ( cls != null ) {
ClassUtils.classes.put( className,
cls );
@@ -152,4 +156,70 @@
return object;
}
+ /**
+ * Populates the import style pattern map from give comma delimited string
+ * @param patterns
+ * @param str
+ */
+ public static void addImportStylePatterns(Map<String, Object> patterns,
+ String str) {
+ if ( str == null || "".equals( str.trim() ) ) {
+ return;
+ }
+
+ String[] items = str.split( " " );
+ for ( int i = 0; i < items.length; i++ ) {
+ String qualifiedNamespace = items[i].substring( 0,
+ items[i].lastIndexOf( '.' ) ).trim();
+ String name = items[i].substring( items[i].lastIndexOf( '.' ) + 1 ).trim();
+ Object object = patterns.get( qualifiedNamespace );
+ if ( object == null ) {
+ if ( STAR.equals( name ) ) {
+ patterns.put( qualifiedNamespace,
+ STAR );
+ } else {
+ // create a new list and add it
+ List list = new ArrayList();
+ list.add( name );
+ patterns.put( qualifiedNamespace,
+ list );
+ }
+ } else if ( name.equals( STAR ) ) {
+ // if its a STAR now add it anyway, we don't care if it was a STAR or a List before
+ patterns.put( qualifiedNamespace,
+ STAR );
+ } else {
+ // its a list so add it if it doesn't already exist
+ List list = (List) object;
+ if ( !list.contains( object ) ) {
+ list.add( name );
+ }
+ }
+ }
+ }
+
+ /**
+ * Determines if a given full qualified class name matches any import style patterns.
+ * @param patterns
+ * @param className
+ * @return
+ */
+ public static boolean isMatched(Map<String, Object> patterns,
+ String className) {
+ String qualifiedNamespace = className.substring( 0,
+ className.lastIndexOf( '.' ) ).trim();
+ String name = className.substring( className.lastIndexOf( '.' ) + 1 ).trim();
+ Object object = patterns.get( qualifiedNamespace );
+ if ( object == null ) {
+ return true;
+ } else if ( STAR.equals( object ) ) {
+ return false;
+ } else if ( patterns.containsKey( "*" ) ) {
+ // for now we assume if the name space is * then we have a catchal *.* pattern
+ return true;
+ } else {
+ List list = (List) object;
+ return !list.contains( name );
+ }
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Entry.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Entry.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/Entry.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -5,9 +5,7 @@
import java.io.Externalizable;
-public interface Entry
- extends
- Externalizable {
+public interface Entry {
public void setNext(Entry next);
public Entry getNext();
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/LeftTupleList.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/LeftTupleList.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/LeftTupleList.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -200,19 +200,4 @@
return builder.toString();
}
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- next = (LeftTuple) in.readObject();
- first = (LeftTuple) in.readObject();
- hashCode = in.readInt();
- index = (Index) in.readObject();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject( next );
- out.writeObject( first );
- out.writeInt( hashCode );
- out.writeObject( index );
- }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashMap.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashMap.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashMap.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -133,7 +133,8 @@
public static class ObjectEntry
implements
- Entry {
+ Entry,
+ Externalizable {
private static final long serialVersionUID = 400L;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashSet.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashSet.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ObjectHashSet.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -5,6 +5,7 @@
import org.drools.util.AbstractHashTable.EqualityEquals;
+import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.IOException;
import java.io.ObjectOutput;
@@ -139,7 +140,8 @@
public static class ObjectEntry
implements
- Entry {
+ Entry,
+ Externalizable {
private static final long serialVersionUID = 400L;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleIndexHashTable.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleIndexHashTable.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleIndexHashTable.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -10,28 +10,31 @@
import org.drools.util.AbstractHashTable.HashTableIterator;
import org.drools.util.AbstractHashTable.Index;
import org.drools.util.AbstractHashTable.ObjectComparator;
+import org.drools.util.LeftTupleIndexHashTable.FieldIndexHashTableFullIterator;
import java.io.ObjectInput;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.Externalizable;
-public class RightTupleIndexHashTable extends AbstractHashTable
+public class RightTupleIndexHashTable extends AbstractHashTable
implements
RightTupleMemory {
- private static final long serialVersionUID = 400L;
+ private static final long serialVersionUID = 400L;
- public static final int PRIME = 31;
+ public static final int PRIME = 31;
- private int startResult;
+ private transient FieldIndexHashTableFullIterator tupleValueFullIterator;
- private int factSize;
+ private int startResult;
- private Index index;
-
+ private int factSize;
+
+ private Index index;
+
public RightTupleIndexHashTable() {
-
+
}
public RightTupleIndexHashTable(final FieldIndex[] index) {
@@ -70,12 +73,13 @@
throw new IllegalArgumentException( "FieldIndexHashTable cannot use an index[] of length great than 3" );
}
}
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
super.readExternal( in );
startResult = in.readInt();
factSize = in.readInt();
- index = ( Index ) in.readObject();
+ index = (Index) in.readObject();
}
public void writeExternal(ObjectOutput out) throws IOException {
@@ -83,7 +87,7 @@
out.writeInt( startResult );
out.writeInt( factSize );
out.writeObject( index );
- }
+ }
public RightTuple getFirst(LeftTuple leftTuple) {
RightTupleList bucket = get( leftTuple );
@@ -119,6 +123,65 @@
return this.table[index];
}
+ public Iterator iterator() {
+ if ( this.tupleValueFullIterator == null ) {
+ this.tupleValueFullIterator = new FieldIndexHashTableFullIterator( this );
+ }
+ this.tupleValueFullIterator.reset();
+ return this.tupleValueFullIterator;
+ }
+
+ public static class FieldIndexHashTableFullIterator
+ implements
+ Iterator {
+ private AbstractHashTable hashTable;
+ private Entry[] table;
+ private int row;
+ private int length;
+ private Entry entry;
+
+ public FieldIndexHashTableFullIterator(final AbstractHashTable hashTable) {
+ this.hashTable = hashTable;
+ }
+
+ /* (non-Javadoc)
+ * @see org.drools.util.Iterator#next()
+ */
+ public Object next() {
+ if ( this.entry == null ) {
+ // keep skipping rows until we come to the end, or find one that is populated
+ while ( this.entry == null ) {
+ this.row++;
+ if ( this.row == this.length ) {
+ return null;
+ }
+ this.entry = (this.table[this.row] != null) ? ((RightTupleList) this.table[this.row]).first : null;
+ }
+ } else {
+ this.entry = this.entry.getNext();
+ if ( this.entry == null ) {
+ this.entry = (Entry) next();
+ }
+ }
+
+ return this.entry;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException( "FieldIndexHashTableFullIterator does not support remove()." );
+ }
+
+ /* (non-Javadoc)
+ * @see org.drools.util.Iterator#reset()
+ */
+ public void reset() {
+ this.table = this.hashTable.getTable();
+ this.length = this.table.length;
+ this.row = -1;
+ this.entry = null;
+ }
+ }
+
public Entry[] toArray() {
Entry[] result = new Entry[this.factSize];
int index = 0;
@@ -268,7 +331,7 @@
if ( entry == null ) {
entry = new RightTupleList( this.index,
- hashCode );
+ hashCode );
entry.next = this.table[index];
this.table[index] = entry;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleList.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleList.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/RightTupleList.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -12,187 +12,203 @@
import org.drools.reteoo.RightTupleMemory;
import org.drools.reteoo.LeftTuple;
import org.drools.util.AbstractHashTable.Index;
+import org.drools.util.LeftTupleList.TupleHashTableIterator;
public class RightTupleList
implements
- RightTupleMemory, Entry {
- private static final long serialVersionUID = 400L;
-
- public Entry previous;
- public Entry next;
-
- public RightTuple first;
- public RightTuple last;
-
- private int hashCode;
- private Index index;
-
- public RightTupleList() {
- // this is not an index bucket
- this.hashCode = 0;
- this.index = null;
+ RightTupleMemory,
+ Entry {
+ private static final long serialVersionUID = 400L;
+
+ public Entry previous;
+ public Entry next;
+
+ public RightTuple first;
+ public RightTuple last;
+
+ private int hashCode;
+ private Index index;
+
+ private TupleHashTableIterator iterator;
+
+ public RightTupleList() {
+ // this is not an index bucket
+ this.hashCode = 0;
+ this.index = null;
+ }
+
+ public RightTupleList(final Index index,
+ final int hashCode) {
+ this.index = index;
+ this.hashCode = hashCode;
+ }
+
+ public RightTuple getFirst(LeftTuple leftTuple) {
+ return this.first;
+ }
+
+ public RightTuple getLast(LeftTuple leftTuple) {
+ return this.last;
+ }
+
+ public void add(final RightTuple rightTuple) {
+ if ( this.first != null ) {
+ this.first.setPrevious( rightTuple );
+ rightTuple.setNext( this.first );
+ this.first = rightTuple;
+ } else {
+ this.first = rightTuple;
+ this.last = rightTuple;;
}
-
- public RightTupleList(final Index index,
- final int hashCode) {
- this.index = index;
- this.hashCode = hashCode;
+ }
+
+ /**
+ * We assume that this rightTuple is contained in this hash table
+ */
+ public void remove(final RightTuple rightTuple) {
+ RightTuple previous = (RightTuple) rightTuple.getPrevious();
+ RightTuple next = (RightTuple) rightTuple.getNext();
+
+ if ( previous != null && next != null ) {
+ // remove from middle
+ previous.setNext( next );
+ next.setPrevious( previous );
+ } else if ( next != null ) {
+ // remove from first
+ this.first = next;
+ next.setPrevious( null );
+ } else if ( previous != null ) {
+ // remove from end
+ this.last = previous;
+ previous.setNext( null );
+ } else {
+ // remove everything
+ this.last = null;
+ this.first = null;
}
-
- public RightTuple getFirst(LeftTuple leftTuple) {
- return this.first;
- }
-
- public RightTuple getLast(LeftTuple leftTuple) {
- return this.last;
- }
+ }
- public void add(final RightTuple rightTuple) {
- if ( this.first != null ) {
- this.first.setPrevious( rightTuple );
- rightTuple.setNext( this.first );
- this.first = rightTuple;
- } else {
- this.first = rightTuple;
- this.last = rightTuple;;
+ public RightTuple get(final InternalFactHandle handle) {
+ RightTuple current = this.first;
+ while ( current != null ) {
+ if ( handle == current.getFactHandle() ) {
+ return current;
}
+ current = (RightTuple) current.getNext();
}
-
- /**
- * We assume that this rightTuple is contained in this hash table
- */
- public void remove(final RightTuple rightTuple) {
- RightTuple previous = ( RightTuple ) rightTuple.getPrevious();
- RightTuple next = ( RightTuple ) rightTuple.getNext();
-
- if ( previous != null && next != null ) {
- // remove from middle
- previous.setNext( next );
- next.setPrevious( previous );
- } else if ( next != null ) {
- // remove from first
- this.first = next;
- next.setPrevious( null );
- } else if ( previous != null ) {
- // remove from end
- this.last = previous;
- previous.setNext( null );
- } else {
- // remove everything
- this.last = null;
- this.first = null;
- }
- }
-
- public RightTuple get(final InternalFactHandle handle) {
- RightTuple current = this.first;
- while ( current != null ) {
- if ( handle == current.getFactHandle() ) {
- return current;
- }
- current = (RightTuple) current.getNext();
+ return null;
+ }
+
+ public boolean contains(final InternalFactHandle handle) {
+ return get( handle ) != null;
+ }
+
+ public RightTuple get(final RightTuple rightTuple) {
+ InternalFactHandle handle = rightTuple.getFactHandle();
+ RightTuple current = this.first;
+ while ( current != null ) {
+ if ( handle == current.getFactHandle() ) {
+ return current;
}
- return null;
+ current = (RightTuple) current.getNext();
}
-
- public boolean contains(final InternalFactHandle handle) {
- return get( handle ) != null;
+ return null;
+ }
+
+ public boolean contains(final RightTuple rightTuple) {
+ return get( rightTuple ) != null;
+ }
+
+ public int size() {
+ int i = 0;
+ RightTuple current = this.first;
+ while ( current != null ) {
+ current = (RightTuple) current.getNext();
+ i++;
}
-
-
- public RightTuple get(final RightTuple rightTuple) {
- InternalFactHandle handle = rightTuple.getFactHandle();
- RightTuple current = this.first;
- while ( current != null ) {
- if ( handle == current.getFactHandle() ) {
- return current;
- }
- current = (RightTuple) current.getNext();
- }
- return null;
+ return i;
+ }
+
+ public Iterator iterator() {
+ if ( this.iterator == null ) {
+ this.iterator = new TupleHashTableIterator();
}
+ this.iterator.reset( this.first );
+ return this.iterator;
+ }
- public boolean contains(final RightTuple rightTuple) {
- return get( rightTuple ) != null;
+ public static class TupleHashTableIterator
+ implements
+ Iterator {
+ private RightTuple current;
+
+ public void reset(RightTuple first) {
+ this.current = first;
}
-
- public int size() {
- int i = 0;
- RightTuple current = this.first;
- while ( current != null ) {
- current = (RightTuple) current.getNext();
- i++;
+
+ public Object next() {
+ if ( this.current != null ) {
+ RightTuple returnValue = this.current;
+ this.current = (RightTuple) current.getNext();
+ return returnValue;
+ } else {
+ return null;
}
- return i;
}
- public boolean matches(final Object object,
- final int objectHashCode) {
- return this.hashCode == objectHashCode && this.index.equal( this.first.getFactHandle().getObject(),
- object );
+ public void remove() {
+ // do nothing
}
+ }
- public boolean matches(final LeftTuple tuple,
- final int tupleHashCode) {
- return this.hashCode == tupleHashCode && this.index.equal( this.first.getFactHandle().getObject(),
- tuple );
- }
+ public boolean matches(final Object object,
+ final int objectHashCode) {
+ return this.hashCode == objectHashCode && this.index.equal( this.first.getFactHandle().getObject(),
+ object );
+ }
- public int hashCode() {
- return this.hashCode;
- }
+ public boolean matches(final LeftTuple tuple,
+ final int tupleHashCode) {
+ return this.hashCode == tupleHashCode && this.index.equal( this.first.getFactHandle().getObject(),
+ tuple );
+ }
- public boolean equals(final Object object) {
- final RightTupleList other = (RightTupleList) object;
- return this.hashCode == other.hashCode && this.index == other.index;
- }
+ public int hashCode() {
+ return this.hashCode;
+ }
- public Entry getPrevious() {
- return null;
- // return this.previous;
- }
+ public boolean equals(final Object object) {
+ final RightTupleList other = (RightTupleList) object;
+ return this.hashCode == other.hashCode && this.index == other.index;
+ }
- public void setPrevious(Entry previous) {
- // this.previous = previous;
- }
-
- public Entry getNext() {
- return this.next;
- }
+ public Entry getPrevious() {
+ return null;
+ // return this.previous;
+ }
- public void setNext(final Entry next) {
- this.next = next;
- }
+ public void setPrevious(Entry previous) {
+ // this.previous = previous;
+ }
- public boolean isIndexed() {
- return ( this.index != null );
- }
-
- public String toString() {
- StringBuilder builder = new StringBuilder();
- for ( RightTuple rightTuple = ( RightTuple ) this.first; rightTuple != null; rightTuple = ( RightTuple ) rightTuple.getNext() ) {
- builder.append( rightTuple );
- }
-
- return builder.toString();
- }
+ public Entry getNext() {
+ return this.next;
+ }
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- previous = ( RightTuple ) in.readObject();
- next = ( RightTuple ) in.readObject();
- first = ( RightTuple ) in.readObject();
- last = ( RightTuple ) in.readObject();
- hashCode = in.readInt();
- index = ( Index ) in.readObject();
+ public void setNext(final Entry next) {
+ this.next = next;
+ }
+
+ public boolean isIndexed() {
+ return (this.index != null);
+ }
+
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ for ( RightTuple rightTuple = (RightTuple) this.first; rightTuple != null; rightTuple = (RightTuple) rightTuple.getNext() ) {
+ builder.append( rightTuple );
}
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject( previous );
- out.writeObject( next );
- out.writeObject( first );
- out.writeObject( last );
- out.writeInt( hashCode );
- out.writeObject( index );
- }
+ return builder.toString();
+ }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java 2008-05-18 03:08:35 UTC (rev 19997)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java 2008-05-18 03:11:26 UTC (rev 19998)
@@ -642,15 +642,15 @@
tuple2.getActivation() );
// "logical" should only appear once
- assertLength( 1,
- workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
+ assertEquals( 1,
+ workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
// retract the logical object
workingMemory.retract( logicalHandle2 );
// The logical object should never appear
- assertLength( 0,
- workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
+ assertEquals( 0,
+ workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
}
@@ -766,8 +766,8 @@
assertSame( logicalHandle1, logicalHandle2 );
// "logical" should only appear once
- assertLength( 1,
- workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
+ assertEquals( 1,
+ workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
// Now lets cancel the first activation
node2.retractLeftTuple( tuple2,
@@ -781,8 +781,8 @@
sink.getRetracted() );
// check "logical" is still in the system
- assertLength( 1,
- workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
+ assertEquals( 1,
+ workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
// now remove that final justification
node.retractLeftTuple( tuple1,
@@ -796,8 +796,8 @@
sink.getRetracted() );
// "logical" fact should no longer be in the system
- assertLength( 0,
- workingMemory.getTruthMaintenanceSystem().getJustifiedMap().values() );
+ assertEquals( 0,
+ workingMemory.getTruthMaintenanceSystem().getJustifiedMap().size() );
}
/**
More information about the jboss-svn-commits
mailing list