1 package org.drools.rule;
2
3 /*
4 $Id: Rule.java,v 1.15 2003/07/10 14:19:49 tdiesler 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.spi.Condition;
50 import org.drools.spi.Consequence;
51 import org.drools.spi.Duration;
52 import org.drools.spi.Tuple;
53
54 import java.util.Set;
55 import java.util.HashSet;
56 import java.util.Iterator;
57 import java.util.Collections;
58
59 /*** A set of <code>Condition</code>s and a <code>Consequence</code>.
60 *
61 * The <code>Conditions</code> describe the circumstances
62 * that represent a match for this rule. The <code>Consequence</code>
63 * gets fired when the <code>Conditions</code> match.
64 *
65 * @see Condition
66 * @see Consequence
67 *
68 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
69 *
70 * @version $Id: Rule.java,v 1.15 2003/07/10 14:19:49 tdiesler Exp $
71 */
72 public class Rule
73 {
74 // ------------------------------------------------------------
75 // Instance members
76 // ------------------------------------------------------------
77
78 /*** Name of the rule. */
79 private String name;
80
81 /*** Salience value. */
82 private int salience;
83
84 /*** All declarations. */
85 private Set allDeclarations;
86
87 /*** Formal parameter decls of the rule. */
88 private Set parameterDeclarations;
89
90 /*** Conditions. */
91 private Set conditions;
92
93 /*** Extractions */
94 private Set extractions;
95
96 /*** Consequence. */
97 private Consequence consequence;
98
99 /*** Truthness duration. */
100 private Duration duration;
101
102 // ------------------------------------------------------------
103 // Constructors
104 // ------------------------------------------------------------
105
106 /*** Construct.
107 *
108 * @param name The name of this rule.
109 */
110 public Rule(String name)
111 {
112 this.name = name;
113
114 this.parameterDeclarations = Collections.EMPTY_SET;
115 this.allDeclarations = Collections.EMPTY_SET;
116
117 this.conditions = Collections.EMPTY_SET;
118 this.extractions = Collections.EMPTY_SET;
119 }
120
121 /*** Set the truthness duration.
122 *
123 * @param seconds The number of seconds the rule
124 * must hold true in order to fire.
125 */
126 public void setDuration(long seconds)
127 {
128 this.duration = new FixedDuration( seconds );
129 }
130
131 /*** Set the truthness duration.
132 *
133 * @param duration The truth duration.
134 */
135 public void setDuration(Duration duration)
136 {
137 this.duration = duration;
138 }
139
140 /*** Retrieve the truthness duration.
141 *
142 * @param tuple The tuple.
143 *
144 * @return The number of seconds the rule must
145 * hold true in order to fire.
146 */
147 public long getDuration(Tuple tuple)
148 {
149 if ( this.duration == null )
150 {
151 return 0;
152 }
153
154 return this.duration.getDuration( tuple );
155 }
156
157 /*** Determine if this rule is internally consistent and valid.
158 *
159 * @return <code>true</code> if this rule is valid, else <code>false</code>.
160 */
161 public boolean isValid()
162 {
163 return ! ( getParameterDeclarations().isEmpty()
164 ||
165 getConditions().isEmpty()
166 ||
167 getExtractions().isEmpty() );
168 }
169
170 /*** Check the validity of this rule, and throw exceptions if
171 * it failed validity tests.
172 *
173 * <p>
174 * Possibly exceptions include:
175 * </p>
176 *
177 * <pre>
178 * NoParameterDeclarationException
179 * </pre>
180 *
181 * <p>
182 * A <code>Rule</code> must include at least one
183 * parameter declaration and one condition.
184 * </p>
185 *
186 * @throws InvalidRuleException if this rule is in any way invalid.
187 */
188 public void checkValidity() throws InvalidRuleException
189 {
190 if ( getParameterDeclarations().isEmpty() )
191 {
192 throw new NoParameterDeclarationException( this );
193 }
194 }
195
196 /*** Retrieve the name of this rule.
197 *
198 * @return The name of this rule.
199 */
200 public String getName()
201 {
202 return this.name;
203 }
204
205 /*** Retrieve the <code>Rule</code> salience.
206 *
207 * @return The salience.
208 */
209 public int getSalience()
210 {
211 return salience;
212 }
213
214 /*** Set the <code>Rule<code> salience.
215 *
216 * @param salience The salience.
217 */
218 public void setSalience( int salience )
219 {
220 this.salience = salience;
221 }
222
223 /*** Add a <i>root fact object</i> parameter <code>Declaration</code>
224 * for this <code>Rule</code>.
225 *
226 * @param declaration The <i>root fact object</i> <code>Declaration</code> to add.
227 */
228 public void addParameterDeclaration(Declaration declaration)
229 {
230 if ( this.parameterDeclarations == Collections.EMPTY_SET )
231 {
232 this.parameterDeclarations = new HashSet();
233 }
234
235 this.parameterDeclarations.add( declaration );
236 addDeclaration( declaration );
237 }
238
239 /*** Add a declaration.
240 *
241 * @param declaration The declaration.
242 */
243 public void addDeclaration(Declaration declaration)
244 {
245 if ( this.allDeclarations == Collections.EMPTY_SET )
246 {
247 this.allDeclarations = new HashSet();
248 }
249
250 this.allDeclarations.add( declaration );
251 }
252
253 /*** Retrieve a parameter <code>Declaration</code> by identifier.
254 *
255 * @param identifier The identifier.
256 *
257 * @return The declaration or <code>null</code> if no
258 * declaration matches the <code>identifier</code>.
259 */
260 public Declaration getParameterDeclaration(String identifier)
261 {
262 Iterator declIter = this.parameterDeclarations.iterator();
263 Declaration eachDecl = null;
264
265 while ( declIter.hasNext() )
266 {
267 eachDecl = (Declaration) declIter.next();
268
269 if ( eachDecl.getIdentifier().equals( identifier ) )
270 {
271 return eachDecl;
272 }
273 }
274
275 return null;
276 }
277
278 /*** Retrieve a <code>Declaration</code> by identifier.
279 *
280 * @param identifier The identifier.
281 *
282 * @return The declaration or <code>null</code> if no
283 * declaration matches the <code>identifier</code>.
284 */
285 public Declaration getDeclaration(String identifier)
286 {
287 Iterator declIter = this.allDeclarations.iterator();
288 Declaration eachDecl = null;
289
290 while ( declIter.hasNext() )
291 {
292 eachDecl = (Declaration) declIter.next();
293
294 if ( eachDecl.getIdentifier().equals( identifier ) )
295 {
296 return eachDecl;
297 }
298 }
299
300 return null;
301 }
302
303 /*** Retrieve the set of all <i>root fact object</i> parameter
304 * <code>Declarations</code>.
305 *
306 * @return The <code>Set</code> of <code>Declarations</code> which
307 * specify the <i>root fact objects</i>.
308 */
309 public Set getParameterDeclarations()
310 {
311 return this.parameterDeclarations;
312 }
313
314 /*** Retrieve the set of all implied local Declarations.
315 *
316 * @return The <code>Set</code> of all implied <code>Declarations</code>
317 * which are implied by the conditions.
318 */
319 public Set getLocalDeclarations()
320 {
321 Set localDecls = new HashSet( this.allDeclarations );
322
323 localDecls.removeAll( this.parameterDeclarations );
324
325 return localDecls;
326 }
327
328 /*** Retrieve the array of all <code>Declaration</code>s of
329 * this rule.
330 *
331 * @return The array of declarations.
332 */
333 public Declaration[] getDeclarationsArray()
334 {
335 Declaration[] decls = new Declaration[ this.allDeclarations.size() ];
336
337 int i = 0;
338 Iterator declIter = this.allDeclarations.iterator();
339
340 while ( declIter.hasNext() )
341 {
342 decls[ i++ ] = (Declaration) declIter.next();
343 }
344
345 return decls;
346 }
347
348 /*** Add a <code>Condition</code> to this rule.
349 *
350 * @param condition The <code>Condition</code> to add.
351 */
352 public void addCondition(Condition condition)
353 {
354 if ( this.conditions == Collections.EMPTY_SET )
355 {
356 this.conditions = new HashSet();
357 }
358
359 this.conditions.add( condition );
360 }
361
362 /*** Add a consistent <code>Extraction</code> to this rule.
363 *
364 * @param extraction the <code>Extraction</code> to add.
365 */
366 public void addExtraction(Extraction extraction)
367 {
368 if ( this.extractions == Collections.EMPTY_SET )
369 {
370 this.extractions = new HashSet();
371 }
372
373 this.extractions.add( extraction );
374
375 Declaration decl = extraction.getTargetDeclaration();
376
377 addDeclaration( decl );
378 }
379
380 /*** Retrieve the <code>Set</code> of <code>Conditions</code> for
381 * this rule.
382 *
383 * @return The <code>Set</code> of <code>Conditions</code>.
384 */
385 public Set getConditions()
386 {
387 return this.conditions;
388 }
389
390 /*** Retrieve the <code>Set</code> of <code>Extractions</code> for
391 * this rule.
392 *
393 * @return The <code>Set</code> of <code>Extractions</code>.
394 */
395 public Set getExtractions()
396 {
397 return this.extractions;
398 }
399
400 /*** Set the <code>Consequence</code> that is associated with the
401 * successful match of this rule.
402 *
403 * @param consequence The <code>Consequence</code> to attach to this <code>Rule</code>.
404 */
405 public void setConsequence(Consequence consequence)
406 {
407 this.consequence = consequence;
408 }
409
410 /*** Retrieve the <code>Consequence</code> associated with this <code>Rule</code>.
411 *
412 * @return The <code>Consequence</code>.
413 */
414 public Consequence getConsequence()
415 {
416 return this.consequence;
417 }
418
419 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
420 // java.lang.Object
421 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
422
423 /*** Produce a debug string.
424 *
425 * @return The debug string.
426 */
427 public String toString()
428 {
429 return "[Rule: name='" + this.name
430 + "'; paramDecls=" + this.parameterDeclarations
431 + "; localDecls=" + getLocalDeclarations()
432 + "; conds=" + this.conditions
433 + "; extractions=" + this.extractions
434 + "]";
435 }
436
437 }
This page was automatically generated by Maven