|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractRuleSessionImpl.java | 70% | 86.8% | 100% | 84.7% |
|
1 | package org.drools.jsr94.rules; | |
2 | ||
3 | /* | |
4 | * $Id: AbstractRuleSessionImpl.java,v 1.1 2004/12/05 20:25:15 dbarnett Exp $ | |
5 | * | |
6 | * Copyright 2002-2004 (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 registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import java.util.Iterator; | |
45 | import java.util.List; | |
46 | import java.util.Map; | |
47 | ||
48 | import javax.rules.InvalidRuleSessionException; | |
49 | import javax.rules.ObjectFilter; | |
50 | import javax.rules.RuleExecutionSetMetadata; | |
51 | import javax.rules.RuleRuntime; | |
52 | import javax.rules.RuleSession; | |
53 | import javax.rules.StatefulRuleSession; | |
54 | import javax.rules.StatelessRuleSession; | |
55 | import javax.rules.admin.RuleExecutionSet; | |
56 | ||
57 | import org.drools.WorkingMemory; | |
58 | import org.drools.jsr94.rules.admin.RuleExecutionSetImpl; | |
59 | import org.drools.jsr94.rules.admin.RuleExecutionSetRepository; | |
60 | ||
61 | /** | |
62 | * The Drools implementation of the <code>RuleSession</code> interface which is | |
63 | * a representation of a client session with a rules engine. A rules engine | |
64 | * session serves as an entry point into an underlying rules engine. The | |
65 | * <code>RuleSession</code> is bound to a rules engine instance and exposes a | |
66 | * vendor-neutral rule processing API for executing <code>Rule</code>s within a | |
67 | * bound <code>RuleExecutionSet</code>. | |
68 | * <p/> | |
69 | * Note: the <code>release</code> method must be called to clean up all | |
70 | * resources used by the <code>RuleSession</code>. Calling <code>release</code> | |
71 | * may make the <code>RuleSession</code> eligible to be returned to a | |
72 | * <code>RuleSession</code> pool. | |
73 | * | |
74 | * @see RuleSession | |
75 | * | |
76 | * @author N. Alex Rupp (n_alex <at>codehaus.org) | |
77 | * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a> | |
78 | */ | |
79 | abstract class AbstractRuleSessionImpl implements RuleSession | |
80 | { | |
81 | /** | |
82 | * The Drools <code>WorkingMemory</code> associated | |
83 | * with this <code>RuleSession</code>. | |
84 | */ | |
85 | private WorkingMemory workingMemory; | |
86 | ||
87 | /** | |
88 | * The Drools <code>RuleExecutionSet</code> associated | |
89 | * with this <code>RuleSession</code>. | |
90 | */ | |
91 | private RuleExecutionSetImpl ruleExecutionSet; | |
92 | ||
93 | /** | |
94 | * A <code>Map</code> of <code>String</code>/<code>Object</code> pairs | |
95 | * passed as application data to the Drools <code>WorkingMemory</code>. | |
96 | */ | |
97 | private Map properties; | |
98 | ||
99 | /** | |
100 | * Initialize this <code>RuleSession</code> | |
101 | * with a new <code>WorkingMemory</code>. | |
102 | * | |
103 | * @see #newWorkingMemory() | |
104 | */ | |
105 | 37 | protected void initWorkingMemory( ) |
106 | { | |
107 | 37 | this.setWorkingMemory( this.newWorkingMemory( ) ); |
108 | } | |
109 | ||
110 | /** | |
111 | * Creates a new <code>WorkingMemory</code> for this | |
112 | * <code>RuleSession</code>. All properties set prior to calling this method | |
113 | * are added as application data to the new <code>WorkingMemory</code>. | |
114 | * The created <code>WorkingMemory</code> uses the default conflict | |
115 | * resolution strategy. | |
116 | * | |
117 | * @return the new <code>WorkingMemory</code>. | |
118 | * | |
119 | * @see #setProperties(Map) | |
120 | * @see WorkingMemory#setApplicationData(String, Object) | |
121 | */ | |
122 | 45 | protected WorkingMemory newWorkingMemory( ) |
123 | { | |
124 | 45 | WorkingMemory newWorkingMemory = |
125 | this.getRuleExecutionSet( ).newWorkingMemory( ); | |
126 | ||
127 | 45 | Map props = this.getProperties( ); |
128 | 45 | if ( props != null ) |
129 | { | |
130 | 0 | for ( Iterator iterator = props.entrySet( ).iterator( ); |
131 | 0 | iterator.hasNext( ); ) |
132 | { | |
133 | 0 | Map.Entry entry = ( Map.Entry ) iterator.next( ); |
134 | 0 | newWorkingMemory.setApplicationData( |
135 | ( String ) entry.getKey( ), entry.getValue( ) ); | |
136 | } | |
137 | } | |
138 | ||
139 | 45 | return newWorkingMemory; |
140 | } | |
141 | ||
142 | /** | |
143 | * Sets additional properties used to create this <code>RuleSession</code>. | |
144 | * | |
145 | * @param properties additional properties used to create the | |
146 | * <code>RuleSession</code> implementation. | |
147 | */ | |
148 | 87 | protected void setProperties( Map properties ) |
149 | { | |
150 | 87 | this.properties = properties; |
151 | } | |
152 | ||
153 | /** | |
154 | * Returns the additional properties used to create this | |
155 | * <code>RuleSession</code>. | |
156 | * | |
157 | * @return the additional properties used to create this | |
158 | * <code>RuleSession</code>. | |
159 | */ | |
160 | 45 | protected Map getProperties( ) |
161 | { | |
162 | 45 | return this.properties; |
163 | } | |
164 | ||
165 | /** | |
166 | * Sets the Drools <code>WorkingMemory</code> associated | |
167 | * with this <code>RuleSession</code>. | |
168 | * | |
169 | * @param workingMemory the <code>WorkingMemory</code> to associate | |
170 | * with this <code>RuleSession</code>. | |
171 | */ | |
172 | 48 | protected void setWorkingMemory( WorkingMemory workingMemory ) |
173 | { | |
174 | 48 | this.workingMemory = workingMemory; |
175 | } | |
176 | ||
177 | /** | |
178 | * Returns the Drools <code>WorkingMemory</code> associated | |
179 | * with this <code>RuleSession</code>. | |
180 | * | |
181 | * @return the Drools <code>WorkingMemory</code> to associate | |
182 | * with this <code>RuleSession</code>. | |
183 | */ | |
184 | 57 | protected WorkingMemory getWorkingMemory( ) |
185 | { | |
186 | 57 | return this.workingMemory; |
187 | } | |
188 | ||
189 | /** | |
190 | * Sets the Drools <code>RuleExecutionSet</code> associated | |
191 | * with this <code>RuleSession</code>. | |
192 | * | |
193 | * @param ruleExecutionSet the Drools <code>RuleExecutionSet</code> to associate | |
194 | * with this <code>RuleSession</code>. | |
195 | */ | |
196 | 85 | protected void setRuleExecutionSet( RuleExecutionSetImpl ruleExecutionSet ) |
197 | { | |
198 | 85 | this.ruleExecutionSet = ruleExecutionSet; |
199 | } | |
200 | ||
201 | /** | |
202 | * Returns the Drools <code>RuleExecutionSet</code> associated | |
203 | * with this <code>RuleSession</code>. | |
204 | * | |
205 | * @return the Drools <code>RuleExecutionSet</code> associated | |
206 | * with this <code>RuleSession</code>. | |
207 | */ | |
208 | 56 | protected RuleExecutionSetImpl getRuleExecutionSet( ) |
209 | { | |
210 | 56 | return this.ruleExecutionSet; |
211 | } | |
212 | ||
213 | /** | |
214 | * Ensures this <code>RuleSession</code> is not | |
215 | * in an illegal rule session state. | |
216 | * | |
217 | * @throws InvalidRuleSessionException on illegal rule session state. | |
218 | */ | |
219 | 55 | protected void checkRuleSessionValidity( ) |
220 | throws InvalidRuleSessionException | |
221 | { | |
222 | 55 | if ( this.workingMemory == null ) |
223 | { | |
224 | 0 | throw new InvalidRuleSessionException( "invalid rule session" ); |
225 | } | |
226 | } | |
227 | ||
228 | /** | |
229 | * Applies the given <code>ObjectFilter</code> to the <code>List</code> of | |
230 | * <code>Object</code>s, removing all <code>Object</code>s from the given | |
231 | * <code>List</code> that do not pass the filter. | |
232 | * | |
233 | * @param objects <code>List</code> of <code>Object</code>s to be filtered | |
234 | * @param objectFilter the <code>ObjectFilter</code> to be applied | |
235 | */ | |
236 | 16 | protected void applyFilter( List objects, ObjectFilter objectFilter ) |
237 | { | |
238 | 16 | if ( objectFilter != null ) |
239 | { | |
240 | 5 | for ( Iterator objectIter = objects.iterator( ); |
241 | 23 | objectIter.hasNext( ); ) |
242 | { | |
243 | 18 | if ( objectFilter.filter( objectIter.next( ) ) == null ) |
244 | { | |
245 | 8 | objectIter.remove( ); |
246 | } | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | // JSR94 interface methods start here ------------------------------------- | |
252 | ||
253 | /** | |
254 | * Returns the meta data for the rule execution set bound to this rule | |
255 | * session. | |
256 | * | |
257 | * @return the RuleExecutionSetMetaData bound to this rule session. | |
258 | */ | |
259 | 3 | public RuleExecutionSetMetadata getRuleExecutionSetMetadata( ) |
260 | { | |
261 | 3 | RuleExecutionSetRepository repository = |
262 | RuleExecutionSetRepository.getInstance( ); | |
263 | ||
264 | 3 | String theBindUri = null; |
265 | 3 | for ( Iterator i = repository.getRegistrations( ).iterator( ); |
266 | 4 | i.hasNext( ); ) |
267 | { | |
268 | 4 | String aBindUri = ( String ) i.next( ); |
269 | 4 | RuleExecutionSet aRuleSet = |
270 | repository.getRuleExecutionSet( aBindUri ); | |
271 | 4 | if ( aRuleSet == this.ruleExecutionSet ) |
272 | { | |
273 | 3 | theBindUri = aBindUri; |
274 | 3 | break; |
275 | } | |
276 | } | |
277 | ||
278 | 3 | return new RuleExecutionSetMetadataImpl( |
279 | theBindUri, | |
280 | this.ruleExecutionSet.getName( ), | |
281 | this.ruleExecutionSet.getDescription( ) ); | |
282 | } | |
283 | ||
284 | /** | |
285 | * Returns the type identifier for this <code>RuleSession</code>. The | |
286 | * type identifiers are defined in the <code>RuleRuntime</code> interface. | |
287 | * | |
288 | * @return the type identifier for this <code>RuleSession</code> | |
289 | * | |
290 | * @throws InvalidRuleSessionException on illegal rule session state. | |
291 | * | |
292 | * @see RuleRuntime#STATEFUL_SESSION_TYPE | |
293 | * @see RuleRuntime#STATELESS_SESSION_TYPE | |
294 | */ | |
295 | 2 | public int getType( ) throws InvalidRuleSessionException |
296 | { | |
297 | 2 | if ( this instanceof StatelessRuleSession ) |
298 | { | |
299 | 1 | return RuleRuntime.STATELESS_SESSION_TYPE; |
300 | } | |
301 | ||
302 | 1 | if ( this instanceof StatefulRuleSession ) |
303 | { | |
304 | 1 | return RuleRuntime.STATEFUL_SESSION_TYPE; |
305 | } | |
306 | ||
307 | 0 | throw new InvalidRuleSessionException( "unknown type" ); |
308 | } | |
309 | ||
310 | /** | |
311 | * Releases all resources used by this rule session. | |
312 | * This method renders this rule session unusable until | |
313 | * it is reacquired through the <code>RuleRuntime</code>. | |
314 | */ | |
315 | 11 | public void release( ) |
316 | { | |
317 | 11 | this.setProperties( null ); |
318 | 11 | this.setWorkingMemory( null ); |
319 | 11 | this.setRuleExecutionSet( null ); |
320 | } | |
321 | ||
322 | /** | |
323 | * Resets this rule session. Calling this method will bring the rule session | |
324 | * state to its initial state for this rule session and will reset any other | |
325 | * state associated with this rule session. | |
326 | * <p/> | |
327 | * A reset will not reset the state on the default object filter for a | |
328 | * <code>RuleExecutionSet</code>. | |
329 | */ | |
330 | 3 | public void reset( ) |
331 | { | |
332 | 3 | this.initWorkingMemory( ); |
333 | } | |
334 | } |
|