001: package org.drools.jsr94.rules.admin;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.rules.ObjectFilter;
025: import javax.rules.admin.RuleExecutionSet;
026:
027: import org.drools.IntegrationException;
028: import org.drools.RuleBase;
029: import org.drools.RuleBaseConfiguration;
030: import org.drools.RuleIntegrationException;
031: import org.drools.StatefulSession;
032: import org.drools.StatelessSession;
033: import org.drools.jsr94.rules.Constants;
034: import org.drools.jsr94.rules.Jsr94FactHandleFactory;
035: import org.drools.rule.Package;
036: import org.drools.rule.Rule;
037: import org.drools.util.UUIDGenerator;
038:
039: /**
040: * The Drools implementation of the <code>RuleExecutionSet</code> interface
041: * which defines a named set of executable <code>Rule</code> instances. A
042: * <code>RuleExecutionSet</code> can be executed by a rules engine via the
043: * <code>RuleSession</code> interface.
044: *
045: * @see RuleExecutionSet
046: *
047: * @author N. Alex Rupp (n_alex <at>codehaus.org)
048: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
049: * @author <a href="mailto:michael.frandsen@syngenio.de">michael frandsen </a>
050: */
051: public class RuleExecutionSetImpl implements RuleExecutionSet {
052: /**
053: *
054: */
055: private static final long serialVersionUID = 400L;
056:
057: /**
058: * A description of this rule execution set or null if no
059: * description is specified.
060: */
061: private String description;
062:
063: /**
064: * The default ObjectFilter class name
065: * associated with this rule execution set.
066: */
067: private String defaultObjectFilterClassName;
068:
069: /** A <code>Map</code> of user-defined and Drools-defined properties. */
070: private Map properties;
071:
072: /**
073: * The <code>RuleBase</code> associated with this
074: * <code>RuleExecutionSet</code>.
075: */
076: private RuleBase ruleBase;
077:
078: /**
079: * The <code>Package</code> associated with this
080: * <code>RuleExecutionSet</code>.
081: */
082: private Package pkg;
083:
084: /**
085: * The default ObjectFilter class name
086: * associated with this rule execution set.
087: */
088: private ObjectFilter objectFilter;
089:
090: /**
091: * Instances of this class should be obtained from the
092: * <code>LocalRuleExecutionSetProviderImpl</code>. Each
093: * <code>RuleExecutionSetImpl</code> corresponds with an
094: * <code>org.drools.Package</code> object.
095: *
096: * @param package The <code>Package</code> to associate with this
097: * <code>RuleExecutionSet</code>.
098: * @param properties A <code>Map</code> of user-defined and
099: * Drools-defined properties. May be <code>null</code>.
100: *
101: * @throws RuleIntegrationException if an error occurs integrating
102: * a <code>Rule</code> or <code>Package</code>
103: * into the <code>RuleBase</code>
104: * @throws RuleSetIntegrationException if an error occurs integrating
105: * a <code>Rule</code> or <code>Package</code>
106: * into the <code>RuleBase</code>
107: */
108: RuleExecutionSetImpl(final Package pkg, final Map properties)
109: throws IntegrationException {
110: if (null == properties) {
111: this .properties = new HashMap();
112: } else {
113: this .properties = properties;
114: }
115: this .pkg = pkg;
116: this .description = pkg.getName();//..getDocumentation( );
117:
118: RuleBaseConfiguration config = (RuleBaseConfiguration) this .properties
119: .get(Constants.RES_RULEBASE_CONFIG);
120: org.drools.reteoo.ReteooRuleBase ruleBase;
121: if (config != null) {
122: ruleBase = new org.drools.reteoo.ReteooRuleBase(
123: UUIDGenerator.getInstance()
124: .generateRandomBasedUUID().toString(),
125: config, new Jsr94FactHandleFactory());
126: } else {
127: ruleBase = new org.drools.reteoo.ReteooRuleBase(
128: UUIDGenerator.getInstance()
129: .generateRandomBasedUUID().toString(),
130: new Jsr94FactHandleFactory());
131: }
132: ruleBase.addPackage(pkg);
133:
134: this .ruleBase = ruleBase;
135: }
136:
137: /**
138: * Get an instance of the default filter, or null.
139: *
140: * @return An instance of the default filter, or null.
141: */
142: public synchronized ObjectFilter getObjectFilter() {
143: if (this .objectFilter != null) {
144: return this .objectFilter;
145: }
146:
147: if (this .defaultObjectFilterClassName != null) {
148: ClassLoader cl = Thread.currentThread()
149: .getContextClassLoader();
150:
151: if (cl == null) {
152: cl = RuleExecutionSetImpl.class.getClassLoader();
153: }
154:
155: try {
156: final Class filterClass = cl
157: .loadClass(this .defaultObjectFilterClassName);
158: this .objectFilter = (ObjectFilter) filterClass
159: .newInstance();
160: } catch (final ClassNotFoundException e) {
161: throw new RuntimeException(e.toString());
162: } catch (final InstantiationException e) {
163: throw new RuntimeException(e.toString());
164: } catch (final IllegalAccessException e) {
165: throw new RuntimeException(e.toString());
166: }
167: }
168:
169: return this .objectFilter;
170: }
171:
172: /**
173: * Returns a new WorkingMemory object.
174: *
175: * @return A new WorkingMemory object.
176: */
177: public StatefulSession newStatefulSession(boolean keepReference) {
178: return this .ruleBase.newStatefulSession(keepReference);
179: }
180:
181: /**
182: * Returns a new WorkingMemory object.
183: *
184: * @return A new WorkingMemory object.
185: */
186: public StatelessSession newStatelessSession() {
187: return this .ruleBase.newStatelessSession();
188: }
189:
190: // JSR94 interface methods start here -------------------------------------
191:
192: /**
193: * Get the name of this rule execution set.
194: *
195: * @return The name of this rule execution set.
196: */
197: public String getName() {
198: return this .pkg.getName();
199: }
200:
201: /**
202: * Get a description of this rule execution set.
203: *
204: * @return A description of this rule execution set or null of no
205: * description is specified.
206: */
207: public String getDescription() {
208: return this .description;
209: }
210:
211: /**
212: * Get a user-defined or Drools-defined property.
213: *
214: * @param key the key to use to retrieve the property
215: *
216: * @return the value bound to the key or null
217: */
218: public Object getProperty(final Object key) {
219: return this .properties.get(key);
220: }
221:
222: /**
223: * Set a user-defined or Drools-defined property.
224: *
225: * @param key the key for the property value
226: * @param value the value to associate with the key
227: */
228: public void setProperty(final Object key, final Object value) {
229: this .properties.put(key, value);
230: }
231:
232: /**
233: * Set the default <code>ObjectFilter</code> class. This class is
234: * instantiated at runtime and used to filter result objects unless
235: * another filter is specified using the available APIs in the runtime
236: * view of a rule engine.
237: * <p/>
238: * Setting the class name to null removes the default
239: * <code>ObjectFilter</code>.
240: *
241: * @param objectFilterClassname the default <code>ObjectFilter</code> class
242: */
243: public void setDefaultObjectFilter(
244: final String objectFilterClassname) {
245: this .defaultObjectFilterClassName = objectFilterClassname;
246: }
247:
248: /**
249: * Returns the default ObjectFilter class name
250: * associated with this rule execution set.
251: *
252: * @return the default ObjectFilter class name
253: */
254: public String getDefaultObjectFilter() {
255: return this .defaultObjectFilterClassName;
256: }
257:
258: /**
259: * Return a list of all <code>Rule</code>s that are part of the
260: * <code>RuleExecutionSet</code>.
261: *
262: * @return a list of all <code>Rule</code>s that are part of the
263: * <code>RuleExecutionSet</code>.
264: */
265: public List getRules() {
266: final List jsr94Rules = new ArrayList();
267:
268: final Rule[] rules = (this .pkg.getRules());
269: for (int i = 0; i < rules.length; ++i) {
270: jsr94Rules.add(new RuleImpl(rules[i]));
271: }
272:
273: return jsr94Rules;
274: }
275: }
|