Java tutorial
/** * Copyright 2011 Frederik Hahne * * AbstractEvaluator.java is part of Plant Evaluation. * * Plant Evaluation 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 3 of the License, or * (at your option) any later version. * * Plant Evaluation 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 Plant Evaluation. If not, see <http://www.gnu.org/licenses/>. */ package de.atomfrede.tools.evalutation.evaluator; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.swing.JProgressBar; import org.apache.commons.lang.ArrayUtils; import au.com.bytecode.opencsv.CSVReader; import au.com.bytecode.opencsv.CSVWriter; import de.atomfrede.tools.evalutation.options.Options; /** * Abstract super class that encapsulates common attributes and method for all * evaluators. */ public abstract class AbstractEvaluator { /** * Progressbar to display the progress of this evaluator */ protected JProgressBar progressBar; /** * The name of this evaluator, that is displayed in the user interface */ protected String name = "AbstractEvaluator"; public double referenceChamberValue = 1.0; /** * The root folder for output files generated by this evaluator */ public File outputRootFolder = Options.getOutputFolder(); /** * The root folder that contains all raw input files generated from the * picarro device */ public File inputRootFolder = Options.getInputFolder(); /** * The concrete outputfolder for this step done by the implementing * evaluator */ public File outputFolder; public List<String[]> allReferenceLines; /** * * @param outputFolderName */ public AbstractEvaluator(String outputFolderName) { // creating progressbar progressBar = new JProgressBar(); // paint the value in percent for this progress progressBar.setStringPainted(true); try { if (!outputRootFolder.exists()) { outputFolder.mkdir(); } if (!inputRootFolder.exists()) throw new RuntimeException("No input folder avaliable!"); outputFolder = new File(outputRootFolder, outputFolderName); if (!outputFolder.exists()) outputFolder.mkdir(); } catch (Exception ioe) { throw new RuntimeException("Outputfolder could not be created!"); } } // the date format: "2011-08-01 00:30:23,54" public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS"); // 22.07.11 10:59:38 public SimpleDateFormat temperatureAndPlantDateFormat = new SimpleDateFormat("dd.MM.yy HH:mm:ss"); /** * This method executes the concrete evaluation (read file, write file, do * computations) * * @return state of the evaluation * @throws Exception */ public abstract boolean evaluate() throws Exception; public String getName() { return name; } public void setName(String name) { this.name = name; } /** * Parsing the value at column type of the given line to a primitive double * * @param line * @param type * column number to parse * @return */ public double parseDoubleValue(String[] line, int type) { return Double.parseDouble(line[type].replace(",", ".")); } public double[] list2DoubleArray(List<Double> values) { return ArrayUtils.toPrimitive(values.toArray(new Double[values.size()])); } public int[] list2IntArray(List<Integer> values) { return ArrayUtils.toPrimitive(values.toArray(new Integer[values.size()])); } public CSVWriter getCsvWriter(File outputFile) throws IOException { return new CSVWriter(new FileWriter(outputFile)); } public List<String[]> readAllLinesInFile(File input) throws IOException { return readAllLinesInFile(input, ','); } public List<String[]> readAllLinesInFile(File input, char sperator) throws IOException { CSVReader reader = null; try { reader = new CSVReader(new FileReader(input), sperator); return reader.readAll(); } finally { if (reader != null) reader.close(); } } /** * Collects all lines in the dataset where the column SOLENOID_VALVE is * equal to the {@link #referenceChamberValue} * * @param lines * @param solenoidValve * @return */ public List<String[]> findAllReferenceLines(List<String[]> lines, int solenoidValve) { List<String[]> referenceLines = new ArrayList<String[]>(); for (int i = 1; i < lines.size(); i++) { if (parseDoubleValue(lines.get(i), solenoidValve) == referenceChamberValue) referenceLines.add(lines.get(i)); } return referenceLines; } /** * Returns a list of indices where the SOLENOID_VALVE is equal to the * {@link #referenceChamberValue} * * @param lines * @param solenoidValve * @return */ public List<Integer> findAllReferenceChambers(List<String[]> lines, int solenoidValve) { List<Integer> referenceChamberLines = new ArrayList<Integer>(); for (int i = 1; i < lines.size(); i++) { if (parseDoubleValue(lines.get(i), solenoidValve) == referenceChamberValue) referenceChamberLines.add(i); } return referenceChamberLines; } /** * Finds that reference line (where SOLENOID_VALVE == * {@link #referenceChamberValue}) that is nearest in time to the given * line. * * @param line * Dataset for what to find the nearest reference line * @param allLines * @param referenceLines * @param timeColumn * @return * @throws ParseException */ public String[] getReferenceLineToUse(String[] line, List<String[]> allLines, List<String[]> referenceLines, int timeColumn) throws ParseException { Date date = dateFormat.parse(line[timeColumn]); long shortestedDistance = Long.MAX_VALUE; String[] refIndex2Use = null; for (String[] refLineIndex : referenceLines) { Date refDate = dateFormat.parse(refLineIndex[timeColumn]); long difference = Math.abs(date.getTime() - refDate.getTime()); if (shortestedDistance > difference) { shortestedDistance = difference; refIndex2Use = refLineIndex; } } return refIndex2Use; } protected void writeValue(CSVWriter writer, String[] currentLine, double value) { // first reuse the old values String[] newLine = new String[currentLine.length + 1]; int i = 0; for (i = 0; i < currentLine.length; i++) { newLine[i] = currentLine[i]; } // append the new value newLine[i] = value + ""; writer.writeNext(newLine); } public JProgressBar getProgressBar() { return progressBar; } public void setProgressBar(JProgressBar progressBar) { this.progressBar = progressBar; } }