|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JoinMemory.java | 21.4% | 42.2% | 77.8% | 42.6% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: JoinMemory.java,v 1.54 2005/02/02 00:23:21 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2001-2003 (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 that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. (http://werken.com/) | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
30 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
39 | * POSSIBILITY OF SUCH DAMAGE. | |
40 | * | |
41 | */ | |
42 | ||
43 | import java.io.Serializable; | |
44 | import java.util.Iterator; | |
45 | import java.util.Set; | |
46 | ||
47 | import org.drools.rule.Declaration; | |
48 | ||
49 | /** | |
50 | * Memory for left and right inputs of a <code>JoinNode</code>. | |
51 | * | |
52 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
53 | * @see ReteTuple | |
54 | */ | |
55 | class JoinMemory | |
56 | implements | |
57 | Serializable | |
58 | { | |
59 | // ------------------------------------------------------------ | |
60 | // Instance members | |
61 | // ------------------------------------------------------------ | |
62 | ||
63 | /** Left-side tuples. */ | |
64 | private final TupleSet leftTuples; | |
65 | ||
66 | /** Right-side tuples. */ | |
67 | private final TupleSet rightTuples; | |
68 | ||
69 | /** Tuple column declarations. */ | |
70 | private final Set tupleDeclarations; | |
71 | ||
72 | /** Join column declarations. */ | |
73 | private final Set commonDeclarations; | |
74 | ||
75 | // ------------------------------------------------------------ | |
76 | // Constructors | |
77 | // ------------------------------------------------------------ | |
78 | ||
79 | /** | |
80 | * Construct. | |
81 | * | |
82 | * @param tupleDeclarations | |
83 | * @param commonDeclarations | |
84 | */ | |
85 | 52 | JoinMemory( Set tupleDeclarations, |
86 | Set commonDeclarations ) | |
87 | { | |
88 | 52 | this.leftTuples = new TupleSet( ); |
89 | 52 | this.rightTuples = new TupleSet( ); |
90 | 52 | this.tupleDeclarations = tupleDeclarations; |
91 | 52 | this.commonDeclarations = commonDeclarations; |
92 | } | |
93 | ||
94 | // ------------------------------------------------------------ | |
95 | // Instance methods | |
96 | // ------------------------------------------------------------ | |
97 | ||
98 | /** | |
99 | * Remove tuples from this memory. | |
100 | * | |
101 | * @param key The key for the tuples to be removed. | |
102 | * @return <code>true</code> if at least one tuple was removed; <code>false</code> otherwise. | |
103 | */ | |
104 | 1462 | boolean removeTuples(TupleKey key) |
105 | { | |
106 | // Single | doesn't short circuit. THIS IS IMPORTANT as we need to | |
107 | // ensure all relevant left AND right side tuples are removed. | |
108 | 1462 | return this.leftTuples.removeAllTuples( key ) |
109 | | this.rightTuples.removeAllTuples( key ); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Add a <code>ReteTuple</code> received from the <code>JoinNode's</code> | |
114 | * left input to the left side of this memory, and attempt to join to | |
115 | * existing <code>Tuples</code> in the right side. | |
116 | * | |
117 | * @param tuple The <code>Tuple</code> to add to the left side memory. | |
118 | * @return A <code>Set</code> of <code>Tuples</code> successfully | |
119 | * created by joining the incoming <code>tuple</code> against | |
120 | * existing <code>Tuples</code> on the right side memory. | |
121 | * @see JoinNode | |
122 | * @see ReteTuple | |
123 | */ | |
124 | 1169 | TupleSet addLeftTuple(ReteTuple tuple) |
125 | { | |
126 | 1169 | this.leftTuples.addTuple( tuple ); |
127 | ||
128 | 1169 | return attemptJoin( tuple, |
129 | this.rightTuples ); | |
130 | } | |
131 | ||
132 | /** | |
133 | * Add a <code>ReteTuple</code> received from the <code>JoinNode's</code> | |
134 | * right input to the right side of this memory, and attempt to join to | |
135 | * existing <code>Tuples</code> in the left side. | |
136 | * | |
137 | * @param tuple The <code>Tuple</code> to add to the right side memory. | |
138 | * @return A <code>Set</code> of <code>Tuples</code> successfully | |
139 | * created by joining the incoming <code>tuple</code> against | |
140 | * existing <code>Tuples</code> on the left side memory. | |
141 | * @see JoinNode | |
142 | * @see ReteTuple | |
143 | */ | |
144 | 926 | TupleSet addRightTuple(ReteTuple tuple) |
145 | { | |
146 | 926 | this.rightTuples.addTuple( tuple ); |
147 | ||
148 | 926 | return attemptJoin( tuple, |
149 | this.leftTuples ); | |
150 | } | |
151 | ||
152 | /** | |
153 | * Attempt to join a given <code>tuple</code> against all tuples in a specified <code>TupleSet</code>. | |
154 | * | |
155 | * @param tuple The <code>Tuple</code> to attempt joining. | |
156 | * @param tupleSet The Tuples to attempt joining to the parameter. | |
157 | * @return The newly joined tuples. | |
158 | */ | |
159 | 2095 | private TupleSet attemptJoin( ReteTuple tuple, |
160 | TupleSet tupleSet ) | |
161 | { | |
162 | 2095 | TupleSet newJoined = new TupleSet( tupleSet.size( ), 1 ); |
163 | ||
164 | 2095 | attemptJoin( tuple, |
165 | tupleSet, | |
166 | newJoined ); | |
167 | ||
168 | 2095 | return newJoined; |
169 | } | |
170 | ||
171 | /** | |
172 | * Attempt to join a given <code>tuple</code> against all tuples in a specified <code>TupleSet</code>. | |
173 | * | |
174 | * @param tuple The <code>Tuple</code> to attempt joining. | |
175 | * @param tupleSet The Tuples to attempt joining to the parameter. | |
176 | * @param newJoined The <code>TupleSet</code> into which newly joined tuples will be added. | |
177 | */ | |
178 | 2095 | private void attemptJoin( ReteTuple tuple, |
179 | TupleSet tupleSet, | |
180 | TupleSet newJoined ) | |
181 | { | |
182 | 2095 | Iterator tupleIter = tupleSet.iterator( ); |
183 | 2095 | while ( tupleIter.hasNext( ) ) |
184 | { | |
185 | 24797 | attemptJoin( tuple, |
186 | ( ReteTuple ) tupleIter.next( ), | |
187 | newJoined ); | |
188 | } | |
189 | } | |
190 | ||
191 | 24797 | private void attemptJoin( ReteTuple left, |
192 | ReteTuple right, | |
193 | TupleSet newJoined ) | |
194 | { | |
195 | 24797 | Iterator declIter = this.commonDeclarations.iterator( ); |
196 | 24797 | Declaration eachDecl; |
197 | ||
198 | 24797 | while ( declIter.hasNext( ) ) |
199 | { | |
200 | 0 | eachDecl = (Declaration) declIter.next( ); |
201 | ||
202 | 0 | if ( !left.getKey( ).get( eachDecl ).equals( right.getKey( ).get( eachDecl ) ) ) |
203 | { | |
204 | 0 | return; |
205 | } | |
206 | } | |
207 | ||
208 | 24797 | newJoined.addTuple( new ReteTuple( left, |
209 | right ) ); | |
210 | } | |
211 | ||
212 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
213 | // java.lang.Object | |
214 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
215 | ||
216 | /** | |
217 | * Produce debug string. | |
218 | * | |
219 | * @return The debug string. | |
220 | */ | |
221 | 0 | public String toString() |
222 | { | |
223 | 0 | return "[JoinMemory \n\tleft=" + this.leftTuples + "\n\tright=" + this.rightTuples + "]"; |
224 | } | |
225 | ||
226 | 0 | public void dump() |
227 | { | |
228 | 0 | System.err.println( "----" ); |
229 | 0 | ReteTuple tuple; |
230 | 0 | Iterator it1 = this.leftTuples.iterator( ); |
231 | 0 | while ( it1.hasNext( ) ) |
232 | { | |
233 | 0 | tuple = (ReteTuple) it1.next( ); |
234 | 0 | System.err.println( "tuple" ); |
235 | 0 | Iterator it2 = this.tupleDeclarations.iterator( ); |
236 | 0 | while ( it2.hasNext( ) ) |
237 | { | |
238 | 0 | Declaration decl = (Declaration) it2.next( ); |
239 | 0 | Object object = tuple.get( decl ); |
240 | 0 | TupleKey key = tuple.getKey( ); |
241 | 0 | System.err.println( "dump memory leftTuples:" + key.get( decl ) + ":" + decl + ":" + object ); |
242 | } | |
243 | } | |
244 | ||
245 | 0 | it1 = this.rightTuples.iterator( ); |
246 | 0 | while ( it1.hasNext( ) ) |
247 | { | |
248 | 0 | tuple = (ReteTuple) it1.next( ); |
249 | 0 | Iterator it2 = this.tupleDeclarations.iterator( ); |
250 | 0 | while ( it2.hasNext( ) ) |
251 | { | |
252 | 0 | Declaration decl = (Declaration) it2.next( ); |
253 | 0 | Object object = tuple.get( decl ); |
254 | 0 | TupleKey key = tuple.getKey( ); |
255 | 0 | System.err.println( "dump memory rightTuples:" + key.get( decl ) + ":" + decl + ":" + object ); |
256 | } | |
257 | } | |
258 | 0 | System.err.println( "----" ); |
259 | } | |
260 | } |
|