001: /*
002: * hammurapi-rules @mesopotamia.version@
003: * Hammurapi rules engine.
004: * Copyright (C) 2005 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.rules;
024:
025: import java.util.Collection;
026:
027: import biz.hammurapi.config.ComponentBase;
028: import biz.hammurapi.config.ConfigurationException;
029: import biz.hammurapi.dispatch.InvocationTarget;
030:
031: public abstract class AbstractRule extends ComponentBase implements
032: InvocationTarget {
033:
034: private String name;
035: private String description;
036:
037: public String getName() {
038: return name;
039: }
040:
041: /**
042: * This method is invoked by rule container to set rule name.
043: * Rule name is used to retrieve collections from collection
044: * manager.
045: * @param name
046: */
047: public void setName(String name) {
048: this .name = name;
049: }
050:
051: public AbstractRule() {
052: super ();
053: // TODO Auto-generated constructor stub
054: }
055:
056: public String getDescription() {
057: return description;
058: }
059:
060: public void setDescription(String description) {
061: this .description = description;
062: }
063:
064: /**
065: * Adds new fact to knowledge base.
066: * Returning value from inference methods has the same effect.
067: * @param fact
068: */
069: protected void post(Object fact) {
070: ((KnowledgeBase) owner).add(fact);
071: }
072:
073: /**
074: * Removes object from knowledge base.
075: * handle manager.
076: * @param obj
077: */
078: protected void remove(Object obj) {
079: ((KnowledgeBase) owner).remove(obj);
080: }
081:
082: /**
083: * Updates fact.
084: * Removes conclusions made on the old fact from the knowledge base
085: * and makes conclusions based on the new object state.
086: * @param oldObject
087: * @param newObject
088: */
089: protected void update(Object fact) {
090: ((KnowledgeBase) owner).remove(fact);
091: ((KnowledgeBase) owner).add(fact);
092: }
093:
094: public String toString() {
095: return "[" + getClass().getName() + "] " + getName() + " - "
096: + getDescription();
097: }
098:
099: public abstract Collection getRemoveHandlers();
100:
101: public void start() throws ConfigurationException {
102: this .collectionManager = (CollectionManager) get("/collection-manager");
103: if (getName() == null) {
104: throw new ConfigurationException(
105: "Rule name must not be null");
106: }
107: if (!(owner instanceof KnowledgeBase)) {
108: throw new ConfigurationException(
109: "Rule container (ruleset) must implement "
110: + KnowledgeBase.class);
111: }
112: }
113:
114: public void stop() {
115: // Override this method to release resources.
116: }
117:
118: /**
119: * Convenience method to retrieve collection from the collection manager.
120: * @param setName
121: * @return
122: */
123: protected Collection getCollection(String collectionName,
124: Object lock) {
125: return collectionManager.get(name, collectionName, lock);
126: }
127:
128: private CollectionManager collectionManager;
129:
130: /**
131: * Invoked in rule session reset() method.
132: * Subclasses can put cleanup logic here.
133: */
134: public void reset() {
135: // No op
136: }
137:
138: }
|