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