Package jmt.gui.common.definitions

Source Code of jmt.gui.common.definitions.StatisticalOutputsLoader$CSVLoader

/**   
  * Copyright (C) 2008, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

  * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */

package jmt.gui.common.definitions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;

import javax.swing.SwingWorker;

import jmt.engine.dataAnalysis.measureOutputs.VerboseCSVMeasureOutputReader;
import jmt.engine.math.DistributionDensity;
import jmt.engine.math.SampleStatistics;


/**
  *<p>Title: StatisticalOutputsParameters</p>
  * <p>Description: Consists of the controlling elements of the StatisticalOutputsEditor,
  *  and the various parameters needed to display statistical outputs and generate required graphs.</p>
  *
  * @author Samarth Shukla, Aneish Goel
  *         Month: June 2013
  */    
public class StatisticalOutputsLoader {
  // Percentage upgrade time. Since it is a costly operation it is performed based on time.
  private static final int UPDATE_PCT_MILLIS = 500;
  private SampleStatistics statistics;
  private DistributionDensity dist;
  private MeasureDefinition measureDefinition;
  private String filename;
  private double progress = 0.0 ;
  private ProgressTimeListener ptlistener = null ;
  private int start = -1;
  private int end = -1;
  private int totalLines = 0;
  private CSVLoader currentLoader;
 
  public StatisticalOutputsLoader(MeasureDefinition measureDefinition, int measureIndex) throws FileNotFoundException{
    this.measureDefinition = measureDefinition;
    this.filename = measureDefinition.getLogFileName(measureIndex);
    this.start = measureDefinition.getDiscardedSamples(measureIndex);
   
    // Starts loading of CSV file in a separate thread.
    reloadData();
  }
 
  public SampleStatistics getStatistics(){
    return statistics ;
  }
 
  public DistributionDensity getDist(){
    return dist;
  }
 
  public void setDistribution(double min, double max, int intervals, int type) {
    if (type == DistributionDensity.TYPE_NONE) {
      dist = null;
    } else {
      dist = new DistributionDensity(min, max, intervals, type);
    }
  }
 
  public double getPercentage(double linesRead, double totalLines){
    double percent = linesRead / totalLines ;
    return percent;
  }
 
  //reads file and generates graph. Code linked with code of CSVOutputReader.
  private void loadCSV() throws IOException, ParseException {
    File measureFile = new File(filename);
    VerboseCSVMeasureOutputReader reader = new VerboseCSVMeasureOutputReader(measureFile, measureDefinition.getLogDecimalSeparator(),measureDefinition.getLogCsvDelimiter());
    statistics = new SampleStatistics();
   
    // Skips header.
    updateProgress(0.0);
    reader.openFile(start > 1 ? start : 1);
    try {
      long lastUpdateTime = System.currentTimeMillis();
      while(reader.hasMoreLines()) {
        reader.readLine();
        statistics.putNewSample(reader.getParsedSample(), reader.getParsedWeight());
        // Fills distribution if present
        if (dist != null) {
          dist.addSample(reader.getParsedSample(), reader.getParsedWeight());
        }
        if (System.currentTimeMillis() - lastUpdateTime > UPDATE_PCT_MILLIS) {
          updateProgress(reader.getParsedPercentage());
          lastUpdateTime = System.currentTimeMillis();
        }
        if (end > 0 && reader.getLineNumber() >= end) {
          break;
        }
      }
      // If totalLines was not set, count them.
      if (totalLines <= 0) {
        while(reader.hasMoreLines()) {
          reader.readLine();
        }
        totalLines = reader.getLineNumber();
      }
    } finally {
      reader.closeFile();
      updateProgress(100.0);
    }
  }
   
  public double getProgress(){
    return progress;
  }
 
  /**
   * Update progress time loading the file
   * @param percentage the progrsss percentage
   */
  private void updateProgress(double percentage){
    progress = percentage;
   
    if (ptlistener != null){
      ptlistener.timeChanged(percentage);
    }
  }
 
  public synchronized void setProgressTimeListener(ProgressTimeListener listener) {
    ptlistener = listener;
  }
 
  public void setStart(int str){
    start = str;
  }
 
 
  public void setEnd(int en){
    end = en;
  }
 
  public int getTotalLines() {
    return totalLines;
  }

  /** listener for ProgressBar in StatisticalOutputsEditor. */
  public interface ProgressTimeListener {
    public void timeChanged(double progressPct);
  }
 
  /**
   * Reloads the data structure
   */
  public void reloadData() {
    if (currentLoader != null) {
      // Avoid concurrent reloads.
      return;
    }
    currentLoader = new CSVLoader();
    currentLoader.execute();
  }

  /**
   * Loads verbose measure from CSV file in the background
   * @author Marco Bertoli
   */
  private class CSVLoader extends SwingWorker<Object, Object> {
    /* (non-Javadoc)
     * @see javax.swing.SwingWorker#doInBackground()
     */
    @Override
    protected Object doInBackground() throws Exception {
      loadCSV();
      currentLoader = null;
      return null;
    }   
  }
}
TOP

Related Classes of jmt.gui.common.definitions.StatisticalOutputsLoader$CSVLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.