001/** 002 * Test5.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 * 11/01/2007 008 */ 009package es.ucm.fdi.gaia.jcolibri.test.test5; 010 011 012import java.util.ArrayList; 013import java.util.Collection; 014 015import es.ucm.fdi.gaia.jcolibri.casebase.LinealCaseBase; 016import es.ucm.fdi.gaia.jcolibri.cbraplications.StandardCBRApplication; 017import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute; 018import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase; 019import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCaseBase; 020import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery; 021import es.ucm.fdi.gaia.jcolibri.cbrcore.Connector; 022import es.ucm.fdi.gaia.jcolibri.connector.DataBaseConnector; 023import es.ucm.fdi.gaia.jcolibri.datatypes.Instance; 024import es.ucm.fdi.gaia.jcolibri.exception.ExecutionException; 025import es.ucm.fdi.gaia.jcolibri.exception.OntologyAccessException; 026import es.ucm.fdi.gaia.jcolibri.method.retrieve.RetrievalResult; 027import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.NNConfig; 028import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.NNScoringMethod; 029import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.global.Average; 030import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.Equal; 031import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.Interval; 032import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.ontology.OntCosine; 033import es.ucm.fdi.gaia.jcolibri.method.retrieve.selection.SelectCases; 034import es.ucm.fdi.gaia.jcolibri.method.reuse.CombineQueryAndCasesMethod; 035import es.ucm.fdi.gaia.jcolibri.method.reuse.NumericDirectProportionMethod; 036import es.ucm.fdi.gaia.jcolibri.util.FileIO; 037import es.ucm.fdi.gaia.ontobridge.OntoBridge; 038import es.ucm.fdi.gaia.ontobridge.OntologyDocument; 039 040 041/** 042 * This example shows how to map an attribute to an ontology and compute an ontology-based similarity function in the KNN. 043 * jCOLIBRI uses the OntoBridge library to do the mapping an manage the ontologies. 044 * <br> 045 * The mapping consists on reading the values of an attribute from the persistence layer and link them with instances of an ontology. 046 * In this example we are going to map the values of the Season column in the travelext database with the instances of the concept "SEASON" of the ontology vacation.owl. 047 * This ontology contains information about travels and vacations. The SEASON concept organizes the information as shown in this figure: 048 * <br><img src="vacation_season.jpg"/><br> 049 * To map this concept with the attributes in the database we have to change the type of the season attribute in the description to <b>Instance</b>. 050 * This type automatically connects with the ontology and links its value to the proper instance. To configure the mapping we have to modify the <b>travelDescription.hbm.xml</b> file pointing out that we are using a Instance: 051 * <pre> 052 * ... 053 * <property name="Season" column="Season"> 054 * <type name="es.ucm.fdi.gaia.jcolibri.connector.databaseutils.GenericUserType"> 055 * <param name="className">jcolibri.datatypes.Instance</param> 056 * </type> 057 * </property> 058 * ... 059 * </pre> 060 * And finally, in the configure() method of our application we have to setup OntoBridge with the ontology: 061 * <pre> 062 * ... 063 * OntoBridge ob = jcolibri.util.OntoBridgeSingleton.getOntoBridge(); 064 * ob.initWithPelletReasoner(); 065 * OntologyDocument mainOnto = new OntologyDocument("http://gaia.fdi.ucm.es/ontologies/vacation.owl", 066 * FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test5/vacation.owl").toURI().toString()); 067 * ArrayList<OntologyDocument> subOntologies = new ArrayList<OntologyDocument>(); 068 * ob.loadOntology(mainOnto, subOntologies, false); 069 * ... 070 * </pre> 071 * With these changes the ontology is loaded and the values of Season are mapped to its instances. 072 * Now, we can compute similarity functions that use information from the ontology. Here we are using the OntCosine() method: 073 * <pre> 074 * simConfig.addMapping(new Attribute("Season", TravelDescription.class), new OntCosine()); 075 * </pre> 076 * For more information about OntoBridge read its documentation or visit the web page: <a href="http://gaia.fdi.ucm.es/projects/ontobridge/">http://gaia.fdi.ucm.es/projects/ontobridge/</a>. 077 * 078 * @author Juan A. Recio-Garcia 079 * @version 1.0 080 * 081 * @see es.ucm.fdi.gaia.jcolibri.test.test5.TravelDescription 082 * @see jcolibri.method.retrieve.NNretrieval.similarity.local.ontology.OntCosine 083 * 084 * 085 */ 086public class Test5 implements StandardCBRApplication { 087 088 Connector _connector; 089 CBRCaseBase _caseBase; 090 091 /* (non-Javadoc) 092 * @see jcolibri.cbraplications.BasicCBRApplication#configure() 093 */ 094 public void configure() throws ExecutionException{ 095 try{ 096 _connector = new DataBaseConnector(); 097 _connector.initFromXMLfile(es.ucm.fdi.gaia.jcolibri.util.FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test5/databaseconfig.xml")); 098 _caseBase = new LinealCaseBase(); 099 100 // Obtain a reference to OntoBridge 101 OntoBridge ob = es.ucm.fdi.gaia.jcolibri.util.OntoBridgeSingleton.getOntoBridge(); 102 // Configure it to work with the Pellet reasoner 103 ob.initWithPelletReasoner(); 104 // Setup the main ontology 105 OntologyDocument mainOnto = new OntologyDocument("http://gaia.fdi.ucm.es/ontologies/vacation.owl", 106 FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test5/vacation.owl").toExternalForm()); 107 // There are not subontologies 108 ArrayList<OntologyDocument> subOntologies = new ArrayList<OntologyDocument>(); 109 // Load the ontology 110 ob.loadOntology(mainOnto, subOntologies, false); 111 112 // Print the instances of SEASON 113 System.out.println("Instances of SEASON"); 114 for(java.util.Iterator<String> i = ob.listInstances("SEASON"); i.hasNext();) 115 System.out.println(i.next()); 116 117 } catch (Exception e){ 118 throw new ExecutionException(e); 119 } 120 } 121 122 123 /* (non-Javadoc) 124 * @see jcolibri.cbraplications.BasicCBRApplication#preCycle() 125 */ 126 public CBRCaseBase preCycle() throws ExecutionException { 127 _caseBase.init(_connector); 128 for(es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase c: _caseBase.getCases()) 129 System.out.println(c); 130 return _caseBase; 131 } 132 133 /* (non-Javadoc) 134 * @see jcolibri.cbraplications.BasicCBRApplication#cycle() 135 */ 136 public void cycle(CBRQuery query) throws ExecutionException 137 { 138 /********* NumericSim Retrieval **********/ 139 140 NNConfig simConfig = new NNConfig(); 141 simConfig.setDescriptionSimFunction(new Average()); 142 simConfig.addMapping(new Attribute("Accommodation", TravelDescription.class), new Equal()); 143 Attribute duration = new Attribute("Duration", TravelDescription.class); 144 simConfig.addMapping(duration, new Interval(31)); 145 simConfig.setWeight(duration, 0.5); 146 simConfig.addMapping(new Attribute("HolidayType", TravelDescription.class), new Equal()); 147 simConfig.addMapping(new Attribute("NumberOfPersons", TravelDescription.class), new Equal()); 148 149 // Configure the OntCosine() function for the similarity of Season 150 simConfig.addMapping(new Attribute("Season", TravelDescription.class), new OntCosine()); 151 152 simConfig.addMapping(new Attribute("Region", TravelDescription.class), new Average()); 153 simConfig.addMapping(new Attribute("region", Region.class), new Equal()); 154 simConfig.addMapping(new Attribute("city", Region.class), new Equal()); 155 simConfig.addMapping(new Attribute("airport", Region.class), new Equal()); 156 simConfig.addMapping(new Attribute("currency", Region.class), new Equal()); 157 158 159 System.out.println("Query:"); 160 System.out.println(query); 161 System.out.println(); 162 163 /********* Execute NN ************/ 164 Collection<RetrievalResult> eval = NNScoringMethod.evaluateSimilarity(_caseBase.getCases(), query, simConfig); 165 166 /********* Select cases **********/ 167 Collection<CBRCase> selectedcases = SelectCases.selectTopK(eval, 1); 168 169 /********* Reuse **********/ 170 171 NumericDirectProportionMethod.directProportion( new Attribute("NumberOfPersons",TravelDescription.class), 172 new Attribute("price",TravelSolution.class), 173 query, selectedcases); 174 NumericDirectProportionMethod.directProportion( new Attribute("Duration",TravelDescription.class), 175 new Attribute("price",TravelSolution.class), 176 query, selectedcases); 177 // Print the retrieval 178 System.out.println("Retrieved cases:"); 179 for(RetrievalResult nse: eval) 180 System.out.println(nse); 181 182 System.out.println("Query:"); 183 System.out.println(query); 184 185 Collection<CBRCase> newcases = CombineQueryAndCasesMethod.combine(query, selectedcases); 186 System.out.println("Combined cases"); 187 for(es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase c: newcases) 188 System.out.println(c); 189 190 /********* Revise **********/ 191 /* 192 HashMap<Attribute, Object> componentsKeys = new HashMap<Attribute,Object>(); 193 componentsKeys.put(new Attribute("caseId",TravelDescription.class), "test3id"); 194 componentsKeys.put(new Attribute("id",TravelSolution.class), "test3id"); 195 jcolibri.method.revise.DefineNewIdsMethod.defineNewIdsMethod(newcases, componentsKeys); 196 197 System.out.println("Cases with new Id"); 198 for(es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase c: newcases) 199 System.out.println(c); 200 */ 201 /********* Retain **********/ 202 //jcolibri.method.retain.StoreCasesMethod.storeCases(_caseBase, newcases); 203 } 204 205 /* (non-Javadoc) 206 * @see jcolibri.cbraplications.BasicCBRApplication#postCycle() 207 */ 208 public void postCycle() throws ExecutionException { 209 this._caseBase.close(); 210 211 } 212 213 /** 214 * @param args 215 */ 216 public static void main(String[] args) { 217 // Launch DDBB manager 218 es.ucm.fdi.gaia.jcolibri.test.database.HSQLDBserver.init(); 219 220 Test5 test4 = new Test5(); 221 try { 222 test4.configure(); 223 test4.preCycle(); 224 225 //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 226 //do 227 //{ 228 /********* Query Definition **********/ 229 TravelDescription queryDesc = new TravelDescription(); 230 queryDesc.setAccommodation(TravelDescription.AccommodationTypes.ThreeStars); 231 queryDesc.setDuration(10); 232 queryDesc.setHolidayType("Recreation"); 233 queryDesc.setNumberOfPersons(4); 234 try { 235 queryDesc.setSeason(new Instance("April")); 236 } catch (OntologyAccessException e) { 237 throw new ExecutionException(e); 238 } 239 240 Region region = new Region(); 241 region.setRegion("Bulgaria"); 242 region.setCity("Sofia"); 243 region.setCurrency("Euro"); 244 region.setAirport("airport"); 245 queryDesc.setRegion(region); 246 247 CBRQuery query = new CBRQuery(); 248 query.setDescription(queryDesc); 249 250 test4.cycle(query); 251 252 // System.out.println("Cycle finished. Type exit to idem"); 253 //}while(!reader.readLine().equals("exit")); 254 255 test4.postCycle(); 256 257 //Shutdown DDBB manager 258 es.ucm.fdi.gaia.jcolibri.test.database.HSQLDBserver.shutDown(); 259 260 } catch (ExecutionException e) { 261 System.out.println(e.getMessage()); 262 e.printStackTrace(); 263 } catch (Exception e) { 264 // TODO Auto-generated catch block 265 e.printStackTrace(); 266 } 267 } 268 269}