scheduler-simulator

git clone git://git.codymlewis.com/scheduler-simulator.git
Log | Files | Refs | README

commit 2e311318fa23b8c137e9f29b9eb8fd745ab48437
parent f9218f91f9f2e5857a1cdb2d75f641644f9dec90
Author: Cody Lewis <cody@codymlewis.com>
Date:   Tue,  3 Sep 2019 13:35:35 +1000

Made the code neater

Diffstat:
MDispatcher.java | 94+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
MNRR.java | 3++-
MProcess.java | 1-
MScheduler.java | 47++++++++++++++++++++++-------------------------
Atest | 2++
5 files changed, 84 insertions(+), 63 deletions(-)

diff --git a/Dispatcher.java b/Dispatcher.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Scanner; import java.util.PriorityQueue; import java.io.File; +import java.io.FileNotFoundException; /** * <h1>Dispatcher - Comp2240A1</h1> @@ -40,10 +41,11 @@ public class Dispatcher { * @return Map between time of process and the list of processes */ private Map<Integer, LinkedList<Process>> parseDataFile(String filename) { - Map<Integer, LinkedList<Process>> data = new HashMap<>(); + Map<Integer, LinkedList<Process>> data = null; try (Scanner fstream = new Scanner(new File(filename))) { String curLine = fstream.nextLine(); + // Check that the file is formated correctly if (!curLine.contains("BEGIN")) { throw new InvalidDataException(); } @@ -57,37 +59,62 @@ public class Dispatcher { if (!fstream.nextLine().contains("END")) { throw new InvalidDataException(); } - String pid = ""; - int arrivalTime = 0; - int serviceTime = 0; - while (!(curLine = fstream.nextLine()).contains("EOF")) { - if (curLine.length() > 1) { - sstream = new Scanner(curLine); - switch (sstream.next()) { - case "ID:": - pid = sstream.next(); - break; - case "Arrive:": - arrivalTime = sstream.nextInt(); - break; - case "ExecSize:": - serviceTime = sstream.nextInt(); - break; - case "END": - if (data.get(arrivalTime) == null) { - data.put( - arrivalTime, - new LinkedList<Process>() - ); - } - data.get(arrivalTime).add( - new Process(pid, arrivalTime, serviceTime) + data = fstreamToMap(fstream); + } catch (FileNotFoundException fnfe) { + System.err.format( + "Error: File %s was not found. Continuing run with no processes.", + filename + ); + } catch (InvalidDataException ide) { + System.err.format( + "Error: %s Continuing run with no processes.", + ide.getMessage() + ); + } + + return data == null ? + new HashMap<Integer, LinkedList<Process>>() : + data; + } + + /** + * Put the file contents from the stream into Map + * + * @param fstream A scanner stream iterated to the process content + * @return Map from the time of arrival to the list of processes + */ + private Map<Integer, LinkedList<Process>> fstreamToMap(Scanner fstream) { + String pid = "", curLine = ""; + Scanner sstream; + int arrivalTime = 0; + int serviceTime = 0; + Map<Integer, LinkedList<Process>> data = new HashMap<>(); + + while (!(curLine = fstream.nextLine()).contains("EOF")) { + if (curLine.length() > 1) { + sstream = new Scanner(curLine); + switch (sstream.next()) { + case "ID:": + pid = sstream.next(); + break; + case "Arrive:": + arrivalTime = sstream.nextInt(); + break; + case "ExecSize:": + serviceTime = sstream.nextInt(); + break; + case "END": + if (data.get(arrivalTime) == null) { + data.put( + arrivalTime, + new LinkedList<Process>() ); - } + } + data.get(arrivalTime).add( + new Process(pid, arrivalTime, serviceTime) + ); } } - } catch (Exception e) { - e.printStackTrace(); } return data; @@ -121,17 +148,12 @@ public class Dispatcher { int time = 0; int timeProcessingTo = 0; PriorityQueue<Integer> eventTimes = new PriorityQueue<>(); - - for (int eventTime: processes.keySet()) { - eventTimes.add(eventTime); - } + processes.keySet().stream().forEach(e -> eventTimes.add(e)); while (!eventTimes.isEmpty()) { time = eventTimes.poll(); if (processes.get(time) != null) { - for (Process process : processes.get(time)) { - scheduler.add(process); - } + processes.get(time).stream().forEach(p -> scheduler.add(p)); processes.remove(time); } if (time >= timeProcessingTo) { diff --git a/NRR.java b/NRR.java @@ -168,7 +168,8 @@ public class NRR extends Scheduler { if (newProcess && !prevPid.equals(head.getProcess().getPid())) { return switchProcess(time); } else { - int processingTime = head.getProcess().process(time, head.getSliceSize()); + int processingTime = head.getProcess() + .process(time, head.getSliceSize()); prevPid = head.getProcess().getPid(); if (head.getProcess().finished()) { processed.add(queue.remove(0).getProcess()); diff --git a/Process.java b/Process.java @@ -159,4 +159,3 @@ public class Process { return serviceTime == 0; } } - diff --git a/Scheduler.java b/Scheduler.java @@ -1,5 +1,6 @@ import java.util.Collections; import java.util.ArrayList; +import java.util.stream.Collectors; /** * <h1>Scheduler - Comp2240A1</h1> @@ -74,20 +75,16 @@ public abstract class Scheduler { * @return Results of the simulation */ public String results() { - String stats = ""; - Collections.sort(processed, new SortbyPid()); - stats += startTimes; - stats += "\nProcess Turnaround Time Waiting Time\n"; - for (Process process : processed) { - stats += String.format( - "%s\t%d\t\t%d\n", - process.getPid(), - process.getTurnaroundTime(), - process.getWaitingTime() - ); - } - - return stats; + return startTimes + "\n" + + "Process Turnaround Time Waiting Time\n" + + processed.stream() + .sorted(new SortbyPid()) + .map(p -> String.format( + "%s\t%d\t\t%d", + p.getPid(), + p.getTurnaroundTime(), + p.getWaitingTime())) + .collect(Collectors.joining("\n")) + "\n"; } /** @@ -96,16 +93,16 @@ public abstract class Scheduler { * @return The average turnaroundTime and average waitingTime */ public String summary() { - double avgTurnaround = 0; - double avgWaiting = 0; - - for (Process process : processed) { - avgTurnaround += process.getTurnaroundTime(); - avgWaiting += process.getWaitingTime(); - } - avgTurnaround /= processed.size(); - avgWaiting /= processed.size(); - - return String.format("%.2f\t\t\t%.2f", avgTurnaround, avgWaiting); + return String.format( + "%.2f\t\t\t%.2f", + processed.stream() + .mapToDouble(p -> p.getTurnaroundTime()) + .average() + .orElse(0), + processed.stream() + .mapToDouble(p -> p.getWaitingTime()) + .average() + .orElse(0) + ); } } diff --git a/test b/test @@ -0,0 +1,2 @@ +renofguweh +oehdfw