JavaDoc added

This commit is contained in:
2025-01-17 12:40:18 +01:00
parent 0d541f7737
commit ccde6a9668
4 changed files with 158 additions and 0 deletions

View File

@@ -6,20 +6,43 @@ import java.util.Map;
import java.util.PriorityQueue;
import net.berack.upo.valpre.rand.Rng;
/**
* A network simulation that uses a discrete event simulation to model the
* behavior of a network of servers.
*/
public class NetSimulation {
public final long seed;
private final Rng rng;
private final Map<String, ServerNode> servers = new HashMap<>();
/**
* Creates a new network simulation with the given seed.
*
* @param seed The seed to use for the random number generator.
*/
public NetSimulation(long seed) {
this.seed = seed;
this.rng = new Rng(seed);
}
/**
* Adds a new server node to the network.
*
* @param node The server node to add.
*/
public void addNode(ServerNode node) {
this.servers.put(node.name, node);
}
/**
* Runs the simulation for the given number of total arrivals, stopping when the
* given node has reached the
* specified number of departures.
*
* @param total The total number of arrivals to simulate.
* @param untilDepartureNode The name of the node to stop at.
* @return A map of statistics for each server node in the network.
*/
public Map<String, Statistics> run(long total, String untilDepartureNode) {
// Initialization
var timeNow = 0.0d;
@@ -49,6 +72,12 @@ public class NetSimulation {
return stats;
}
/**
* Represents a statistical summary of the behavior of a server node in the
* network.
* It is used by the simulation to track the number of arrivals and departures,
* the maximum queue length, the busy time, and the response time.
*/
public static class Statistics {
public int numArrivals = 0;
public int numDepartures = 0;
@@ -61,10 +90,18 @@ public class NetSimulation {
private ArrayDeque<Double> queue = new ArrayDeque<>();
private final Rng rng;
/**
* Creates a new statistics object with the given random number generator.
*
* @param rng The random number generator to use.
*/
public Statistics(Rng rng) {
this.rng = rng;
}
/**
* Resets the statistics to their initial values.
*/
public void reset() {
this.numArrivals = 0;
this.numDepartures = 0;
@@ -74,6 +111,17 @@ public class NetSimulation {
this.queue.clear();
}
/**
* Processes an arrival event for the given node at the given time.
* The event is processed by adding the arrival time to the queue, updating the
* maximum queue length, and checking if a server is available to process the
* arrival. If a server is available, a departure event is created and added to
* the future event list.
*
* @param event The arrival event to process.
* @param timeNow The current time of the simulation.
* @param fel The future event list to add new events to.
*/
private void processArrival(Event event, double timeNow, PriorityQueue<Event> fel) {
this.numArrivals++;
this.queue.add(event.time);
@@ -92,6 +140,18 @@ public class NetSimulation {
this.addArrivalIf(event.node.isSource, event.node, timeNow, fel);
}
/**
* Processes a departure event for the given node at the given time.
* The event is processed by removing the departure time from the queue,
* updating the busy time, and checking if there are any arrivals in the queue.
* If there are, a new departure event is created and added to the fel.
* At the end it will add an arrival to the next node if the current node has a
* child.
*
* @param event The departure event to process.
* @param timeNow The current time of the simulation.
* @param fel The future event list to add new events to.
*/
private void processDeparture(Event event, double timeNow, PriorityQueue<Event> fel) {
var startService = this.queue.poll();
var response = timeNow - startService;
@@ -113,6 +173,15 @@ public class NetSimulation {
this.addArrivalIf(!event.node.isSink, next, timeNow, fel);
}
/**
* Adds an arrival event to the future event list if the given condition is
* true.
*
* @param condition The condition to check.
* @param node The node to add the arrival event for.
* @param timeNow The current time of the simulation.
* @param fel The future event list to add the event to.
*/
private void addArrivalIf(boolean condition, ServerNode node, double timeNow, PriorityQueue<Event> fel) {
if (condition && node != null) {
var delay = node.distribution.sample(this.rng);