-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
659 lines (574 loc) · 21.6 KB
/
app.js
File metadata and controls
659 lines (574 loc) · 21.6 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// app.js
document.addEventListener('DOMContentLoaded', () => {
// ----------------------
// Dark Mode Toggle
// ----------------------
const darkModeToggle = document.getElementById('darkModeToggle');
if (darkModeToggle) {
darkModeToggle.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
});
}
// ----------------------
// Simulation Variables
// ----------------------
let simulationHistory = []; // Stores the history of simulation steps
let currentStep = 0; // Tracks the current step in the simulation
let intervalId = null; // Stores the interval ID for play/pause functionality
// ----------------------
// Event Listener for Start Simulation Button
// ----------------------
const startSimulationBtn = document.getElementById('startSimulation');
if (startSimulationBtn) {
startSimulationBtn.addEventListener('click', () => {
// Retrieve and process user inputs
const pageRefsInput = document.getElementById('pageReferences').value.trim();
const pageRefs = pageRefsInput.split(/\s+/).map(Number);
const frameCount = parseInt(document.getElementById('frameCount').value);
const algorithm = document.getElementById('algorithm').value;
// Validate inputs
if (validateInput(pageRefs, frameCount)) {
let simulationResult;
// Execute the selected algorithm
switch (algorithm) {
case 'FIFO':
simulationResult = simulateFIFO(pageRefs, frameCount);
break;
case 'ModifiedFIFO':
simulationResult = simulateModifiedFIFO(pageRefs, frameCount);
break;
case 'LRU':
simulationResult = simulateLRU(pageRefs, frameCount);
break;
case 'Optimal':
simulationResult = simulateOptimal(pageRefs, frameCount);
break;
default:
alert('Algorithm not implemented.');
return;
}
// Update simulation history and reset current step
simulationHistory = simulationResult.history;
currentStep = 0;
// Expose simulationHistory globally for chart.js
window.simulationHistory = simulationHistory;
// Display total page faults
const totalPageFaultsElem = document.getElementById('totalPageFaults');
if (totalPageFaultsElem) {
totalPageFaultsElem.innerText = `Total Page Faults: ${simulationResult.pageFaults}`;
}
// Clear previous visualization, narration, and feedback
const visualizationArea = document.getElementById('visualizationArea');
if (visualizationArea) visualizationArea.innerHTML = '';
const narrationText = document.getElementById('narrationText');
if (narrationText) narrationText.innerText = '';
const aiFeedback = document.getElementById('aiFeedback');
if (aiFeedback) {
aiFeedback.innerText = '';
aiFeedback.classList.remove('text-red-500');
aiFeedback.classList.add('text-gray-800', 'dark:text-gray-200');
}
// Ensure any simulation-specific errors are cleared
const simulationError = document.getElementById('simulationError');
if (simulationError) {
simulationError.innerText = '';
simulationError.classList.add('hidden');
}
// Initialize the visualization with frame labels
initializeVisualization(frameCount);
// Show the first step
showStep(0);
currentStep = 1; // Since we have shown the first step
// Enable simulation controls appropriately
const nextStepBtn = document.getElementById('nextStep');
const prevStepBtn = document.getElementById('prevStep');
const playPauseBtn = document.getElementById('playPause');
if (nextStepBtn) nextStepBtn.disabled = false;
if (prevStepBtn) prevStepBtn.disabled = true;
if (playPauseBtn) playPauseBtn.disabled = false;
// Generate AI feedback based on the simulation results
generateFeedback({
algorithm: algorithm,
pageFaults: simulationResult.pageFaults,
frames: frameCount,
pageReferences: pageRefs,
});
} else {
console.log('Validation failed.');
}
});
}
// ----------------------
// Input Validation Function
// ----------------------
function validateInput(pages, frameCount) {
let isValid = true;
// Validate Page References
const pageReferencesInput = document.getElementById('pageReferences');
const pageReferencesError = document.getElementById('pageReferencesError');
if (!pageReferencesInput || !pageReferencesError) {
console.error('Page References input or error element not found.');
return false;
}
if (pages.length === 0 || pages.some((p) => isNaN(p))) {
isValid = false;
pageReferencesInput.classList.add('input-error');
pageReferencesError.classList.remove('hidden');
} else {
pageReferencesInput.classList.remove('input-error');
pageReferencesError.classList.add('hidden');
}
// Validate Frame Count
const frameCountInput = document.getElementById('frameCount');
const frameCountError = document.getElementById('frameCountError');
if (!frameCountInput || !frameCountError) {
console.error('Frame Count input or error element not found.');
return false;
}
if (isNaN(frameCount) || frameCount <= 0) {
isValid = false;
frameCountInput.classList.add('input-error');
frameCountError.classList.remove('hidden');
} else {
frameCountInput.classList.remove('input-error');
frameCountError.classList.add('hidden');
}
return isValid;
}
// ----------------------
// Remove Error Styles on Input
// ----------------------
const pageReferencesInput = document.getElementById('pageReferences');
const frameCountInput = document.getElementById('frameCount');
if (pageReferencesInput) {
pageReferencesInput.addEventListener('input', () => {
const error = document.getElementById('pageReferencesError');
pageReferencesInput.classList.remove('input-error');
if (error) error.classList.add('hidden');
});
}
if (frameCountInput) {
frameCountInput.addEventListener('input', () => {
const error = document.getElementById('frameCountError');
frameCountInput.classList.remove('input-error');
if (error) error.classList.add('hidden');
});
}
// ----------------------
// Page Replacement Algorithms
// ----------------------
function simulateFIFO(pages, frameCount) {
let frames = Array(frameCount).fill(null); // Initialize frames
let pageFaults = 0;
let history = [];
let pointer = 0; // Points to the frame to be replaced next
pages.forEach((page, index) => {
let fault = false;
let frameUpdated = null;
let hitFrames = [];
if (!frames.includes(page)) {
fault = true;
frames[pointer] = page;
frameUpdated = pointer;
pointer = (pointer + 1) % frameCount;
pageFaults++;
} else {
// Identify the frame that was hit
const hitIndex = frames.indexOf(page);
hitFrames.push(hitIndex);
}
history.push({
step: index + 1,
page: page,
frames: [...frames],
fault: fault,
frameUpdated: frameUpdated,
hitFrames: hitFrames, // Array of frame indices that had hits
});
});
return { history, pageFaults };
}
// Modified FIFO (Second-Chance Algorithm) Implementation
function simulateModifiedFIFO(pages, frameCount) {
let frames = Array(frameCount).fill(null); // Initialize frames
let referenceBits = Array(frameCount).fill(0); // Reference bits for second chance
let pageFaults = 0;
let history = [];
let pointer = 0; // Points to the frame to be replaced next
pages.forEach((page, index) => {
let fault = false;
let frameUpdated = null;
let hitFrames = [];
if (frames.includes(page)) {
// Page hit
const frameIndex = frames.indexOf(page);
referenceBits[frameIndex] = 1; // Set reference bit
hitFrames.push(frameIndex);
} else {
// Page fault
fault = true;
while (true) {
if (referenceBits[pointer] === 0) {
// Replace this page
frames[pointer] = page;
frameUpdated = pointer;
referenceBits[pointer] = 0; // Reset reference bit
pointer = (pointer + 1) % frameCount;
break;
} else {
// Give a second chance
referenceBits[pointer] = 0;
pointer = (pointer + 1) % frameCount;
}
}
pageFaults++;
}
history.push({
step: index + 1,
page: page,
frames: [...frames],
fault: fault,
frameUpdated: frameUpdated,
hitFrames: hitFrames, // Array of frame indices that had hits
});
});
return { history, pageFaults };
}
function simulateLRU(pages, frameCount) {
let frames = Array(frameCount).fill(null);
let pageFaults = 0;
let history = [];
let recentUsage = []; // Tracks the order of page usage
pages.forEach((page, index) => {
let fault = false;
let frameUpdated = null;
let hitFrames = [];
if (!frames.includes(page)) {
fault = true;
if (frames.includes(null)) {
const emptyIndex = frames.indexOf(null);
frames[emptyIndex] = page;
frameUpdated = emptyIndex;
} else {
// Find the least recently used page
const lruPage = recentUsage.shift();
const lruIndex = frames.indexOf(lruPage);
frames[lruIndex] = page;
frameUpdated = lruIndex;
}
pageFaults++;
} else {
// Page hit
const hitIndex = frames.indexOf(page);
hitFrames.push(hitIndex);
// Update recent usage by removing the page from its current position
const usageIndex = recentUsage.indexOf(page);
if (usageIndex !== -1) {
recentUsage.splice(usageIndex, 1);
}
}
// Update recent usage by adding the current page
recentUsage.push(page);
history.push({
step: index + 1,
page: page,
frames: [...frames],
fault: fault,
frameUpdated: frameUpdated,
hitFrames: hitFrames, // Array of frame indices that had hits
});
});
return { history, pageFaults };
}
function simulateOptimal(pages, frameCount) {
let frames = Array(frameCount).fill(null);
let pageFaults = 0;
let history = [];
pages.forEach((page, index) => {
let fault = false;
let frameUpdated = null;
let hitFrames = [];
if (!frames.includes(page)) {
fault = true;
if (frames.includes(null)) {
const emptyIndex = frames.indexOf(null);
frames[emptyIndex] = page;
frameUpdated = emptyIndex;
} else {
// Predict future usage for each page in frames
let futureIndices = frames.map((framePage) => {
let nextUse = pages.slice(index + 1).indexOf(framePage);
return nextUse === -1 ? Infinity : nextUse;
});
// Select the frame with the farthest next use
let maxFuture = Math.max(...futureIndices);
let victimIndices = futureIndices
.map((val, idx) => ({ val, idx }))
.filter(obj => obj.val === maxFuture)
.map(obj => obj.idx);
// If multiple victims, select the first one
let victimIndex = victimIndices[0];
frames[victimIndex] = page;
frameUpdated = victimIndex;
}
pageFaults++;
} else {
// Page hit
const hitIndex = frames.indexOf(page);
hitFrames.push(hitIndex);
}
history.push({
step: index + 1,
page: page,
frames: [...frames],
fault: fault,
frameUpdated: frameUpdated,
hitFrames: hitFrames, // Array of frame indices that had hits
});
});
return { history, pageFaults };
}
// ----------------------
// Initialize Visualization Function
// ----------------------
function initializeVisualization(frameCount) {
const visualizationArea = document.getElementById('visualizationArea');
if (!visualizationArea) {
console.error('Visualization area not found.');
return;
}
visualizationArea.innerHTML = ''; // Clear previous content
// Create the table element
const table = document.createElement('table');
table.className = 'w-full border-collapse text-center';
table.id = 'simulationTable';
// Create the header row
const headerRow = document.createElement('tr');
headerRow.id = 'tableHeaderRow';
const emptyHeader = document.createElement('th');
emptyHeader.className = 'border px-2 py-1';
emptyHeader.innerText = 'Frame';
headerRow.appendChild(emptyHeader);
table.appendChild(headerRow);
// Create rows for each frame
for (let i = 0; i < frameCount; i++) {
const row = document.createElement('tr');
row.classList.add('frame-row');
row.dataset.frameIndex = i;
// Frame label cell
const frameCell = document.createElement('td');
frameCell.className = 'border px-2 py-1 font-semibold';
frameCell.innerText = `Frame ${i + 1}`;
row.appendChild(frameCell);
table.appendChild(row);
}
visualizationArea.appendChild(table);
}
// ----------------------
// Show Step Function
// ----------------------
function showStep(stepIndex) {
const table = document.getElementById('simulationTable');
if (!table) {
console.error('Simulation table not found.');
return;
}
const step = simulationHistory[stepIndex];
if (!step) {
console.error(`Step ${stepIndex} not found in simulation history.`);
return;
}
// Add a new header cell for the current step
const headerRow = document.getElementById('tableHeaderRow');
const th = document.createElement('th');
th.className = 'border px-2 py-1';
th.innerText = `T${step.step}`;
headerRow.appendChild(th);
// For each frame, add a new cell
const frameRows = table.querySelectorAll('.frame-row');
frameRows.forEach((row) => {
const frameIndex = parseInt(row.dataset.frameIndex);
const cell = document.createElement('td');
cell.className = 'border px-2 py-1 relative';
const pageInFrame = step.frames[frameIndex];
if (pageInFrame !== null) {
cell.innerText = pageInFrame;
}
// Remove any existing color classes to prevent conflicts
cell.classList.remove('bg-red-200', 'bg-green-200', 'text-red-800', 'text-green-800', 'text-red-200', 'text-green-200');
// Apply custom classes based on faults and hits
if (step.frameUpdated === frameIndex) {
if (step.fault) {
// Page fault occurred in this frame
cell.classList.add('page-fault', 'has-tooltip');
cell.setAttribute('data-tippy-content', `Page fault: Loaded page ${pageInFrame} into Frame ${frameIndex + 1}`);
} else {
// Page hit occurred in this frame
cell.classList.add('page-hit', 'has-tooltip');
cell.setAttribute('data-tippy-content', `Page hit: Page ${pageInFrame} was already in Frame ${frameIndex + 1}`);
}
}
// Apply hit class for hits that are not the updated frame
if (!step.fault && step.hitFrames.includes(frameIndex)) {
cell.classList.add('page-hit', 'has-tooltip');
cell.setAttribute('data-tippy-content', `Page hit: Page ${pageInFrame} was already in Frame ${frameIndex + 1}`);
}
row.appendChild(cell);
// Initialize tooltip for this cell
if (typeof tippy === 'function') { // Ensure tippy is loaded
tippy(cell, {
placement: 'top',
arrow: true,
animation: 'scale',
});
}
});
// Update narration
const narrationText = document.getElementById('narrationText');
if (narrationText) {
if (step.fault) {
narrationText.innerText = `At time T${step.step}, page ${step.page} caused a page fault and was loaded into Frame ${step.frameUpdated + 1}.`;
} else if (step.hitFrames.length > 0) {
narrationText.innerText = `At time T${step.step}, page ${step.page} was already in memory (Hit).`;
} else {
narrationText.innerText = `At time T${step.step}, page ${step.page} was already in memory. No page fault occurred.`;
}
}
}
// ----------------------
// Controls Event Listeners
// ----------------------
const nextStepBtn = document.getElementById('nextStep');
const prevStepBtn = document.getElementById('prevStep');
const playPauseBtn = document.getElementById('playPause');
if (nextStepBtn) {
nextStepBtn.addEventListener('click', () => {
if (currentStep < simulationHistory.length) {
showStep(currentStep);
currentStep++;
if (prevStepBtn) prevStepBtn.disabled = false;
}
if (currentStep >= simulationHistory.length) {
if (nextStepBtn) nextStepBtn.disabled = true;
}
});
}
if (prevStepBtn) {
prevStepBtn.addEventListener('click', () => {
if (currentStep > 1) {
currentStep--;
removeStep(currentStep);
if (nextStepBtn) nextStepBtn.disabled = false;
} else if (currentStep === 1) {
currentStep--;
removeStep(0);
if (prevStepBtn) prevStepBtn.disabled = true;
if (nextStepBtn) nextStepBtn.disabled = false;
}
});
}
if (playPauseBtn) {
playPauseBtn.addEventListener('click', () => {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
playPauseBtn.innerText = 'Play';
} else {
intervalId = setInterval(() => {
if (currentStep < simulationHistory.length) {
showStep(currentStep);
currentStep++;
if (prevStepBtn) prevStepBtn.disabled = false;
}
if (currentStep >= simulationHistory.length) {
clearInterval(intervalId);
intervalId = null;
playPauseBtn.innerText = 'Play';
if (nextStepBtn) nextStepBtn.disabled = true;
}
}, 1000); // Adjust the speed as needed (milliseconds)
playPauseBtn.innerText = 'Pause';
}
});
}
// ----------------------
// Remove Step Function (for Previous button)
// ----------------------
function removeStep(stepIndex) {
const table = document.getElementById('simulationTable');
if (!table) {
console.error('Simulation table not found.');
return;
}
// Remove the last header cell
const headerRow = document.getElementById('tableHeaderRow');
if (headerRow && headerRow.lastChild) {
headerRow.removeChild(headerRow.lastChild);
} else {
console.warn('No header cell to remove.');
}
// Remove the last cell from each frame row
const frameRows = table.querySelectorAll('.frame-row');
frameRows.forEach((row) => {
if (row.lastChild) {
// Remove tooltip-related data attributes and classes
const cell = row.lastChild;
cell.classList.remove('bg-red-200', 'bg-green-200', 'has-tooltip');
cell.removeAttribute('data-tippy-content');
row.removeChild(cell);
} else {
console.warn(`No cell to remove from frame row ${row.dataset.frameIndex}.`);
}
});
// Update narration
const narrationText = document.getElementById('narrationText');
if (stepIndex > 0) {
const step = simulationHistory[stepIndex - 1];
if (narrationText) {
if (step.fault) {
narrationText.innerText = `At time T${step.step}, page ${step.page} caused a page fault and was loaded into Frame ${step.frameUpdated + 1}.`;
} else if (step.hitFrames.length > 0) {
narrationText.innerText = `At time T${step.step}, page ${step.page} was already in memory (Hit).`;
} else {
narrationText.innerText = `At time T${step.step}, page ${step.page} was already in memory. No page fault occurred.`;
}
}
} else {
if (narrationText) {
narrationText.innerText = 'Awaiting simulation...';
}
}
}
// ----------------------
// Generate AI Feedback Function
// ----------------------
async function generateFeedback(simulationData) {
const prompt = `The user has completed a page replacement simulation using the ${simulationData.algorithm} algorithm with ${simulationData.frames} frames and the page reference sequence ${simulationData.pageReferences.join(
', '
)}. There were ${simulationData.pageFaults} page faults. Provide a simple explanation of the results and suggest if a different algorithm might perform better.`;
try {
const response = await fetch('/api/ai-feedback', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: prompt }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
const aiFeedback = document.getElementById('aiFeedback');
if (aiFeedback) {
aiFeedback.innerText = data.feedback;
}
} catch (error) {
console.error('Error fetching AI feedback:', error);
// Display error message with highlighting
const aiFeedback = document.getElementById('aiFeedback');
if (aiFeedback) {
aiFeedback.innerText = 'Error fetching AI feedback.';
aiFeedback.classList.add('text-red-500');
aiFeedback.classList.remove('text-gray-800', 'dark:text-gray-200');
}
}
}
});