1 package org.drools;
2
3 /*
4 $Id: WorkingMemory.java,v 1.13 2003/03/04 05:09:38 kaz 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.JoinNode;
50 import org.drools.reteoo.JoinMemory;
51 import org.drools.reteoo.Agenda;
52 import org.drools.reteoo.impl.JoinNodeImpl;
53 import org.drools.reteoo.impl.JoinMemoryImpl;
54 import org.drools.reteoo.impl.AgendaImpl;
55
56 import java.util.Map;
57 import java.util.HashMap;
58
59 /*** A knowledge session for a <code>RuleBase</code>.
60 *
61 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
62 */
63 public class WorkingMemory
64 {
65 // ------------------------------------------------------------
66 // Instance members
67 // ------------------------------------------------------------
68
69 /*** The <code>RuleBase</code> with which this memory is associated. */
70 private RuleBase ruleBase;
71
72 /*** The actual memory for the <code>JoinNode</code>s. */
73 private Map joinMemories;
74
75 /*** Rule-firing agenda. */
76 private AgendaImpl agenda;
77
78 /*** Flag to determine if a rule is currently being fired. */
79 private boolean firing;
80
81 /*** Application data which is associated with this memory. */
82 private Object applicationData;
83
84 // ------------------------------------------------------------
85 // Constructors
86 // ------------------------------------------------------------
87
88 /*** Construct a new working memory for a ruleBase.
89 *
90 * @param ruleBase The rule base with which this memory is associated.
91 */
92 protected WorkingMemory(RuleBase ruleBase)
93 {
94 this.ruleBase = ruleBase;
95 this.joinMemories = new HashMap();
96
97 this.agenda = new AgendaImpl( this );
98 }
99
100 // ------------------------------------------------------------
101 // Instance methods
102 // ------------------------------------------------------------
103
104 /*** Retrieve the application data that is associated with
105 * this memory.
106 *
107 * @return The application data or <code>null</code> if
108 * no data has been set for this memory.
109 */
110 public Object getApplicationData()
111 {
112 return this.applicationData;
113 }
114
115 /*** Set the application data associated with this memory.
116 *
117 * @param appData The application data for this memory.
118 */
119 public void setApplicationData( Object appData )
120 {
121 this.applicationData = appData;
122 }
123
124 /*** Retrieve the rule-firing <code>Agenda</code> for
125 * this <code>WorkingMemory</code>.
126 *
127 * @return The <code>Agenda</code>.
128 */
129 public Agenda getAgenda()
130 {
131 return this.agenda;
132 }
133
134 /*** Retrieve the <code>RuleBase</code>
135 * of this working memory.
136 *
137 * @return The <code>RuleBase</code>.
138 */
139 public RuleBase getRuleBase()
140 {
141 return this.ruleBase;
142 }
143
144 /*** Fire all items on the agenda until empty.
145 *
146 * @throws AssertionException If an assertion error occurs.
147 */
148 private void fireAgenda() throws AssertionException
149 {
150 Agenda agenda = getAgenda();
151
152 // If we're already firing a rule, then it'll pick up
153 // the firing for any other assertObject(..) that get
154 // nested inside, avoiding concurrent-modification
155 // exceptions, depending on code paths of the actions.
156
157 if ( ! this.firing )
158 {
159 try
160 {
161 this.firing = true;
162
163 while ( ! agenda.isEmpty() )
164 {
165 getAgenda().fireNextItem();
166 }
167 }
168 finally
169 {
170 this.firing = false;
171 }
172 }
173 }
174
175 /*** Assert a new fact object into this working memory.
176 *
177 * @param object The object to assert.
178 *
179 * @throws AssertionException if an error occurs during assertion.
180 */
181 public synchronized void assertObject(Object object) throws AssertionException
182 {
183 getRuleBase().assertObject( object,
184 this );
185
186 fireAgenda();
187 }
188
189 /*** Retract a fact object from this working memory.
190 *
191 * @param object The object to retract.
192 *
193 * @throws RetractionException if an error occurs during retraction.
194 */
195 public synchronized void retractObject(Object object) throws RetractionException
196 {
197 getRuleBase().retractObject( object,
198 this );
199 }
200
201 /*** Modify a fact object in this working memory.
202 *
203 * With the exception of time-based nodes, modification of
204 * a fact object is semantically equivelent to retracting and
205 * re-asserting it.
206 *
207 * @param object The object to modify.
208 *
209 * @throws FactException if an error occurs during modification.
210 */
211 public synchronized void modifyObject(Object object) throws FactException
212 {
213 getRuleBase().modifyObject( object,
214 this );
215
216 fireAgenda();
217 }
218
219 /*** Retrieve the <code>JoinMemory</code> for a particular <code>JoinNode</code>.
220 *
221 * @param node The <code>JoinNode</code> key.
222 *
223 * @return The node's memory.
224 */
225 public JoinMemory getJoinMemory(JoinNode node)
226 {
227 JoinMemory memory = (JoinMemory) this.joinMemories.get( node );
228
229 if ( memory == null )
230 {
231 memory = new JoinMemoryImpl( (JoinNodeImpl) node );
232
233 this.joinMemories.put( node,
234 memory );
235 }
236
237 return memory;
238 }
239
240 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
241 // java.lang.Object
242 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
243
244 /*** Produce a debug string.
245 *
246 * @return A debug string.
247 */
248 public String toString()
249 {
250 return "[WorkingMemory: " + this.joinMemories + "]";
251 }
252
253 }
This page was automatically generated by Maven