co.turnus.analysis.data.pipelining.io.XlsSimplePipeliningDataWriter.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.data.pipelining.io.XlsSimplePipeliningDataWriter.java

Source

/* 
 * TURNUS, the co-exploration framework
 * 
 * Copyright (C) 2014 EPFL SCI STI MM
 *
 * This file is part of TURNUS.
 *
 * TURNUS 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.
 *
 * TURNUS 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 TURNUS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7
 * 
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or 
 * an Eclipse library), containing parts covered by the terms of the 
 * Eclipse Public License (EPL), the licensors of this Program grant you 
 * additional permission to convey the resulting work.  Corresponding Source 
 * for a non-source form of such a combination shall include the source code 
 * for the parts of Eclipse libraries used as well as that of the  covered work.
 * 
 */
package co.turnus.analysis.data.pipelining.io;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import co.turnus.TurnusRuntimeException;
import co.turnus.analysis.data.pipelining.SimpleActionPipeliningData;
import co.turnus.analysis.data.pipelining.SimplePipelingData;
import co.turnus.common.StatisticalData;
import co.turnus.dataflow.Action;
import co.turnus.dataflow.Actor;

public class XlsSimplePipeliningDataWriter {

    private HSSFFont titleFont;
    private CellStyle cellStyle;

    public void write(SimplePipelingData report, File file) {
        try {

            HSSFWorkbook workbook = new HSSFWorkbook();

            titleFont = workbook.createFont();
            titleFont.setFontName("Arial");
            titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"));

            HSSFSheet sheet = workbook.createSheet("Pipelinable <Actor, Action>");
            // Action Actor Class Results
            Cell cell = sheet.createRow(0).createCell(0);
            HSSFRichTextString title = new HSSFRichTextString("Action and Actor Pipelining Results");
            title.applyFont(titleFont);
            cell.setCellValue(title);

            Row row = sheet.createRow(1);
            sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0));
            row.createCell(0).setCellValue("Actor");
            sheet.addMergedRegion(new CellRangeAddress(1, 3, 1, 1));
            row.createCell(1).setCellValue("Action");
            sheet.addMergedRegion(new CellRangeAddress(1, 3, 2, 2));
            row.createCell(2).setCellValue("pipelinable");
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 3, 10));
            row.createCell(3).setCellValue("Consecutive Executions");
            sheet.addMergedRegion(new CellRangeAddress(1, 3, 11, 11));
            row.createCell(11).setCellValue("Splittable Actions");

            row = sheet.createRow(2);
            sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 6));
            row.createCell(3).setCellValue("Pipelinable");
            sheet.addMergedRegion(new CellRangeAddress(2, 2, 7, 10));
            row.createCell(7).setCellValue("Unconstrained");
            row = sheet.createRow(3);
            row.createCell(3).setCellValue("min");
            row.createCell(4).setCellValue("mean");
            row.createCell(5).setCellValue("max");
            row.createCell(6).setCellValue("var");
            row.createCell(7).setCellValue("min");
            row.createCell(8).setCellValue("mean");
            row.createCell(9).setCellValue("max");
            row.createCell(10).setCellValue("var");

            int rowId = 4;
            Map<Actor, Map<Action, SimpleActionPipeliningData>> table = report.asTable().rowMap();
            for (Map<Action, SimpleActionPipeliningData> ac : table.values()) {
                for (SimpleActionPipeliningData data : ac.values()) {
                    row = sheet.createRow(rowId);
                    row.createCell(0).setCellValue(data.getActor().getId());
                    row.createCell(1).setCellValue(data.getAction().getId());

                    row.createCell(2).setCellValue(data.isPipelinable());

                    StatisticalData stat = data.getPipelinableRepetitions();
                    if (stat.getSamples() > 0) {
                        row.createCell(3).setCellValue(stat.getMin());
                        row.createCell(4).setCellValue(stat.getMean());
                        row.createCell(5).setCellValue(stat.getMax());
                        row.createCell(6).setCellValue(stat.getVariance());
                    } else {
                        row.createCell(3).setCellValue("-");
                        row.createCell(4).setCellValue("-");
                        row.createCell(5).setCellValue("-");
                        row.createCell(6).setCellValue("-");
                    }

                    stat = data.getUnconstrainedRepetitions();
                    if (stat.getSamples() > 0) {
                        row.createCell(7).setCellValue(stat.getMin());
                        row.createCell(8).setCellValue(stat.getMean());
                        row.createCell(9).setCellValue(stat.getMax());
                        row.createCell(10).setCellValue(stat.getVariance());
                    } else {
                        row.createCell(7).setCellValue("-");
                        row.createCell(8).setCellValue("-");
                        row.createCell(9).setCellValue("-");
                        row.createCell(10).setCellValue("-");
                    }

                    StringBuffer b = new StringBuffer();
                    for (Action action : data.getSplittableActions()) {
                        b.append(action.getId()).append(" ");
                    }
                    row.createCell(11).setCellValue(b.toString());

                    rowId++;
                }
            }

            OutputStream out = new FileOutputStream(file);
            out = new BufferedOutputStream(out);
            workbook.write(out);
            out.close();

        } catch (Exception e) {
            throw new TurnusRuntimeException("Error writing the excel file " + file, e.getCause());
        }
    }
}