001/** 002 * Test10.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 * 07/06/2007 008 */ 009package es.ucm.fdi.gaia.jcolibri.test.test10; 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.OntologyConnector; 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.ontology.OntCosine; 032import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.ontology.OntDeep; 033import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.ontology.OntDeepBasic; 034import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local.ontology.OntDetail; 035import es.ucm.fdi.gaia.jcolibri.method.retrieve.selection.SelectCases; 036import es.ucm.fdi.gaia.jcolibri.util.CopyUtils; 037 038 039/** 040 * This example shows how to use the ontology connector and the ontology-based similarity functions. 041 * <br> 042 * To use the ontology connector, all the attributes of the description and the solution must be Instance typed. 043 * <p> 044 * This test shows an example where a case is composed by a description and a solution following the mapping: 045 * <p><center><img src="test10mapping.jpg"/></center></p> 046 * To configure the connector with that mapping we use the configuration file: 047 * <pre> 048 * <OntologyConfiguration> 049 * <MainOntology> 050 * <URL>http://gaia.fdi.ucm.es/ontologies/vacation.owl</URL> 051 * <LocalCopy>es/ucm/fdi/gaia/jcolibri/test/test10/vacation.owl</LocalCopy> 052 * </MainOntology> 053 * 054 * <!-- There are not subontologies --> 055 * 056 * <CaseMainConcept>VACATION_CASE</CaseMainConcept> 057 * 058 * <DescriptionClassName>es.ucm.fdi.gaia.jcolibri.test.test10.TravelDescription</DescriptionClassName> 059 * <DescriptionMappings> 060 * <Map> 061 * <Property>HAS-DESTINATION</Property> 062 * <Concept>DESTINATION</Concept> 063 * <Attribute>Destination</Attribute> 064 * </Map> 065 * <Map> 066 * <Property>HAS-CATEGORY</Property> 067 * <Concept>CATEGORY</Concept> 068 * <Attribute>Accommodation</Attribute> 069 * </Map> 070 * <Map> 071 * <Property>HAS-PERSONS</Property> 072 * <Concept>PERSONS</Concept> 073 * <Attribute>NumberOfPersons</Attribute> 074 * </Map> 075 * <Map> 076 * <Property>HAS-TRANSPORTATION</Property> 077 * <Concept>TRANSPORTATION</Concept> 078 * <Attribute>Transportation</Attribute> 079 * </Map> 080 * <Map> 081 * <Property>HAS-SEASON</Property> 082 * <Concept>SEASON</Concept> 083 * <Attribute>Season</Attribute> 084 * </Map> 085 * <Map> 086 * <Property>HAS-HOLIDAY_TYPE</Property> 087 * <Concept>HOLIDAY_TYPE</Concept> 088 * <Attribute>HolidayType</Attribute> 089 * </Map> 090 * <Map> 091 * <Property>HAS-DURATION</Property> 092 * <Concept>DURATION</Concept> 093 * <Attribute>Duration</Attribute> 094 * </Map> 095 * </DescriptionMappings> 096 * 097 * <SolutionClassName>es.ucm.fdi.gaia.jcolibri.test.test10.TravelSolution</SolutionClassName> 098 * <SolutionMappings> 099 * <Map> 100 * <Property>HAS-PRICE</Property> 101 * <Concept>PRICE</Concept> 102 * <Attribute>price</Attribute> 103 * </Map> 104 * </SolutionMappings> 105 * </OntologyConfiguration> 106 * </pre> 107 * <p> 108 * The source code shows how to use the connector and then an complete CBR cycle 109 * using the similarity functions implemented in the package: 110 * jcolibri.method.retrieve.NNretrieval.similarity.local.ontology 111 * <p><img src="ontsim.jpg"/></p> 112 * 113 * 114 * @author Juan A. Recio-Garcia 115 * @version 1.0 116 * @see jcolibri.connector.OntologyConnector 117 * @see jcolibri.method.retrieve.NNretrieval.similarity.local.ontology 118 * @see es.ucm.fdi.gaia.jcolibri.test.test10.TravelDescription 119 * @see es.ucm.fdi.gaia.jcolibri.test.test10.TravelSolution 120 */ 121public class Test10 implements StandardCBRApplication { 122 123 Connector _connector; 124 CBRCaseBase _caseBase; 125 126 /* (non-Javadoc) 127 * @see jcolibri.cbraplications.BasicCBRApplication#configure() 128 */ 129 public void configure() throws ExecutionException{ 130 try{ 131 _connector = new OntologyConnector(); 132 _connector.initFromXMLfile(es.ucm.fdi.gaia.jcolibri.util.FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test10/ontologyconfig.xml")); 133 _caseBase = new LinealCaseBase(); 134 135 } catch (Exception e){ 136 throw new ExecutionException(e); 137 } 138 } 139 140 141 /* (non-Javadoc) 142 * @see jcolibri.cbraplications.BasicCBRApplication#preCycle() 143 */ 144 public CBRCaseBase preCycle() throws ExecutionException { 145 _caseBase.init(_connector); 146 for(es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase c: _caseBase.getCases()) 147 System.out.println(c); 148 return _caseBase; 149 } 150 151 /* (non-Javadoc) 152 * @see jcolibri.cbraplications.BasicCBRApplication#cycle() 153 */ 154 public void cycle(CBRQuery query) throws ExecutionException 155 { 156 /********* NumericSim Retrieval **********/ 157 // We are using ontology-based similarity functions 158 NNConfig simConfig = new NNConfig(); 159 simConfig.setDescriptionSimFunction(new Average()); 160 simConfig.addMapping(new Attribute("Accommodation", TravelDescription.class), new OntCosine()); 161 simConfig.addMapping(new Attribute("Duration", TravelDescription.class), new OntDetail()); 162 simConfig.addMapping(new Attribute("HolidayType", TravelDescription.class), new OntDeep()); 163 simConfig.addMapping(new Attribute("NumberOfPersons", TravelDescription.class), new OntDeepBasic()); 164 simConfig.addMapping(new Attribute("Transportation", TravelDescription.class), new OntDeepBasic()); 165 simConfig.addMapping(new Attribute("Season", TravelDescription.class), new Equal()); 166 167 168 System.out.println("Query:"); 169 System.out.println(query); 170 System.out.println(); 171 172 173 /********* Execute NN ************/ 174 Collection<RetrievalResult> eval = NNScoringMethod.evaluateSimilarity(_caseBase.getCases(), query, simConfig); 175 176 /********* Select cases **********/ 177 eval = SelectCases.selectTopKRR(eval, 10); 178 179 // Print the retrieval 180 System.out.println("Retrieved cases:"); 181 for(RetrievalResult nse: eval) 182 System.out.println(nse); 183 184 185 } 186 187 /* (non-Javadoc) 188 * @see jcolibri.cbraplications.BasicCBRApplication#postCycle() 189 */ 190 public void postCycle() throws ExecutionException { 191 this._caseBase.close(); 192 193 } 194 195 /** 196 * Tests the OntologyConnector. It reads the cases, creates a new one, 197 * stores the new case in the case base, closes the connector generating a new owl file, 198 * reads the cases again, deletes the new case and closes generating a new owl file that is equal to the original one. 199 */ 200 public static void main(String[] args) { 201 try { 202 203 //Configure connector and caseBase 204 OntologyConnector _connector = new OntologyConnector(); 205 _connector.initFromXMLfile(es.ucm.fdi.gaia.jcolibri.util.FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test10/ontologyconfig.xml")); 206 CBRCaseBase _caseBase = new LinealCaseBase(); 207 208 //Load cases 209 _caseBase.init(_connector); 210 System.out.println("Loaded cases:"); 211 for(es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase c: _caseBase.getCases()) 212 System.out.println(c); 213 214 //Obtain the first case 215 CBRCase aCase = _caseBase.getCases().iterator().next(); 216 217 //Create a new copy 218 aCase = CopyUtils.copyCBRCase(aCase); 219 220 //Lets modify the id attribute with a new instance 221 Instance newId = Instance.createInstance("newInstance", _connector.getCaseMainConcept()); 222 223 //modify the description id 224 Attribute descriptionIdAttribute = aCase.getDescription().getIdAttribute(); 225 descriptionIdAttribute.setValue(aCase.getDescription(), newId); 226 //modify the solution id 227 Attribute solutionIdAttribute = aCase.getSolution().getIdAttribute(); 228 solutionIdAttribute.setValue(aCase.getSolution(), newId); 229 230 //Store the new case 231 ArrayList<CBRCase> cases = new ArrayList<CBRCase>(); 232 cases.add(aCase); 233 _connector.storeCases(cases); 234 235 //Close the connector. This generates a new owl file 236 _connector.close(); 237 238 //Lets start again 239 _connector = new OntologyConnector(); 240 _connector.initFromXMLfile(es.ucm.fdi.gaia.jcolibri.util.FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test10/ontologyconfig.xml")); 241 _caseBase = new LinealCaseBase(); 242 243 _caseBase.init(_connector); 244 245 //Print cases with the new one 246 System.out.println("Loaded cases: (the new case should be there)"); 247 for(es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase c: _caseBase.getCases()) 248 { 249 System.out.println(c); 250 //Obtain a reference to the new case 251 if(c.getID().toString().equals("newInstance")) 252 aCase = c; 253 } 254 255 //forget that case 256 cases.clear(); 257 cases.add(aCase); 258 _caseBase.forgetCases(cases); 259 260 //close and generate a new owl file equal to the original one. 261 _connector.close(); 262 263 264 265 /*************************************************************************************************************/ 266 /****************** Lets test the ontological similarity functions *******************************************/ 267 268 Test10 test = new Test10(); 269 270 //Normal configure and preCycle calls 271 test.configure(); 272 test.preCycle(); 273 274 //Create a query 275 TravelDescription queryDesc = new TravelDescription(); 276 try { 277 queryDesc.setAccommodation(new Instance("THREESTARS")); 278 queryDesc.setDuration(new Instance("I10")); 279 queryDesc.setHolidayType(new Instance("RECREATION")); 280 queryDesc.setNumberOfPersons(new Instance("I4")); 281 queryDesc.setSeason(new Instance("April")); 282 queryDesc.setTransportation(new Instance("PLANE")); 283 } catch (OntologyAccessException e) { 284 throw new ExecutionException(e); 285 } 286 CBRQuery query = new CBRQuery(); 287 query.setDescription(queryDesc); 288 289 //Execute cycle with the query 290 test.cycle(query); 291 292 //Postcycle 293 test.postCycle(); 294 } catch (ExecutionException e) { 295 System.out.println(e.getMessage()); 296 e.printStackTrace(); 297 } catch (Exception e) { 298 // TODO Auto-generated catch block 299 e.printStackTrace(); 300 } 301 } 302 303}