[jboss-svn-commits] JBL Code SVN: r6358 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Sep 22 10:47:46 EDT 2006
Author: mark.proctor at jboss.com
Date: 2006-09-22 10:47:44 -0400 (Fri, 22 Sep 2006)
New Revision: 6358
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java
Log:
JBRULES-498 Optimised HashMap impl
-Updated ReteTuple to have a better hashCode and equals method
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java 2006-09-22 14:47:44 UTC (rev 6357)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteTuple.java 2006-09-22 14:47:44 UTC (rev 6358)
@@ -1,27 +1,18 @@
package org.drools.reteoo;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalFactHandle;
-import org.drools.common.InternalWorkingMemory;
import org.drools.rule.Declaration;
import org.drools.spi.Activation;
-import org.drools.spi.PropagationContext;
import org.drools.spi.Tuple;
-import org.drools.util.BaseMultiLinkedListNode;
-import org.drools.util.LinkedList;
-import org.drools.util.LinkedListEntry;
-import org.drools.util.LinkedListNode;
+import org.drools.util.BaseEntry;
-public class ReteTuple extends BaseMultiLinkedListNode
+public class ReteTuple extends BaseEntry
implements
Tuple {
- private static final long serialVersionUID = -4221694077704683140L;
+ private static final long serialVersionUID = 320L;
private int index;
@@ -29,93 +20,40 @@
private ReteTuple parent;
- private final TupleSink sink;
-
- private LinkedList children;
-
- /** The <code>Map</code> of <code>FactHandleImpl</code> matches */
- private Map matches = Collections.EMPTY_MAP;
-
private Activation activation;
private long recency;
+
+ private int hashCode;
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
- public ReteTuple(final ReteTuple parentEntry) {
- this( parentEntry, null );
- }
- public ReteTuple(final InternalFactHandle handle,
- final TupleSink sink) {
+ public ReteTuple(final InternalFactHandle handle) {
this.index = 0;
this.parent = null;
this.recency = handle.getRecency();
this.handle = handle;
- this.sink = sink;
+ this.hashCode = handle.hashCode();
}
- public ReteTuple(final ReteTuple entry,
- final TupleSink sink) {
- this.index = entry.index;
- this.parent = entry.parent;
- this.recency = entry.recency;
- this.handle = entry.handle;
- this.sink = sink;
+ public ReteTuple(final ReteTuple tuple) {
+ this.index = tuple.index;
+ this.parent = tuple.parent;
+ this.recency = tuple.recency;
+ this.handle = tuple.handle;
+ this.hashCode = tuple.hashCode();
}
- public ReteTuple(final ReteTuple parentEntry,
- final InternalFactHandle handle,
- final TupleSink sink) {
- this.index = parentEntry.index + 1;
- this.parent = parentEntry;
- this.recency = parentEntry.recency + handle.getRecency();
+ public ReteTuple(final ReteTuple parentTuple,
+ final InternalFactHandle handle) {
+ this.index = parentTuple.index + 1;
+ this.parent = parentTuple;
+ this.recency = parentTuple.recency + handle.getRecency();
this.handle = handle;
- this.sink = sink;
+ this.hashCode = parentTuple.hashCode ^( handle.hashCode() * 31 );
}
- public void addChildEntry(ReteTuple entry) {
- if ( this.children == null ) {
- this.children = new LinkedList();
- }
- this.children.add( new LinkedListEntry( entry ) );
- }
-
- public void modifyChildEntries(PropagationContext context,
- InternalWorkingMemory workingMemory) {
- for ( LinkedListNode node = this.children.getFirst(); node != null; node = node.getNext() ) {
- ReteTuple tuple = (ReteTuple) ((LinkedListEntry) node).getObject();
- tuple.modifyTuple( context,
- workingMemory );
- }
- }
-
- public void retractChildEntries(PropagationContext context,
- InternalWorkingMemory workingMemory) {
- for ( LinkedListNode node = this.children.getFirst(); node != null; node = node.getNext() ) {
- ReteTuple tuple = (ReteTuple) ((LinkedListEntry) node).getObject();
- tuple.retractTuple( context,
- workingMemory );
- }
- }
-
- public void addTupleMatch(InternalFactHandle handle,
- TupleMatch tupleMatch) {
- if ( this.matches == Collections.EMPTY_MAP ) {
- this.matches = new HashMap();
- }
- this.matches.put( handle,
- tupleMatch );
- }
-
- public void clearChildEntries() {
- this.children.clear();
- }
-
- public void clearTupleMatches() {
- this.matches.clear();
- }
-
public InternalFactHandle get(int index) {
ReteTuple entry = this;
while ( entry.index != index ) {
@@ -136,10 +74,6 @@
return this.activation;
}
- public LinkedList getChildEntries() {
- return this.children;
- }
-
public InternalFactHandle[] getFactHandles() {
List list = new ArrayList();
ReteTuple entry = this;
@@ -155,48 +89,6 @@
return this.recency;
}
- public TupleMatch getTupleMatch(DefaultFactHandle handle) {
- return (TupleMatch) this.matches.get( handle );
- }
-
- public Map getTupleMatches() {
- return this.matches;
- }
-
- public TupleSink getTupleSink() {
- return this.sink;
- }
-
- public int matchesSize() {
- return this.matches.size();
- }
-
- public TupleMatch removeMatch(InternalFactHandle handle) {
- return (TupleMatch) this.matches.remove( handle );
- }
-
- public void retractTuple(PropagationContext context,
- InternalWorkingMemory workingMemory) {
- this.parent = null;
- this.sink.retractTuple( this,
- context,
- workingMemory );
- }
-
- public void assertTuple(PropagationContext context,
- InternalWorkingMemory workingMemory) {
- this.sink.assertTuple( this,
- context,
- workingMemory );
- }
-
- public void modifyTuple(PropagationContext context,
- InternalWorkingMemory workingMemory) {
- this.sink.modifyTuple( this,
- context,
- workingMemory );
- }
-
public void setActivation(Activation activation) {
this.activation = activation;
}
@@ -206,17 +98,31 @@
return this.handle.hashCode();
}
- public boolean equals(Object object) {
- if ( object == this ) {
+ /**
+ * We use this equals method to avoid the cast
+ * @param tuple
+ * @return
+ */
+ public boolean equals(ReteTuple other) {
+ // we know the object is never null and always of the type ReteTuple
+ if ( other == this ) {
return true;
}
- if ( object == null || object.getClass() == getClass() ) {
+ // A ReteTuple is only the same if it has the same hashCode, factId and parent
+ if ( this.hashCode != other.hashCode ) {
return false;
}
- ReteTuple other = ( ReteTuple ) object;
+ if ( this.handle.getId() != other.handle.getId() ) {
+ return false;
+ }
- return ( this.handle.getId() == other.handle.getId() );
+ return this.parent.equals( other.parent );
}
+
+ public boolean equals(Object object) {
+ // we know the object is never null and always of the type ReteTuple
+ return equals((ReteTuple)object);
+ }
}
More information about the jboss-svn-commits
mailing list