summaryrefslogtreecommitdiff
path: root/src/main/java/testing/HeadlessSimulationRunner.java
blob: 9d2274c5fca692eeeb84c51ebf5aef6e830a23e2 (plain)
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
package testing;

import simulator.*;
import simulator.engine.*;
import simulator.messaging.*;
import core.*;
import prefs.*;
import events.*;
import serialize.VSSerialize;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.*;

/**
 * Runs DS-Sim simulations in headless mode without GUI dependencies.
 * Captures logs and provides verification capabilities for automated testing.
 */
public class HeadlessSimulationRunner {
    private final VSDefaultPrefs prefs;
    private VSSimulator simulator;
    private VSSimulatorVisualization viz;
    private LogCapture logCapture;
    private final ExecutorService executor;
    private boolean printLogs = false;
    
    public HeadlessSimulationRunner() {
        this.prefs = new VSDefaultPrefs();
        this.prefs.fillWithDefaults();
        VSRegisteredEvents.init(prefs);
        this.executor = Executors.newSingleThreadExecutor();
    }
    
    /**
     * Run a simulation from a saved file for a specified duration.
     * 
     * @param simulationFile Path to the saved simulation .dat file
     * @param maxTime Maximum simulation time in milliseconds
     * @return SimulationResult containing logs and metrics
     */
    public SimulationResult runSimulation(String simulationFile, long maxTime) 
            throws Exception {
        return runSimulation(simulationFile, maxTime, null);
    }
    
    /**
     * Run a simulation with an optional log listener.
     */
    public SimulationResult runSimulation(String simulationFile, long maxTime, LogListener listener) 
            throws Exception {
        System.out.println("Loading simulation: " + simulationFile);
        
        try {
            // Use HeadlessLoader to avoid any GUI initialization
            HeadlessLoader.LoadedSimulation loaded = HeadlessLoader.load(simulationFile, prefs);
            simulator = loaded.getSimulator();
            viz = loaded.getVisualization();
            
            if (simulator == null || viz == null) {
                throw new IllegalStateException("Failed to load simulation");
            }
            
            // Set up headless message handlers for all processes
            setupHeadlessMessageHandlers(viz);
            
            // Install log capture
            logCapture = new LogCapture();
            logCapture.setPrintLogs(printLogs);
            if (listener != null) {
                logCapture.addListener(listener);
            }
            installLogCapture();
            
            // Get the simulation's configured end time
            long untilTime = viz.getUntilTime();
            long actualMaxTime = Math.min(maxTime, untilTime);
            
            System.out.println("Running simulation for up to " + actualMaxTime + "ms (until time: " + untilTime + "ms)...");
            
            // Run simulation
            Future<Void> runFuture = executor.submit(() -> {
                try {
                    runSimulationSteps(actualMaxTime);
                } catch (Exception e) {
                    System.err.println("Error during simulation: " + e.getMessage());
                    e.printStackTrace();
                }
                return null;
            });
            
            // Wait for completion or timeout
            try {
                runFuture.get(actualMaxTime * 2, TimeUnit.MILLISECONDS);
            } catch (TimeoutException e) {
                System.out.println("Simulation timeout - stopping...");
                runFuture.cancel(true);
            }
            
            System.out.println("Simulation complete. Captured " + 
                             logCapture.getTotalLogCount() + " log entries.");
            
            return new SimulationResult(
                logCapture.getCapturedLogs(),
                logCapture.getProcessLogs(),
                getSimulationMetrics()
            );
        } catch (Exception e) {
            System.err.println("Failed to load simulation: " + e.getMessage());
            throw e;
        }
    }
    
    private void runSimulationSteps(long maxTime) throws Exception {
        VSTaskManager taskManager = viz.getTaskManager();
        
        // Get necessary fields via reflection
        Field timeField = VSSimulatorVisualization.class
            .getDeclaredField("time");
        timeField.setAccessible(true);
        
        // Get simulatorTime field for accurate time tracking
        Field simulatorTimeField = VSSimulatorVisualization.class
            .getDeclaredField("simulatorTime");
        simulatorTimeField.setAccessible(true);
        
        // Get isPaused and hasFinished fields
        Field isPausedField = VSSimulatorVisualization.class
            .getDeclaredField("isPaused");
        isPausedField.setAccessible(true);
        Field hasFinishedField = VSSimulatorVisualization.class
            .getDeclaredField("hasFinished");
        hasFinishedField.setAccessible(true);
        
        // Get clockSpeed field and ensure it's set
        Field clockSpeedField = VSSimulatorVisualization.class
            .getDeclaredField("clockSpeed");
        clockSpeedField.setAccessible(true);
        double clockSpeed = clockSpeedField.getDouble(viz);
        if (clockSpeed == 0) {
            // Set default clock speed if not initialized
            clockSpeed = 1.0;
            clockSpeedField.setDouble(viz, clockSpeed);
        }
        
        // Get task queue fields for checking if tasks remain
        Field globalTasksField = VSTaskManager.class.getDeclaredField("globalTasks");
        globalTasksField.setAccessible(true);
        
        // Set isPaused to false to allow simulation to run
        isPausedField.setBoolean(viz, false);
        hasFinishedField.setBoolean(viz, false);
        
        long startTime = timeField.getLong(viz);
        long currentTime = startTime;
        long endTime = startTime + maxTime;
        int noActivityCount = 0;
        int lastLogCount = 0;
        long lastActiveTime = 0;
        
        // Call updateSimulator method to advance simulation
        Method updateSimulatorMethod = VSSimulatorVisualization.class
            .getDeclaredMethod("updateSimulator", long.class, long.class);
        updateSimulatorMethod.setAccessible(true);
        
        System.out.println("Starting simulation at time " + startTime + ", running until " + endTime);
        
        while (currentTime < endTime) {
            // Sync all process times BEFORE running tasks
            for (int i = 0; i < viz.getNumProcesses(); i++) {
                VSInternalProcess process = viz.getProcess(i);
                if (process != null) {
                    process.syncTime(currentTime);
                }
            }
            
            // Update simulation time - this also runs tasks internally
            updateSimulatorMethod.invoke(viz, currentTime, currentTime - 1);
            long simulatorTime = simulatorTimeField.getLong(viz);
            
            // Check if there's been any activity
            int currentLogCount = logCapture.getTotalLogCount();
            boolean hasActivity = currentLogCount > lastLogCount || 
                                 hasPendingActivity(taskManager, globalTasksField, simulatorTime);
            
            if (hasActivity) {
                noActivityCount = 0;
                lastLogCount = currentLogCount;
                lastActiveTime = currentTime;
            } else {
                noActivityCount++;
                // If no activity for 3000ms (3 seconds) of simulation time, stop
                // This accounts for message delivery times of 500-2000ms plus some buffer
                if (noActivityCount > 3000 && (currentTime - lastActiveTime) > 3000) {
                    System.out.println("No activity detected for 3 seconds - simulation complete at time " + simulatorTime);
                    break;
                }
            }
            
            // Advance time by 1ms
            currentTime++;
            timeField.setLong(viz, currentTime);
            
            // No delay needed - let simulation run at full speed
        }
        
        // Set isPaused back to true when done
        isPausedField.setBoolean(viz, true);
    }
    
    private boolean hasPendingActivity(VSTaskManager taskManager, Field globalTasksField, long currentTime) {
        try {
            // Check global tasks
            Queue<?> globalTasks = (Queue<?>) globalTasksField.get(taskManager);
            if (globalTasks != null && !globalTasks.isEmpty()) {
                return true; // If any global tasks exist, keep running
            }
            
            // Check process-specific tasks
            for (int i = 0; i < viz.getNumProcesses(); i++) {
                VSInternalProcess process = viz.getProcess(i);
                if (process != null) {
                    Queue<VSTask> tasks = process.getTasks();
                    if (tasks != null && !tasks.isEmpty()) {
                        return true; // If any process tasks exist, keep running
                    }
                }
            }
            
            // Check for messages in transit
            Field messageLinesField = VSSimulatorVisualization.class.getDeclaredField("messageLines");
            messageLinesField.setAccessible(true);
            LinkedList<?> messageLines = (LinkedList<?>) messageLinesField.get(viz);
            if (messageLines != null && !messageLines.isEmpty()) {
                return true; // Messages are still being delivered
            }
            
            return false;
        } catch (Exception e) {
            // If we can't check, assume there might be activity
            return true;
        }
    }
    
    private void installLogCapture() throws Exception {
        // Set simulatorVisualization reference in logCapture
        logCapture.setSimulatorCanvas(viz);
        
        // Install on visualization
        Field logingField = VSSimulatorVisualization.class
            .getDeclaredField("loging");
        logingField.setAccessible(true);
        logingField.set(viz, logCapture);
        
        // Install on all processes
        for (int i = 0; i < viz.getNumProcesses(); i++) {
            VSInternalProcess process = viz.getProcess(i);
            if (process != null) {
                Field processLogingField = VSAbstractProcess.class
                    .getDeclaredField("loging");
                processLogingField.setAccessible(true);
                processLogingField.set(process, logCapture);
            }
        }
    }
    
    private SimulationMetrics getSimulationMetrics() {
        return new SimulationMetrics(
            viz.getNumProcesses(),
            logCapture.getTotalLogCount(),
            logCapture.getProcessMessageCounts()
        );
    }
    
    public void setPrintLogs(boolean printLogs) {
        this.printLogs = printLogs;
        if (logCapture != null) {
            logCapture.setPrintLogs(printLogs);
        }
    }
    
    public void addLogListener(LogListener listener) {
        if (logCapture != null) {
            logCapture.addListener(listener);
        }
    }
    
    public void shutdown() {
        executor.shutdown();
        try {
            if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
        }
    }
    
    /**
     * Sets up headless message handlers for all processes to avoid GUI dependencies.
     */
    private void setupHeadlessMessageHandlers(VSSimulatorVisualization viz) {
        // Create a headless simulation engine
        HeadlessSimulationEngine engine = new HeadlessSimulationEngine(prefs, logCapture);
        
        // Set the task manager from the visualization
        engine.setTaskManager(viz.getTaskManager());
        
        // Copy processes to engine
        for (int i = 0; i < viz.getNumProcesses(); i++) {
            VSInternalProcess process = viz.getProcess(i);
            if (process != null) {
                engine.addProcess(process);
                
                // Create and set headless message handler
                MessageHandler handler = new HeadlessMessageHandler(engine);
                process.setMessageHandler(handler);
            }
        }
    }
}