|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConditionNode.java | 100% | 89.5% | 77.8% | 88.2% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: ConditionNode.java,v 1.35 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.util.Set; | |
44 | ||
45 | import org.drools.AssertionException; | |
46 | import org.drools.RetractionException; | |
47 | import org.drools.rule.Rule; | |
48 | import org.drools.spi.Condition; | |
49 | ||
50 | /** | |
51 | * Node which filters <code>ReteTuple</code>s. | |
52 | * | |
53 | * <p> | |
54 | * Using a semantic <code>Condition</code>, this node may allow or disallow | |
55 | * <code>Tuples</code> to proceed further through the Rete-OO network. | |
56 | * </p> | |
57 | * | |
58 | * @see ConditionNode | |
59 | * @see Condition | |
60 | * @see ReteTuple | |
61 | * | |
62 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
63 | */ | |
64 | class ConditionNode extends TupleSource | |
65 | implements | |
66 | TupleSink | |
67 | { | |
68 | // ------------------------------------------------------------ | |
69 | // Instance members | |
70 | // ------------------------------------------------------------ | |
71 | ||
72 | /** The <code>Rule</code>. */ | |
73 | private final Rule rule; | |
74 | ||
75 | /** The semantic <code>Condition</code>. */ | |
76 | private final Condition condition; | |
77 | ||
78 | /** The source of incoming <code>Tuples</code>. */ | |
79 | private final TupleSource tupleSource; | |
80 | ||
81 | // ------------------------------------------------------------ | |
82 | // Constructors | |
83 | // ------------------------------------------------------------ | |
84 | ||
85 | /** | |
86 | * Construct. | |
87 | * | |
88 | * @param rule The rule | |
89 | * @param tupleSource The source of incoming <code>Tuples</code>. | |
90 | * @param condition | |
91 | */ | |
92 | 137 | ConditionNode( Rule rule, |
93 | TupleSource tupleSource, | |
94 | Condition condition ) | |
95 | { | |
96 | 137 | this.rule = rule; |
97 | 137 | this.condition = condition; |
98 | 137 | this.tupleSource = tupleSource; |
99 | } | |
100 | ||
101 | /** | |
102 | * Attaches this node into the network. | |
103 | */ | |
104 | 122 | public void attach() |
105 | { | |
106 | 122 | this.tupleSource.addTupleSink( this ); |
107 | } | |
108 | ||
109 | // ------------------------------------------------------------ | |
110 | // Instance methods | |
111 | // ------------------------------------------------------------ | |
112 | ||
113 | /** | |
114 | * Retrieve the <code>Condition</code> associated with this node. | |
115 | * | |
116 | * @return The <code>Condition</code>. | |
117 | */ | |
118 | 0 | public Condition getCondition() |
119 | { | |
120 | 0 | return this.condition; |
121 | } | |
122 | ||
123 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
124 | // org.drools.reteoo.impl.TupleSource | |
125 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
126 | ||
127 | /** | |
128 | * Retrieve the <code>Set</code> of <code>Declaration</code> s in the | |
129 | * propagated <code>Tuples</code>. | |
130 | * | |
131 | * @return The <code>Set</code> of <code>Declarations</code> in progated | |
132 | * <code>Tuples</code>. | |
133 | */ | |
134 | 266 | public Set getTupleDeclarations() |
135 | { | |
136 | 266 | return this.tupleSource.getTupleDeclarations( ); |
137 | } | |
138 | ||
139 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
140 | // org.drools.reteoo.impl.TupleSink | |
141 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
142 | ||
143 | /** | |
144 | * Assert a new <code>Tuple</code>. | |
145 | * | |
146 | * @param tuple The <code>Tuple</code> being asserted. | |
147 | * @param workingMemory The working memory seesion. | |
148 | * | |
149 | * @throws AssertionException If an error occurs while asserting. | |
150 | */ | |
151 | 41832 | public void assertTuple( ReteTuple tuple, |
152 | WorkingMemoryImpl workingMemory ) throws AssertionException | |
153 | { | |
154 | 41832 | boolean allowed = this.condition.isAllowed( tuple ); |
155 | ||
156 | 41832 | workingMemory.getEventSupport( ).fireConditionTested( this.rule, |
157 | this.condition, | |
158 | tuple, | |
159 | allowed ); | |
160 | ||
161 | 41832 | if ( allowed ) |
162 | { | |
163 | 22759 | propagateAssertTuple( tuple, |
164 | workingMemory ); | |
165 | } | |
166 | ||
167 | } | |
168 | ||
169 | /** | |
170 | * Retract tuples. | |
171 | * | |
172 | * @param key The tuple key. | |
173 | * @param workingMemory The working memory seesion. | |
174 | * | |
175 | * @throws RetractionException If an error occurs while retracting. | |
176 | */ | |
177 | 2559 | public void retractTuples( TupleKey key, |
178 | WorkingMemoryImpl workingMemory ) throws RetractionException | |
179 | { | |
180 | 2559 | propagateRetractTuples( key, |
181 | workingMemory ); | |
182 | } | |
183 | ||
184 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
185 | // java.lang.Object | |
186 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
187 | ||
188 | /** | |
189 | * Produce a debug string. | |
190 | * | |
191 | * @return The debug string. | |
192 | */ | |
193 | 0 | public String toString() |
194 | { | |
195 | 0 | return "[ConditionNode: cond=" + this.condition + "]"; |
196 | } | |
197 | ||
198 | 6119 | public int hashCode() |
199 | { | |
200 | 6119 | return this.tupleSource.hashCode( ) ^ this.condition.hashCode( ); |
201 | } | |
202 | ||
203 | 1106 | public boolean equals( Object object ) |
204 | { | |
205 | 1106 | if ( this == object ) |
206 | { | |
207 | 927 | return true; |
208 | } | |
209 | ||
210 | 179 | if ( object == null || getClass( ) != object.getClass( ) ) |
211 | { | |
212 | 92 | return false; |
213 | } | |
214 | ||
215 | 87 | ConditionNode other = ( ConditionNode ) object; |
216 | ||
217 | 87 | return this.tupleSource.equals( other.tupleSource ) && this.condition.equals( other.condition ); |
218 | } | |
219 | } |
|