KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > rules > RuleExecution


1 /*
2   Copyright (C) 2006 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.rules;
34
35 import java.util.Map JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Date JavaDoc;
39 import java.math.BigDecimal JavaDoc;
40
41 /**
42  * <p>RuleExecution</p>
43  * RuleExecution represents an abstract operation performed on a set of data.
44  * Rule Executions are composed of three items:<br>
45  * 1. The Parameters Collection<br>
46  * 2. The Assertions Collection<br>
47  * 3. The Rule Execution Code<br>
48  * Also RuleExecution can used additional global properties and asserts from its parent RuleEngine.
49  * @author Sergio Montoro Ten
50  * @version 1.0
51  */

52 public abstract class RuleExecution {
53
54   private RuleEngine oRuleEngine;
55
56   private HashMap JavaDoc oParams;
57   private HashMap JavaDoc oAssrts;
58
59   /**
60    * Create RuleExecution on given RuleEngine
61    * @param oEngine RuleEngine
62    */

63   public RuleExecution(RuleEngine oEngine) {
64     oRuleEngine = oEngine;
65     oParams = new HashMap JavaDoc(29);
66     oAssrts = new HashMap JavaDoc(29);
67   }
68
69   /**
70    * Create copy from another RuleExecution
71    * @param oRexec RuleExecution
72    */

73   public RuleExecution(RuleExecution oRexec) {
74     oRuleEngine = oRexec.getRuleEngine();
75     oParams = (HashMap JavaDoc) oRexec.getParamMap().clone();
76     oAssrts = (HashMap JavaDoc) oRexec.getAssertMap().clone();
77   }
78
79   /**
80    * Clear all parameters and asserts
81    */

82   public void clear() {
83     oParams.clear();
84     oAssrts.clear();
85   }
86
87   /**
88    * Get RuleEngine to which this RuleExecution belongs
89    * @return RuleEngine
90    */

91   public RuleEngine getRuleEngine() {
92     return oRuleEngine;
93   }
94
95   /**
96    * Set true or false status for a fact
97    * @param sAssertKey String A unique key in this RuleExecution for the fact
98    * @param bTrueOrFalse boolean
99    */

100   public void setAssert(String JavaDoc sAssertKey, boolean bTrueOrFalse) {
101     if (oAssrts.containsKey(sAssertKey)) oAssrts.remove(sAssertKey);
102     oAssrts.put(sAssertKey, new Boolean JavaDoc(bTrueOrFalse));
103   }
104
105   /**
106    * Get status of a given fact
107    * @param sAssertKey String A unique key in this RuleExecution for the fact
108    * @return boolean If no assertion with given key is found then <b>false</b> is returned.
109    */

110   public boolean getAssert(String JavaDoc sAssertKey) {
111     Boolean JavaDoc bAssrt = (Boolean JavaDoc) oAssrts.get(sAssertKey);
112     if (bAssrt==null)
113       return false;
114     else
115       return bAssrt.booleanValue();
116   }
117
118   /**
119    * Get map of parameters used by this RuleExecution
120    * @return HashMap
121    */

122   public HashMap JavaDoc getParamMap() {
123     return oParams;
124   }
125
126   /**
127    * Get map of assertions used by this RuleExecution
128    * @return HashMap
129    */

130   public HashMap JavaDoc getAssertMap() {
131     return oAssrts;
132   }
133
134   /**
135    * Find out whether or not a given parameter is <b>null</b>
136    * @param sKey String Parameter name
137    * @return boolean If parameter is <b>null</b> or if it has not defined value
138    * then the return value is <b>true</b>, else if the parameter is defined and
139    * has a value other than <b>null</b> the return value is <b>false</b>
140    */

141   public boolean isNull(String JavaDoc sKey) {
142     if (oParams.containsKey(sKey))
143       return (null==oParams.get(sKey));
144     else
145       return true;
146   }
147
148   /**
149    * Find out whether or not a given parameter has a defined value
150    * @param sKey String Parameter Name
151    * @return boolean
152    */

153   public boolean isDefined(String JavaDoc sKey) {
154     return oParams.containsKey(sKey);
155   }
156
157   /**
158    * <p>Set value for parameter</p>
159    * If parameter already exists then its value is replaced with the new one
160    * @param sKey String Parameter name
161    * @param oValue Object
162    */

163   public void setParam(String JavaDoc sKey, Object JavaDoc oValue) {
164     if (oParams.containsKey(sKey)) oParams.remove(sKey);
165     oParams.put(sKey, oValue);
166   }
167
168   /**
169    * Remove parameter value
170    * @param sKey String Parameter name
171    */

172   public void resetParam(String JavaDoc sKey) {
173     if (oParams.containsKey(sKey)) oParams.remove(sKey);
174   }
175
176   /**
177    * Set values for a set of parameters
178    * If parameters already exist then theirs values are replaced with the new ones
179    * @param oMap Map of values to be set
180    */

181   public void setParams(Map JavaDoc oMap) {
182     Iterator JavaDoc oKeys = oMap.keySet().iterator();
183     while (oKeys.hasNext()) {
184       Object JavaDoc oKey = (String JavaDoc) oKeys.next();
185       if (oParams.containsKey(oKey)) oParams.remove(oKey);
186       oParams.put(oKey, oMap.get(oKey));
187     } // wend
188
}
189
190   /**
191    * <p>Get parameter value</p>
192    * @param sKey String Parameter Name
193    * @return Object If parameter is undefined then return value is <b>null</b>
194    */

195   public Object JavaDoc getParam(String JavaDoc sKey) {
196     if (oParams.containsKey(sKey))
197       return oParams.get(sKey);
198     else
199       return null;
200   }
201
202   /**
203    * <p>Get parameter value</p>
204    * @param sKey String Parameter Name
205    * @param oDefault Default value
206    * @return Object If parameter is undefined then return value is oDefault
207    */

208   public Object JavaDoc getParam(String JavaDoc sKey, Object JavaDoc oDefault) {
209     if (oParams.containsKey(sKey))
210       return oParams.get(sKey);
211     else
212       return oDefault;
213   }
214
215   /**
216    * Get parameter as String
217    * @param sKey String Parameter Name
218    * @param sDefault String Default value
219    * @return String
220    */

221   public String JavaDoc getParamStr(String JavaDoc sKey, String JavaDoc sDefault) {
222     Object JavaDoc oParam = oParams.get(sKey);
223     if (oParam==null)
224       return sDefault;
225     else
226       return oParam.toString();
227   }
228
229   /**
230    * Get parameter as String
231    * @param sKey String Parameter Name
232    * @return String
233    */

234   public String JavaDoc getParamStr(String JavaDoc sKey) {
235       return getParamStr(sKey, null);
236   }
237
238   /**
239    * Get parameter as java.util.Date
240    * @param sKey String Parameter Name
241    * @param dtDefault Date Default value
242    * @return Date If parameter is undefined then return value is dtDefault
243    * @throws ClassCastException
244    */

245   public Date JavaDoc getParamDate(String JavaDoc sKey, Date JavaDoc dtDefault)
246     throws ClassCastException JavaDoc {
247     Object JavaDoc oParam = oParams.get(sKey);
248     if (oParam==null)
249       return dtDefault;
250     else
251       return (Date JavaDoc) oParam;
252   }
253
254   /**
255    * Get parameter as java.util.Date
256    * @param sKey String Parameter Name
257    * @return Date If parameter is undefined then return value is <b>null</b>
258    * @throws ClassCastException
259    */

260   public Date JavaDoc getParamDate(String JavaDoc sKey)
261     throws ClassCastException JavaDoc {
262     return getParamDate(sKey, null);
263   }
264
265   /**
266    * Get parameter as BigDecimal
267    * @param sKey String Parameter Name
268    * @param oDefault BigDecimal
269    * @return BigDecimal If parameter is undefined then return value is dtDefault
270    * @throws ClassCastException
271    */

272   public BigDecimal JavaDoc getParamDec(String JavaDoc sKey, BigDecimal JavaDoc oDefault)
273     throws ClassCastException JavaDoc {
274     Object JavaDoc oParam = oParams.get(sKey);
275     if (oParam==null)
276       return oDefault;
277     else
278       return (BigDecimal JavaDoc) oParam;
279   }
280
281   /**
282    * Get parameter as BigDecimal
283    * @param sKey String Parameter Name
284    * @return BigDecimal
285    * @throws ClassCastException
286    */

287   public BigDecimal JavaDoc getParamDec(String JavaDoc sKey)
288     throws ClassCastException JavaDoc {
289     return getParamDec(sKey, null);
290   }
291
292   /**
293    * Get parameter as Integer
294    * @param sKey String Parameter Name
295    * @param iDefault Integer Default value
296    * @return Integer If parameter is undefined then return value is iDefault
297    * @throws ClassCastException
298    */

299   public Integer JavaDoc getParamInt(String JavaDoc sKey, Integer JavaDoc iDefault)
300     throws ClassCastException JavaDoc {
301     Object JavaDoc oParam = oParams.get(sKey);
302     if (oParam==null)
303       return iDefault;
304     else
305       return (Integer JavaDoc) oParam;
306   }
307
308   /**
309    * Get parameter as Integer
310    * @param sKey String Parameter Name
311    * @return Integer If parameter is undefined then return value is <b>null</b>
312    * @throws ClassCastException
313    */

314   public Integer JavaDoc getParamInt(String JavaDoc sKey)
315     throws ClassCastException JavaDoc {
316     return getParamInt(sKey, null);
317   }
318
319   /**
320    * <p>Get result of applying this rule</p>
321    * This method must be implemented on eeach derived class for providing
322    * the actual behaviour of the RuleExecution instance
323    * @return Object
324    * @throws RuleExecutionException
325    */

326   public abstract Object JavaDoc getResult() throws RuleExecutionException;
327 }
328
Popular Tags