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: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Set;
029:
030: /**
031: * This class "maximizes knowledge", which means when more specific (superceding)
032: * conclusions are added to the set less specific are removed. Conclusions less specific than present in
033: * the set are not added at all.
034: * @author Pavel Vlasov
035: * @revision $Revision$
036: */
037: public class KnowledgeMaximizingSet implements Set {
038: private Collection master;
039:
040: /**
041: * Adds object if collection doesn't contain equal or superceding object.
042: */
043: public boolean add(Object arg) {
044: if (arg instanceof Supercedable) {
045: Supercedable nc = (Supercedable) arg;
046: Iterator it = master.iterator();
047: while (it.hasNext()) {
048: Object o = it.next();
049: if (o instanceof Supercedable) {
050: Supercedable ec = (Supercedable) o;
051: if (ec.equals(nc) || ec.super cedes(nc)) {
052: return false;
053: }
054:
055: if (nc.super cedes(ec)) {
056: it.remove();
057: break; // There can be only one existing object in the collection superceded by this one.
058: }
059: }
060: }
061: return master.add(arg);
062: }
063:
064: return master.add(arg);
065: }
066:
067: public boolean addAll(Collection arg) {
068: boolean ret = false;
069: Iterator it = arg.iterator();
070: while (it.hasNext()) {
071: ret = ret || add(it.next());
072: }
073: return ret;
074: }
075:
076: public void clear() {
077: master.clear();
078: }
079:
080: public boolean contains(Object arg0) {
081: return master.contains(arg0);
082: }
083:
084: public boolean containsAll(Collection arg0) {
085: return master.containsAll(arg0);
086: }
087:
088: public boolean equals(Object arg0) {
089: return master.equals(arg0);
090: }
091:
092: public int hashCode() {
093: return master.hashCode();
094: }
095:
096: public boolean isEmpty() {
097: return master.isEmpty();
098: }
099:
100: public Iterator iterator() {
101: return master.iterator();
102: }
103:
104: public boolean remove(Object arg0) {
105: return master.remove(arg0);
106: }
107:
108: public boolean removeAll(Collection arg0) {
109: return master.removeAll(arg0);
110: }
111:
112: public boolean retainAll(Collection arg0) {
113: return master.retainAll(arg0);
114: }
115:
116: public int size() {
117: return master.size();
118: }
119:
120: public Object[] toArray() {
121: return master.toArray();
122: }
123:
124: public Object[] toArray(Object[] arg0) {
125: return master.toArray(arg0);
126: }
127:
128: /**
129: * @param master Master collection
130: */
131: public KnowledgeMaximizingSet(Collection master) {
132: this .master = master;
133: }
134:
135: /**
136: * Constructs new KnowledgeMaximizingSet with java.util.HashSet as master.
137: */
138: public KnowledgeMaximizingSet() {
139: this (new HashSet());
140: }
141:
142: }
|