scheduler-simulator

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

Scheduler.java (2786B)


      1 import java.util.Collections;
      2 import java.util.ArrayList;
      3 import java.util.stream.Collectors;
      4 
      5 /**
      6  * <h1>Scheduler - Comp2240A1</h1>
      7  *
      8  * Interface for the scheduler classes
      9  *
     10  * @author Cody Lewis (c3283349)
     11  * @version 1
     12  * @since 2019-08-17
     13  */
     14 
     15 public abstract class Scheduler {
     16     protected ArrayList<Process> processed;
     17     protected Integer switchProcessTime;
     18     protected boolean newProcess;
     19     protected String startTimes;
     20 
     21     /**
     22      * Default constructor
     23      */
     24     protected Scheduler() {
     25         processed = new ArrayList<>();
     26         switchProcessTime = 0;
     27         newProcess = true;
     28         startTimes = "";
     29     }
     30 
     31     /**
     32      * Input constructor
     33      *
     34      * @param switchProcessTime Time to take to switch processes
     35      */
     36     protected Scheduler(int switchProcessTime) {
     37         this();
     38         this.switchProcessTime = switchProcessTime;
     39     }
     40 
     41     /**
     42      * Add a process to the scheduler
     43      *
     44      * @param process Process to add to the schedule
     45      */
     46     public abstract void add(Process process);
     47 
     48     /**
     49      * Check whether the schedule is empty
     50      *
     51      * @return True if schedule is empty else false
     52      */
     53     public abstract boolean empty();
     54 
     55     /**
     56      * Process the Process at the head of the queue
     57      *
     58      * @param time current time
     59      * @return time the processing operation took
     60      */
     61     public abstract int process(int time);
     62 
     63     /**
     64      * Switch to a new process
     65      *
     66      * @param time current time
     67      * @return time that the switching operation took
     68      */
     69     protected abstract int switchProcess(int time);
     70 
     71     /**
     72      * Give the results of the simulation, such as the times the process was
     73      * execute
     74      *
     75      * @return Results of the simulation
     76      */
     77     public String results() {
     78         return startTimes + "\n" +
     79             "Process Turnaround Time Waiting Time\n" +
     80             processed.stream()
     81                 .sorted(new SortbyPid())
     82                 .map(p -> String.format(
     83                             "%s\t%d\t\t%d",
     84                             p.getPid(),
     85                             p.getTurnaroundTime(),
     86                             p.getWaitingTime()))
     87                 .collect(Collectors.joining("\n")) + "\n";
     88     }
     89 
     90     /**
     91      * Give summary data of the simulation
     92      *
     93      * @return The average turnaroundTime and average waitingTime
     94      */
     95     public String summary() {
     96         return String.format(
     97                 "%.2f\t\t\t%.2f",
     98                 processed.stream()
     99                     .mapToDouble(p -> p.getTurnaroundTime())
    100                     .average()
    101                     .orElse(0),
    102                 processed.stream()
    103                     .mapToDouble(p -> p.getWaitingTime())
    104                     .average()
    105                     .orElse(0)
    106         );
    107     }
    108 }