|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JoinNode.java | 92.9% | 97% | 92.3% | 95% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: JoinNode.java,v 1.44 2005/02/02 00:23:22 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.util.Collections; | |
44 | import java.util.HashSet; | |
45 | import java.util.Iterator; | |
46 | import java.util.Set; | |
47 | ||
48 | import org.drools.AssertionException; | |
49 | import org.drools.RetractionException; | |
50 | import org.drools.rule.Declaration; | |
51 | ||
52 | /** | |
53 | * A two-input Rete-OO <i>join node </i>. | |
54 | * | |
55 | * @see org.drools.reteoo.TupleSource | |
56 | * @see org.drools.reteoo.TupleSink | |
57 | * | |
58 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
59 | */ | |
60 | class JoinNode extends TupleSource | |
61 | { | |
62 | // ------------------------------------------------------------ | |
63 | // Instance members | |
64 | // ------------------------------------------------------------ | |
65 | ||
66 | /** The left input <code>TupleSource</code>. */ | |
67 | private final TupleSource leftInput; | |
68 | ||
69 | /** The right input <code>TupleSource</code>. */ | |
70 | private final TupleSource rightInput; | |
71 | ||
72 | /** <code>Declarations</code> in both left and right input sources combined. */ | |
73 | private final Set tupleDeclarations; | |
74 | ||
75 | /** <code>Declarations</code> common to both left and right input sources. */ | |
76 | private final Set commonDeclarations; | |
77 | ||
78 | // ------------------------------------------------------------ | |
79 | // Constructors | |
80 | // ------------------------------------------------------------ | |
81 | ||
82 | /** | |
83 | * Construct. | |
84 | * | |
85 | * @param leftInput The left input <code>TupleSource</code>. | |
86 | * @param rightInput The right input <code>TupleSource</code>. | |
87 | */ | |
88 | 97 | public JoinNode( TupleSource leftInput, |
89 | TupleSource rightInput ) | |
90 | { | |
91 | 97 | this.leftInput = leftInput; |
92 | 97 | this.rightInput = rightInput; |
93 | 97 | this.tupleDeclarations = determineTupleDeclarations( ); |
94 | 97 | this.commonDeclarations = determineCommonDeclarations( ); |
95 | } | |
96 | ||
97 | // ------------------------------------------------------------ | |
98 | // Instance methods | |
99 | // ------------------------------------------------------------ | |
100 | ||
101 | /** | |
102 | * Set up the <code>Set</code> of common <code>Declarations</code> | |
103 | * across the two input <code>TupleSources</code>. | |
104 | */ | |
105 | 97 | private Set determineCommonDeclarations() |
106 | { | |
107 | 97 | Set commonDeclarations = new HashSet( this.leftInput.getTupleDeclarations( ) ); |
108 | ||
109 | 97 | commonDeclarations.retainAll( this.rightInput.getTupleDeclarations( ) ); |
110 | ||
111 | 97 | return commonDeclarations.isEmpty( ) ? Collections.EMPTY_SET : Collections.unmodifiableSet( commonDeclarations ); |
112 | } | |
113 | ||
114 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
115 | // org.drools.reteoo.JoinNode | |
116 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
117 | ||
118 | /** | |
119 | * Retrieve the set of common <code>Declarations</code> across the two | |
120 | * input <code>TupleSources</code>. | |
121 | * | |
122 | * @return The <code>Set</code> of common <code>Declarations</code>. | |
123 | */ | |
124 | 52 | public Set getCommonDeclarations() |
125 | { | |
126 | 52 | return this.commonDeclarations; |
127 | } | |
128 | ||
129 | /** | |
130 | * Propagate joined asserted tuples. | |
131 | * | |
132 | * @param joinedTuples The tuples to propagate. | |
133 | * @param workingMemory The working memory session. | |
134 | * @throws AssertionException If an errors occurs while asserting. | |
135 | */ | |
136 | 1487 | private void propagateAssertTuples( TupleSet joinedTuples, |
137 | WorkingMemoryImpl workingMemory ) throws AssertionException | |
138 | { | |
139 | 1487 | Iterator tupleIter = joinedTuples.iterator( ); |
140 | 1487 | while ( tupleIter.hasNext( ) ) |
141 | { | |
142 | 24797 | propagateAssertTuple( ( ReteTuple ) tupleIter.next( ), |
143 | workingMemory ); | |
144 | } | |
145 | } | |
146 | ||
147 | /** | |
148 | * Assert a new <code>Tuple</code> from the left input. | |
149 | * | |
150 | * @param tuple The <code>Tuple</code> being asserted. | |
151 | * @param workingMemory The working memory seesion. | |
152 | * @throws AssertionException If an error occurs while asserting. | |
153 | */ | |
154 | 1169 | void assertLeftTuple( ReteTuple tuple, |
155 | WorkingMemoryImpl workingMemory ) throws AssertionException | |
156 | { | |
157 | 1169 | TupleSet joinedTuples = workingMemory.getJoinMemory( this ).addLeftTuple( tuple ); |
158 | ||
159 | 1169 | if ( !joinedTuples.isEmpty( ) ) |
160 | { | |
161 | 566 | propagateAssertTuples( joinedTuples, |
162 | workingMemory ); | |
163 | } | |
164 | } | |
165 | ||
166 | /** | |
167 | * Assert a new <code>Tuple</code> from the right input. | |
168 | * | |
169 | * @param tuple The <code>Tuple</code> being asserted. | |
170 | * @param workingMemory The working memory seesion. | |
171 | * @throws AssertionException If an error occurs while asserting. | |
172 | */ | |
173 | 926 | void assertRightTuple( ReteTuple tuple, |
174 | WorkingMemoryImpl workingMemory ) throws AssertionException | |
175 | { | |
176 | 926 | TupleSet joinedTuples = workingMemory.getJoinMemory( this ).addRightTuple( tuple ); |
177 | ||
178 | 926 | if ( !joinedTuples.isEmpty( ) ) |
179 | { | |
180 | 921 | propagateAssertTuples( joinedTuples, |
181 | workingMemory ); | |
182 | } | |
183 | } | |
184 | ||
185 | /** | |
186 | * Retract tuples. | |
187 | * | |
188 | * @param key The tuple key. | |
189 | * @param workingMemory The working memory seesion. | |
190 | * @throws RetractionException If an error occurs while retracting. | |
191 | */ | |
192 | 1462 | public void retractTuples( TupleKey key, |
193 | WorkingMemoryImpl workingMemory ) throws RetractionException | |
194 | { | |
195 | 1462 | if ( workingMemory.getJoinMemory( this ).removeTuples( key ) ) |
196 | { | |
197 | 995 | propagateRetractTuples( key, |
198 | workingMemory ); | |
199 | } | |
200 | } | |
201 | ||
202 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
203 | // org.drools.reteoo.TupleSource | |
204 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
205 | ||
206 | /** | |
207 | * Retrieve the <code>Set</code> of <code>Declaration</code>s. in the | |
208 | * propagated <code>Tuples</code>. | |
209 | * | |
210 | * @see Declaration | |
211 | * | |
212 | * @return The <code>Set</code> of <code>Declarations</code> in progated | |
213 | * <code>Tuples</code>. | |
214 | */ | |
215 | 209 | public Set getTupleDeclarations() |
216 | { | |
217 | 209 | return this.tupleDeclarations; |
218 | } | |
219 | ||
220 | 97 | public void attach() |
221 | { | |
222 | 97 | leftInput.addTupleSink( new JoinNodeInput( this, |
223 | JoinNodeInput.LEFT ) ); | |
224 | ||
225 | 97 | rightInput.addTupleSink( new JoinNodeInput( this, |
226 | JoinNodeInput.RIGHT ) ); | |
227 | } | |
228 | ||
229 | 97 | private Set determineTupleDeclarations() |
230 | { | |
231 | 97 | Set decls = new HashSet( leftInput.getTupleDeclarations( ) ); |
232 | ||
233 | 97 | decls.addAll( rightInput.getTupleDeclarations( ) ); |
234 | ||
235 | 97 | return Collections.unmodifiableSet( decls ); |
236 | } | |
237 | ||
238 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
239 | ||
240 | 0 | public String toString() |
241 | { | |
242 | 0 | return "[JoinNode: common=" + this.commonDeclarations + "; decls=" + this.tupleDeclarations + "]"; |
243 | } | |
244 | ||
245 | 5970 | public int hashCode() |
246 | { | |
247 | 5970 | return this.leftInput.hashCode( ) ^ this.rightInput.hashCode( ); |
248 | } | |
249 | ||
250 | 1673 | public boolean equals( Object object ) |
251 | { | |
252 | 1673 | if ( this == object ) |
253 | { | |
254 | 33 | return true; |
255 | } | |
256 | ||
257 | 1640 | if ( object == null || getClass( ) != object.getClass( ) ) |
258 | { | |
259 | 148 | return false; |
260 | } | |
261 | ||
262 | 1492 | JoinNode other = ( JoinNode ) object; |
263 | ||
264 | 1492 | return this.leftInput.equals( other.leftInput ) && this.rightInput.equals( other.rightInput ); |
265 | } | |
266 | } |
|