001/** 002 * OntDeep.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 * 13/01/2007 008 */ 009package es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.ontology; 010 011import java.util.ArrayList; 012 013import es.ucm.fdi.gaia.jcolibri.datatypes.Instance; 014import es.ucm.fdi.gaia.jcolibri.exception.NoApplicableSimilarityFunctionException; 015import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction; 016import es.ucm.fdi.gaia.jcolibri.util.FileIO; 017import es.ucm.fdi.gaia.jcolibri.util.OntoBridgeSingleton; 018import es.ucm.fdi.gaia.ontobridge.OntoBridge; 019import es.ucm.fdi.gaia.ontobridge.OntologyDocument; 020import org.apache.logging.log4j.LogManager; 021 022/** 023 * This function computes the fdeep similarity. See package documentation for 024 * details. 025 * 026 * @author Juan A. Recio-Garcia 027 * @version 1.0 028 */ 029public class OntDeep implements LocalSimilarityFunction 030{ 031 032 /* 033 * (non-Javadoc) 034 * 035 * @see jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction#compute(java.lang.Object, 036 * java.lang.Object, jcolibri.cbrcore.CaseComponent, 037 * jcolibri.cbrcore.CaseComponent, jcolibri.cbrcore.CBRCase, 038 * jcolibri.cbrcore.CBRQuery, java.lang.String) 039 */ 040 public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException 041 { 042 if ((caseObject == null) || (queryObject == null)) 043 return 0; 044 if (!(caseObject instanceof Instance)) 045 throw new NoApplicableSimilarityFunctionException(this.getClass(), caseObject.getClass()); 046 if (!(queryObject instanceof Instance)) 047 throw new NoApplicableSimilarityFunctionException(this.getClass(), queryObject 048 .getClass()); 049 050 Instance i1 = (Instance) caseObject; 051 Instance i2 = (Instance) queryObject; 052 053 if (i1.equals(i2)) 054 return 1; 055 056 OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); 057 058 double up = ob.maxProfLCS(i1.toString(), i2.toString()); 059 double down; 060 061 int prof1 = ob.profInstance(i1.toString()); 062 int prof2 = ob.profInstance(i2.toString()); 063 if (prof1 > prof2) 064 down = prof1; 065 else 066 down = prof2; 067 068 return up / down; 069 } 070 071 /** Applicable to Instance */ 072 public boolean isApplicable(Object o1, Object o2) 073 { 074 if ((o1 == null) && (o2 == null)) 075 return true; 076 else if (o1 == null) 077 return o2 instanceof Instance; 078 else if (o2 == null) 079 return o1 instanceof Instance; 080 else 081 return (o1 instanceof Instance) && (o2 instanceof Instance); 082 } 083 084 /** 085 * Testing method using test5 ontology 086 */ 087 public static void main(String[] args) 088 { 089 try 090 { 091 // Obtain a reference to OntoBridge 092 OntoBridge ob = OntoBridgeSingleton.getOntoBridge(); 093 // Configure it to work with the Pellet reasoner 094 ob.initWithPelletReasoner(); 095 // Setup the main ontology 096 OntologyDocument mainOnto = new OntologyDocument("http://gaia.fdi.ucm.es/ontologies/vacation.owl", FileIO 097 .findFile("jcolibri/test/test5/vacation.owl").toExternalForm()); 098 // There are not subontologies 099 ArrayList<OntologyDocument> subOntologies = new ArrayList<OntologyDocument>(); 100 // Load the ontology 101 ob.loadOntology(mainOnto, subOntologies, false); 102 103 OntDeep sim = new OntDeep(); 104 105 System.out.println("deep(CAR,TRAIN)=" + sim.compute(new Instance("CAR"), new Instance("TRAIN"))); 106 System.out.println("deep(CAR,IBIZA)=" + sim.compute(new Instance("CAR"), new Instance("IBIZA"))); 107 System.out.println("deep(CAR,I101)=" + sim.compute(new Instance("CAR"), new Instance("I101"))); 108 109 } catch (Exception e) 110 { 111 LogManager.getLogger(OntDeepBasic.class).error(e); 112 } 113 } 114 115}