Refactor SimulationBuilder and SimulationMultiple to accept PrintStream for output; update tests to use null output stream

This commit is contained in:
2025-02-19 11:22:52 +01:00
parent 121d4cd44a
commit 611e14e6fa
3 changed files with 62 additions and 31 deletions

View File

@@ -2,6 +2,7 @@ package net.berack.upo.valpre;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.concurrent.ExecutionException;
import com.esotericsoftware.kryo.KryoException;
@@ -13,6 +14,7 @@ import net.berack.upo.valpre.sim.EndCriteria.MaxTime;
import net.berack.upo.valpre.sim.Net;
import net.berack.upo.valpre.sim.SimulationMultiple;
import net.berack.upo.valpre.sim.stats.CsvResult;
import net.berack.upo.valpre.sim.stats.Result;
/**
* This class is responsible for running the simulation. It parses the arguments
@@ -230,23 +232,38 @@ public class SimulationBuilder {
* @throws ExecutionException If the simulation has an error.
* @throws IOException If the CSV file has a problem.
*/
public void run() throws InterruptedException, ExecutionException, IOException {
public Result.Summary run() throws InterruptedException, ExecutionException, IOException {
return this.run(System.out);
}
/**
* Run the simulation with the given parameters.
* At the end it prints the results and saves them to a CSV file if requested.
*
* @param out the output stream to print the results
* @throws InterruptedException If the simulation is interrupted.
* @throws ExecutionException If the simulation has an error.
* @throws IOException If the CSV file has a problem.
*/
public Result.Summary run(PrintStream out) throws InterruptedException, ExecutionException, IOException {
var nano = System.nanoTime();
var sim = new SimulationMultiple(this.net);
var summary = switch (this.type) {
case Incremental -> sim.runIncremental(this.seed, this.runs, this.confidences, this.endCriteria);
case Incremental -> sim.runIncremental(this.seed, this.runs, out, this.confidences, this.endCriteria);
case Parallel -> sim.runParallel(this.seed, this.runs, this.endCriteria);
case Normal -> sim.run(this.seed, this.runs, this.endCriteria);
};
nano = System.nanoTime() - nano;
System.out.print(summary);
System.out.println("Final time " + nano / 1e6 + "ms");
out.print(summary);
out.println("Final time " + nano / 1e6 + "ms");
if (csv != null) {
new CsvResult(this.csv).saveResults(summary.getRuns());
System.out.println("Data saved to " + this.csv);
out.println("Data saved to " + this.csv);
}
return summary;
}
/**