co.turnus.profiling.io.XlsProfilingDataWriter.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.profiling.io.XlsProfilingDataWriter.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.profiling.io;

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

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.dataflow.Fifo;
import co.turnus.profiling.FifoProfilingData;
import co.turnus.profiling.ProfilingData;

public class XlsProfilingDataWriter {

    private HSSFFont titleFont;
    private CellStyle cellStyle;

    public void write(File file, ProfilingData data) {
        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"));

            // writeSummary(workbook, data.getNetwork(), data);
            writeFifosData(workbook, data);

            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());
        }
    }

    private void writeFifosData(HSSFWorkbook workbook, ProfilingData data) {
        HSSFSheet sheet = workbook.createSheet("Fifos Data");
        // Action Actor Class Results
        Cell cell = sheet.createRow(0).createCell(0);
        HSSFRichTextString title = new HSSFRichTextString("Communication data summary");
        title.applyFont(titleFont);
        cell.setCellValue(title);

        int rowIdx = 2;
        Row row = sheet.createRow(rowIdx++);
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 1));
        row.createCell(0).setCellValue("Source");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 2, 3));
        row.createCell(2).setCellValue("Target");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 5));
        row.createCell(4).setCellValue("Type");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 12));
        row.createCell(6).setCellValue("Writing (tokens)");
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 13, 20));
        row.createCell(13).setCellValue("Reading (tokens)");

        // header
        row = sheet.createRow(rowIdx++);
        row.createCell(0).setCellValue("Actor");
        row.createCell(1).setCellValue("Out Port");
        row.createCell(2).setCellValue("Actor");
        row.createCell(3).setCellValue("In Port");

        // type
        row.createCell(4).setCellValue("Name");
        row.createCell(5).setCellValue("Bit:Token");

        // writing
        row.createCell(6).setCellValue("hits");
        row.createCell(7).setCellValue("misses");
        row.createCell(8).setCellValue("total");
        row.createCell(9).setCellValue("average");
        row.createCell(10).setCellValue("min");
        row.createCell(11).setCellValue("max");
        row.createCell(12).setCellValue("variance");

        // reading
        row.createCell(13).setCellValue("peeks");
        row.createCell(14).setCellValue("hits");
        row.createCell(15).setCellValue("misses");
        row.createCell(16).setCellValue("total");
        row.createCell(17).setCellValue("average");
        row.createCell(18).setCellValue("min");
        row.createCell(19).setCellValue("max");
        row.createCell(20).setCellValue("variance");

        for (FifoProfilingData fData : data.getFifosData()) {
            row = sheet.createRow(rowIdx++);
            Fifo fifo = fData.getFifo();
            // header
            row.createCell(0).setCellValue(fifo.getSourceActor().getId());
            row.createCell(1).setCellValue(fifo.getSourcePort().getName());
            row.createCell(2).setCellValue(fifo.getTargetActor().getId());
            row.createCell(3).setCellValue(fifo.getTargetPort().getName());
            // type
            row.createCell(4).setCellValue(fifo.getType().toString());
            row.createCell(5).setCellValue(fifo.getType().getBits());
            // writes
            row.createCell(6).setCellValue(fData.getWriteHit());
            row.createCell(7).setCellValue(fData.getWriteMiss());
            row.createCell(8).setCellValue(fData.getWriteTokens().getSum());
            row.createCell(9).setCellValue(fData.getWriteTokens().getMean());
            row.createCell(10).setCellValue(fData.getWriteTokens().getMin());
            row.createCell(11).setCellValue(fData.getWriteTokens().getMax());
            row.createCell(12).setCellValue(fData.getWriteTokens().getVariance());
            // reads
            row.createCell(13).setCellValue(fData.getPeeks());
            row.createCell(14).setCellValue(fData.getReadHit());
            row.createCell(15).setCellValue(fData.getWriteMiss());
            row.createCell(16).setCellValue(fData.getReadTokens().getSum());
            row.createCell(17).setCellValue(fData.getWriteTokens().getMean());
            row.createCell(18).setCellValue(fData.getWriteTokens().getMin());
            row.createCell(19).setCellValue(fData.getWriteTokens().getMax());
            row.createCell(20).setCellValue(fData.getWriteTokens().getVariance());

        }

    }

}