-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.java
146 lines (128 loc) · 5.06 KB
/
Controller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cpuschedulingsimulator;
import java.awt.Color;
import java.awt.Component;
import java.util.LinkedList;
import javax.swing.*;
public class Controller extends JFrame
{
//DECLARING VARIABLES USED BY CONTROLLER TO UPDATE SIMULATION
double delay;
int clock;
int totalBurstTime;
boolean cont;
Process[] processList;
private boolean finishedSimulation;
double sumtrTS = 0.0;
double avgTrTs;
//GUI COMPONENTS UPDATED BY CONTROLLER
JPanel jPanel;
JPanel controlButtonsPanel;
JProgressBar clockCycle;
Process selectProcess;
Component[] miscellaneousComp;
SchAlgorithm algo;
LinkedList<ComputeGanttChart> ganttChart = new LinkedList<>();;
//CONSTRUCTOR
Controller(Process[] processList, JProgressBar clockCycle, int delay, JPanel jPanel,Component[] miscellaneousComp, JPanel controlButtonsPanel) {
this.clock = 0;
int totalProcess = processList.length;
this.clockCycle = clockCycle;
this.processList = processList;
this.finishedSimulation = false;
setDelay(delay);
this.jPanel = jPanel;
this.controlButtonsPanel = controlButtonsPanel;
selectProcess = processList[0];
totalBurstTime = 0;
for (int i = 0; i < totalProcess; i++) {
totalBurstTime += processList[i].getBurstTime();
}
clockCycle.setMaximum(totalBurstTime);
this.miscellaneousComp = miscellaneousComp;
this.cont = false;
}
//DECLARING SETTERS AND GETTERS
public void setDelay(int delay) {
this.delay = (double) (delay / 50.0);
}
void setAlgorithm(SchAlgorithm algo) {
this.algo = algo;
}
public void setClock(int clock) {
this.clock = clock;
}
public void setCont(boolean cont) {
this.cont = cont;
}
public void setFinishedSimulation(boolean finishedSimulation) {
this.finishedSimulation = finishedSimulation;
this.clock = 0;
}
public boolean isFinishedSimulation() {
return finishedSimulation;
}
//CONTROLLER FUNCTION TO INCREMENT CPU CLOCK AND UPDATE GUI
void update() {
clock++;
clockCycle.setValue(clock);
JLabel cpuSelectedProcess = (JLabel) miscellaneousComp[2];
cpuSelectedProcess.setText(this.selectProcess.getProcessID());
if (clock >= totalBurstTime) {
if (!finishedSimulation) {
for (Component comp: controlButtonsPanel.getComponents()) {
if (comp.isEnabled()) {
comp.setEnabled(false);
}
else {
comp.setEnabled(true);
}
}
miscellaneousComp[3].setEnabled(true);
miscellaneousComp[4].setEnabled(true);
cpuSelectedProcess.setText("Completed");
cpuSelectedProcess.setForeground(Color.WHITE);
finishedSimulation = true;
}
obtainResult();
}
}
//GETTING RESULTS AFTER COMPLETION OF SIMULATION
void obtainResult() {
double avgTurnAroundTime = 0.0;
double trTs = 0.0;
int[] turnaround = new int[processList.length];
int waiting[] = new int[processList.length];
for (int i = 0; i < processList.length; i++) {
for (ComputeGanttChart comp: ganttChart) {
if (comp.getProcess() == processList[i]) {
turnaround[i] = comp.getEndTime() - comp.getProcess().getArrivalTime();
processList[i].setTurnAroundTime(turnaround[i]);
}
}
waiting[i] = turnaround[i] - processList[i].getBurstTime();
processList[i].setWaitTime(waiting[i]);
}
for(int i=0; i < processList.length; i++)
{
double trTsIndividual = (double)(turnaround[i]/processList[i].getBurstTime());
sumtrTS += trTsIndividual;
}
avgTrTs = (double) (sumtrTS / processList.length);
double avgWaitTime = 0.0;
for (int i = 0; i < processList.length; i++) {
avgTurnAroundTime += turnaround[i];
avgWaitTime += waiting[i];
}
//LOGIC TO CALCULATE VARIOUS METRICS
// TR/TS REMAINING
avgTurnAroundTime = (double) (avgTurnAroundTime / processList.length);
avgWaitTime = (double) (avgWaitTime / processList.length);
JTextField turnAroundLabel = (JTextField) miscellaneousComp[1];
turnAroundLabel.setText(String.valueOf(avgTurnAroundTime));
JTextField waitingTime = (JTextField) miscellaneousComp[0];
waitingTime.setText(String.valueOf(avgWaitTime));
//System.out.println("Average TurnAround Time / Burst Time: " + avgTrTs);
// JTextField avgTrByTs = (JTextField) miscellaneousComp[5];
// avgTrByTs.setText(String.valueOf(avgTrTs));
}
}