
/**
 * Title:        MappedArray<p>
 * Description:  A MappedArray is an OrthogonalArray that has been mapped onto
 * a specific testing problem. The entries in the array are problem domain
 * values.<p>
 * Copyright:    Copyright (c) 2000<p>
 * Company:      mctest<p>
 * @author John D. McGregor
 * @version 1.0
 */

/*
The OATS Orthognal Array Test Design Tool
Copyright (C) 2000  McTest

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.

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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


package oats;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class MappedArray extends OrthogonalArray {

  /**
   * This constructor initializes the mapped array to be based on the
   * orthogonal array passed as a parameter.
   * @param oa The orthogonal array that is to be mapped to the problem.
   */
  public MappedArray(OrthogonalArray oa) {
    super(oa.getNumberOfFactors(),oa.getNumberOfRows());
    levelValues = new String [maxRows][numFactors];
    factorName = new String [numFactors];
  }

  /**
   * This constructor loads the basic orthogonal array from the file whose name
   * is passed as a parameter.
   * @param fileName The file that contains the orthogonal array to be used as a
   * basis for the mapped array.
   */
  public MappedArray(String fileName){
     super.read(fileName);
     levelValues = new String [maxRows][numFactors];
     factorName = new String [numFactors];
  }

  /**
   * This constructor loads the flat file of an existing mapper array
   */
    public MappedArray(String fileName,int dummy){
     read(fileName);
  }

  /**
   * This constructor initializes the mapped array to be based on the
   * orthogonal array passed as a parameter.
   * @param oa The orthogonal array that is to be mapped to the problem.
   */
  public MappedArray(int numFactors,int numRows) {
    super(numFactors,numRows);
    levelValues = new String [maxRows][numFactors];
    factorName = new String [numFactors];
  }

  /**
   * Modifier method that sets a specific problem value to correspond to a
   * specific problem domain value
   * @param i The level value
   * @param j The factor
   * @param s The problem domain value
   */
  public void putLevelValueAt(int i, int j, String s){
    levelValues[i][j] = s;
  }

  /**
   * Modifier method that sets a specific problem value to correspond to a
   * specific problem domain value
   * @param i Which factor
   * @param s The name for the factor
   */
  public void putFactorNameAt(int i, String s){
    factorName[i] = s;
  }

  /**
   * Modifier method that sets a specific problem value to correspond to a
   * specific problem domain value
   * @param i Which factor
   * @param s The name for the factor
   */
  public String getFactorNameAt(int i){
    return factorName[i];
  }

  /**
   * Accessor method that returns the problem domain value for a specific level
   * value.
   * @param i The level value
   * @param j The factor
   */
  public String getLevelValueAt(int i, int j){
    return levelValues [i][j];
  }

   /**
   * This method writes the array to a file
   * @param newName The filename to be used to save the array
   */
  public void save(String newName){
    FileWriter fileWriter;
    try{
      fileWriter = new FileWriter(newName);
      fileName = newName;
      fileWriter.write(new Integer(numFactors).toString()+"\n");
      fileWriter.write(new Integer(maxRows).toString()+"\n");
      for(int i=0;i<numFactors;i++){
        fileWriter.write(numLevels.elementAt(i).toString()+"\n");
      }

      for(int i=0;i<maxRows;i++){
        for(int j=0;j<numFactors;j++){
             fileWriter.write(array[i][j]+"\n");
        }
      }
      for(int i=0;i<numFactors;i++){
        fileWriter.write(factorName[i]+"\n");
      }
      for(int i=0;i<maxRows;i++){
        for(int j=0;j<numFactors;j++){
             fileWriter.write(levelValues[i][j]+"\n");
        }
      }
      fileWriter.close();
    }catch(IOException e){e.printStackTrace();}
  }

   /**
   * This method writes the array to a text file suitable for importing
   * into a word processor
   * @param newName The filename to be used to save the array
   */
  public void saveText(String newName){
    FileWriter fileWriter;
    try{
      fileWriter = new FileWriter(newName);
      fileName = newName;
      for(int i=0;i<numFactors;i++){
        System.out.println(factorName[i]);
        if(!factorName[i].equals("nis")){
          fileWriter.write(factorName[i]+" ");
        }
      }
      fileWriter.write("\n");
      for(int i=0;i<maxRows;i++){
        for(int j=0;j<numFactors;j++){
            if(array[i][j] != " "){
              fileWriter.write(array[i][j]+" ");
            }
        }
        fileWriter.write("\n");
      }
      fileWriter.close();
    }catch(IOException e){e.printStackTrace();}
  }


  /**
   * This method reads the array from a file
   */
  public void read(String s){
    FileReader fileReader;
    BufferedReader bufReader;
    try{

      fileReader = new FileReader(s);
      bufReader = new BufferedReader(fileReader);

      numFactors = Integer.parseInt(bufReader.readLine());
      maxRows = Integer.parseInt(bufReader.readLine());
      //System.out.println("Values "+numFactors+" "+maxRows);
      levelValues = new String[maxRows][numFactors];
      array = new String[maxRows][numFactors];
      factorName = new String[numFactors];
      for(int i=0;i<numFactors;i++){
        numLevels.add(i, bufReader.readLine());
      }
      // System.out.println("S is "+s);
      for(int i=0;i<maxRows;i++){
      //  int tmp = ((Integer)numLevels.elementAt(i)).intValue();
        for(int j=0;j<numFactors;j++){
             array[i][j] = bufReader.readLine();
             //System.out.println(array[i][j]);
        }
      }
       for(int i=0;i<numFactors;i++){
        factorName[i] = bufReader.readLine();
      }
      for(int i=0;i<maxRows;i++){
      //  int tmp = ((Integer)numLevels.elementAt(i)).intValue();
        for(int j=0;j<numFactors;j++){
             levelValues[i][j] = bufReader.readLine();
             //System.out.println(array[i][j]);
        }
      }
      bufReader.close();
    }catch(IOException e){e.printStackTrace();}
  }


  private String [][] levelValues;
  private String [] factorName;
}