From 1eb147d69b6a3f242e9b0e1b4a32196c317b20a8 Mon Sep 17 00:00:00 2001 From: Stefan Derkits Date: Wed, 15 May 2013 17:51:06 +0200 Subject: [PATCH] implemented parsing of CSV in SchedSimulator --- build.xml | 6 +-- src/at/ac/tuwien/lsdc/SchedSimulator.java | 17 ++++++- .../tuwien/lsdc/sched/AbstractScheduler.java | 8 ++-- src/at/ac/tuwien/lsdc/types/Application.java | 12 ++++- src/at/ac/tuwien/lsdc/util/CSVParser.java | 44 +++++++++++++++++-- 5 files changed, 73 insertions(+), 14 deletions(-) diff --git a/build.xml b/build.xml index 4bfe460..04771da 100644 --- a/build.xml +++ b/build.xml @@ -39,7 +39,7 @@ - + @@ -49,7 +49,7 @@ - + @@ -59,7 +59,7 @@ - + diff --git a/src/at/ac/tuwien/lsdc/SchedSimulator.java b/src/at/ac/tuwien/lsdc/SchedSimulator.java index 93d2093..aa59cef 100644 --- a/src/at/ac/tuwien/lsdc/SchedSimulator.java +++ b/src/at/ac/tuwien/lsdc/SchedSimulator.java @@ -11,6 +11,7 @@ import at.ac.tuwien.lsdc.sched.AbstractScheduler; import at.ac.tuwien.lsdc.sched.SchedulerA; import at.ac.tuwien.lsdc.sched.SchedulerB; import at.ac.tuwien.lsdc.sched.SchedulerC; +import at.ac.tuwien.lsdc.types.Application; import at.ac.tuwien.lsdc.types.ScenarioData; import at.ac.tuwien.lsdc.types.SchedulerEvent; import at.ac.tuwien.lsdc.types.SchedulerType; @@ -44,7 +45,7 @@ public class SchedSimulator { System.exit(1); } } - LinkedList apps = CSVParser.parseFile(inputFile); + LinkedList apps = CSVParser.parseFile(inputFile); AbstractScheduler scheduler; switch (schedulerType) { case A: @@ -72,7 +73,15 @@ public class SchedSimulator { if (!inputFile.canRead()) throw new IOException("Can not read input file"); logFile = new File(logFileStr); - if (!logFile.canWrite()) + boolean logFileOK = false; + if ( logFile.canWrite() ) { + logFileOK = true; + } + else if (!logFile.exists()) { + createLogFile(); + logFileOK = true; + } + if (!logFileOK) throw new IOException("Can not write to log file"); boolean fileWriteOK = false; scenarioLogFile = new File(scenarioLogFileStr); @@ -93,4 +102,8 @@ public class SchedSimulator { System.exit(1); } } + + private static void createLogFile() { + + } } diff --git a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java index 3e0cec5..7200946 100644 --- a/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java +++ b/src/at/ac/tuwien/lsdc/sched/AbstractScheduler.java @@ -1,7 +1,6 @@ package at.ac.tuwien.lsdc.sched; import java.util.LinkedList; -import java.util.List; import java.util.Map; import at.ac.tuwien.lsdc.types.Application; @@ -33,12 +32,13 @@ public abstract class AbstractScheduler { // Initialize Scheduler with Data from CSV // CSV will be parsed and sent as List to Scheduler - public ScenarioData initAndStart(LinkedList apps) { - for (SchedulerEvent a : apps) { + public ScenarioData initAndStart(LinkedList apps) { + for (Application a : apps) { + System.out.println(a); // read start timestamp // save event in map } - startScheduling(); + //startScheduling(); return doEndLogging(); } diff --git a/src/at/ac/tuwien/lsdc/types/Application.java b/src/at/ac/tuwien/lsdc/types/Application.java index c1bb6c1..8f465b4 100644 --- a/src/at/ac/tuwien/lsdc/types/Application.java +++ b/src/at/ac/tuwien/lsdc/types/Application.java @@ -2,19 +2,25 @@ package at.ac.tuwien.lsdc.types; public class Application { + private int id; private long timestamp; private int size; private int ram; private int cpu; private int duration; - public Application(long timestamp, int size, int ram, int cpu, int duration) { + public Application(int id, long timestamp, int size, int ram, int cpu, int duration) { + this.id = id; this.timestamp = timestamp; this.size = size; this.ram = ram; this.cpu = cpu; this.duration = duration; } + + public int getID() { + return id; + } public long getTimestamp() { return timestamp; @@ -35,5 +41,9 @@ public class Application { public int getDuration() { return duration; } + + public String toString() { + return new String("App ID: " + id + " Timestamp: " + timestamp + " size: " + size + " ram: " + ram + " cpu: " + cpu + " duration: " + duration); + } } diff --git a/src/at/ac/tuwien/lsdc/util/CSVParser.java b/src/at/ac/tuwien/lsdc/util/CSVParser.java index 93dad47..39d26a5 100644 --- a/src/at/ac/tuwien/lsdc/util/CSVParser.java +++ b/src/at/ac/tuwien/lsdc/util/CSVParser.java @@ -6,16 +6,52 @@ import java.io.IOException; import java.util.LinkedList; import java.util.List; -import at.ac.tuwien.lsdc.types.SchedulerEvent; +import at.ac.tuwien.lsdc.types.Application; import au.com.bytecode.opencsv.CSVReader; +import au.com.bytecode.opencsv.CSVWriter; public class CSVParser { - public static LinkedList parseFile(File csvFile) throws IOException { - CSVReader reader = new CSVReader(new FileReader(csvFile)); + private static int count = 0; + + public static LinkedList parseFile(File csvFile) + throws IOException { + CSVReader reader = new CSVReader(new FileReader(csvFile), ';', + CSVWriter.NO_QUOTE_CHARACTER); List file = reader.readAll(); + LinkedList apps = new LinkedList(); + // start at 1 to ignore header + for (int i = 1; i < file.size(); i++) { + String[] line = file.get(i); + if (line.length != 5) { + reader.close(); + throw new IOException("Too many elements in line " + i + 1); + } else { + try { + Application a = parseLine(line); + apps.add(a); + } catch (NumberFormatException e) { + reader.close(); + throw new IOException("Couldn't parse line"); + } + } + } reader.close(); - return new LinkedList(); + return apps; + } + + private static Application parseLine(String[] appStr) + throws NumberFormatException { + int id = count; + count++; + long timestamp = Long.parseLong(appStr[0]); + int size = Integer.parseInt(appStr[1]); + int ram = Integer.parseInt(appStr[2]); + int cpu = Integer.parseInt(appStr[3]); + int duration = Integer.parseInt(appStr[4]); + Application app = new Application(id, timestamp, size, ram, cpu, + duration); + return app; } } -- 2.43.0