|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
StatelessRuleSessionImpl.java | 100% | 93.8% | 100% | 95.7% |
|
1 | package org.drools.jsr94.rules; | |
2 | ||
3 | /* | |
4 | * $Id: StatelessRuleSessionImpl.java,v 1.12 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.RuleExecutionSetNotFoundException; | |
51 | import javax.rules.StatelessRuleSession; | |
52 | ||
53 | import org.drools.FactException; | |
54 | import org.drools.WorkingMemory; | |
55 | import org.drools.jsr94.rules.admin.RuleExecutionSetImpl; | |
56 | import org.drools.jsr94.rules.admin.RuleExecutionSetRepository; | |
57 | ||
58 | /** | |
59 | * The Drools implementation of the <code>StatelessRuleSession</code> interface | |
60 | * which is a representation of a stateless rules engine session. A stateless | |
61 | * rules engine session exposes a stateless rule execution API to an underlying | |
62 | * rules engine. | |
63 | * | |
64 | * @see StatelessRuleSession | |
65 | * | |
66 | * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a> | |
67 | */ | |
68 | public class StatelessRuleSessionImpl extends AbstractRuleSessionImpl | |
69 | implements StatelessRuleSession | |
70 | { | |
71 | /** | |
72 | * Gets the <code>RuleExecutionSet</code> for this URI and associates it | |
73 | * with a RuleBase. | |
74 | * | |
75 | * @param bindUri the URI the <code>RuleExecutionSet</code> has been bound | |
76 | * to | |
77 | * @param properties additional properties used to create the | |
78 | * <code>RuleSession</code> implementation. | |
79 | * | |
80 | * @throws RuleExecutionSetNotFoundException if there is no rule set under | |
81 | * the given URI | |
82 | */ | |
83 | 41 | StatelessRuleSessionImpl( String bindUri, Map properties ) |
84 | throws RuleExecutionSetNotFoundException | |
85 | { | |
86 | 41 | this.setProperties( properties ); |
87 | ||
88 | 41 | RuleExecutionSetRepository repository = |
89 | RuleExecutionSetRepository.getInstance( ); | |
90 | ||
91 | 41 | RuleExecutionSetImpl ruleSet = |
92 | ( RuleExecutionSetImpl ) repository.getRuleExecutionSet( bindUri ); | |
93 | ||
94 | 41 | if ( ruleSet == null ) |
95 | { | |
96 | 1 | throw new RuleExecutionSetNotFoundException( |
97 | "RuleExecutionSet unbound: " + bindUri ); | |
98 | } | |
99 | ||
100 | 40 | this.setRuleExecutionSet( ruleSet ); |
101 | } | |
102 | ||
103 | /** | |
104 | * Executes the rules in the bound rule execution set using the supplied | |
105 | * list of objects. A <code>List</code> is returned containing the objects | |
106 | * created by (or passed into the rule session) the executed rules that | |
107 | * pass the filter test of the default <code>RuleExecutionSet</code> | |
108 | * <code>ObjectFilter</code> (if present). | |
109 | * <p/> | |
110 | * The returned list may not neccessarily include all objects passed, and | |
111 | * may include <code>Object</code>s created by side-effects. The execution | |
112 | * of a <code>RuleExecutionSet</code> can add, remove and update objects. | |
113 | * Therefore the returned object list is dependent on the rules that are | |
114 | * part of the executed <code>RuleExecutionSet</code> as well as Drools | |
115 | * specific rule engine behavior. | |
116 | * | |
117 | * @param objects the objects used to execute rules. | |
118 | * | |
119 | * @return a <code>List</code> containing the objects | |
120 | * as a result of executing the rules. | |
121 | * | |
122 | * @throws InvalidRuleSessionException on illegal rule session state. | |
123 | */ | |
124 | 6 | public List executeRules( List objects ) throws InvalidRuleSessionException |
125 | { | |
126 | 6 | return this.executeRules( |
127 | objects, this.getRuleExecutionSet( ).getObjectFilter( ) ); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Executes the rules in the bound rule execution set using the supplied | |
132 | * list of objects. A <code>List</code> is returned containing the objects | |
133 | * created by (or passed into the rule engine) the executed rules and | |
134 | * filtered with the supplied object filter. | |
135 | * <p/> | |
136 | * The returned list may not neccessarily include all objects passed, and | |
137 | * may include <code>Object</code>s created by side-effects. The execution | |
138 | * of a <code>RuleExecutionSet</code> can add, remove and update objects. | |
139 | * Therefore the returned object list is dependent on the rules that are | |
140 | * part of the executed <code>RuleExecutionSet</code> as well as Drools | |
141 | * specific rule engine behavior. | |
142 | * | |
143 | * @param objects the objects used to execute rules. | |
144 | * @param filter the object filter. | |
145 | * | |
146 | * @return a <code>List</code> containing the objects as a result | |
147 | * of executing rules, after passing through the supplied | |
148 | * object filter. | |
149 | * | |
150 | * @throws InvalidRuleSessionException on illegal rule session state. | |
151 | */ | |
152 | 8 | public List executeRules( List objects, ObjectFilter filter ) |
153 | throws InvalidRuleSessionException | |
154 | { | |
155 | 8 | WorkingMemory workingMemory = this.newWorkingMemory( ); |
156 | ||
157 | 8 | try |
158 | { | |
159 | 8 | for ( Iterator objectIter = objects.iterator( ); |
160 | 266 | objectIter.hasNext( ); ) |
161 | { | |
162 | 258 | workingMemory.assertObject( objectIter.next( ) ); |
163 | } | |
164 | ||
165 | 8 | workingMemory.fireAllRules( ); |
166 | } | |
167 | catch ( FactException e ) | |
168 | { | |
169 | 0 | throw new InvalidRuleSessionException( e.getMessage( ), e ); |
170 | } | |
171 | ||
172 | 8 | List results = workingMemory.getObjects( ); |
173 | ||
174 | 8 | this.applyFilter( results, filter ); |
175 | ||
176 | 8 | return results; |
177 | } | |
178 | } |
|