001    /**
002     * Equal.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 28/10/2007
008     */
009    package jcolibri.method.retrieve.FilterBasedRetrieval.predicates;
010    
011    import java.util.Set;
012    
013    import jcolibri.datatypes.Instance;
014    import jcolibri.exception.NoApplicableFilterPredicateException;
015    import es.ucm.fdi.gaia.ontobridge.OntoBridge;
016    
017    /**
018     * Predicate that compares if two objects (that must be Instance typed) are compatible.
019     * Compatible means that the Least-Common-Subsumer of the query and the case instances
020     * is the direct parent of the query. Informally, it means that que case is "under" 
021     * the query in the ontology tree.
022     * Only applicable to Instances. 
023     * 
024     * @author Juan A. Recio-Garcia
025     * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
026     * @version 1.0
027     * @see jcolibri.method.retrieve.FilterBasedRetrieval.FilterBasedRetrievalMethod
028     * @see jcolibri.method.retrieve.FilterBasedRetrieval.FilterConfig
029     */
030    public class OntologyCompatible implements FilterPredicate
031    {
032        public boolean compute(Object caseObject, Object queryObject) throws NoApplicableFilterPredicateException
033        {
034            if((caseObject == null)&&(queryObject==null))
035                return true;
036            else if(caseObject == null)
037                return false;
038            else if(queryObject == null)
039                return true;
040            else if (! (caseObject instanceof Instance))
041                throw new jcolibri.exception.NoApplicableFilterPredicateException(this.getClass(), caseObject.getClass());
042            else if (! (queryObject instanceof Instance))
043                throw new jcolibri.exception.NoApplicableFilterPredicateException(this.getClass(), queryObject.getClass());
044            else 
045            {
046                Instance caseInstance = (Instance) caseObject;
047                Instance queryInstance = (Instance) queryObject;
048                OntoBridge ob = jcolibri.util.OntoBridgeSingleton.getOntoBridge();
049    
050                Set<String> lcs = ob.LCS(caseInstance.toString(), queryInstance.toString());
051                Set<String> directParents = ob.LCS(queryInstance.toString(), queryInstance.toString());
052                
053                lcs.retainAll(directParents);
054                return !lcs.isEmpty();
055    
056            }
057        }
058    
059    }