1 package org.drools;
2
3 /*
4 $Id: RuleBase.java,v 1.14 2002/08/20 18:33:17 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.reteoo.Builder;
50 import org.drools.reteoo.Rete;
51 import org.drools.reteoo.impl.ReteImpl;
52 import org.drools.rule.Rule;
53 import org.drools.rule.RuleSet;
54
55 import java.util.Iterator;
56
57 /*** Collection of <code>Rule</code>s.
58 *
59 * @see Rule
60 * @see RuleSet
61 *
62 * @author <a href="bob@werken.com">bob mcwhirter</a>
63 */
64 public class RuleBase
65 {
66 // ------------------------------------------------------------
67 // Instance members
68 // ------------------------------------------------------------
69
70 /*** The root Rete-OO for this <code>RuleBase</code>. */
71 private ReteImpl rete;
72
73 /*** Rete-OO Network builder. */
74 private Builder builder;
75
76 // ------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------
79
80 /*** Construct.
81 */
82 public RuleBase()
83 {
84 this.rete = new ReteImpl();
85 this.builder = new Builder( this.rete );
86 }
87
88 // ------------------------------------------------------------
89 // Instance methods
90 // ------------------------------------------------------------
91
92 /*** Retrieve this <code>RuleBase's</code> <code>Builder</code>.
93 *
94 * @return The <code>Builder</code>.
95 */
96 private Builder getBuilder()
97 {
98 return this.builder;
99 }
100
101 /*** Add a <code>RuleSet</code> of <code>Rules</code> to this <code>RuleBase</code>.
102 *
103 * <p>
104 * A <code>RuleSet</code> may be added to multiple <code>RuleBases</code>.
105 * Any changes to a <code>RuleSet</code> or its component <code>Rule</code>
106 * once it has been added are ignored.
107 * </p>
108 *
109 * @param ruleSet The <code>RuleSet</code> to add.
110 *
111 * @throws RuleIntegrationException If a member rule does not allow for
112 * complete and correct integration into the underlying Rete network.
113 */
114 public void addRuleSet(RuleSet ruleSet) throws RuleIntegrationException
115 {
116 Iterator ruleIter = ruleSet.getRuleIterator();
117 Rule eachRule = null;
118
119 while ( ruleIter.hasNext() )
120 {
121 eachRule = (Rule) ruleIter.next();
122
123 addRule( eachRule );
124 }
125 }
126
127 /*** Add a <code>Rule</code> to this <code>RuleBase</code>.
128 *
129 * <p>
130 * A <code>Rule</code> may be added to multiple <code>RuleBases</code>.
131 * Any changes to a <code>Rule</code> once it has been added are ignored.
132 * </p>
133 *
134 * @param rule The rule to add.
135 *
136 * @throws RuleIntegrationException If the rule does not allow for
137 * complete and correct integration into the underlying Rete
138 * network.
139 */
140 public void addRule(Rule rule) throws RuleIntegrationException
141 {
142 getBuilder().addRule( rule );
143 }
144
145 /*** Create a <code>WorkingMemory</code> session for
146 * this <code>RuleBase</code>.
147 *
148 * @see WorkingMemory
149 *
150 * @return A newly initialized <code>WorkingMemory</code>.
151 */
152 public WorkingMemory createWorkingMemory()
153 {
154 return new WorkingMemory( this );
155 }
156
157 /*** Create a <code>TransactionalWorkingMemory</code> session for
158 * this <code>RuleBase</code>.
159 *
160 * @see TransactionalWorkingMemory
161 *
162 * @return A newly initialized <code>TransactionalWorkingMemory</code>.
163 */
164 public TransactionalWorkingMemory createTransactionalWorkingMemory()
165 {
166 return new TransactionalWorkingMemory( this );
167 }
168
169 /*** Retrieve the Rete-OO network for this <code>RuleBase</code>.
170 *
171 * @return The RETE-OO network.
172 */
173 Rete getRete()
174 {
175 return this.rete;
176 }
177
178 /*** Assert a new fact object into this <code>RuleBase</code>
179 * and the specified <code>WorkingMemory</code>.
180 *
181 * @param object The object to assert.
182 * @param workingMemory The working memory session.
183 *
184 * @throws AssertionException if an error occurs during assertion.
185 */
186 void assertObject(Object object,
187 WorkingMemory workingMemory) throws AssertionException
188 {
189 getRete().assertObject( object,
190 workingMemory );
191 }
192
193 /*** Retract a fact object from this <code>RuleBase</code>
194 * and the specified <code>WorkingMemory</code>.
195 *
196 * @param object The object to retract.
197 * @param workingMemory The working memory session.
198 *
199 * @throws RetractionException if an error occurs during retraction.
200 */
201 void retractObject(Object object,
202 WorkingMemory workingMemory) throws RetractionException
203 {
204 getRete().retractObject( object,
205 workingMemory );
206 }
207
208 /*** Modify a fact object in this <code>RuleBase</code>
209 * and the specified <code>WorkingMemory</code>.
210 *
211 * With the exception of time-based nodes, modification of
212 * a fact object is semantically equivelent to retracting and
213 * re-asserting it.
214 *
215 * @param object The object to modify.
216 * @param workingMemory The working memory session.
217 *
218 * @throws FactException if an error occurs during assertion.
219 */
220 void modifyObject(Object object,
221 WorkingMemory workingMemory) throws FactException
222 {
223 getRete().modifyObject( object,
224 workingMemory );
225 }
226 }
This page was automatically generated by Maven