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.ArrayList;
026    import java.util.Iterator;
027    import java.util.List;
028    
029    public class KnowledgeMaximizingCompactor implements KnowledgeCompactor {
030    
031            public KnowledgeMaximizingCompactor() {
032                    super();
033                    // TODO Auto-generated constructor stub
034            }
035    
036            /**
037             * This method preprocesses objects before returning them from getObjects() method.
038             * This implementation "maximizes knowledge" i.e. removes conclusions for which there are
039             * superceding conslusions. E.g. Father(John, Peter) supercedes Parent(John, Peter) and if 
040             * both these conclusions are in the source collection then Parent(John, Peter) will not be 
041             * included in the target collection. If there are two equal conclusions then their derivations
042             * are merged and only one conclusion is retained.
043             * @param results
044             * @param finalCollection
045             */
046            public List compact(List results) {
047                    List ret=new ArrayList();
048                    Iterator it=results.iterator();
049                    Z: while (it.hasNext()) {
050                            Object newFact=it.next();
051                            if (newFact instanceof Supercedable) {
052                                    Iterator fit=ret.iterator();
053                                    while (fit.hasNext()) {
054                                            Object existingFact=fit.next();
055                                            if (existingFact instanceof Supercedable) {
056                                                    Supercedable ec=(Supercedable) existingFact;
057                                                    Supercedable nc=(Supercedable) newFact;
058                                                    
059                                                    if (ec instanceof Conclusion && nc instanceof Conclusion && ec.equals(nc)) {
060                                                            ((Conclusion) ec).mergeDerivations((Conclusion) nc);
061                                                            continue Z;
062                                                    }
063                                                    
064                                                    if (ec.supercedes(nc)) { // Existing fact is more specific than new fact.
065                                                            continue Z;
066                                                    }
067                                                    
068                                                    if (nc.supercedes(ec)) {
069                                                            fit.remove();
070                                                    }
071                                            }
072                                    }
073                            }
074                                    
075                            ret.add(newFact);
076                    }
077                    return ret;
078            }
079                    
080    }