[jboss-svn-commits] JBL Code SVN: r19279 - labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Mar 27 23:27:19 EDT 2008
Author: mark.proctor at jboss.com
Date: 2008-03-27 23:27:19 -0400 (Thu, 27 Mar 2008)
New Revision: 19279
Modified:
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexHashTableTest.java
Log:
JBRULES-1520 RightTuple merge for asymmetrical Rete propagations
-Fixed FieldIndexHashTableTest
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexHashTableTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexHashTableTest.java 2008-03-28 00:40:01 UTC (rev 19278)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/util/FieldIndexHashTableTest.java 2008-03-28 03:27:19 UTC (rev 19279)
@@ -15,16 +15,16 @@
import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalFactHandle;
import org.drools.reteoo.LeftTuple;
+import org.drools.reteoo.RightTuple;
import org.drools.rule.Declaration;
import org.drools.rule.Pattern;
import org.drools.spi.FieldExtractor;
import org.drools.util.AbstractHashTable.FactEntryImpl;
import org.drools.util.AbstractHashTable.FieldIndex;
-import org.drools.util.RightTupleIndexHashTable.FieldIndexEntry;
import org.drools.util.ObjectHashMap.ObjectEntry;
public class FieldIndexHashTableTest extends TestCase {
- ClassFieldExtractorCache cache = ClassFieldExtractorCache.getInstance();
+ ClassFieldExtractorCache cache = ClassFieldExtractorCache.getInstance();
EqualityEvaluatorsDefinition equals = new EqualityEvaluatorsDefinition();
public void testSingleEntry() throws Exception {
@@ -41,7 +41,8 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.STRING_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.STRING_TYPE,
+ Operator.EQUAL ) );
final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
@@ -52,14 +53,17 @@
assertEquals( 0,
map.size() );
- assertNull( map.get( new LeftTuple( cheddarHandle1 ) ) );
+ assertNull( map.get( new LeftTuple( cheddarHandle1,
+ null ) ) );
final Cheese stilton1 = new Cheese( "stilton",
35 );
- final InternalFactHandle stiltonHandle1 = new DefaultFactHandle( 1,
- stilton1 );
- map.add( stiltonHandle1 );
+ RightTuple stiltonRighTuple = new RightTuple( new DefaultFactHandle( 1,
+ stilton1 ),
+ null );
+ map.add( stiltonRighTuple );
+
assertEquals( 1,
map.size() );
assertEquals( 1,
@@ -70,10 +74,11 @@
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle( 2,
stilton2 );
- final FieldIndexEntry stiltonEntry = map.get( new LeftTuple( stiltonHandle2 ) );
- assertSame( stiltonHandle1,
- stiltonEntry.getFirst().getFactHandle() );
- assertNull( stiltonEntry.getFirst().getNext() );
+ final RightTupleList list = map.get( new LeftTuple( stiltonHandle2,
+ null ) );
+ assertSame( stiltonRighTuple.getFactHandle(),
+ list.first.getFactHandle() );
+ assertNull( list.first.getNext() );
}
public void testTwoDifferentEntries() throws Exception {
@@ -90,7 +95,8 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.STRING_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.STRING_TYPE,
+ Operator.EQUAL ) );
final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
@@ -101,13 +107,15 @@
35 );
final InternalFactHandle stiltonHandle1 = new DefaultFactHandle( 1,
stilton1 );
- map.add( stiltonHandle1 );
+ map.add( new RightTuple( stiltonHandle1,
+ null ) );
final Cheese cheddar1 = new Cheese( "cheddar",
35 );
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle( 2,
cheddar1 );
- map.add( cheddarHandle1 );
+ map.add( new RightTuple( cheddarHandle1,
+ null ) );
assertEquals( 2,
map.size() );
@@ -118,19 +126,21 @@
77 );
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle( 2,
stilton2 );
- final FieldIndexEntry stiltonEntry = map.get( new LeftTuple( stiltonHandle2 ) );
+ RightTupleList list = map.get( new LeftTuple( stiltonHandle2,
+ null ) );
assertSame( stiltonHandle1,
- stiltonEntry.getFirst().getFactHandle() );
- assertNull( stiltonEntry.getFirst().getNext() );
+ list.first.getFactHandle() );
+ assertNull( list.first.getNext() );
final Cheese cheddar2 = new Cheese( "cheddar",
5 );
final InternalFactHandle cheddarHandle2 = new DefaultFactHandle( 2,
cheddar2 );
- final FieldIndexEntry cheddarEntry = map.get( new LeftTuple( cheddarHandle2 ) );
+ list = map.get( new LeftTuple( cheddarHandle2,
+ null ) );
assertSame( cheddarHandle1,
- cheddarEntry.getFirst().getFactHandle() );
- assertNull( cheddarEntry.getFirst().getNext() );
+ list.first.getFactHandle() );
+ assertNull( list.first.getNext() );
}
public void testTwoEqualEntries() throws Exception {
@@ -147,7 +157,8 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.STRING_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.STRING_TYPE,
+ Operator.EQUAL ) );
final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
@@ -158,19 +169,22 @@
35 );
final InternalFactHandle stiltonHandle1 = new DefaultFactHandle( 1,
stilton1 );
- map.add( stiltonHandle1 );
+ map.add( new RightTuple( stiltonHandle1,
+ null ) );
final Cheese cheddar1 = new Cheese( "cheddar",
35 );
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle( 2,
cheddar1 );
- map.add( cheddarHandle1 );
+ map.add( new RightTuple( cheddarHandle1,
+ null ) );
final Cheese stilton2 = new Cheese( "stilton",
81 );
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle( 3,
stilton2 );
- map.add( stiltonHandle2 );
+ map.add( new RightTuple( stiltonHandle2,
+ null ) );
assertEquals( 3,
map.size() );
@@ -183,11 +197,12 @@
final InternalFactHandle stiltonHandle3 = new DefaultFactHandle( 4,
stilton2 );
- final FieldIndexEntry stiltonEntry = map.get( new LeftTuple( stiltonHandle3 ) );
+ final RightTupleList list = map.get( new LeftTuple( stiltonHandle3,
+ null ) );
assertSame( stiltonHandle2,
- stiltonEntry.getFirst().getFactHandle() );
+ list.first.getFactHandle() );
assertSame( stiltonHandle1,
- ((FactEntryImpl) stiltonEntry.getFirst().getNext()).getFactHandle() );
+ ((RightTuple) list.first.getNext()).getFactHandle() );
}
public void testTwoDifferentEntriesSameHashCode() throws Exception {
@@ -204,7 +219,8 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.OBJECT_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.OBJECT_TYPE,
+ Operator.EQUAL ) );
final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
@@ -215,14 +231,16 @@
final InternalFactHandle ch1 = new DefaultFactHandle( 1,
c1 );
- map.add( ch1 );
+ map.add( new RightTuple( ch1,
+ null ) );
final TestClass c2 = new TestClass( 0,
new TestClass( 20,
"cheddar" ) );
final InternalFactHandle ch2 = new DefaultFactHandle( 2,
c2 );
- map.add( ch2 );
+ map.add( new RightTuple( ch2,
+ null ) );
// same hashcode, but different values, so it should result in a size of 2
assertEquals( 2,
@@ -233,8 +251,18 @@
tablePopulationSize( map ) );
// this table bucket will have two FieldIndexEntries, as they are actually two different values
- final FieldIndexEntry entry = (FieldIndexEntry) getEntries( map )[0];
+ Entry[] entries = getEntries( map );
+ assertEquals( 1,
+ entries.length );
+ RightTupleList list = (RightTupleList) entries[0];
+ assertSame( ch2,
+ list.first.getFactHandle() );
+ assertNull( list.first.getNext() );
+ assertSame( ch1,
+ ((RightTupleList) list.next).first.getFactHandle() );
+ assertNull( ((RightTupleList) list.next).first.getNext() );
+ assertNull( ((RightTupleList) list.next).next );
}
public void testRemove() throws Exception {
@@ -251,7 +279,8 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.STRING_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.STRING_TYPE,
+ Operator.EQUAL ) );
final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
@@ -262,42 +291,48 @@
35 );
final InternalFactHandle stiltonHandle1 = new DefaultFactHandle( 1,
stilton1 );
- map.add( stiltonHandle1 );
+ RightTuple stiltonRightTuple1 = new RightTuple( stiltonHandle1,
+ null );
+ map.add( stiltonRightTuple1 );
final Cheese cheddar1 = new Cheese( "cheddar",
35 );
final InternalFactHandle cheddarHandle1 = new DefaultFactHandle( 2,
cheddar1 );
- map.add( cheddarHandle1 );
+ RightTuple cheddarRightTuple1 = new RightTuple( cheddarHandle1,
+ null );
+ map.add( cheddarRightTuple1 );
final Cheese stilton2 = new Cheese( "stilton",
81 );
final InternalFactHandle stiltonHandle2 = new DefaultFactHandle( 3,
stilton2 );
- map.add( stiltonHandle2 );
+ RightTuple stiltonRightTuple2 = new RightTuple( stiltonHandle2,
+ null );
+ map.add( stiltonRightTuple2 );
assertEquals( 3,
map.size() );
assertEquals( 2,
tablePopulationSize( map ) );
- // cheddar is in its own buccket, which should be removed once empty. We cannot have
+ // cheddar is in its own bucket, which should be removed once empty. We cannot have
// empty FieldIndexEntries in the Map, as they get their value from the first FactEntry.
- map.remove( cheddarHandle1 );
+ map.remove( cheddarRightTuple1 );
assertEquals( 2,
map.size() );
assertEquals( 1,
tablePopulationSize( map ) );
// We remove t he stiltonHandle2, but there is still one more stilton, so size should be the same
- map.remove( stiltonHandle2 );
+ map.remove( stiltonRightTuple2 );
assertEquals( 1,
map.size() );
assertEquals( 1,
tablePopulationSize( map ) );
// No more stiltons, so the table should be empty
- map.remove( stiltonHandle1 );
+ map.remove( stiltonRightTuple1 );
assertEquals( 0,
map.size() );
assertEquals( 0,
@@ -318,92 +353,93 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.STRING_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.STRING_TYPE,
+ Operator.EQUAL ) );
- final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
+ final RightTupleIndexHashTable map = new RightTupleIndexHashTable( 16, 0.75f, new FieldIndex[]{fieldIndex} );
assertEquals( 0,
map.size() );
final Cheese stilton1 = new Cheese( "stilton",
35 );
- map.add( new DefaultFactHandle( 1,
- stilton1 ) );
+ map.add( newRightTuple( 1,
+ stilton1 ) );
final Cheese stilton2 = new Cheese( "stilton",
81 );
- map.add( new DefaultFactHandle( 2,
- stilton2 ) );
+ map.add( newRightTuple( 2,
+ stilton2 ) );
final Cheese cheddar1 = new Cheese( "cheddar",
35 );
- map.add( new DefaultFactHandle( 3,
- cheddar1 ) );
+ map.add( newRightTuple( 3,
+ cheddar1 ) );
final Cheese cheddar2 = new Cheese( "cheddar",
38 );
- map.add( new DefaultFactHandle( 4,
- cheddar2 ) );
+ map.add( newRightTuple( 4,
+ cheddar2 ) );
final Cheese brie = new Cheese( "brie",
293 );
- map.add( new DefaultFactHandle( 5,
- brie ) );
+ map.add( newRightTuple( 5,
+ brie ) );
final Cheese mozerella = new Cheese( "mozerella",
15 );
- map.add( new DefaultFactHandle( 6,
- mozerella ) );
+ map.add( newRightTuple( 6,
+ mozerella ) );
final Cheese dolcelatte = new Cheese( "dolcelatte",
284 );
- map.add( new DefaultFactHandle( 7,
- dolcelatte ) );
+ map.add( newRightTuple( 7,
+ dolcelatte ) );
final Cheese camembert1 = new Cheese( "camembert",
924 );
- map.add( new DefaultFactHandle( 8,
- camembert1 ) );
+ map.add( newRightTuple( 8,
+ camembert1 ) );
final Cheese camembert2 = new Cheese( "camembert",
765 );
- map.add( new DefaultFactHandle( 9,
- camembert2 ) );
+ map.add( newRightTuple( 9,
+ camembert2 ) );
final Cheese redLeicestor = new Cheese( "red leicestor",
23 );
- map.add( new DefaultFactHandle( 10,
- redLeicestor ) );
+ map.add( newRightTuple( 10,
+ redLeicestor ) );
final Cheese wensleydale = new Cheese( "wensleydale",
20 );
- map.add( new DefaultFactHandle( 11,
- wensleydale ) );
+ map.add( newRightTuple( 11,
+ wensleydale ) );
final Cheese edam = new Cheese( "edam",
12 );
- map.add( new DefaultFactHandle( 12,
- edam ) );
+ map.add( newRightTuple( 12,
+ edam ) );
final Cheese goude1 = new Cheese( "goude",
93 );
- map.add( new DefaultFactHandle( 13,
- goude1 ) );
+ map.add( newRightTuple( 13,
+ goude1 ) );
final Cheese goude2 = new Cheese( "goude",
88 );
- map.add( new DefaultFactHandle( 14,
- goude2 ) );
+ map.add( newRightTuple( 14,
+ goude2 ) );
final Cheese gruyere = new Cheese( "gruyere",
82 );
- map.add( new DefaultFactHandle( 15,
- gruyere ) );
+ map.add( newRightTuple( 15,
+ gruyere ) );
final Cheese emmental = new Cheese( "emmental",
98 );
- map.add( new DefaultFactHandle( 16,
- emmental ) );
+ map.add( newRightTuple( 16,
+ emmental ) );
// At this point we have 16 facts but only 12 different types of cheeses
// so no table resize and thus its size is 16
@@ -417,8 +453,8 @@
final Cheese feta = new Cheese( "feta",
48 );
- map.add( new DefaultFactHandle( 2,
- feta ) );
+ map.add( newRightTuple( 2,
+ feta ) );
// This adds our 13th type of cheese. The map is set with an initial capacity of 16 and
// a threshold of 75%, that after 12 it should resize the map to 32.
@@ -431,16 +467,24 @@
final Cheese haloumi = new Cheese( "haloumi",
48 );
- map.add( new DefaultFactHandle( 2,
- haloumi ) );
+ map.add( newRightTuple( 2,
+ haloumi ) );
final Cheese chevre = new Cheese( "chevre",
48 );
- map.add( new DefaultFactHandle( 2,
- chevre ) );
+ map.add( newRightTuple( 2,
+ chevre ) );
}
+ private RightTuple newRightTuple(int id,
+ Object object) {
+ return new RightTuple( new DefaultFactHandle( id,
+ object ),
+ null );
+
+ }
+
public static class TestClass {
private int hashCode;
private Object object;
@@ -535,7 +579,8 @@
final FieldIndex fieldIndex = new FieldIndex( extractor,
declaration,
- equals.getEvaluator( ValueType.STRING_TYPE, Operator.EQUAL ) );
+ equals.getEvaluator( ValueType.STRING_TYPE,
+ Operator.EQUAL ) );
final RightTupleIndexHashTable map = new RightTupleIndexHashTable( new FieldIndex[]{fieldIndex} );
@@ -543,11 +588,8 @@
55 );
final InternalFactHandle stiltonHandle = new DefaultFactHandle( 2,
stilton );
-
- final Iterator it = map.iterator( new LeftTuple( stiltonHandle ) );
- for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
- fail( "Map is empty, there should be no iteration" );
- }
+
+ assertNull( map.getFirst( new LeftTuple( stiltonHandle, null ) ) );
}
}
More information about the jboss-svn-commits
mailing list