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