1 package org.drools.reteoo.impl;
2
3 /*
4 $Id: JoinNodeImpl.java,v 1.7 2002/08/10 20:57:34 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 org.drools.WorkingMemory;
50 import org.drools.FactException;
51 import org.drools.AssertionException;
52 import org.drools.RetractionException;
53 import org.drools.reteoo.JoinNode;
54 import org.drools.reteoo.TupleSource;
55 import org.drools.rule.Declaration;
56
57 import java.util.Set;
58 import java.util.HashSet;
59 import java.util.Iterator;
60
61 /*** A two-input Rete-OO <i>join node</i>.
62 *
63 * @see org.drools.reteoo.TupleSource
64 * @see org.drools.reteoo.TupleSink
65 *
66 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
67 */
68 public class JoinNodeImpl extends TupleSourceImpl implements JoinNode
69 {
70 // ------------------------------------------------------------
71 // Instance members
72 // ------------------------------------------------------------
73
74 /*** The left input <code>TupleSource</code>.
75 */
76 private TupleSourceImpl leftInput;
77
78 /*** The right input <code>TupleSource</code>.
79 */
80 private TupleSourceImpl rightInput;
81
82 /*** A <code>Set</code> of <code>Declarations</code>
83 * common to both left and right input sources.
84 */
85 private Set commonDeclarations;
86
87 // ------------------------------------------------------------
88 // Constructors
89 // ------------------------------------------------------------
90
91 /*** Construct.
92 *
93 * @param leftInput The left input <code>TupleSource</code>.
94 * @param rightInput The right input <code>TupleSource</code>.
95 */
96 public JoinNodeImpl(TupleSourceImpl leftInput,
97 TupleSourceImpl rightInput)
98 {
99 this.leftInput = leftInput;
100 this.rightInput = rightInput;
101
102 determineCommonDeclarations();
103
104 leftInput.setTupleSink( getLeftNodeInput() );
105 rightInput.setTupleSink( getRightNodeInput() );
106 }
107
108 // ------------------------------------------------------------
109 // Instance methods
110 // ------------------------------------------------------------
111
112 /*** Set up the <code>Set</code> of common <code>Declarations</code>
113 * across the two input <code>TupleSources</code>.
114 */
115 private void determineCommonDeclarations()
116 {
117 this.commonDeclarations = new HashSet();
118
119 Set leftDecls = leftInput.getTupleDeclarations();
120 Set rightDecls = rightInput.getTupleDeclarations();
121
122 Iterator declIter = rightDecls.iterator();
123 Declaration eachDecl = null;
124
125 while ( declIter.hasNext() )
126 {
127 eachDecl = (Declaration) declIter.next();
128
129 if ( leftDecls.contains( eachDecl ) )
130 {
131 this.commonDeclarations.add( eachDecl );
132 }
133 }
134 }
135
136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137 // org.drools.reteoo.JoinNode
138 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
140 /*** Retrieve the set of common <code>Declarations</code>
141 * across the two input <code>TupleSources</code>.
142 *
143 * @return The <code>Set</code> of common <code>Declarations</code>.
144 */
145 public Set getCommonDeclarations()
146 {
147 return this.commonDeclarations;
148 }
149
150 /*** Retrieve the left input <code>TupleSource</code>.
151 *
152 * @return The left input <code>TupleSource</code>.
153 */
154 public TupleSource getLeftInput()
155 {
156 return this.leftInput;
157 }
158
159 /*** Retrieve the right input <code>TupleSource</code>.
160 *
161 * @return The right input <code>TupleSource</code>.
162 */
163 public TupleSource getRightInput()
164 {
165 return this.rightInput;
166 }
167
168 /*** Propagate joined asserted tuples.
169 *
170 * @param joinedTuples The tuples to propagate.
171 * @param workingMemory The working memory session.
172 *
173 * @throws AssertionException If an errors occurs while asserting.
174 */
175 void propagateAssertTuples(Set joinedTuples,
176 WorkingMemory workingMemory) throws AssertionException
177 {
178 Iterator tupleIter = joinedTuples.iterator();
179 ReteTuple eachTuple = null;
180
181 while ( tupleIter.hasNext() )
182 {
183 eachTuple = (ReteTuple) tupleIter.next();
184
185 propagateAssertTuple( eachTuple,
186 workingMemory );
187 }
188 }
189
190 /*** Assert a new <code>Tuple</code> from the left input.
191 *
192 * @param tuple The <code>Tuple</code> being asserted.
193 * @param workingMemory The working memory seesion.
194 *
195 * @throws AssertionException If an error occurs while asserting.
196 */
197 void assertLeftTuple(ReteTuple tuple,
198 WorkingMemory workingMemory) throws AssertionException
199 {
200 JoinMemoryImpl memory = (JoinMemoryImpl) workingMemory.getJoinMemory( this );
201 Set joinedTuples = memory.addLeftTuple( tuple );
202
203 if ( joinedTuples.isEmpty() )
204 {
205 return;
206 }
207
208 propagateAssertTuples( joinedTuples,
209 workingMemory );
210 }
211
212 /*** Assert a new <code>Tuple</code> from the right input.
213 *
214 * @param tuple The <code>Tuple</code> being asserted.
215 * @param workingMemory The working memory seesion.
216 *
217 * @throws AssertionException If an error occurs while asserting.
218 */
219 void assertRightTuple(ReteTuple tuple,
220 WorkingMemory workingMemory) throws AssertionException
221 {
222 JoinMemoryImpl memory = (JoinMemoryImpl) workingMemory.getJoinMemory( this );
223 Set joinedTuples = memory.addRightTuple( tuple );
224
225 if ( joinedTuples.isEmpty() )
226 {
227 return;
228 }
229
230 propagateAssertTuples( joinedTuples,
231 workingMemory );
232 }
233
234 /*** Retrieve the node input for the left side.
235 *
236 * @return The node input for the left side.
237 */
238 JoinNodeInput getLeftNodeInput()
239 {
240 return new JoinNodeInput( this,
241 JoinNodeInput.LEFT );
242 }
243
244 /*** Retrieve the node input for the right side.
245 *
246 * @return The node input for the right side.
247 */
248 JoinNodeInput getRightNodeInput()
249 {
250 return new JoinNodeInput( this,
251 JoinNodeInput.RIGHT );
252 }
253
254 /*** Retract tuples.
255 *
256 * @param key The tuple key.
257 * @param workingMemory The working memory seesion.
258 *
259 * @throws RetractionException If an error occurs while retracting.
260 */
261 public void retractTuples(TupleKey key,
262 WorkingMemory workingMemory) throws RetractionException
263 {
264 JoinMemoryImpl memory = (JoinMemoryImpl) workingMemory.getJoinMemory( this );
265
266 memory.retractTuples( key );
267
268 propagateRetractTuples( key,
269 workingMemory );
270 }
271
272 /*** Modify tuples from the left input.
273 *
274 * @param trigger The root fact object.
275 * @param newTuples Modification replacement tuples.
276 * @param workingMemory The working memory session.
277 *
278 * @throws FactException If an error occurs while modifying.
279 */
280 void modifyLeftTuples(Object trigger,
281 TupleSet newTuples,
282 WorkingMemory workingMemory) throws FactException
283 {
284 JoinMemoryImpl memory = (JoinMemoryImpl) workingMemory.getJoinMemory( this );
285
286 memory.modifyLeftTuples( trigger,
287 newTuples,
288 this,
289 workingMemory );
290 }
291
292 /*** Modify tuples from the right input.
293 *
294 * @param trigger The root fact object.
295 * @param newTuples Modification replacement tuples.
296 * @param workingMemory The working memory session.
297 *
298 * @throws FactException If an error occurs while modifying.
299 */
300 void modifyRightTuples(Object trigger,
301 TupleSet newTuples,
302 WorkingMemory workingMemory) throws FactException
303 {
304 JoinMemoryImpl memory = (JoinMemoryImpl) workingMemory.getJoinMemory( this );
305
306 memory.modifyRightTuples( trigger,
307 newTuples,
308 this,
309 workingMemory );
310 }
311
312 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
313 // org.drools.reteoo.TupleSource
314 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
315
316 /*** Retrieve the <code>Set</code> of <code>Declaration</code>s.
317 * in the propagated <code>Tuples</code>.
318 *
319 * @see Declaration
320 *
321 * @return The <code>Set</code> of <code>Declarations</code>
322 * in progated <code>Tuples</code>.
323 */
324 public Set getTupleDeclarations()
325 {
326 Set decls = new HashSet();
327
328 decls.addAll( getLeftInput().getTupleDeclarations() );
329 decls.addAll( getRightInput().getTupleDeclarations() );
330
331 return decls;
332 }
333 }
This page was automatically generated by Maven