1 package org.drools.io;
2
3 /*
4 $Id: RuleSetLoader.java,v 1.4 2003/06/19 09:27:10 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.RuleBase;
50 import org.drools.rule.RuleSet;
51 import org.drools.tags.rule.RuleTagLibrary;
52
53 import org.apache.commons.jelly.Script;
54 import org.apache.commons.jelly.JellyContext;
55 import org.apache.commons.jelly.XMLOutput;
56 import org.apache.commons.jelly.parser.XMLParser;
57
58 import java.io.IOException;
59 import java.io.Reader;
60 import java.io.InputStream;
61 import java.io.InputStreamReader;
62 import java.net.URL;
63 import java.util.ArrayList;
64 import java.util.List;
65 import java.util.Iterator;
66 import java.util.HashMap;
67
68 /*** Loads <code>RuleSet</code> definitions from XML descriptor.
69 *
70 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
71 *
72 * @version $Id: RuleSetLoader.java,v 1.4 2003/06/19 09:27:10 tdiesler Exp $
73 */
74 public class RuleSetLoader
75 {
76 // ------------------------------------------------------------
77 // Instance members
78 // ------------------------------------------------------------
79
80 /*** Rule-sets. */
81 private List ruleSets;
82
83 /***
84 * Cache a list of RuleSet objects, key is the url.
85 * This is a temporary workaround for the OutOfMemoryError in OutOfMemory when a script is parsed repeatedly.
86 */
87 private static HashMap ruleSetCache = new HashMap();
88
89 // ------------------------------------------------------------
90 // Constructors
91 // ------------------------------------------------------------
92
93 /*** Construct.
94 */
95 public RuleSetLoader()
96 {
97 // intentionally left blank
98 }
99
100 // ------------------------------------------------------------
101 // Instance methods
102 // ------------------------------------------------------------
103
104 /*** Add a <code>RuleSet</code> to this loader.
105 *
106 * @param ruleSet The rule-set to add.
107 */
108 public void addRuleSet(RuleSet ruleSet)
109 {
110 this.ruleSets.add(ruleSet);
111 }
112
113 /*** Load <code>RuleSet</code> definitions from a Reader.
114 *
115 * @param reader The Reader of the rule-set definitions.
116 *
117 * @return The list of loaded rule-sets.
118 *
119 * @throws IOException If an IO errors occurs.
120 * @throws Exception If an error occurs evaluating the definition.
121 */
122 public List load(Reader reader) throws IOException, Exception
123 {
124
125 this.ruleSets = new ArrayList();
126
127 XMLParser parser = new XMLParser();
128
129 JellyContext context = new JellyContext();
130
131 context.registerTagLibrary("http://drools.org/rules",
132 new RuleTagLibrary());
133
134 context.setVariable("org.drools.io.RuleSetLoader",
135 this);
136
137 parser.setContext(context);
138
139 Script script = parser.parse(reader);
140
141 XMLOutput output = XMLOutput.createXMLOutput(System.err,
142 false);
143
144 script.run(context,
145 output);
146
147 List answer = this.ruleSets;
148
149 this.ruleSets = null;
150
151 return answer;
152 }
153
154 /*** Load <code>RuleSet</code> definitions from a URL.
155 *
156 * @param url The URL of the rule-set definitions.
157 *
158 * @return The list of loaded rule-sets.
159 *
160 * @throws IOException If an IO errors occurs.
161 * @throws Exception If an error occurs evaluating the definition.
162 */
163 public List load(URL url) throws IOException, Exception
164 {
165 // This is a workaround for an OutOfMemoryError in Jelly when a script is parsed repeatedly
166 // Note, the rules for a given url cannot be changed after they are chached
167 List cachedRuleSets = (List) ruleSetCache.get(url);
168 if (cachedRuleSets != null)
169 {
170 return cachedRuleSets;
171 }
172
173 InputStream urlStream = url.openStream();
174
175 try
176 {
177 List answer = load (new InputStreamReader(urlStream));
178
179 ruleSetCache.put(url, answer);
180
181 return answer;
182 }
183 finally
184 {
185 urlStream.close();
186 }
187 }
188
189 /*** Load <code>RuleSet</code> deifnitions from a URL
190 * into a <code>RuleBase</code>.
191 *
192 * @param url The URL of the rule-set definitions.
193 * @param ruleBase The rule-base to populate.
194 *
195 * @throws IOException If an IO errors occurs.
196 * @throws Exception If an error occurs evaluating the definition.
197 */
198 public void load(URL url,
199 RuleBase ruleBase) throws IOException, Exception
200 {
201 List ruleSets = load(url);
202
203 Iterator ruleSetIter = ruleSets.iterator();
204 RuleSet eachRuleSet = null;
205
206 while (ruleSetIter.hasNext())
207 {
208 eachRuleSet = (RuleSet) ruleSetIter.next();
209
210 ruleBase.addRuleSet(eachRuleSet);
211 }
212 }
213
214 /*** Load <code>RuleSet</code> deifnitions from a URL.
215 *
216 * @param url The URL of the rule-set definitions.
217 *
218 * @return The list of loaded rule-sets.
219 *
220 * @throws IOException If an IO errors occurs.
221 * @throws Exception If an error occurs evaluating the definition.
222 */
223 public List load(String url) throws IOException, Exception
224 {
225 return load(new URL(url));
226 }
227 }
This page was automatically generated by Maven