01: /*
02: * hammurapi-rules @mesopotamia.version@
03: * Hammurapi rules engine.
04: * Copyright (C) 2005 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://http://www.hammurapi.biz
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.rules;
24:
25: import java.util.ArrayList;
26: import java.util.Iterator;
27: import java.util.List;
28:
29: public class KnowledgeMaximizingCompactor implements KnowledgeCompactor {
30:
31: public KnowledgeMaximizingCompactor() {
32: super ();
33: // TODO Auto-generated constructor stub
34: }
35:
36: /**
37: * This method preprocesses objects before returning them from getObjects() method.
38: * This implementation "maximizes knowledge" i.e. removes conclusions for which there are
39: * superceding conslusions. E.g. Father(John, Peter) supercedes Parent(John, Peter) and if
40: * both these conclusions are in the source collection then Parent(John, Peter) will not be
41: * included in the target collection. If there are two equal conclusions then their derivations
42: * are merged and only one conclusion is retained.
43: * @param results
44: * @param finalCollection
45: */
46: public List compact(List results) {
47: List ret = new ArrayList();
48: Iterator it = results.iterator();
49: Z: while (it.hasNext()) {
50: Object newFact = it.next();
51: if (newFact instanceof Supercedable) {
52: Iterator fit = ret.iterator();
53: while (fit.hasNext()) {
54: Object existingFact = fit.next();
55: if (existingFact instanceof Supercedable) {
56: Supercedable ec = (Supercedable) existingFact;
57: Supercedable nc = (Supercedable) newFact;
58:
59: if (ec instanceof Conclusion
60: && nc instanceof Conclusion
61: && ec.equals(nc)) {
62: ((Conclusion) ec)
63: .mergeDerivations((Conclusion) nc);
64: continue Z;
65: }
66:
67: if (ec.super cedes(nc)) { // Existing fact is more specific than new fact.
68: continue Z;
69: }
70:
71: if (nc.supercedes(ec)) {
72: fit.remove();
73: }
74: }
75: }
76: }
77:
78: ret.add(newFact);
79: }
80: return ret;
81: }
82:
83: }
|