001: package org.drools.jsr94.rules;
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: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021:
022: import javax.rules.InvalidRuleSessionException;
023: import javax.rules.ObjectFilter;
024: import javax.rules.RuleExecutionSetNotFoundException;
025: import javax.rules.RuleRuntime;
026: import javax.rules.StatelessRuleSession;
027:
028: import org.drools.FactException;
029: import org.drools.StatelessSession;
030: import org.drools.StatelessSessionResult;
031: import org.drools.WorkingMemory;
032: import org.drools.jsr94.rules.admin.RuleExecutionSetImpl;
033: import org.drools.jsr94.rules.admin.RuleExecutionSetRepository;
034:
035: /**
036: * The Drools implementation of the <code>StatelessRuleSession</code>
037: * interface which is a representation of a stateless rules engine session. A
038: * stateless rules engine session exposes a stateless rule execution API to an
039: * underlying rules engine.
040: *
041: * @see StatelessRuleSession
042: *
043: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
044: */
045: public class StatelessRuleSessionImpl extends AbstractRuleSessionImpl
046: implements StatelessRuleSession {
047: /**
048: * Gets the <code>RuleExecutionSet</code> for this URI and associates it
049: * with a RuleBase.
050: *
051: * @param bindUri
052: * the URI the <code>RuleExecutionSet</code> has been bound to
053: * @param properties
054: * additional properties used to create the
055: * <code>RuleSession</code> implementation.
056: *
057: * @throws RuleExecutionSetNotFoundException
058: * if there is no rule set under the given URI
059: */
060: StatelessRuleSessionImpl(final String bindUri,
061: final Map properties,
062: final RuleExecutionSetRepository repository)
063: throws RuleExecutionSetNotFoundException {
064: super (repository);
065: setProperties(properties);
066:
067: final RuleExecutionSetImpl ruleSet = (RuleExecutionSetImpl) repository
068: .getRuleExecutionSet(bindUri);
069:
070: if (ruleSet == null) {
071: throw new RuleExecutionSetNotFoundException(
072: "RuleExecutionSet unbound: " + bindUri);
073: }
074:
075: setRuleExecutionSet(ruleSet);
076: }
077:
078: /**
079: * Initialize this <code>RuleSession</code>
080: * with a new <code>WorkingMemory</code>.
081: */
082: protected StatelessSession newStatelessSession() {
083: final StatelessSession session = this .getRuleExecutionSet()
084: .newStatelessSession();
085:
086: final Map props = this .getProperties();
087: if (props != null) {
088: for (final Iterator iterator = props.entrySet().iterator(); iterator
089: .hasNext();) {
090: final Map.Entry entry = (Map.Entry) iterator.next();
091: session.setGlobal((String) entry.getKey(), entry
092: .getValue());
093: }
094: }
095: return session;
096: }
097:
098: /**
099: * Executes the rules in the bound rule execution set using the supplied
100: * list of objects. A <code>List</code> is returned containing the objects
101: * created by (or passed into the rule session) the executed rules that pass
102: * the filter test of the default <code>RuleExecutionSet</code>
103: * <code>ObjectFilter</code>
104: * (if present). <p/> The returned list may not neccessarily include all
105: * objects passed, and may include <code>Object</code>s created by
106: * side-effects. The execution of a <code>RuleExecutionSet</code> can add,
107: * remove and update objects. Therefore the returned object list is
108: * dependent on the rules that are part of the executed
109: * <code>RuleExecutionSet</code> as well as Drools specific rule engine
110: * behavior.
111: *
112: * @param objects
113: * the objects used to execute rules.
114: *
115: * @return a <code>List</code> containing the objects as a result of
116: * executing the rules.
117: *
118: * @throws InvalidRuleSessionException
119: * on illegal rule session state.
120: */
121: public List executeRules(final List objects)
122: throws InvalidRuleSessionException {
123: return executeRules(objects, this .getRuleExecutionSet()
124: .getObjectFilter());
125: }
126:
127: /**
128: * Executes the rules in the bound rule execution set using the supplied
129: * list of objects. A <code>List</code> is returned containing the objects
130: * created by (or passed into the rule engine) the executed rules and
131: * filtered with the supplied object filter. <p/> The returned list may not
132: * neccessarily include all objects passed, and may include
133: * <code>Object</code>s created by side-effects. The execution of a
134: * <code>RuleExecutionSet</code> can add, remove and update objects.
135: * Therefore the returned object list is dependent on the rules that are
136: * part of the executed <code>RuleExecutionSet</code> as well as Drools
137: * specific rule engine behavior.
138: *
139: * @param objects
140: * the objects used to execute rules.
141: * @param filter
142: * the object filter.
143: *
144: * @return a <code>List</code> containing the objects as a result of
145: * executing rules, after passing through the supplied object
146: * filter.
147: *
148: * @throws InvalidRuleSessionException
149: * on illegal rule session state.
150: */
151: public List executeRules(final List objects,
152: final ObjectFilter filter)
153: throws InvalidRuleSessionException {
154: StatelessSession session = newStatelessSession();
155: StatelessSessionResult results = session
156: .executeWithResults(objects);
157:
158: return IteratorToList.convert(results
159: .iterateObjects(new ObjectFilterAdapter(filter)));
160: }
161:
162: public int getType() throws InvalidRuleSessionException {
163: return RuleRuntime.STATELESS_SESSION_TYPE;
164: }
165:
166: /**
167: * Ensures this <code>RuleSession</code> is not
168: * in an illegal rule session state.
169: *
170: * @throws InvalidRuleSessionException on illegal rule session state.
171: */
172: protected void checkRuleSessionValidity()
173: throws InvalidRuleSessionException {
174: if (getRuleExecutionSet() == null) {
175: throw new InvalidRuleSessionException(
176: "invalid rule session");
177: }
178: }
179: }
|