1 package org.drools.rule;
2
3 /*
4 $Id: RuleSet.java,v 1.1 2002/08/01 18:47:33 bob 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 java.util.List;
50 import java.util.ArrayList;
51 import java.util.Set;
52 import java.util.HashSet;
53 import java.util.Iterator;
54
55 /*** Collection of related <code>Rule</code>s.
56 *
57 * @see Rule
58 *
59 * @author <a href="mail:bob@eng.werken.com">bob mcwhirter</a>
60 */
61 public class RuleSet
62 {
63 // ------------------------------------------------------------
64 // Instance members
65 // ------------------------------------------------------------
66
67 /*** The name of the ruleset. */
68 private String name;
69
70 /*** Set of all rule-names in this <code>RuleSet</code>. */
71 private Set names;
72
73 /*** Ordered list of all <code>Rules</code> in this <code>RuleSet</code>. */
74 private List rules;
75
76 // ------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------
79
80 /*** Construct.
81 *
82 * @param name The name of this <code>RuleSet</code>.
83 */
84 public RuleSet(String name)
85 {
86 this.name = name;
87 this.names = new HashSet();
88 this.rules = new ArrayList();
89 }
90
91 // ------------------------------------------------------------
92 // Instance methods
93 // ------------------------------------------------------------
94
95 /*** Set the name of this <code>RuleSet</code>
96 *
97 * @param name The name of this <code>RuleSet</code>
98 */
99 public void setName(String name)
100 {
101 this.name = name;
102 }
103
104 /*** Retrieve the name of this <code>RuleSet</code>.
105 *
106 * @return The name of this <code>RuleSet</code>.
107 */
108 public String getName()
109 {
110 return this.name;
111 }
112
113 /*** Add a <code>Rule</code> to this <code>RuleSet</code>.
114 *
115 * @param rule The rule to add.
116 *
117 * @throws DuplicateRuleNameException If the <code>Rule</code> attempting to be
118 * added has the same name as another previously added <code>Rule</code>.
119 * @throws InvalidRuleException If the <code>Rule</code> is not valid.
120 */
121 public void addRule(Rule rule) throws DuplicateRuleNameException, InvalidRuleException
122 {
123 rule.checkValidity();
124
125 String name = rule.getName();
126
127 if ( containsRule( name ) )
128 {
129 throw new DuplicateRuleNameException( this,
130 getRule( name ),
131 rule );
132 }
133
134 this.names.add( name );
135 this.rules.add( rule );
136 }
137
138 /*** Retrieve a <code>Rule</code> by name.
139 *
140 * @param name The name of the <code>Rule</code> to retrieve.
141 *
142 * @return The named <code>Rule</code>, or <code>null</code> if not
143 * such <code>Rule</code> has been added to this <code>RuleSet</code>.
144 */
145 public Rule getRule(String name)
146 {
147 Iterator ruleIter = getRuleIterator();
148 Rule eachRule = null;
149
150 while ( ruleIter.hasNext() )
151 {
152 eachRule = (Rule) ruleIter.next();
153
154 if ( eachRule.getName().equals( name ) )
155 {
156 return eachRule;
157 }
158 }
159
160 return null;
161 }
162
163 /*** Determine if this <code>RuleSet</code> contains a <code>Rule</code
164 * with the specified name.
165 *
166 * @param name The name of the <code>Rule</code>.
167 *
168 * @return <code>true</code> if this <code>RuleSet</code> contains a
169 * <code>Rule</code> with the specified name, else <code>false</code>.
170 */
171 public boolean containsRule(String name)
172 {
173 return this.names.contains( name );
174 }
175
176 /*** Retrieve a <code>List</code> of all <code>Rules</code>
177 * in this <code>RuleSet</code>.
178 *
179 * @return A <code>List</code> of all <code>Rules</code>
180 * in this <code>RuleSet</code>.
181 */
182 public List getRules()
183 {
184 return this.rules;
185 }
186
187 /*** Retrieve an <code>Iterator</code> of all <code>Rules</code>
188 * in this <code>RuleSet</code>.
189 *
190 * @return A <code>Iterator</code> over all <code>Rules</code>
191 * in this <code>RuleSet</code>.
192 */
193 public Iterator getRuleIterator()
194 {
195 return getRules().iterator();
196 }
197 }
This page was automatically generated by Maven