package jp.ac.takushoku_u.cs.*;
import java.io.*;
import java.util.ArrayList;

/**
 *TaskFileReaderクラスはCSV形式ファイル,LFGファイルを読み込み,帰属度関数を算出
 *するためのクラスです。
 *@version 1.0.0
 *@author 河原 慎治(Kawahara Shinji)
 */
/*
 *最終更新日 2005年06月13日
 */
public final class TaskFileReader{

    /**
     *読み込んだ要素に設定する定数
     */
    private static final double READER_DOUBLE_TRUE = 1.0;
    
    
    /**
     *CSV形式ファイルに格納されている列の要素をArithmeticMatrixで返します
     */
    public static final ArithmeticMatrix getCsvArithmeticMatrixReader(String filename)throws java.io.FileNotFoundException,java.io.IOException{
      
	String ret[] = getLines(filename);
	ArithmeticMatrix mat;
	/*行数*/
	int row = ret.length;

	/*列数*/
	String col_data[];
	col_data = ret[0].split(",");
	int col = col_data.length;

	double [][]sequ = new double[row][col];
	String data[];
	try{
	    for(int i=0; i<ret.length;i++){
		data = ret[i].split(",");
		for(int j=0; j<data.length; j++){
		    sequ[i][j] = Double.valueOf(data[j]);
		}
	    }
	}catch(ArrayIndexOutOfBoundsException e){}

	mat = new ArithmeticMatrix(sequ);
	return mat;
	
    }
   
 /**
    /*CSV形式ファイルに格納されている列の要素をdouble型の二次元配列で返します
 */
    public static final double[][] getCsvTwoDoubleArray(String filename){
	String ret[] = getLines(filename);

	/*行数設定*/
	int row = ret.length;

	/*列数設定*/
	String col_data[];
	col_data = ret[0].split(",");
	int col = col_data.length;

	double [][]sequ = new double[row][col];

	String data[];
       	try{
	    for(int i=0; i<ret.length; i++){
		data = ret[i].split(",");
		for(int j=0; j<data.length; j++){
		 sequ[i][j] = Double.valueOf(data[j]);
	       
		}
	    }
	}catch(ArrayIndexOutOfBoundsException e){}
	
	return sequ;       
    }
    
    /**
     *CSV形式ファイルの系列をSequenceに格納します
     */
    public static final Sequence getSequence(String filename,int Student){
	String ret[] = getLines(filename);
	ArithmeticMatrix mat;
	/*行数*/
	int row = ret.length;
	
	/*列数*/
	String col_data[];
	col_data = ret[0].split(",");
	int col = col_data.length;
	
	double [][]sequ = new double[row][col];
	String data[];
	try{
	    for(int i=0; i<ret.length;i ++){
		data = ret[i].split(",");
		for(int j=0; j<data.length; j++){
		    sequ[i][j] = Double.valueOf(data[j]);
		}
	    }
	}catch(ArrayIndexOutOfBoundsException e){}
	
	int[] sdata = new int[col];
	for(int i=0; i<col; i++){
	    sdata[i] = (int)(sequ[Student-1][i]-1);
	}
	Sequence sq = new Sequence(sdata);
	return sq;
    }
    /**    
     *LFGファイルに格納されている列の要素をArithmeticMatrixで返します
     */
    public static final ArithmeticMatrix getLfgArithmeticMatrixReader(String filename)throws java.io.FileNotFoundException,java.io.IOException{
	FileReader fr;
	ArithmeticMatrix mat;
	try{
	    fr = new FileReader(filename);
	    BufferedReader in = new BufferedReader(fr);
	
	    int row;
	    int col;
	    boolean flag;
	    String s;
	    s = in.readLine();
	    mat = new ArithmeticMatrix(Integer.parseInt(s),Integer.parseInt(s),1);

	    while((s = in.readLine())!=null){
	       
		row = 0;
		col = 0;
		flag = true;
		for(int i=0; i<s.length(); i++){
		    if(s.charAt(i) != ' ' && flag){
			row = row*10 + Character.getNumericValue(s.charAt(i));
		    }
		    else if(s.charAt(i) != ' ' && (flag == false)){
			col = col*10 + Character.getNumericValue(s.charAt(i));
		    }
		    else{
			flag = false;
		    }
		}
		mat.setCell(row-1, col-1, READER_DOUBLE_TRUE);
	    }
	}
	catch(NotElementException e){
	    throw new NotElementException(e+"");
	}
	return mat;
    }

    
    
     
  
    /**
     *ファイルを読み込み一列ずつ格納します
     */
    
    private static String[] getLines(String fileName){

	ArrayList list = new ArrayList();
	try{
	    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
	    String line = null;

	    while((line = br.readLine()) != null){
		list.add(line);
	    }
	    br.close();
	}catch(IOException err){}
    
	return(String[]) list.toArray(new String[list.size()]);
    }
}
