1 package org.drools.reteoo.impl;
2
3 /*
4 $Id: ExtractionNodeImpl.java,v 1.4 2002/11/22 03:08:46 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.ExtractionNode;
54 import org.drools.rule.Declaration;
55 import org.drools.spi.Extractor;
56
57 import java.util.Set;
58 import java.util.HashSet;
59 import java.util.Iterator;
60
61 /*** <i> extraction</i> node in the Rete-OO network.
62 *
63 * @see ExtractionNode
64 *
65 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
66 */
67 public class ExtractionNodeImpl extends TupleSourceImpl implements ExtractionNode, TupleSinkImpl
68 {
69 // ------------------------------------------------------------
70 // Instance members
71 // ------------------------------------------------------------
72
73 /*** All declarations for this node. */
74 private Set tupleDeclarations;
75
76 /*** Declaration on LHS. */
77 private Declaration targetDeclaration;
78
79 /*** extrator. */
80 private Extractor extractor;
81
82 // ------------------------------------------------------------
83 // Constructors
84 // ------------------------------------------------------------
85
86 /*** Construct.
87 *
88 * @param tupleSource Parent tuple source.
89 * @param targetDeclaration Target of extraction.
90 * @param extractor The fact extractor to use.
91 */
92 public ExtractionNodeImpl(TupleSourceImpl tupleSource,
93 Declaration targetDeclaration,
94 Extractor extractor)
95 {
96 this.extractor = extractor;
97 this.targetDeclaration = targetDeclaration;
98
99 Set sourceDecls = tupleSource.getTupleDeclarations();
100
101 this.tupleDeclarations = new HashSet( sourceDecls.size() + 1 );
102
103 this.tupleDeclarations.addAll( sourceDecls );
104 this.tupleDeclarations.add( targetDeclaration );
105
106 tupleSource.setTupleSink( this );
107 }
108
109 // ------------------------------------------------------------
110 // Instance methods
111 // ------------------------------------------------------------
112
113 /*** Retrieve the <code>Declaration</code> which is the target of
114 * the extraction.
115 *
116 * @see Declaration
117 *
118 * @return The target <code>Declaration</code>.
119 */
120 public Declaration getTargetDeclaration()
121 {
122 return this.targetDeclaration;
123 }
124
125 /*** Retrieve the <code>Extractor</code> used to generate the
126 * right-hand-side value for the extraction.
127 *
128 * @see Extractor
129 *
130 * @return The <code>Extrator</code>.
131 */
132 public Extractor getExtractor()
133 {
134 return this.extractor;
135 }
136
137 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138 // org.drools.reteoo.impl.TupleSource
139 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
140
141 /*** Retrieve the <code>Set</code> of <code>Declaration</code>s
142 * in the propagated <code>Tuples</code>.
143 *
144 * @see Declaration
145 *
146 * @return The <code>Set</code> of <code>Declarations</code>
147 * in progated <code>Tuples</code>.
148 */
149 public Set getTupleDeclarations()
150 {
151 return this.tupleDeclarations;
152 }
153
154 /*** Assert a new <code>Tuple</code>.
155 *
156 * @param tuple The <code>Tuple</code> being asserted.
157 * @param workingMemory The working memory seesion.
158 *
159 * @throws AssertionException If an error occurs while asserting.
160 */
161 public void assertTuple(ReteTuple tuple,
162 WorkingMemory workingMemory) throws AssertionException
163 {
164 Object value = getExtractor().extractFact( tuple );
165
166 ReteTuple newTuple = new ReteTuple( tuple );
167
168 newTuple.putOtherColumn( getTargetDeclaration(),
169 value );
170
171 propagateAssertTuple( newTuple,
172 workingMemory );
173 }
174
175 /*** Retract tuples.
176 *
177 * @param key The tuple key.
178 * @param workingMemory The working memory seesion.
179 *
180 * @throws RetractionException If an error occurs while retracting.
181 */
182 public void retractTuples(TupleKey key,
183 WorkingMemory workingMemory) throws RetractionException
184 {
185 propagateRetractTuples( key,
186 workingMemory );
187 }
188
189 /*** Modify tuples.
190 *
191 * @param trigger The root fact object.
192 * @param newTuples Modification replacement tuples.
193 * @param workingMemory The working memory session.
194 *
195 * @throws FactException If an error occurs while modifying.
196 */
197 public void modifyTuples(Object trigger,
198 TupleSet newTuples,
199 WorkingMemory workingMemory) throws FactException
200 {
201 Set retractedKeys = new HashSet();
202
203 Iterator tupleIter = newTuples.iterator();
204 ReteTuple eachTuple = null;
205
206 while ( tupleIter.hasNext() )
207 {
208 eachTuple = (ReteTuple) tupleIter.next();
209
210 retractedKeys.add( eachTuple.getKey() );
211 }
212
213 Iterator keyIter = retractedKeys.iterator();
214 TupleKey eachKey = null;
215
216 while ( keyIter.hasNext() )
217 {
218 eachKey = (TupleKey) keyIter.next();
219
220 propagateRetractTuples( eachKey,
221 workingMemory );
222 }
223
224 tupleIter = newTuples.iterator();
225
226 while ( tupleIter.hasNext() )
227 {
228 eachTuple = (ReteTuple) tupleIter.next();
229
230 assertTuple( eachTuple,
231 workingMemory );
232 }
233 }
234
235 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
236 // java.lang.Object
237 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
238
239 /*** Produce a debug string.
240 *
241 * @return The debug string.
242 */
243 public String toString()
244 {
245 return "[ExtractionNodeImpl: target=" + getTargetDeclaration()
246 + "; extractor=" + getExtractor() + "]";
247 }
248 }
This page was automatically generated by Maven