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    
032    public abstract class AbstractRule extends ComponentBase implements 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()+" - "+getDescription();
096            }
097    
098            public abstract Collection getRemoveHandlers();
099            
100            public void start() throws ConfigurationException {
101                    this.collectionManager=(CollectionManager) get("/collection-manager");
102                    if (getName()==null) {
103                            throw new ConfigurationException("Rule name must not be null");
104                    }
105                    if (!(owner instanceof KnowledgeBase)) {
106                            throw new ConfigurationException("Rule container (ruleset) must implement "+KnowledgeBase.class);
107                    }
108            }
109            
110            public void stop() {
111                    // Override this method to release resources.
112            }                       
113            
114            /**
115             * Convenience method to retrieve collection from the collection manager.
116             * @param setName
117             * @return
118             */
119            protected Collection getCollection(String collectionName, Object lock) {
120                    return collectionManager.get(name, collectionName, lock);
121            }
122                    
123            private CollectionManager collectionManager;
124    
125            /**
126             * Invoked in rule session reset() method.
127             * Subclasses can put cleanup logic here.
128             */
129            public void reset() {
130                    // No op
131            }
132            
133    }