1 package org.drools.jsr94.rules;
2
3 /*
4 $Id: StatelessRuleSessionImpl.java,v 1.5 2003/06/19 09:28:35 tdiesler Exp $
5
6 Copyright 2002 (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
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "drools" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "drools"
27 nor may "drools" appear in their names without prior written
28 permission of The Werken Company. "drools" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://drools.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import org.drools.AssertionException;
50 import org.drools.RuleBase;
51 import org.drools.jsr94.rules.admin.RuleExecutionSetImpl;
52 import org.drools.jsr94.rules.admin.RuleExecutionSetRepository;
53
54 import javax.rules.InvalidRuleSessionException;
55 import javax.rules.ObjectFilter;
56 import javax.rules.RuleExecutionSetNotFoundException;
57 import javax.rules.StatelessRuleSession;
58 import java.util.ArrayList;
59 import java.util.List;
60 import java.util.Map;
61
62 /***
63 * This interface is a representation of a stateless rules engine session.
64 * A stateless rules engine session exposes a stateless rule execution API to an underlying rules engine.
65 *
66 * @see StatelessRuleSession
67 *
68 * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler</a>
69 */
70 public class StatelessRuleSessionImpl extends RuleSessionImpl implements StatelessRuleSession
71 {
72
73 /*** the rule set from the repository */
74 private RuleExecutionSetImpl ruleSet;
75
76 /*** The rule base this session is associated with. */
77 private RuleBase ruleBase;
78 private Map properties;
79
80 /***
81 * Gets the <code>RuleExecutionSet</code> for this URI and associated it with a RuleBase.
82 *
83 * @param bindUri the URI the <code>RuleExecutionSet</code> has been bound to
84 * @throws RuleExecutionSetNotFoundException if there is no rule set under the given URI
85 */
86 StatelessRuleSessionImpl( String bindUri, Map properties ) throws RuleExecutionSetNotFoundException
87 {
88 this.properties = properties;
89
90 // get the rule set from the repository
91 RuleExecutionSetRepository repository = RuleExecutionSetRepository.getInstance();
92 ruleSet = (RuleExecutionSetImpl) repository.getRuleExecutionSet( bindUri );
93 if ( ruleSet == null ) throw new RuleExecutionSetNotFoundException( "RuleExecutionSet unbound: " + bindUri );
94
95 ruleBase = ruleSet.getRuleBase();
96 }
97
98 /***
99 * Executes the rules in the bound rule execution set using the supplied list of objects
100 * until no rule is executable anymore.
101 * A List is returned over all objects created by the executed rules
102 * that pass the default <code>RuleExecutionSet</code> <code>ObjectFilter</code> (if present).
103
104 * @see StatelessRuleSession#executeRules(List)
105 */
106 public List executeRules( List list ) throws InvalidRuleSessionException
107 {
108
109 // Note: this breaks the factory intension of RuleBase.createWorkingMemory
110 JSR94WorkingMemory workingMemory = new JSR94WorkingMemory( ruleBase );
111 workingMemory.setApplicationData( properties );
112
113 try
114 {
115 for ( int i = 0; i < list.size(); i++ )
116 {
117 Object obj = list.get( i );
118 workingMemory.assertObject( obj );
119 }
120 }
121 catch ( AssertionException ex )
122 {
123 throw new InvalidRuleSessionException( ex.getMessage(), ex );
124 }
125
126 List outList = workingMemory.getObjectList();
127
128 // apply the default filter
129 ObjectFilter objectFilter = ruleSet.getObjectFilter();
130 if ( objectFilter != null )
131 {
132
133 // apply the filter
134 List cpyList = new ArrayList();
135 for ( int i = 0; i < outList.size(); i++ )
136 {
137 Object obj = objectFilter.filter( outList.get( i ) );
138 if ( obj != null ) cpyList.add( obj );
139 }
140 outList = cpyList;
141 }
142
143 return outList;
144 }
145
146 /***
147 * Executes the rules in the bound rule execution set using the supplied list of objects until no rule is executable anymore.
148 * An iterator is returned over all objects created by the executed rules and filtered with the supplied object filter.
149
150 * @see StatelessRuleSession#executeRules(List,ObjectFilter)
151 */
152 public List executeRules( List list, ObjectFilter objectFilter ) throws InvalidRuleSessionException
153 {
154
155 // Note: this breaks the factory intension of RuleBase.createWorkingMemory
156 JSR94WorkingMemory workingMemory = new JSR94WorkingMemory( ruleBase );
157
158 try
159 {
160 for ( int i = 0; i < list.size(); i++ )
161 {
162 Object obj = list.get( i );
163 workingMemory.assertObject( obj );
164 }
165 }
166 catch ( AssertionException ex )
167 {
168 throw new InvalidRuleSessionException( ex.getMessage(), ex );
169 }
170
171 List outList = workingMemory.getObjectList();
172
173 // apply the filter
174 List cpyList = new ArrayList();
175 for ( int i = 0; i < outList.size(); i++ )
176 {
177 Object obj = objectFilter.filter( outList.get( i ) );
178 if ( obj != null ) cpyList.add( obj );
179 }
180
181 return cpyList;
182 }
183
184 /***
185 * Releases all resources used by this rule session.
186 * This method renders this rule session unusable until it is reacquired through the RuleRuntime.
187 */
188 public void release()
189 {
190 }
191 }
This page was automatically generated by Maven