001    package aima.logic.fol.inference.otter.defaultimpl;
002    
003    import java.util.ArrayList;
004    import java.util.Collections;
005    import java.util.Comparator;
006    import java.util.List;
007    import java.util.Set;
008    import java.util.SortedSet;
009    import java.util.TreeSet;
010    
011    import aima.logic.fol.inference.otter.LightestClauseHeuristic;
012    import aima.logic.fol.kb.data.Clause;
013    
014    /**
015     * @author Ciaran O'Reilly
016     * 
017     */
018    public class DefaultLightestClauseHeuristic implements LightestClauseHeuristic {
019    
020            private LightestClauseSorter c = new LightestClauseSorter();
021            private SortedSet<Clause> sos = new TreeSet<Clause>(c);
022    
023            public DefaultLightestClauseHeuristic() {
024    
025            }
026    
027            //
028            // START-LightestClauseHeuristic
029            public Clause getLightestClause() {
030                    Clause lightest = null;
031    
032                    if (sos.size() > 0) {
033                            lightest = sos.first();
034                    }
035    
036                    return lightest;
037            }
038    
039            public void initialSOS(Set<Clause> clauses) {
040                    sos.clear();
041                    sos.addAll(clauses);
042            }
043    
044            public void addedClauseToSOS(Clause clause) {
045                    sos.add(clause);
046            }
047    
048            public void removedClauseFromSOS(Clause clause) {
049                    sos.remove(clause);
050            }
051    
052            // END-LightestClauseHeuristic
053            //
054    }
055    
056    class LightestClauseSorter implements Comparator<Clause> {
057            public int compare(Clause c1, Clause c2) {
058                    if (c1 == c2) {
059                            return 0;
060                    }
061                    int c1Val = c1.getNumberLiterals();
062                    int c2Val = c2.getNumberLiterals();
063                    return (c1Val < c2Val ? -1 : (c1Val == c2Val ? (compareEqualityIdentities(c1, c2)) : 1));
064            }
065            
066            private int compareEqualityIdentities(Clause c1, Clause c2) {
067                    int c1Len = c1.getEqualityIdentity().length();
068                    int c2Len = c2.getEqualityIdentity().length();
069                    
070                    return (c1Len < c2Len ? -1 : (c1Len == c2Len ? c1.getEqualityIdentity().compareTo(c2.getEqualityIdentity()): 1));
071            }
072    }