|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
RuleExecutionSetImpl.java | 60% | 70.6% | 100% | 74.1% |
|
1 | package org.drools.jsr94.rules.admin; | |
2 | ||
3 | /* | |
4 | * $Id: RuleExecutionSetImpl.java,v 1.22 2005/02/04 02:13:38 mproctor 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.ArrayList; | |
45 | import java.util.HashMap; | |
46 | import java.util.List; | |
47 | import java.util.Map; | |
48 | ||
49 | import javax.rules.ObjectFilter; | |
50 | import javax.rules.admin.RuleExecutionSet; | |
51 | ||
52 | import org.drools.RuleBase; | |
53 | import org.drools.RuleBaseBuilder; | |
54 | import org.drools.RuleIntegrationException; | |
55 | import org.drools.RuleSetIntegrationException; | |
56 | import org.drools.WorkingMemory; | |
57 | import org.drools.jsr94.rules.Jsr94FactHandleFactory; | |
58 | import org.drools.rule.Rule; | |
59 | import org.drools.rule.RuleSet; | |
60 | ||
61 | /** | |
62 | * The Drools implementation of the <code>RuleExecutionSet</code> interface | |
63 | * which defines a named set of executable <code>Rule</code> instances. A | |
64 | * <code>RuleExecutionSet</code> can be executed by a rules engine via the | |
65 | * <code>RuleSession</code> interface. | |
66 | * | |
67 | * @see RuleExecutionSet | |
68 | * | |
69 | * @author N. Alex Rupp (n_alex <at>codehaus.org) | |
70 | * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a> | |
71 | */ | |
72 | public class RuleExecutionSetImpl implements RuleExecutionSet | |
73 | { | |
74 | /** | |
75 | * A description of this rule execution set or null if no | |
76 | * description is specified. | |
77 | */ | |
78 | private String description; | |
79 | ||
80 | /** | |
81 | * The default ObjectFilter class name | |
82 | * associated with this rule execution set. | |
83 | */ | |
84 | private String defaultObjectFilterClassName; | |
85 | ||
86 | /** A <code>Map</code> of user-defined and Drools-defined properties. */ | |
87 | private Map properties; | |
88 | ||
89 | /** | |
90 | * The <code>RuleBase</code> associated with this | |
91 | * <code>RuleExecutionSet</code>. | |
92 | */ | |
93 | private RuleBase ruleBase; | |
94 | ||
95 | /** | |
96 | * The <code>RuleSet</code> associated with this | |
97 | * <code>RuleExecutionSet</code>. | |
98 | */ | |
99 | private RuleSet ruleSet; | |
100 | ||
101 | /** | |
102 | * The default ObjectFilter class name | |
103 | * associated with this rule execution set. | |
104 | */ | |
105 | private ObjectFilter objectFilter; | |
106 | ||
107 | /** | |
108 | * Instances of this class should be obtained from the | |
109 | * <code>LocalRuleExecutionSetProviderImpl</code>. Each | |
110 | * <code>RuleExecutionSetImpl</code> corresponds with an | |
111 | * <code>org.drools.RuleSet</code> object. | |
112 | * | |
113 | * @param ruleSet The <code>RuleSet</code> to associate with this | |
114 | * <code>RuleExecutionSet</code>. | |
115 | * @param properties A <code>Map</code> of user-defined and | |
116 | * Drools-defined properties. May be <code>null</code>. | |
117 | * | |
118 | * @throws RuleIntegrationException if an error occurs integrating | |
119 | * a <code>Rule</code> or <code>RuleSet</code> | |
120 | * into the <code>RuleBase</code> | |
121 | * @throws RuleSetIntegrationException if an error occurs integrating | |
122 | * a <code>Rule</code> or <code>RuleSet</code> | |
123 | * into the <code>RuleBase</code> | |
124 | */ | |
125 | 61 | RuleExecutionSetImpl( RuleSet ruleSet, Map properties ) |
126 | throws RuleIntegrationException, RuleSetIntegrationException | |
127 | { | |
128 | 61 | if ( null == properties ) |
129 | { | |
130 | 60 | this.properties = new HashMap( ); |
131 | } | |
132 | else | |
133 | { | |
134 | 1 | this.properties = properties; |
135 | } | |
136 | 61 | this.ruleSet = ruleSet; |
137 | 61 | this.description = ruleSet.getDocumentation( ); |
138 | ||
139 | 61 | RuleBaseBuilder builder = new RuleBaseBuilder( ); |
140 | 61 | builder.setFactHandleFactory( Jsr94FactHandleFactory.getInstance( ) ); |
141 | ||
142 | 61 | builder.addRuleSet( ruleSet ); |
143 | 61 | this.ruleBase = builder.build( ); |
144 | } | |
145 | ||
146 | /** | |
147 | * Get an instance of the default filter, or null. | |
148 | * | |
149 | * @return An instance of the default filter, or null. | |
150 | */ | |
151 | 11 | public synchronized ObjectFilter getObjectFilter( ) |
152 | { | |
153 | 11 | if ( this.objectFilter != null ) |
154 | { | |
155 | 0 | return this.objectFilter; |
156 | } | |
157 | ||
158 | 11 | if ( this.defaultObjectFilterClassName != null ) |
159 | { | |
160 | 0 | ClassLoader cl = Thread.currentThread( ).getContextClassLoader( ); |
161 | ||
162 | 0 | if ( cl == null ) |
163 | { | |
164 | 0 | cl = RuleExecutionSetImpl.class.getClassLoader( ); |
165 | } | |
166 | ||
167 | 0 | try |
168 | { | |
169 | 0 | Class filterClass = |
170 | cl.loadClass( this.defaultObjectFilterClassName ); | |
171 | 0 | this.objectFilter = ( ObjectFilter ) filterClass.newInstance( ); |
172 | } | |
173 | catch ( ClassNotFoundException e ) | |
174 | { | |
175 | 0 | throw new RuntimeException( e.toString( ) ); |
176 | } | |
177 | catch ( InstantiationException e ) | |
178 | { | |
179 | 0 | throw new RuntimeException( e.toString( ) ); |
180 | } | |
181 | catch ( IllegalAccessException e ) | |
182 | { | |
183 | 0 | throw new RuntimeException( e.toString( ) ); |
184 | } | |
185 | } | |
186 | ||
187 | 11 | return this.objectFilter; |
188 | } | |
189 | ||
190 | /** | |
191 | * Returns a new WorkingMemory object. | |
192 | * | |
193 | * @return A new WorkingMemory object. | |
194 | */ | |
195 | 45 | public WorkingMemory newWorkingMemory( ) |
196 | { | |
197 | 45 | return this.ruleBase.newWorkingMemory( ); |
198 | } | |
199 | ||
200 | // JSR94 interface methods start here ------------------------------------- | |
201 | ||
202 | /** | |
203 | * Get the name of this rule execution set. | |
204 | * | |
205 | * @return The name of this rule execution set. | |
206 | */ | |
207 | 11 | public String getName( ) |
208 | { | |
209 | 11 | return this.ruleSet.getName( ); |
210 | } | |
211 | ||
212 | /** | |
213 | * Get a description of this rule execution set. | |
214 | * | |
215 | * @return A description of this rule execution set or null of no | |
216 | * description is specified. | |
217 | */ | |
218 | 5 | public String getDescription( ) |
219 | { | |
220 | 5 | return this.description; |
221 | } | |
222 | ||
223 | /** | |
224 | * Get a user-defined or Drools-defined property. | |
225 | * | |
226 | * @param key the key to use to retrieve the property | |
227 | * | |
228 | * @return the value bound to the key or null | |
229 | */ | |
230 | 2 | public Object getProperty( Object key ) |
231 | { | |
232 | 2 | return this.properties.get( key ); |
233 | } | |
234 | ||
235 | /** | |
236 | * Set a user-defined or Drools-defined property. | |
237 | * | |
238 | * @param key the key for the property value | |
239 | * @param value the value to associate with the key | |
240 | */ | |
241 | 2 | public void setProperty( Object key, Object value ) |
242 | { | |
243 | 2 | this.properties.put( key, value ); |
244 | } | |
245 | ||
246 | /** | |
247 | * Set the default <code>ObjectFilter</code> class. This class is | |
248 | * instantiated at runtime and used to filter result objects unless | |
249 | * another filter is specified using the available APIs in the runtime | |
250 | * view of a rule engine. | |
251 | * <p/> | |
252 | * Setting the class name to null removes the default | |
253 | * <code>ObjectFilter</code>. | |
254 | * | |
255 | * @param objectFilterClassname the default <code>ObjectFilter</code> class | |
256 | */ | |
257 | 1 | public void setDefaultObjectFilter( String objectFilterClassname ) |
258 | { | |
259 | 1 | this.defaultObjectFilterClassName = objectFilterClassname; |
260 | } | |
261 | ||
262 | /** | |
263 | * Returns the default ObjectFilter class name | |
264 | * associated with this rule execution set. | |
265 | * | |
266 | * @return the default ObjectFilter class name | |
267 | */ | |
268 | 2 | public String getDefaultObjectFilter( ) |
269 | { | |
270 | 2 | return this.defaultObjectFilterClassName; |
271 | } | |
272 | ||
273 | /** | |
274 | * Return a list of all <code>Rule</code>s that are part of the | |
275 | * <code>RuleExecutionSet</code>. | |
276 | * | |
277 | * @return a list of all <code>Rule</code>s that are part of the | |
278 | * <code>RuleExecutionSet</code>. | |
279 | */ | |
280 | 11 | public List getRules( ) |
281 | { | |
282 | 11 | List jsr94Rules = new ArrayList( ); |
283 | ||
284 | 11 | Rule[] rules = ( this.ruleSet.getRules( ) ); |
285 | 11 | for ( int i = 0; i < rules.length; ++i ) |
286 | { | |
287 | 11 | jsr94Rules.add( new RuleImpl( rules[i] ) ); |
288 | } | |
289 | ||
290 | 11 | return jsr94Rules; |
291 | } | |
292 | } |
|