001/**
002 * Test6.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 * 03/05/2007
008 */
009package es.ucm.fdi.gaia.jcolibri.test.test6;
010
011
012import java.util.ArrayList;
013import java.util.Date;
014
015import org.apache.log4j.LogManager;
016
017import es.ucm.fdi.gaia.jcolibri.casebase.LinealCaseBase;
018import es.ucm.fdi.gaia.jcolibri.cbraplications.StandardCBRApplication;
019import es.ucm.fdi.gaia.jcolibri.cbrcore.Attribute;
020import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
021import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCaseBase;
022import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
023import es.ucm.fdi.gaia.jcolibri.cbrcore.Connector;
024import es.ucm.fdi.gaia.jcolibri.connector.PlainTextConnector;
025import es.ucm.fdi.gaia.jcolibri.exception.AttributeAccessException;
026import es.ucm.fdi.gaia.jcolibri.exception.ExecutionException;
027
028/**
029 * This example shows how to use the Plain Text connector.
030 * Here we only read the cases and store a new one in the persistence file.
031 * <p>
032 * The case base (iris_data_jCOLIBRI.txt) contains information about iris:
033 * <pre>
034 * #Columns are: Sepal Length, Sepal Width, Petal Length, Petal Width, Type of Iris,
035 * 
036 * Case 1,5.1,3.5,1.4,0.2,Iris-setosa
037 * Case 2,4.9,3,1.4,0.2,Iris-setosa
038 * Case 3,4.7,3.2,1.3,0.2,Iris-setosa
039 * ...
040 * </pre>
041 * 
042 * These cases are mapped into the following structure:
043 *  * <pre>
044 * Case
045 *  |
046 *  +- Description
047 *  |       |
048 *  |       +- id *          (1)
049 *  |       +- sepalLength   (2)
050 *  |       +- sepalWidth    (3)
051 *  |       +- petalLength   (4)
052 *  |       +- petalWidth    (5)
053 *  |
054 *  +- Solution
055 *          |
056 *          +- type *        (6)
057 * </pre>
058 * The attributes with * are the ids of the compound objects and the numbers between parenthesis are the corresponding columns in the text file.
059 * <p>
060 * The mapping is configured by the <b>plaintextconfig.xml</b> file following the schema defined in PlainTextConnector:
061 * <pre>
062 * &lt;TextFileConfiguration&gt;
063 *      &lt;FilePath&gt;es/ucm/fdi/gaia/jcolibri/test/test6/iris_data_jCOLIBRI.txt&lt;/FilePath&gt;
064 *      &lt;Delimiters&gt;,&lt;/Delimiters&gt;
065 *      &lt;DescriptionClassName&gt;es.ucm.fdi.gaia.jcolibri.test.test6.IrisDescription&lt;/DescriptionClassName&gt;
066 *      &lt;DescriptionMappings&gt;
067 *              &lt;Map&gt;sepalLength&lt;/Map&gt;
068 *              &lt;Map&gt;sepalWidth&lt;/Map&gt;
069 *              &lt;Map&gt;petalLength&lt;/Map&gt;
070 *              &lt;Map&gt;petalWidth&lt;/Map&gt;               
071 *      &lt;/DescriptionMappings&gt;
072 *      &lt;SolutionClassName&gt;es.ucm.fdi.gaia.jcolibri.test.test6.IrisSolution&lt;/SolutionClassName&gt;
073 *      &lt;SolutionMappings&gt;
074 *      &lt;Map&gt;type&lt;/Map&gt;
075 *      &lt;/SolutionMappings&gt;
076 * &lt;/TextFileConfiguration&gt;
077 * </pre>
078 * First, we define the path containing the data and the characters used as delimiters (comma in this example).
079 * <br>
080 * Then we map each part of the case. Following the order of the columns in the text file we have to indicate to which attributes are mapped.
081 * This connector only uses the id of the description. It must be the first column of each row and is not included in the mapping file
082 * <br>
083 * 
084 * 
085 * @author Juan A. Recio-Garcia
086 * @version 1.0
087 * 
088 * @see jcolibri.connector.PlainTextConnector
089 */
090public class Test6 implements StandardCBRApplication {
091
092        Connector _connector;
093        CBRCaseBase _caseBase;
094        
095        
096        /* (non-Javadoc)
097         * @see jcolibri.cbraplications.StandardCBRApplication#configure()
098         */
099        public void configure() throws ExecutionException {
100                try{
101                        _connector = new PlainTextConnector();
102                        _connector.initFromXMLfile(es.ucm.fdi.gaia.jcolibri.util.FileIO.findFile("es/ucm/fdi/gaia/jcolibri/test/test6/plaintextconfig.xml"));
103                        _caseBase  = new LinealCaseBase();
104                        } catch (Exception e){
105                                throw new ExecutionException(e);
106                }
107
108        }
109        
110        /* (non-Javadoc)
111         * @see jcolibri.cbraplications.StandardCBRApplication#preCycle()
112         */
113        public CBRCaseBase preCycle() throws ExecutionException {
114                _caseBase.init(_connector);
115                java.util.Collection<CBRCase> cases = _caseBase.getCases();
116                for(CBRCase c: cases)
117                        System.out.println(c);
118                return _caseBase;
119        }
120
121        /* (non-Javadoc)
122         * @see jcolibri.cbraplications.StandardCBRApplication#cycle()
123         */
124        public void cycle(CBRQuery query) throws ExecutionException {
125                //Obtain only the first case
126                CBRCase newcase = _caseBase.getCases().iterator().next();
127                //Modify its id attribute and store it back
128                Attribute id = newcase.getDescription().getIdAttribute();
129                try {
130                        Date d = new Date();
131                        id.setValue(newcase.getDescription(), ("case "+d.toString()).replaceAll(" ", "_"));
132                } catch (AttributeAccessException e) {
133                        LogManager.getLogger(this.getClass()).error(e);
134                }
135                
136                ArrayList<CBRCase> casestoLearnt = new ArrayList<CBRCase>();
137                casestoLearnt.add(newcase);
138                _caseBase.learnCases(casestoLearnt);
139
140        }
141
142        /* (non-Javadoc)
143         * @see jcolibri.cbraplications.StandardCBRApplication#postCycle()
144         */
145        public void postCycle() throws ExecutionException {
146                _connector.close();
147
148        }
149
150
151        /**
152         * @param args
153         */
154        public static void main(String[] args) {
155                Test6 test = new Test6();
156                try {
157                        test.configure();
158                        test.preCycle();
159                        test.cycle(null);
160                } catch (ExecutionException e) {
161                        LogManager.getLogger(Test6.class).error(e);
162                }
163
164        }
165
166}