|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
RuleSet.java | 100% | 97.3% | 93.8% | 96.6% |
|
1 | package org.drools.rule; | |
2 | ||
3 | /* | |
4 | * $Id: RuleSet.java,v 1.19.2.1 2005/04/30 12:37:34 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2001-2003 (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 trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. (http://werken.com/) | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
30 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
39 | * POSSIBILITY OF SUCH DAMAGE. | |
40 | * | |
41 | */ | |
42 | ||
43 | import java.io.Serializable; | |
44 | import java.util.ArrayList; | |
45 | import java.util.HashMap; | |
46 | import java.util.HashSet; | |
47 | import java.util.List; | |
48 | import java.util.Map; | |
49 | import java.util.Set; | |
50 | ||
51 | import org.drools.spi.Functions; | |
52 | import org.drools.spi.ImportEntry; | |
53 | import org.drools.spi.Importer; | |
54 | import org.drools.spi.RuleBaseContext; | |
55 | ||
56 | /** | |
57 | * Collection of related <code>Rule</code>s. | |
58 | * | |
59 | * @see Rule | |
60 | * | |
61 | * @author <a href="mail:bob@werken.com">bob mcwhirter </a> | |
62 | * | |
63 | * @version $Id: RuleSet.java,v 1.19.2.1 2005/04/30 12:37:34 mproctor Exp $ | |
64 | */ | |
65 | public class RuleSet | |
66 | implements | |
67 | Serializable | |
68 | { | |
69 | // ------------------------------------------------------------ | |
70 | // Constants | |
71 | // ------------------------------------------------------------ | |
72 | ||
73 | /** Empty <code>RuleSet</code> array. */ | |
74 | public static final RuleSet[] EMPTY_ARRAY = new RuleSet[0]; | |
75 | ||
76 | // ------------------------------------------------------------ | |
77 | // Instance members | |
78 | // ------------------------------------------------------------ | |
79 | ||
80 | /** Name of the ruleset. */ | |
81 | private String name; | |
82 | ||
83 | /** Documentation. */ | |
84 | private String documentation; | |
85 | ||
86 | /** Set of all rule-names in this <code>RuleSet</code>. */ | |
87 | private Set ruleNames; | |
88 | ||
89 | /** Ordered list of all <code>Rules</code> in this <code>RuleSet</code>. */ | |
90 | private List rules; | |
91 | ||
92 | private Importer importer; | |
93 | ||
94 | private Map applicationData; | |
95 | ||
96 | private Map functions; | |
97 | ||
98 | private RuleBaseContext ruleBaseContext; | |
99 | ||
100 | // ------------------------------------------------------------ | |
101 | // Constructors | |
102 | // ------------------------------------------------------------ | |
103 | ||
104 | /** | |
105 | * Construct. | |
106 | * | |
107 | * @param name | |
108 | * The name of this <code>RuleSet</code>. | |
109 | */ | |
110 | 24 | public RuleSet(String name) |
111 | { | |
112 | 24 | this.name = name; |
113 | 24 | this.ruleNames = new HashSet( ); |
114 | 24 | this.rules = new ArrayList( ); |
115 | 24 | this.applicationData = new HashMap( ); |
116 | 24 | this.functions = new HashMap( ); |
117 | 24 | this.ruleBaseContext = new RuleBaseContext( ); |
118 | } | |
119 | ||
120 | /** | |
121 | * Construct. | |
122 | * | |
123 | * @param name | |
124 | * The name of this <code>RuleSet</code>. | |
125 | * @param ruleBaseContext | |
126 | */ | |
127 | 94 | public RuleSet(String name, |
128 | RuleBaseContext ruleBaseContext) | |
129 | { | |
130 | 94 | this.name = name; |
131 | 94 | this.ruleNames = new HashSet( ); |
132 | 94 | this.rules = new ArrayList( ); |
133 | 94 | this.applicationData = new HashMap( ); |
134 | 94 | this.functions = new HashMap( ); |
135 | 94 | this.ruleBaseContext = ruleBaseContext; |
136 | } | |
137 | ||
138 | // ------------------------------------------------------------ | |
139 | // Instance methods | |
140 | // ------------------------------------------------------------ | |
141 | ||
142 | /** | |
143 | * Retrieve the name of this <code>RuleSet</code>. | |
144 | * | |
145 | * @return The name of this <code>RuleSet</code>. | |
146 | */ | |
147 | 12 | public String getName() |
148 | { | |
149 | 12 | return this.name; |
150 | } | |
151 | ||
152 | /** | |
153 | * Set the documentation. | |
154 | * | |
155 | * @param documentation | |
156 | * The documentation. | |
157 | */ | |
158 | 67 | public void setDocumentation(String documentation) |
159 | { | |
160 | 67 | this.documentation = documentation; |
161 | } | |
162 | ||
163 | /** | |
164 | * Retrieve the documentation. | |
165 | * | |
166 | * @return The documentation or <code>null</code> if none. | |
167 | */ | |
168 | 63 | public String getDocumentation() |
169 | { | |
170 | 63 | return this.documentation; |
171 | } | |
172 | ||
173 | /** | |
174 | * Add a <code>Rule</code> to this <code>RuleSet</code>. | |
175 | * | |
176 | * @param rule | |
177 | * The rule to add. | |
178 | * | |
179 | * @throws DuplicateRuleNameException | |
180 | * If the <code>Rule</code> attempting to be added has the | |
181 | * same name as another previously added <code>Rule</code>. | |
182 | * @throws InvalidRuleException | |
183 | * If the <code>Rule</code> is not valid. | |
184 | */ | |
185 | 146 | public void addRule(Rule rule) throws DuplicateRuleNameException, |
186 | InvalidRuleException | |
187 | { | |
188 | 146 | rule.checkValidity( ); |
189 | ||
190 | 146 | String name = rule.getName( ); |
191 | ||
192 | 146 | if ( containsRule( name ) ) |
193 | { | |
194 | 1 | throw new DuplicateRuleNameException( this, |
195 | getRule( name ), | |
196 | rule ); | |
197 | } | |
198 | ||
199 | 145 | this.ruleNames.add( name ); |
200 | 145 | rule.setLoadOrder( rules.size( ) ); |
201 | 145 | rule.setImporter( this.importer ); |
202 | 145 | this.rules.add( rule ); |
203 | } | |
204 | ||
205 | /** | |
206 | * Retrieve a <code>Rule</code> by name. | |
207 | * | |
208 | * @param name | |
209 | * The name of the <code>Rule</code> to retrieve. | |
210 | * | |
211 | * @return The named <code>Rule</code>, or <code>null</code> if not | |
212 | * such <code>Rule</code> has been added to this | |
213 | * <code>RuleSet</code>. | |
214 | */ | |
215 | 8 | public Rule getRule(String name) |
216 | { | |
217 | 8 | Rule[] rules = getRules( ); |
218 | ||
219 | 8 | for ( int i = 0; i < rules.length; ++i ) |
220 | { | |
221 | 9 | if ( rules[i].getName( ).equals( name ) ) |
222 | { | |
223 | 6 | return rules[i]; |
224 | } | |
225 | } | |
226 | ||
227 | 2 | return null; |
228 | } | |
229 | ||
230 | /** | |
231 | * Determine if this <code>RuleSet</code> contains a <code>Rule</code | |
232 | * with the specified name. | |
233 | * | |
234 | * @param name The name of the <code>Rule</code>. | |
235 | * | |
236 | * @return <code>true</code> if this <code>RuleSet</code> contains a | |
237 | * <code>Rule</code> with the specified name, else <code>false</code>. | |
238 | */ | |
239 | 146 | public boolean containsRule(String name) |
240 | { | |
241 | 146 | return this.ruleNames.contains( name ); |
242 | } | |
243 | ||
244 | /** | |
245 | * Retrieve all <code>Rules</code> in this <code>RuleSet</code>. | |
246 | * | |
247 | * @return An array of all <code>Rules</code> in this <code>RuleSet</code>. | |
248 | */ | |
249 | 90 | public Rule[] getRules() |
250 | { | |
251 | 90 | return (Rule[]) this.rules.toArray( new Rule[this.rules.size( )] ); |
252 | } | |
253 | ||
254 | 112 | public Importer getImporter() |
255 | { | |
256 | 112 | return this.importer; |
257 | } | |
258 | ||
259 | 70 | public void setImporter(Importer importer) |
260 | { | |
261 | 70 | this.importer = importer; |
262 | } | |
263 | ||
264 | 1 | public void addApplicationData(ApplicationData applicationData) |
265 | { | |
266 | 1 | this.applicationData.put( applicationData.getIdentifier( ), |
267 | applicationData.getType( ) ); | |
268 | } | |
269 | ||
270 | 145 | public Map getApplicationData() |
271 | { | |
272 | 145 | return this.applicationData; |
273 | } | |
274 | ||
275 | 0 | public void addFunctions(Functions functions) |
276 | { | |
277 | 0 | this.functions.put( functions.getSemantic( ), |
278 | functions ); | |
279 | } | |
280 | ||
281 | 263 | public Functions getFunctions(String semantic) |
282 | { | |
283 | 263 | return (Functions) this.functions.get( semantic ); |
284 | } | |
285 | ||
286 | 264 | public RuleBaseContext getRuleBaseContext() |
287 | { | |
288 | 264 | return this.ruleBaseContext; |
289 | } | |
290 | } |
|