View Javadoc
1 package org.drools.reteoo.impl; 2 3 /* 4 $Id: TupleSet.java,v 1.2 2002/07/28 15:49:50 bob Exp $ 5 6 Copyright 2002 (C) The Werken Company. All Rights Reserved. 7 8 Redistribution and use of this software and associated documentation 9 ("Software"), with or without modification, are permitted provided 10 that the following conditions are met: 11 12 1. Redistributions of source code must retain copyright 13 statements and notices. Redistributions must also contain a 14 copy of this document. 15 16 2. Redistributions in binary form must reproduce the 17 above copyright notice, this list of conditions and the 18 following disclaimer in the documentation and/or other 19 materials provided with the distribution. 20 21 3. The name "drools" must not be used to endorse or promote 22 products derived from this Software without prior written 23 permission of The Werken Company. For written permission, 24 please contact bob@werken.com. 25 26 4. Products derived from this Software may not be called "drools" 27 nor may "drools" appear in their names without prior written 28 permission of The Werken Company. "drools" is a registered 29 trademark of The Werken Company. 30 31 5. Due credit should be given to The Werken Company. 32 (http://drools.werken.com/). 33 34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 OF THE POSSIBILITY OF SUCH DAMAGE. 46 47 */ 48 49 import java.util.Map; 50 import java.util.HashMap; 51 import java.util.Set; 52 import java.util.HashSet; 53 import java.util.Iterator; 54 import java.util.NoSuchElementException; 55 56 /*** A set of <code>Tuple<code>s indexed by <code>TupleKey<code>s. 57 * 58 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 59 */ 60 class TupleSet 61 { 62 // ------------------------------------------------------------ 63 // Instance members 64 // ------------------------------------------------------------ 65 66 /*** Tuples, indexed by TupleKey. */ 67 private Map tuples; 68 69 // ------------------------------------------------------------ 70 // Constructors 71 // ------------------------------------------------------------ 72 73 /*** Construct. 74 */ 75 TupleSet() 76 { 77 this.tuples = new HashMap(); 78 } 79 80 /*** Construct with a single tuple. 81 * 82 * @param tuple The tuple. 83 */ 84 TupleSet(ReteTuple tuple) 85 { 86 this( 1 ); 87 addTuple( tuple ); 88 } 89 90 /*** Construct with a set of tuples. 91 * 92 * @param tuples The tuples. 93 */ 94 TupleSet(Set tuples) 95 { 96 this.tuples = new HashMap( tuples.size() ); 97 98 addAllTuples( tuples ); 99 } 100 101 /*** Construct with a size hint. 102 * 103 * @param sizeHint Hint as to desired size. 104 */ 105 TupleSet(int sizeHint) 106 { 107 this.tuples = new HashMap( sizeHint ); 108 } 109 110 /*** Retrieve the size (number of tuples) in this set. 111 * 112 * @return The size of this set. 113 */ 114 public int size() 115 { 116 return this.tuples.size(); 117 } 118 119 /*** Add a <code>Set</code> of <code>Tuple</code>s to this set. 120 * 121 * @param tuples The tuples. 122 */ 123 public void addAllTuples(Set tuples) 124 { 125 Iterator tupleIter = tuples.iterator(); 126 ReteTuple eachTuple = null; 127 128 while ( tupleIter.hasNext() ) 129 { 130 eachTuple = (ReteTuple) tupleIter.next(); 131 132 addTuple( eachTuple ); 133 } 134 } 135 136 /*** Add a <code>TupleSet</code> of <code>Tuple<code>s to this set. 137 * 138 * @param tupleSet The tuple set. 139 */ 140 public void addAllTuples(TupleSet tupleSet) 141 { 142 this.tuples.putAll( tupleSet.tuples ); 143 } 144 145 /*** Add a single <code>Tuple</code> to this set. 146 * 147 * @param tuple The tuple. 148 */ 149 public void addTuple(ReteTuple tuple) 150 { 151 this.tuples.put( tuple.getKey(), 152 tuple ); 153 } 154 155 /*** Remove a tuple from this set. 156 * 157 * @param key Key matching the tuple. 158 */ 159 public void removeTuple(TupleKey key) 160 { 161 this.tuples.remove( key ); 162 } 163 164 /*** Remove several tuples matching a subsset key. 165 * 166 * @param key The partial key to match. 167 */ 168 public void removeTuplesByPartialKey(TupleKey key) 169 { 170 Iterator tupleIter = iterator(); 171 ReteTuple eachTuple = null; 172 173 while ( tupleIter.hasNext() ) 174 { 175 eachTuple = (ReteTuple) tupleIter.next(); 176 177 if ( eachTuple.getKey().containsAll( key ) ) 178 { 179 tupleIter.remove(); 180 } 181 } 182 } 183 184 /*** Retrieve all <code>Tuple</code>s. 185 * 186 * @see org.drools.spi.Tuple 187 * 188 * @return The set of tuples. 189 */ 190 public Set getTuples() 191 { 192 return new HashSet( this.tuples.values() ); 193 } 194 195 /*** Retrieve an iterator over the tuples. 196 * 197 * @return The iterator. 198 */ 199 public Iterator iterator() 200 { 201 return new Itr( this.tuples ); 202 } 203 204 /*** Retrieve all tuples related to a specified 205 * root fact object. 206 * 207 * @param rootFactObject The root fact object. 208 * 209 * @return Matching tuples. 210 */ 211 public Set getTuples(Object rootFactObject) 212 { 213 Set matchingTuples = new HashSet(); 214 215 Iterator keyIter = getKeys().iterator(); 216 TupleKey eachKey = null; 217 218 while ( keyIter.hasNext() ) 219 { 220 eachKey = (TupleKey) keyIter.next(); 221 222 if ( eachKey.containsRootFactObject( rootFactObject ) ) 223 { 224 matchingTuples.add( getTuple( eachKey ) ); 225 } 226 } 227 228 return matchingTuples; 229 } 230 231 /*** Retriave all <code>TupleKey<code>s. 232 * 233 * @see TupleKey 234 * 235 * @return The set of tuple keys. 236 */ 237 public Set getKeys() 238 { 239 return this.tuples.keySet(); 240 } 241 242 /*** Retrieve a <code>Tuple</code> by <code>TupleKey</code>. 243 * 244 * @see org.drools.spi.Tuple 245 * @see #containsTuple 246 * 247 * @param key The tuple key. 248 * 249 * @return The matching tuple or <code>null</code> if this 250 * set contains no matching tuple. 251 */ 252 public ReteTuple getTuple(TupleKey key) 253 { 254 return (ReteTuple) this.tuples.get( key ); 255 } 256 257 /*** Determine if this set contains a <code>Tuple</code> matching 258 * the specified <code>TupleKey</code>. 259 * 260 * @param key The tuple key. 261 * 262 * @return <code>true</code> if a matching tuple exists within 263 * this set, otherwise <code>false<code>. 264 */ 265 public boolean containsTuple(TupleKey key) 266 { 267 return this.tuples.containsKey( key ); 268 } 269 270 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 271 // java.lang.Object 272 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 273 274 /*** Produce a debug string. 275 * 276 * @return The debug string. 277 */ 278 public String toString() 279 { 280 return "[TupleSet tuple=" + this.tuples + "]"; 281 } 282 283 } 284 285 /*** Iterator over tuples. 286 * 287 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 288 */ 289 class Itr implements Iterator 290 { 291 // ------------------------------------------------------------ 292 // Instance members 293 // ------------------------------------------------------------ 294 295 /*** Tuples. */ 296 private Map tuples; 297 298 /*** Internal iterator. */ 299 private Iterator keyIter; 300 301 // ------------------------------------------------------------ 302 // Constructors 303 // ------------------------------------------------------------ 304 305 /*** Construct. 306 * 307 * @param tuples The tuples. 308 */ 309 Itr(Map tuples) 310 { 311 this.tuples = tuples; 312 this.keyIter = tuples.keySet().iterator(); 313 } 314 315 // ------------------------------------------------------------ 316 // Instance methods 317 // ------------------------------------------------------------ 318 319 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 320 // java.util.Iterator 321 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 322 323 /*** Determine if this iterator has an element in the next position. 324 * 325 * @return <code>true</code> if an element exists in the next 326 * position, otherwise <code>false</code>. 327 */ 328 public boolean hasNext() 329 { 330 return this.keyIter.hasNext(); 331 } 332 333 /*** Retrieve the element in the next position. 334 * 335 * @return The element in the next position. 336 * 337 * @throws NoSuchElementException If there exists no element 338 * in the next position. 339 */ 340 public Object next() throws NoSuchElementException 341 { 342 return this.tuples.get( this.keyIter.next() ); 343 } 344 345 /*** Remove the element at the current position. 346 */ 347 public void remove() 348 { 349 this.keyIter.remove(); 350 } 351 352 353 }

This page was automatically generated by Maven