Search in sources :

Example 11 with CSVWriter

use of com.opencsv.CSVWriter in project AnyMemo by helloworld1.

the class CSVExporter method convert.

@Override
public void convert(String src, String dest) throws Exception {
    new File(dest).delete();
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(src);
    final CardDao cardDao = helper.getCardDao();
    final CategoryDao categoryDao = helper.getCategoryDao();
    CSVWriter writer;
    if (separator == null) {
        writer = new CSVWriter(new FileWriter(dest));
    } else {
        writer = new CSVWriter(new FileWriter(dest), separator);
    }
    try {
        final List<Card> cardList = cardDao.queryForAll();
        // Populate all category field in a transaction.
        categoryDao.callBatchTasks(new Callable<Void>() {

            public Void call() throws Exception {
                for (Card c : cardList) {
                    categoryDao.refresh(c.getCategory());
                }
                return null;
            }
        });
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
        if (cardList.size() == 0) {
            throw new IOException("Can't retrieve cards for database: " + src);
        }
        String[] entries = new String[4];
        for (Card card : cardList) {
            entries[0] = card.getQuestion();
            entries[1] = card.getAnswer();
            entries[2] = card.getCategory().getName();
            entries[3] = card.getNote();
            writer.writeNext(entries);
        }
    } finally {
        writer.close();
    }
}
Also used : CategoryDao(org.liberty.android.fantastischmemo.dao.CategoryDao) FileWriter(java.io.FileWriter) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) IOException(java.io.IOException) Card(org.liberty.android.fantastischmemo.entity.Card) AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) File(java.io.File) CardDao(org.liberty.android.fantastischmemo.dao.CardDao)

Example 12 with CSVWriter

use of com.opencsv.CSVWriter in project Java-Tutorial by gpcodervn.

the class OpenCsvWriterExmaple method main.

public static void main(String[] args) throws IOException {
    String csv = "data/data.csv";
    try (Writer writer = new FileWriter(csv);
        CSVWriter csvWriter = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END)) {
        String[] headerRecord = { "id", "code", "name" };
        csvWriter.writeNext(headerRecord);
        csvWriter.writeNext(new String[] { "1", "US", "United States" });
        csvWriter.writeNext(new String[] { "2", "VN", "Viet Nam" });
        csvWriter.writeNext(new String[] { "3", "AU", "Australia" });
    }
}
Also used : FileWriter(java.io.FileWriter) CSVWriter(com.opencsv.CSVWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) CSVWriter(com.opencsv.CSVWriter)

Example 13 with CSVWriter

use of com.opencsv.CSVWriter in project alf.io by alfio-event.

the class ExportUtils method exportCsv.

public static void exportCsv(String fileName, String[] header, Stream<String[]> data, HttpServletResponse response) throws IOException {
    response.setContentType("text/csv;charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    try (ServletOutputStream out = response.getOutputStream();
        CSVWriter writer = new CSVWriter(new OutputStreamWriter(out, UTF_8))) {
        for (int marker : ExportUtils.BOM_MARKERS) {
            out.write(marker);
        }
        writer.writeNext(header);
        data.forEachOrdered(writer::writeNext);
        writer.flush();
        out.flush();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) CSVWriter(com.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 14 with CSVWriter

use of com.opencsv.CSVWriter in project SIMVA-SoS by SESoS.

the class KIISEMain method main.

public static void main(String[] args) throws IOException {
    Constituent cs1 = new Constituent("CS1", 120);
    Constituent cs2 = new Constituent("CS2", 120);
    Constituent cs3 = new Constituent("CS3", 120);
    Action a1 = new Action("Action1", 2, 1);
    a1.setActionType(Action.TYPE.NORMAL);
    Action a2 = new Action("Action2", 2, 2);
    a2.setActionType(Action.TYPE.NORMAL);
    Action a3 = new Action("Action3", 3, 3);
    a3.setActionType(Action.TYPE.NORMAL);
    cs1.addCapability(a1, 1);
    cs1.addCapability(a2, 2);
    cs2.addCapability(a2, 2);
    cs2.addCapability(a3, 3);
    cs3.addCapability(a1, 1);
    cs3.addCapability(a3, 3);
    Constituent[] CSs = { cs1, cs2, cs3 };
    Action[] actions = { a1, a2, a3 };
    SoS sos = new SoS("SoS Manager", CSs, actions);
    Environment env = new Environment(CSs, actions);
    Simulator sim = new Simulator(CSs, sos, env);
    sim.setEndTick(300);
    int[] boundArr = { 120, 125, 130, 135, 140, 145, 150 };
    for (int bound : boundArr) {
        String outputName = "SIM_" + bound + ".csv";
        CSVWriter cw = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputName), "UTF-8"), ',', '"');
        cw.writeNext(new String[] { "prob", "num_of_samples", "execution_time", "min_tick", "max_tick", "result" });
        ArrayList<SMCResult> resList = new ArrayList<SMCResult>();
        System.out.println("----------------------------------------------------");
        System.out.println("SoS-level benefit is greater than " + bound + ".");
        BaseChecker checker = new BaseChecker();
        checker.init(10000, bound, BaseChecker.comparisonType.GREATER_THAN_AND_EQUAL_TO);
        SPRTMethod sprt = new SPRTMethod(0.01, 0.01, 0.005);
        for (int i = 1; i < 100; i++) {
            // theta
            double theta = 0.01 * i;
            long start = System.currentTimeMillis();
            sprt.setExpression(theta);
            while (!sprt.checkStopCondition()) {
                sim.execute();
                SIMResult res = sim.getResult();
                int checkResult = checker.evaluateSample(res);
                sprt.updateResult(checkResult);
            }
            // Result
            boolean h0 = sprt.getResult();
            int numSamples = sprt.getNumSamples();
            // System.out.print("SMC decides that your hypothesis is ");
            // if(h0)
            // {
            // System.out.println("accepted at " + theta + " / number of samples: " + numSamples);
            // }
            // else
            // {
            // System.out.println("not accepted at " + theta + " / number of samples: " + numSamples);
            // }
            // exec time
            long exec_time = System.currentTimeMillis() - start;
            int minTick = checker.getMinTick();
            int maxTick = checker.getMaxTick();
            sprt.reset();
            resList.add(new SMCResult(theta, numSamples, exec_time, minTick, maxTick, h0));
            if (h0)
                System.out.print("T");
            else
                System.out.print("F");
        }
        System.out.println();
        System.out.print("w");
        for (SMCResult r : resList) {
            System.out.print(".");
            cw.writeNext(r.getArr());
        }
        cw.close();
        resList.clear();
        System.out.println();
    }
    System.out.println("Finished");
}
Also used : ArrayList(java.util.ArrayList) SPRTMethod(kr.ac.kaist.se.simulator.method.SPRTMethod) CSVWriter(com.opencsv.CSVWriter) SIMResult(kr.ac.kaist.se.simulator.SIMResult) BaseChecker(kr.ac.kaist.se.mc.BaseChecker) Environment(kr.ac.kaist.se.simulator.Environment) Simulator(kr.ac.kaist.se.simulator.Simulator)

Example 15 with CSVWriter

use of com.opencsv.CSVWriter in project SIMVA-SoS by SESoS.

the class Executor method Perform_Experiment.

public static void Perform_Experiment(NormalDistributor distributor, Simulator sim, String caseName, int bound) throws IOException {
    int endTick = sim.getScenario().getEndTick();
    Existence checker = new Existence();
    checker.init(endTick, bound, Existence.comparisonType.GREATER_THAN_AND_EQUAL_TO);
    System.out.println("==========================================\n" + "[ Simulation Description ]\n" + "Scenario: " + sim.getScenario().getDescription() + "\n" + "Checker: " + checker.getName() + "\n" + "Statement: " + checker.getDescription());
    System.out.println("==========================================\n" + "[ Simulation Log ]");
    long totalstart = System.currentTimeMillis();
    long totaltime = 0;
    int totalsamples = 0;
    for (double alpha_beta : ARR_ALPHA_BETA) {
        Date nowDate = new Date();
        SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String pre = transFormat.format(nowDate);
        for (int trial = 1; trial <= 3; trial++) {
            System.out.println("Trial " + trial + " is Started");
            checker.init(endTick, bound, Existence.comparisonType.GREATER_THAN_AND_EQUAL_TO);
            // 신뢰도 99%
            SPRTMethod sprt = new SPRTMethod(alpha_beta, alpha_beta, 0.01);
            ArrayList<SMCResult> resList = new ArrayList<>();
            for (int t = 80; t < 81; t++) {
                // theta
                double theta = 0.01 * t;
                long start = System.currentTimeMillis();
                sprt.setExpression(theta);
                while (!sprt.checkStopCondition()) {
                    // Initialize Patient map
                    sim.getScenario().init();
                    // 매번 다른 distribution 이 필요함
                    ArrayList<Integer> list = new ArrayList<>();
                    list.clear();
                    list = distributor.getDistributionArray(sim.getScenario().getActionList().size());
                    sim.setActionPlan(list);
                    // sim.setEndTick(endTick);
                    sim.execute();
                    SIMResult res = sim.getResult();
                    int checkResult = checker.evaluateSample(res);
                    sprt.updateResult(checkResult);
                    System.gc();
                    sim.reset();
                    sim.setActionPlan(list);
                }
                // Result
                boolean h0 = sprt.getResult();
                int numSamples = sprt.getNumSamples();
                // exec time
                long exec_time = System.currentTimeMillis() - start;
                int minTick = checker.getMinTick();
                int maxTick = checker.getMaxTick();
                totaltime += exec_time;
                totalsamples += numSamples;
                sprt.reset();
                resList.add(new SMCResult(theta, numSamples, exec_time, minTick, maxTick, h0));
                System.out.print("The statement is");
                if (h0) {
                    System.out.print(" TRUE");
                } else {
                    System.out.print(" FALSE");
                }
                System.out.print(" for theta: " + String.format("%.2f", theta));
                System.out.print(" by examining " + numSamples + " samples");
                System.out.println(" [Time to Decide: " + String.format("%.2f", exec_time / 1000.0) + " secs]");
            }
            String outputName = caseName + "_result/" + pre + caseName + bound + "_" + String.format("%.3f", alpha_beta) + "t" + String.valueOf(trial) + ".csv";
            CSVWriter cw = new CSVWriter(new OutputStreamWriter(new FileOutputStream(outputName), "UTF-8"), ',', '"');
            cw.writeNext(new String[] { "prob", "num_of_samples", "execution_time", "result" });
            for (SMCResult r : resList) {
                System.out.print(".");
                cw.writeNext(r.getArr());
            }
            cw.close();
            resList.clear();
            System.out.println();
            System.out.println("Trial " + trial + " is Finished");
        }
    }
    System.out.println("==========================================\n" + "[ Simulation Result ]\n" + "Result: The statement is --- with a probability --- than --.\n" + "Total Examined Samples: " + totalsamples + " samples\n" + "Total Time to Decide: " + String.format("%.2f", totaltime / 1000.0) + " secs\n" + "Total Elapsed Time: " + String.format("%.2f", (System.currentTimeMillis() - totalstart) / 1000.0) + " secs\n" + "==========================================\n" + "Finished.");
}
Also used : SPRTMethod(kr.ac.kaist.se.simulator.method.SPRTMethod) CSVWriter(com.opencsv.CSVWriter) FileOutputStream(java.io.FileOutputStream) SMCResult(mci.SMCResult) OutputStreamWriter(java.io.OutputStreamWriter) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

CSVWriter (com.opencsv.CSVWriter)21 OutputStreamWriter (java.io.OutputStreamWriter)7 FileOutputStream (java.io.FileOutputStream)5 FileWriter (java.io.FileWriter)5 File (java.io.File)4 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 Entry (org.jbei.ice.storage.model.Entry)4 IOException (java.io.IOException)3 Writer (java.io.Writer)3 HashSet (java.util.HashSet)3 SPRTMethod (kr.ac.kaist.se.simulator.method.SPRTMethod)3 GroupController (org.jbei.ice.lib.group.GroupController)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringWriter (java.io.StringWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ZipEntry (java.util.zip.ZipEntry)2 BaseChecker (kr.ac.kaist.se.mc.BaseChecker)2 Simulator (kr.ac.kaist.se.simulator.Simulator)2 EntryFieldLabel (org.jbei.ice.lib.dto.entry.EntryFieldLabel)2