1 package org.drools.jsr94.rules;
2
3 /*
4 $Id: StatefulRuleSessionImpl.java,v 1.5 2003/06/19 09:28:35 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.AssertionException;
50 import org.drools.DroolsException;
51 import org.drools.RetractionException;
52 import org.drools.jsr94.rules.admin.RuleExecutionSetImpl;
53 import org.drools.jsr94.rules.admin.RuleExecutionSetRepository;
54
55 import javax.rules.*;
56 import java.util.ArrayList;
57 import java.util.List;
58 import java.util.Map;
59
60 /***
61 * This interface is a representation of a stateful rules engine session.
62 * A stateful rules engine session exposes a stateful rule execution API to an underlying rules engine.
63 * The session allows arbitrary objects to be added and removed to and from the working memory of the associated rules engine.
64 * Additionally, objects currently in the working memory may be updated, and the working memory itself can be iterated.
65 *
66 * @see StatefulRuleSession
67 *
68 * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler</a>
69 */
70 public class StatefulRuleSessionImpl extends RuleSessionImpl implements StatefulRuleSession
71 {
72
73 /*** the rule set from the repository */
74 private RuleExecutionSetImpl ruleSet;
75
76 /*** The working memory this session is associated with. */
77 private JSR94TransactionalWorkingMemory workingMemory;
78
79 /***
80 * Gets the <code>RuleExecutionSet</code> for this URI and associated it with a RuleBase.
81 *
82 * @param bindUri the URI the <code>RuleExecutionSet</code> has been bound to
83 * @throws RuleExecutionSetNotFoundException if there is no rule set under the given URI
84 */
85 StatefulRuleSessionImpl( String bindUri, Map properties ) throws RuleExecutionSetNotFoundException
86 {
87
88 // get the rule set from the repository
89 RuleExecutionSetRepository repository = RuleExecutionSetRepository.getInstance();
90 ruleSet = (RuleExecutionSetImpl) repository.getRuleExecutionSet( bindUri );
91 if ( ruleSet == null ) throw new RuleExecutionSetNotFoundException( "no execution set bound to: " + bindUri );
92
93 // Note: this breaks the factory intension of RuleBase.createTransactionalWorkingMemory
94 workingMemory = new JSR94TransactionalWorkingMemory( ruleSet.getRuleBase() );
95 workingMemory.setApplicationData( properties );
96
97 }
98
99 /***
100 * Returns true if the given object is contained within the working memory of this rule session.
101 *
102 * @see StatefulRuleSessionImpl#containsObject
103 */
104 public boolean containsObject( Handle handle )
105 {
106 return workingMemory.getObjectHandles().contains( handle );
107 }
108
109 /***
110 * Adds a given object to the working memory of this rule session.
111 * The argument to this method is Object because in the non-managed env. not all objects should have
112 * to implement Serialzable.
113 * If the RuleSession is Serializable and it contains non-serializable fields a runtime exception will be thrown.
114 *
115 * @see StatefulRuleSessionImpl#addObject
116 */
117 public Handle addObject( Object object ) throws InvalidRuleSessionException
118 {
119 try
120 {
121 Handle handle = workingMemory.getNextHandle();
122 workingMemory.assertObjectForHandle( handle, object );
123 return handle;
124 }
125 catch ( AssertionException ex )
126 {
127 throw new InvalidRuleSessionException( "cannot assert object", ex );
128 }
129 }
130
131 /***
132 * Adds a List of Objects to the working memory of this rule session.
133 *
134 * @see StatefulRuleSessionImpl#addObjects
135 */
136 public List addObjects( List list ) throws InvalidRuleSessionException
137 {
138
139 // return a list of handles
140 List retList = new ArrayList();
141 for ( int i = 0; i < list.size(); i++ )
142 {
143 retList.add( addObject( list.get( i ) ) );
144 }
145 return retList;
146 }
147
148 /***
149 * Notifies the rules engine that a given object in working memory has changed.
150 * The semantics of this call are equivalent to calling <code>removeObjectForHandle</code>
151 * followed by <code>addObject</code>.
152 * The original Handle is rebound to the new value for the Object however.
153 *
154 * @see StatefulRuleSessionImpl#updateObject
155 */
156 public void updateObject( Handle handle, Object object ) throws InvalidRuleSessionException
157 {
158 try
159 {
160 workingMemory.removeObjectForHandle( handle );
161 workingMemory.assertObjectForHandle( handle, object );
162 }
163 catch ( RetractionException ex )
164 {
165 throw new InvalidRuleSessionException( "cannot retract object", ex );
166 }
167 catch ( AssertionException ex )
168 {
169 throw new InvalidRuleSessionException( "cannot assert object", ex );
170 }
171 }
172
173 /***
174 * Removes a given object from the working memory of this rule session.
175 *
176 * @see StatefulRuleSessionImpl#removeObject
177 */
178 public void removeObject( Handle handle ) throws InvalidRuleSessionException
179 {
180 try
181 {
182 workingMemory.removeObjectForHandle( handle );
183 }
184 catch ( RetractionException ex )
185 {
186 throw new InvalidRuleSessionException( "cannot retract object", ex );
187 }
188 }
189
190 /***
191 * Returns a List of all objects in the working memory of this rule session
192 * that pass the default <code>RuleExecutionSet</code> filter (if present).
193 *
194 * @see StatefulRuleSessionImpl#getObjects
195 */
196 public List getObjects()
197 {
198
199 List outList = new ArrayList();
200 outList.addAll( workingMemory.getObjects() );
201
202 // apply the default filter
203 ObjectFilter objectFilter = ruleSet.getObjectFilter();
204 if ( objectFilter != null )
205 {
206
207 // apply the filter
208 List cpyList = new ArrayList();
209 for ( int i = 0; i < outList.size(); i++ )
210 {
211 Object obj = objectFilter.filter( outList.get( i ) );
212 if ( obj != null ) cpyList.add( obj );
213 }
214 outList = cpyList;
215 }
216
217 return outList;
218 }
219
220 /***
221 * Returns a List over the objects in the working memory of this rule session based upon a given object filter.
222 *
223 * @see StatefulRuleSessionImpl#getObjects(ObjectFilter)
224 */
225 public List getObjects( ObjectFilter objectFilter )
226 {
227
228 List outList = new ArrayList();
229 outList.addAll( workingMemory.getObjects() );
230
231 // apply the filter
232 List cpyList = new ArrayList();
233 for ( int i = 0; i < outList.size(); i++ )
234 {
235 Object obj = objectFilter.filter( outList.get( i ) );
236 if ( obj != null ) cpyList.add( obj );
237 }
238
239 return cpyList;
240 }
241
242 /***
243 * Executes the rules in the bound rule execution set using the objects present in working memory
244 * until no rule is executable anymore.
245 *
246 * @see StatefulRuleSessionImpl#executeRules
247 */
248 public void executeRules() throws InvalidRuleSessionException
249 {
250 try
251 {
252 workingMemory.commit();
253 }
254 catch ( DroolsException ex )
255 {
256 throw new InvalidRuleSessionException( "cannot commit working memory", ex );
257 }
258 }
259
260 /***
261 * Resets this rule session. Calling this method will remove all objects from the working memory
262 * of this rule session and will reset any other state associated with this rule session.
263 *
264 * @see StatefulRuleSessionImpl#reset
265 */
266 public void reset()
267 {
268 workingMemory.abort();
269 workingMemory = new JSR94TransactionalWorkingMemory( ruleSet.getRuleBase() );
270 }
271
272 /***
273 * Returns the Object within the <code>StatefulRuleSession</code> associated with a Handle.
274 *
275 * @see StatefulRuleSessionImpl#getObject
276 */
277 public Object getObject( Handle handle )
278 {
279 return workingMemory.getObject( handle );
280 }
281
282 /***
283 * Releases all resources used by this rule session.
284 * This method renders this rule session unusable until it is reacquired through the RuleRuntime.
285 */
286 public void release()
287 {
288 }
289 }
This page was automatically generated by Maven