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