co.turnus.analysis.data.buffers.io.XlsBufferMinimizationDataWriter.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.analysis.data.buffers.io.XlsBufferMinimizationDataWriter.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.buffers.io;

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

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.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import co.turnus.TurnusRuntimeException;
import co.turnus.analysis.data.buffers.BufferMinimizationData;
import co.turnus.analysis.data.buffers.BuffersData;
import co.turnus.dataflow.Fifo;
import co.turnus.dataflow.Network;

public class XlsBufferMinimizationDataWriter {

    private HSSFFont titleFont;

    public void write(BufferMinimizationData report, File file) {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();

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

            int solutionId = 1;
            for (BuffersData data : report.getBuffersData()) {
                writeData(workbook, data, report.getNetwork(), report.getAlgorithm(), solutionId++);
            }

            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 writeData(HSSFWorkbook workbook, BuffersData data, Network netork, String algorithm,
            int solutionId) {
        HSSFSheet sheet = workbook.createSheet("Solution " + solutionId);

        Cell cell = sheet.createRow(0).createCell(0);
        HSSFRichTextString title = new HSSFRichTextString(algorithm + " Results");
        title.applyFont(titleFont);
        cell.setCellValue(title);

        Row row = sheet.createRow(3);
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 3));
        row.createCell(0).setCellValue("Algorithm specific Parameters");
        row = sheet.createRow(4);
        int cellNum = 0;
        for (String attrName : data.getKeyAttributes()) {
            row.createCell(cellNum++).setCellValue(attrName);
            Object val = data.getAttribute(attrName);
            row.createCell(cellNum++).setCellValue(val.toString());
        }

        row = sheet.createRow(1);
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));
        row.createCell(0).setCellValue("Generic Parameters");
        row = sheet.createRow(2);
        row.createCell(0).setCellValue("Total bits");
        row.createCell(1).setCellValue(data.getTotalBitSize());
        row.createCell(2).setCellValue("Total Tokens");
        row.createCell(3).setCellValue(data.getTotalTokenSize());
        row.createCell(4).setCellValue("Solution Time (ms)");
        row.createCell(5).setCellValue(data.getExecutionTime());
        row.createCell(6).setCellValue("number of Actors");
        row.createCell(7).setCellValue(netork.getActors().size());
        row.createCell(8).setCellValue("number of Fifos");
        row.createCell(9).setCellValue(netork.getFifos().size());
        row.createCell(10).setCellValue("bit accurate");
        row.createCell(11).setCellValue(data.isBitAccurate());
        row.createCell(12).setCellValue("deadlock free");
        row.createCell(13).setCellValue(data.isDeadlockFree());

        row = sheet.createRow(5);
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 0, 6));
        row.createCell(0).setCellValue("Buffers Size Configuration");
        row = sheet.createRow(6);
        sheet.addMergedRegion(new CellRangeAddress(6, 7, 0, 0));
        row.createCell(0).setCellValue("Source Actor");
        sheet.addMergedRegion(new CellRangeAddress(6, 7, 1, 1));
        row.createCell(1).setCellValue("Source Port");
        sheet.addMergedRegion(new CellRangeAddress(6, 7, 2, 2));
        row.createCell(2).setCellValue("Traget Actor");
        sheet.addMergedRegion(new CellRangeAddress(6, 7, 3, 4));
        row.createCell(3).setCellValue("Traget Port");
        sheet.addMergedRegion(new CellRangeAddress(6, 7, 4, 4));
        row.createCell(4).setCellValue("Type");
        sheet.addMergedRegion(new CellRangeAddress(6, 6, 5, 6));
        row.createCell(5).setCellValue("Size");
        sheet.addMergedRegion(new CellRangeAddress(6, 6, 8, 9));
        row.createCell(8).setCellValue("Algorithm Starting Point");
        row = sheet.createRow(7);
        row.createCell(5).setCellValue("bit");
        row.createCell(6).setCellValue("tokens");
        row.createCell(8).setCellValue("bit");
        row.createCell(9).setCellValue("tokens");

        int rowi = 8;
        for (Entry<Fifo, Integer> entry : data.getFifosSize().getFifosSizeMap().entrySet()) {
            Fifo fifo = entry.getKey();
            int tokens = entry.getValue();

            row = sheet.createRow(rowi++);
            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());
            row.createCell(4).setCellValue(fifo.getType().toString());
            row.createCell(5).setCellValue(tokens * fifo.getType().getBits());
            row.createCell(6).setCellValue(tokens);

            // do the same for the starting point (if it exists)
            tokens = data.getStartingPoint().containsKey(fifo) ? data.getStartingPoint().get(fifo) : 0;
            row.createCell(8).setCellValue(tokens * fifo.getType().getBits());
            row.createCell(9).setCellValue(tokens);
        }

    }

}