1 package org.drools.reteoo.impl;
2
3 /*
4 $Id: ConditionNodeImpl.java,v 1.4 2002/08/27 23:31:08 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.ConditionNode;
54 import org.drools.spi.Condition;
55
56 import java.util.Set;
57 import java.util.HashSet;
58 import java.util.Iterator;
59
60 /*** Node which filters <code>ReteTuple</code>s.
61 *
62 * <p>
63 * Using a semantic <code>Condition</code>, this node
64 * may allow or disallow <code>Tuples</code> to proceed
65 * further through the Rete-OO network.
66 * </p>
67 *
68 * @see ConditionNode
69 * @see Condition
70 * @see ReteTuple
71 *
72 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
73 */
74 public class ConditionNodeImpl extends TupleSourceImpl implements ConditionNode, TupleSinkImpl
75 {
76 // ------------------------------------------------------------
77 // Instance members
78 // ------------------------------------------------------------
79
80 /*** The semantic <code>Condition</code>. */
81 private Condition condition;
82
83 /*** The source of incoming <code>Tuples</code>. */
84 private TupleSourceImpl tupleSource;
85
86 // ------------------------------------------------------------
87 // Constructors
88 // ------------------------------------------------------------
89
90 /*** Construct.
91 *
92 * @param tupleSource The source of incoming <code>Tuples</code>.
93 * @param condition The semantic <code>Condition</code>.
94 */
95 public ConditionNodeImpl(TupleSourceImpl tupleSource,
96 Condition condition)
97 {
98 this.condition = condition;
99 this.tupleSource = tupleSource;
100
101 if ( tupleSource != null )
102 {
103 this.tupleSource.setTupleSink( this );
104 }
105 }
106
107 // ------------------------------------------------------------
108 // Instance methods
109 // ------------------------------------------------------------
110
111 /*** Retrieve the <code>Condition</code> associated
112 * with this node.
113 *
114 * @return The <code>Condition</code>.
115 */
116 public Condition getCondition()
117 {
118 return this.condition;
119 }
120
121 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122 // org.drools.reteoo.impl.TupleSource
123 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124
125 /*** Retrieve the <code>Set</code> of <code>Declaration</code>s
126 * in the propagated <code>Tuples</code>.
127 *
128 * @return The <code>Set</code> of <code>Declarations</code>
129 * in progated <code>Tuples</code>.
130 */
131 public Set getTupleDeclarations()
132 {
133 return this.tupleSource.getTupleDeclarations();
134 }
135
136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137 // org.drools.reteoo.impl.TupleSink
138 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
140 /*** Assert a new <code>Tuple</code>.
141 *
142 * @param tuple The <code>Tuple</code> being asserted.
143 * @param workingMemory The working memory seesion.
144 *
145 * @throws AssertionException If an error occurs while asserting.
146 */
147 public void assertTuple(ReteTuple tuple,
148 WorkingMemory workingMemory) throws AssertionException
149 {
150 if ( getCondition().isAllowed( tuple ) )
151 {
152 propagateAssertTuple( tuple,
153 workingMemory );
154 }
155 }
156
157 /*** Retract tuples.
158 *
159 * @param key The tuple key.
160 * @param workingMemory The working memory seesion.
161 *
162 * @throws RetractionException If an error occurs while retracting.
163 */
164 public void retractTuples(TupleKey key,
165 WorkingMemory workingMemory) throws RetractionException
166 {
167 propagateRetractTuples( key,
168 workingMemory );
169 }
170
171 /*** Modify tuples.
172 *
173 * @param trigger The root fact object.
174 * @param newTuples Modification replacement tuples.
175 * @param workingMemory The working memory session.
176 *
177 * @throws FactException If an error occurs while modifying.
178 */
179 public void modifyTuples(Object trigger,
180 TupleSet newTuples,
181 WorkingMemory workingMemory) throws FactException
182 {
183 Set retractedKeys = new HashSet();
184
185 Iterator tupleIter = newTuples.iterator();
186 ReteTuple eachTuple = null;
187
188 while ( tupleIter.hasNext() )
189 {
190 eachTuple = (ReteTuple) tupleIter.next();
191
192 if ( ! getCondition().isAllowed( eachTuple ) )
193 {
194 tupleIter.remove();
195 retractedKeys.add( eachTuple.getKey() );
196 }
197 }
198
199 Iterator keyIter = retractedKeys.iterator();
200 TupleKey eachKey = null;
201
202 while ( keyIter.hasNext() )
203 {
204 eachKey = (TupleKey) keyIter.next();
205
206 propagateRetractTuples( eachKey,
207 workingMemory );
208 }
209
210 propagateModifyTuples( trigger,
211 newTuples,
212 workingMemory );
213 }
214
215 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
216 // java.lang.Object
217 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218
219 /*** Produce a debug string.
220 *
221 * @return The debug string.
222 */
223 public String toString()
224 {
225 return "[ConditionNodeImpl: cond=" + this.condition + "]";
226 }
227 }
This page was automatically generated by Maven