PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drools-core/src/test/java/org/drools/core/util/RightTupleIndexHashTableIteratorTest.java

https://gitlab.com/MichelZuniga/drools
Java | 255 lines | 184 code | 47 blank | 24 comment | 21 complexity | 8295260d014556e2e1102f8f6e91656b MD5 | raw file
  1. /*
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.core.util;
  17. import org.drools.core.RuleBaseConfiguration;
  18. import org.drools.core.base.ClassFieldAccessorCache;
  19. import org.drools.core.base.ClassFieldAccessorStore;
  20. import org.drools.core.base.ClassObjectType;
  21. import org.drools.core.base.evaluators.EvaluatorRegistry;
  22. import org.drools.core.base.evaluators.Operator;
  23. import org.drools.core.common.BetaConstraints;
  24. import org.drools.core.common.InternalFactHandle;
  25. import org.drools.core.common.SingleBetaConstraints;
  26. import org.drools.core.impl.InternalKnowledgeBase;
  27. import org.drools.core.reteoo.BetaMemory;
  28. import org.drools.core.reteoo.LeftTupleImpl;
  29. import org.drools.core.reteoo.NodeTypeEnums;
  30. import org.drools.core.reteoo.RightTuple;
  31. import org.drools.core.rule.Declaration;
  32. import org.drools.core.rule.MvelConstraintTestUtil;
  33. import org.drools.core.rule.Pattern;
  34. import org.drools.core.spi.BetaNodeFieldConstraint;
  35. import org.drools.core.spi.InternalReadAccessor;
  36. import org.drools.core.util.index.RightTupleIndexHashTable;
  37. import org.drools.core.util.index.RightTupleIndexHashTable.FieldIndexHashTableFullIterator;
  38. import org.drools.core.util.index.RightTupleList;
  39. import org.junit.Test;
  40. import org.kie.internal.KnowledgeBaseFactory;
  41. import org.kie.internal.runtime.StatefulKnowledgeSession;
  42. import java.util.ArrayList;
  43. import java.util.List;
  44. import static org.hamcrest.CoreMatchers.is;
  45. import static org.hamcrest.CoreMatchers.sameInstance;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertThat;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.when;
  50. public class RightTupleIndexHashTableIteratorTest {
  51. public static EvaluatorRegistry registry = new EvaluatorRegistry();
  52. @Test
  53. public void test1() {
  54. BetaNodeFieldConstraint constraint0 = getConstraint( "d", Operator.EQUAL, "this", Foo.class );
  55. BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
  56. RuleBaseConfiguration config = new RuleBaseConfiguration();
  57. BetaConstraints betaConstraints = null;
  58. betaConstraints = new SingleBetaConstraints(constraints, config);
  59. BetaMemory betaMemory = betaConstraints.createBetaMemory( config, NodeTypeEnums.JoinNode );
  60. InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
  61. StatefulKnowledgeSession ss = kBase.newStatefulKnowledgeSession();
  62. InternalFactHandle fh1 = (InternalFactHandle) ss.insert( new Foo( "brie", 1) );
  63. InternalFactHandle fh2 = (InternalFactHandle) ss.insert( new Foo( "brie", 1) );
  64. InternalFactHandle fh3 = (InternalFactHandle) ss.insert( new Foo( "soda", 1) );
  65. InternalFactHandle fh4 = (InternalFactHandle) ss.insert( new Foo( "soda", 1) );
  66. InternalFactHandle fh5 = (InternalFactHandle) ss.insert( new Foo( "bread", 3) );
  67. InternalFactHandle fh6 = (InternalFactHandle) ss.insert( new Foo( "bread", 3) );
  68. InternalFactHandle fh7 = (InternalFactHandle) ss.insert( new Foo( "cream", 3) );
  69. InternalFactHandle fh8 = (InternalFactHandle) ss.insert( new Foo( "gorda", 15) );
  70. InternalFactHandle fh9 = (InternalFactHandle) ss.insert( new Foo( "beer", 16) );
  71. InternalFactHandle fh10 = (InternalFactHandle) ss.insert( new Foo( "mars", 0) );
  72. InternalFactHandle fh11 = (InternalFactHandle) ss.insert( new Foo( "snicker", 0) );
  73. InternalFactHandle fh12 = (InternalFactHandle) ss.insert( new Foo( "snicker", 0) );
  74. InternalFactHandle fh13 = (InternalFactHandle) ss.insert( new Foo( "snicker", 0) );
  75. betaMemory.getRightTupleMemory().add( new RightTuple( fh1, null ) );
  76. betaMemory.getRightTupleMemory().add( new RightTuple( fh2, null ) );
  77. betaMemory.getRightTupleMemory().add( new RightTuple( fh3, null ) );
  78. betaMemory.getRightTupleMemory().add( new RightTuple( fh4, null ) );
  79. betaMemory.getRightTupleMemory().add( new RightTuple( fh5, null ) );
  80. betaMemory.getRightTupleMemory().add( new RightTuple( fh6, null ) );
  81. betaMemory.getRightTupleMemory().add( new RightTuple( fh7, null ) );
  82. betaMemory.getRightTupleMemory().add( new RightTuple( fh8, null ) );
  83. betaMemory.getRightTupleMemory().add( new RightTuple( fh9, null ) );
  84. RightTupleIndexHashTable hashTable = (RightTupleIndexHashTable) betaMemory.getRightTupleMemory();
  85. // can't create a 0 hashCode, so forcing
  86. RightTupleList rightTupleList = new RightTupleList();
  87. rightTupleList.add( new RightTuple( fh10, null) );
  88. hashTable.getTable()[0] = rightTupleList;
  89. rightTupleList = new RightTupleList();
  90. rightTupleList.add( new RightTuple( fh11, null ) );
  91. rightTupleList.add( new RightTuple( fh12, null ) );
  92. rightTupleList.add( new RightTuple( fh13, null ) );
  93. ((RightTupleList)hashTable.getTable()[0]).setNext( rightTupleList );
  94. Entry[] table = hashTable.getTable();
  95. List list = new ArrayList();
  96. for ( int i = 0; i < table.length; i++ ) {
  97. if ( table[i] != null ) {
  98. List entries = new ArrayList();
  99. entries.add( i );
  100. Entry entry = table[i];
  101. while ( entry != null ) {
  102. entries.add( entry );
  103. entry = entry.getNext();
  104. }
  105. list.add( entries.toArray() );
  106. }
  107. }
  108. assertEquals( 5, list.size() );
  109. Object[] entries = (Object[]) list.get( 0 );
  110. assertEquals( 0, entries[0]);
  111. assertEquals( 3, entries.length );
  112. entries = (Object[]) list.get( 1 );
  113. assertEquals( 102, entries[0]);
  114. assertEquals( 2, entries.length );
  115. entries = (Object[]) list.get( 2 );
  116. assertEquals( 103, entries[0]);
  117. assertEquals( 2, entries.length );
  118. entries = (Object[]) list.get( 3 );
  119. assertEquals( 115, entries[0]);
  120. assertEquals( 3, entries.length );
  121. entries = (Object[]) list.get( 4 );
  122. assertEquals( 117, entries[0]);
  123. assertEquals( 3, entries.length );
  124. //System.out.println( entries );
  125. list = new ArrayList<LeftTupleImpl>();
  126. Iterator it = betaMemory.getRightTupleMemory().iterator();
  127. for ( RightTuple rightTuple = ( RightTuple ) it.next(); rightTuple != null; rightTuple = ( RightTuple ) it.next() ) {
  128. list.add( rightTuple );
  129. }
  130. assertEquals( 13, list.size() );
  131. }
  132. @Test
  133. public void testLastBucketInTheTable() {
  134. // JBRULES-2574
  135. // setup the entry array with an element in the first bucket, one
  136. // in the middle and one in the last bucket
  137. Entry[] entries = new Entry[10];
  138. entries[0] = mock( RightTupleList.class );
  139. entries[5] = mock( RightTupleList.class );
  140. entries[9] = mock( RightTupleList.class );
  141. RightTuple[] tuples = new RightTuple[]{mock( RightTuple.class ), mock( RightTuple.class ), mock( RightTuple.class )};
  142. // set return values for methods
  143. when( entries[0].getNext() ).thenReturn( null );
  144. when( ((RightTupleList) entries[0]).getFirst() ).thenReturn( tuples[0] );
  145. when( entries[5].getNext() ).thenReturn( null );
  146. when( ((RightTupleList) entries[5]).getFirst( ) ).thenReturn( tuples[1] );
  147. when( entries[9].getNext() ).thenReturn( null );
  148. when( ((RightTupleList) entries[9]).getFirst( ) ).thenReturn( tuples[2] );
  149. // create the mock table for the iterator
  150. AbstractHashTable table = mock( AbstractHashTable.class );
  151. when( table.getTable() ).thenReturn( entries );
  152. // create the iterator
  153. FieldIndexHashTableFullIterator iterator = new FieldIndexHashTableFullIterator( table );
  154. // test it
  155. assertThat( iterator.next(),
  156. sameInstance( (Object) tuples[0] ) );
  157. assertThat( iterator.next(),
  158. sameInstance( (Object) tuples[1] ) );
  159. assertThat( iterator.next(),
  160. sameInstance( (Object) tuples[2] ) );
  161. assertThat( iterator.next(),
  162. is( (Object) null ) );
  163. }
  164. protected BetaNodeFieldConstraint getConstraint(String identifier,
  165. Operator operator,
  166. String fieldName,
  167. Class clazz) {
  168. ClassFieldAccessorStore store = new ClassFieldAccessorStore();
  169. store.setClassFieldAccessorCache( new ClassFieldAccessorCache( Thread.currentThread().getContextClassLoader() ) );
  170. store.setEagerWire( true );
  171. InternalReadAccessor extractor = store.getReader( clazz,
  172. fieldName,
  173. getClass().getClassLoader() );
  174. Declaration declaration = new Declaration( identifier,
  175. extractor,
  176. new Pattern( 0,
  177. new ClassObjectType( clazz ) ) );
  178. String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
  179. return new MvelConstraintTestUtil(expression, declaration, extractor);
  180. }
  181. public static class Foo {
  182. private String val;
  183. private int hashCode;
  184. public Foo(String val, int hashCode) {
  185. this.val = val;
  186. this.hashCode = hashCode;
  187. }
  188. public String getVal() {
  189. return val;
  190. }
  191. @Override
  192. public int hashCode() {
  193. return hashCode;
  194. }
  195. @Override
  196. public boolean equals(Object obj) {
  197. if ( this == obj ) return true;
  198. if ( obj == null ) return false;
  199. if ( getClass() != obj.getClass() ) return false;
  200. Foo other = (Foo) obj;
  201. if ( hashCode != other.hashCode ) return false;
  202. if ( val == null ) {
  203. if ( other.val != null ) return false;
  204. } else if ( !val.equals( other.val ) ) return false;
  205. return true;
  206. }
  207. }
  208. }