001package es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.local;
002
003import java.io.BufferedReader;
004import java.io.InputStream;
005import java.io.InputStreamReader;
006import java.util.ArrayList;
007import java.util.StringTokenizer;
008
009import org.apache.commons.logging.LogFactory;
010import org.apache.log4j.LogManager;
011
012import es.ucm.fdi.gaia.jcolibri.exception.NoApplicableSimilarityFunctionException;
013import es.ucm.fdi.gaia.jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;
014import es.ucm.fdi.gaia.jcolibri.util.FileIO;
015
016
017/**
018 * Similarity function that uses a table to obtain the similarity between two values. 
019 * Allowed values are Strings or Enums. The table is read from a text file with the following format:
020 * <ul>
021 * <li>1st line: coma separated n categories
022 * <li>following n lines: n double values separated by comma. 
023 * </ul> 
024 */
025public class Table implements LocalSimilarityFunction {
026
027    double matrix[][] = null;
028
029    ArrayList<String> categories = new ArrayList<String>();
030
031    public Table(String filename)
032    {
033        try
034        {
035            InputStream is = FileIO.openFile(filename);
036            BufferedReader br = null;
037            br = new BufferedReader(new InputStreamReader(is));
038//          if (br == null)
039//          {
040//              LogManager.getLogger(this.getClass()).error("Cannot find file: " + filename);
041//              return;
042//          }
043            String line = br.readLine();
044            StringTokenizer st = new StringTokenizer(line, ",");
045            while (st.hasMoreTokens())
046                categories.add(st.nextToken());
047            int size = categories.size();
048
049            matrix = new double[size][size];
050
051            int i = 0;
052            while ((line = br.readLine()) != null)
053            {
054                st = new StringTokenizer(line, ",");
055                int j = 0;
056                while (st.hasMoreTokens())
057                    matrix[i][j++] = Double.parseDouble(st.nextToken());
058                i++;
059            }
060
061        } catch (Exception e)
062        {
063            LogManager.getLogger(this.getClass()).error(e);
064
065        }
066    }
067
068    /**
069     * Applies the similarity function.
070     * 
071     * @param caseObject is a String or Enum
072     * @param queryObject is a String or Enum
073     * @return result of apply the similarity function.
074     */
075    public double compute(Object caseObject, Object queryObject) throws NoApplicableSimilarityFunctionException
076    {
077        if ((caseObject == null) || (queryObject == null))
078                return 0;
079        if (! ((caseObject instanceof String)||(caseObject instanceof Enum)))
080                throw new NoApplicableSimilarityFunctionException(this.getClass(), caseObject.getClass());
081        if (! ((queryObject instanceof String)||(queryObject instanceof Enum)))
082                throw new NoApplicableSimilarityFunctionException(this.getClass(), queryObject.getClass());
083
084        String caseS;
085        String queryS;
086        if(caseObject instanceof String)
087        {    
088            caseS = (String) caseObject;
089            queryS = (String) queryObject;
090        }else
091        {
092            caseS = ((Enum<?>)caseObject).toString();
093            queryS = ((Enum<?>)queryObject).toString();
094        }
095        if (matrix == null)
096        {
097            LogFactory.getLog(this.getClass()).error("Similarity table empty");
098            return 0;
099        }
100
101        int pos1 = categories.indexOf(caseS);
102        if (pos1 == -1)
103        {
104            LogFactory.getLog(this.getClass()).error(caseS + " not found in table");
105            return 0;
106        }
107        int pos2 = categories.indexOf(queryS);
108        if (pos2 == -1)
109        {
110            LogFactory.getLog(this.getClass()).error(queryS + " not found in table");
111            return 0;
112        }
113        return matrix[pos1][pos2];
114
115    }
116
117    /** Applicable to String or Enum */
118    public boolean isApplicable(Object o1, Object o2)
119    {
120        if((o1==null)&&(o2==null))
121                return true;
122        else if(o1==null)
123                return (o2 instanceof String)||(o2 instanceof Enum);
124        else if(o2==null)
125                return (o1 instanceof String)||(o1 instanceof Enum);
126        else
127                return ((o1 instanceof String)&&(o2 instanceof String)) ||
128                        ((o1 instanceof Enum)&&(o2 instanceof Enum));
129    }
130}