package ac.essex.ooechs.ecj.jasmine.nodes.classification;

import ac.essex.ooechs.ecj.jasmine.nodes.Return;

import java.util.Vector;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 26-Oct-2007
 * @version 1.0
 */
public class Loader {

    public static final int TRAINING = 1;
    public static final int TESTING = 2;

    Normaliser n;

    /**
     * Loads the data. Normalisation factors are calculated in training mode and then applied
     * to both training and testing data.
     * @param trainingFile
     * @param n
     * @param mode
     * @return
     * @throws IOException
     */
    public Vector<TrainingData> getTrainingData(File trainingFile, int mode) throws IOException {

        if (trainingFile == null) return null;

        if (mode == TRAINING) {
            n = new Normaliser(FeatureERC.NUM_FEATURES);
        } else {
            if (n == null) {
                throw new RuntimeException("Please load the training data first to initialise the normalisation factors.");
            }
        }

        Vector<TrainingData> trainingData = new Vector<TrainingData>(100);

        BufferedReader in = new BufferedReader(new FileReader(trainingFile));

        // ignore the first line
        in.readLine();

        Vector<Integer> distinctClasses = new Vector<Integer>(10);

        String line = in.readLine();
        while (line != null) {
            TrainingData ts = new TrainingData(line, mode == TRAINING? n : null);
            distinctClasses.add(ts.getClassID());
            trainingData.add(ts);
            line = in.readLine();
        }

        // now go through and normalise
        for (int i = 0; i < trainingData.size(); i++) {
            TrainingData td =  trainingData.elementAt(i);
           //jasmintd.normalise(n);
        }

        Return.classes = distinctClasses;

        return trainingData;
    }

}
