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 * &lt;OntologyConfiguration&gt;
049 *      &lt;MainOntology&gt;
050 *              &lt;URL&gt;http://gaia.fdi.ucm.es/ontologies/vacation.owl&lt;/URL&gt;
051 *              &lt;LocalCopy&gt;es/ucm/fdi/gaia/jcolibri/test/test10/vacation.owl&lt;/LocalCopy&gt;
052 *      &lt;/MainOntology&gt;   
053 *      
054 *      &lt;!-- There are not subontologies --&gt;
055 *      
056 *      &lt;CaseMainConcept&gt;VACATION_CASE&lt;/CaseMainConcept&gt;
057 *      
058 *      &lt;DescriptionClassName&gt;es.ucm.fdi.gaia.jcolibri.test.test10.TravelDescription&lt;/DescriptionClassName&gt;
059 *      &lt;DescriptionMappings&gt;
060 *              &lt;Map&gt;
061 *                      &lt;Property&gt;HAS-DESTINATION&lt;/Property&gt;
062 *                      &lt;Concept&gt;DESTINATION&lt;/Concept&gt;
063 *                      &lt;Attribute&gt;Destination&lt;/Attribute&gt;
064 *              &lt;/Map&gt;
065 *              &lt;Map&gt;
066 *                      &lt;Property&gt;HAS-CATEGORY&lt;/Property&gt;
067 *                      &lt;Concept&gt;CATEGORY&lt;/Concept&gt;
068 *                      &lt;Attribute&gt;Accommodation&lt;/Attribute&gt;
069 *              &lt;/Map&gt;
070 *              &lt;Map&gt;
071 *                      &lt;Property&gt;HAS-PERSONS&lt;/Property&gt;
072 *                      &lt;Concept&gt;PERSONS&lt;/Concept&gt;
073 *                      &lt;Attribute&gt;NumberOfPersons&lt;/Attribute&gt;
074 *              &lt;/Map&gt;
075 *              &lt;Map&gt;
076 *                      &lt;Property&gt;HAS-TRANSPORTATION&lt;/Property&gt;
077 *                      &lt;Concept&gt;TRANSPORTATION&lt;/Concept&gt;
078 *                      &lt;Attribute&gt;Transportation&lt;/Attribute&gt;
079 *              &lt;/Map&gt;
080 *              &lt;Map&gt;
081 *                      &lt;Property&gt;HAS-SEASON&lt;/Property&gt;
082 *                      &lt;Concept&gt;SEASON&lt;/Concept&gt;
083 *                      &lt;Attribute&gt;Season&lt;/Attribute&gt;
084 *              &lt;/Map&gt;
085 *              &lt;Map&gt;
086 *                      &lt;Property&gt;HAS-HOLIDAY_TYPE&lt;/Property&gt;
087 *                      &lt;Concept&gt;HOLIDAY_TYPE&lt;/Concept&gt;
088 *                      &lt;Attribute&gt;HolidayType&lt;/Attribute&gt;
089 *              &lt;/Map&gt;
090 *              &lt;Map&gt;
091 *                      &lt;Property&gt;HAS-DURATION&lt;/Property&gt;
092 *                      &lt;Concept&gt;DURATION&lt;/Concept&gt;
093 *                      &lt;Attribute&gt;Duration&lt;/Attribute&gt;
094 *              &lt;/Map&gt;
095 *      &lt;/DescriptionMappings&gt;
096 *      
097 *      &lt;SolutionClassName&gt;es.ucm.fdi.gaia.jcolibri.test.test10.TravelSolution&lt;/SolutionClassName&gt;
098 *      &lt;SolutionMappings&gt;
099 *              &lt;Map&gt;
100 *                      &lt;Property&gt;HAS-PRICE&lt;/Property&gt;
101 *                      &lt;Concept&gt;PRICE&lt;/Concept&gt;
102 *                      &lt;Attribute&gt;price&lt;/Attribute&gt;
103 *              &lt;/Map&gt;
104 *      &lt;/SolutionMappings&gt;
105 * &lt;/OntologyConfiguration&gt;
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}