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