001: /*
002: Copyright (C) 2006 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.rules;
034:
035: import java.util.Map;
036: import java.util.Iterator;
037: import java.util.HashMap;
038: import java.util.Date;
039: import java.math.BigDecimal;
040:
041: /**
042: * <p>RuleExecution</p>
043: * RuleExecution represents an abstract operation performed on a set of data.
044: * Rule Executions are composed of three items:<br>
045: * 1. The Parameters Collection<br>
046: * 2. The Assertions Collection<br>
047: * 3. The Rule Execution Code<br>
048: * Also RuleExecution can used additional global properties and asserts from its parent RuleEngine.
049: * @author Sergio Montoro Ten
050: * @version 1.0
051: */
052: public abstract class RuleExecution {
053:
054: private RuleEngine oRuleEngine;
055:
056: private HashMap oParams;
057: private HashMap oAssrts;
058:
059: /**
060: * Create RuleExecution on given RuleEngine
061: * @param oEngine RuleEngine
062: */
063: public RuleExecution(RuleEngine oEngine) {
064: oRuleEngine = oEngine;
065: oParams = new HashMap(29);
066: oAssrts = new HashMap(29);
067: }
068:
069: /**
070: * Create copy from another RuleExecution
071: * @param oRexec RuleExecution
072: */
073: public RuleExecution(RuleExecution oRexec) {
074: oRuleEngine = oRexec.getRuleEngine();
075: oParams = (HashMap) oRexec.getParamMap().clone();
076: oAssrts = (HashMap) oRexec.getAssertMap().clone();
077: }
078:
079: /**
080: * Clear all parameters and asserts
081: */
082: public void clear() {
083: oParams.clear();
084: oAssrts.clear();
085: }
086:
087: /**
088: * Get RuleEngine to which this RuleExecution belongs
089: * @return RuleEngine
090: */
091: public RuleEngine getRuleEngine() {
092: return oRuleEngine;
093: }
094:
095: /**
096: * Set true or false status for a fact
097: * @param sAssertKey String A unique key in this RuleExecution for the fact
098: * @param bTrueOrFalse boolean
099: */
100: public void setAssert(String sAssertKey, boolean bTrueOrFalse) {
101: if (oAssrts.containsKey(sAssertKey))
102: oAssrts.remove(sAssertKey);
103: oAssrts.put(sAssertKey, new Boolean(bTrueOrFalse));
104: }
105:
106: /**
107: * Get status of a given fact
108: * @param sAssertKey String A unique key in this RuleExecution for the fact
109: * @return boolean If no assertion with given key is found then <b>false</b> is returned.
110: */
111: public boolean getAssert(String sAssertKey) {
112: Boolean bAssrt = (Boolean) oAssrts.get(sAssertKey);
113: if (bAssrt == null)
114: return false;
115: else
116: return bAssrt.booleanValue();
117: }
118:
119: /**
120: * Get map of parameters used by this RuleExecution
121: * @return HashMap
122: */
123: public HashMap getParamMap() {
124: return oParams;
125: }
126:
127: /**
128: * Get map of assertions used by this RuleExecution
129: * @return HashMap
130: */
131: public HashMap getAssertMap() {
132: return oAssrts;
133: }
134:
135: /**
136: * Find out whether or not a given parameter is <b>null</b>
137: * @param sKey String Parameter name
138: * @return boolean If parameter is <b>null</b> or if it has not defined value
139: * then the return value is <b>true</b>, else if the parameter is defined and
140: * has a value other than <b>null</b> the return value is <b>false</b>
141: */
142: public boolean isNull(String sKey) {
143: if (oParams.containsKey(sKey))
144: return (null == oParams.get(sKey));
145: else
146: return true;
147: }
148:
149: /**
150: * Find out whether or not a given parameter has a defined value
151: * @param sKey String Parameter Name
152: * @return boolean
153: */
154: public boolean isDefined(String sKey) {
155: return oParams.containsKey(sKey);
156: }
157:
158: /**
159: * <p>Set value for parameter</p>
160: * If parameter already exists then its value is replaced with the new one
161: * @param sKey String Parameter name
162: * @param oValue Object
163: */
164: public void setParam(String sKey, Object oValue) {
165: if (oParams.containsKey(sKey))
166: oParams.remove(sKey);
167: oParams.put(sKey, oValue);
168: }
169:
170: /**
171: * Remove parameter value
172: * @param sKey String Parameter name
173: */
174: public void resetParam(String sKey) {
175: if (oParams.containsKey(sKey))
176: oParams.remove(sKey);
177: }
178:
179: /**
180: * Set values for a set of parameters
181: * If parameters already exist then theirs values are replaced with the new ones
182: * @param oMap Map of values to be set
183: */
184: public void setParams(Map oMap) {
185: Iterator oKeys = oMap.keySet().iterator();
186: while (oKeys.hasNext()) {
187: Object oKey = (String) oKeys.next();
188: if (oParams.containsKey(oKey))
189: oParams.remove(oKey);
190: oParams.put(oKey, oMap.get(oKey));
191: } // wend
192: }
193:
194: /**
195: * <p>Get parameter value</p>
196: * @param sKey String Parameter Name
197: * @return Object If parameter is undefined then return value is <b>null</b>
198: */
199: public Object getParam(String sKey) {
200: if (oParams.containsKey(sKey))
201: return oParams.get(sKey);
202: else
203: return null;
204: }
205:
206: /**
207: * <p>Get parameter value</p>
208: * @param sKey String Parameter Name
209: * @param oDefault Default value
210: * @return Object If parameter is undefined then return value is oDefault
211: */
212: public Object getParam(String sKey, Object oDefault) {
213: if (oParams.containsKey(sKey))
214: return oParams.get(sKey);
215: else
216: return oDefault;
217: }
218:
219: /**
220: * Get parameter as String
221: * @param sKey String Parameter Name
222: * @param sDefault String Default value
223: * @return String
224: */
225: public String getParamStr(String sKey, String sDefault) {
226: Object oParam = oParams.get(sKey);
227: if (oParam == null)
228: return sDefault;
229: else
230: return oParam.toString();
231: }
232:
233: /**
234: * Get parameter as String
235: * @param sKey String Parameter Name
236: * @return String
237: */
238: public String getParamStr(String sKey) {
239: return getParamStr(sKey, null);
240: }
241:
242: /**
243: * Get parameter as java.util.Date
244: * @param sKey String Parameter Name
245: * @param dtDefault Date Default value
246: * @return Date If parameter is undefined then return value is dtDefault
247: * @throws ClassCastException
248: */
249: public Date getParamDate(String sKey, Date dtDefault)
250: throws ClassCastException {
251: Object oParam = oParams.get(sKey);
252: if (oParam == null)
253: return dtDefault;
254: else
255: return (Date) oParam;
256: }
257:
258: /**
259: * Get parameter as java.util.Date
260: * @param sKey String Parameter Name
261: * @return Date If parameter is undefined then return value is <b>null</b>
262: * @throws ClassCastException
263: */
264: public Date getParamDate(String sKey) throws ClassCastException {
265: return getParamDate(sKey, null);
266: }
267:
268: /**
269: * Get parameter as BigDecimal
270: * @param sKey String Parameter Name
271: * @param oDefault BigDecimal
272: * @return BigDecimal If parameter is undefined then return value is dtDefault
273: * @throws ClassCastException
274: */
275: public BigDecimal getParamDec(String sKey, BigDecimal oDefault)
276: throws ClassCastException {
277: Object oParam = oParams.get(sKey);
278: if (oParam == null)
279: return oDefault;
280: else
281: return (BigDecimal) oParam;
282: }
283:
284: /**
285: * Get parameter as BigDecimal
286: * @param sKey String Parameter Name
287: * @return BigDecimal
288: * @throws ClassCastException
289: */
290: public BigDecimal getParamDec(String sKey)
291: throws ClassCastException {
292: return getParamDec(sKey, null);
293: }
294:
295: /**
296: * Get parameter as Integer
297: * @param sKey String Parameter Name
298: * @param iDefault Integer Default value
299: * @return Integer If parameter is undefined then return value is iDefault
300: * @throws ClassCastException
301: */
302: public Integer getParamInt(String sKey, Integer iDefault)
303: throws ClassCastException {
304: Object oParam = oParams.get(sKey);
305: if (oParam == null)
306: return iDefault;
307: else
308: return (Integer) oParam;
309: }
310:
311: /**
312: * Get parameter as Integer
313: * @param sKey String Parameter Name
314: * @return Integer If parameter is undefined then return value is <b>null</b>
315: * @throws ClassCastException
316: */
317: public Integer getParamInt(String sKey) throws ClassCastException {
318: return getParamInt(sKey, null);
319: }
320:
321: /**
322: * <p>Get result of applying this rule</p>
323: * This method must be implemented on eeach derived class for providing
324: * the actual behaviour of the RuleExecution instance
325: * @return Object
326: * @throws RuleExecutionException
327: */
328: public abstract Object getResult() throws RuleExecutionException;
329: }
|