Rename sourceLimited method to terminal and update related logic for improved clarity; adjust spawn behavior to use -1 for infinite arrivals

This commit is contained in:
2025-02-19 11:49:31 +01:00
parent 8fe2f9d781
commit 744df75d41
6 changed files with 34 additions and 31 deletions

View File

@@ -78,22 +78,24 @@ public class NetBuilderInteractive {
* @return the node * @return the node
*/ */
private ServerNode buildNode() { private ServerNode buildNode() {
var choice = choose("Choose the type of node to create:", "Source", "Queue", "Queue with unavailable time"); var choice = choose("Choose the type of node to create:", "Source", "Terminal", "Queue",
"Queue with unavailable time");
var name = ask("Node name: "); var name = ask("Node name: ");
var distribution = askDistribution("Service distribution"); var distribution = askDistribution("Service distribution");
return switch (choice) { return switch (choice) {
case 1 -> { case 1 -> ServerNode.Builder.source(name, distribution);
case 2 -> {
var limit = ask("Arrivals limit (0 for Int.Max): ", Integer::parseInt); var limit = ask("Arrivals limit (0 for Int.Max): ", Integer::parseInt);
if (limit <= 0) if (limit <= 0)
limit = Integer.MAX_VALUE; limit = Integer.MAX_VALUE;
yield ServerNode.Builder.sourceLimited(name, limit, distribution); yield ServerNode.Builder.terminal(name, limit, distribution);
} }
case 2 -> { case 3 -> {
var servers = ask("Number of servers: ", Integer::parseInt); var servers = ask("Number of servers: ", Integer::parseInt);
yield ServerNode.Builder.queue(name, servers, distribution, null); yield ServerNode.Builder.queue(name, servers, distribution, null);
} }
case 3 -> { case 4 -> {
var servers = ask("Number of servers: ", Integer::parseInt); var servers = ask("Number of servers: ", Integer::parseInt);
var unavailable = askDistribution("Unavailable distribution"); var unavailable = askDistribution("Unavailable distribution");
yield ServerNode.Builder.queue(name, servers, distribution, unavailable); yield ServerNode.Builder.queue(name, servers, distribution, unavailable);
@@ -179,7 +181,7 @@ public class NetBuilderInteractive {
try { try {
var value = parser.apply(line); var value = parser.apply(line);
return value; return value;
} catch (Exception e) { } catch (Exception e) {
} }
} }
} }

View File

@@ -18,7 +18,7 @@ public class ServerNode {
/** /**
* Creates a generic node with the given name and distribution. * Creates a generic node with the given name and distribution.
* The servers number must be 1 or higher; if lower will be put to 1. * The servers number must be 1 or higher; if lower will be put to 1.
* The spawn number must be 0 or higher; if lower will be put to 0. * The spawn number must be 0 or higher; if lower will be put to -1 (infinite).
* The queue number must be equal or higher than the servers number; if lower * The queue number must be equal or higher than the servers number; if lower
* will be put to the servers number. * will be put to the servers number.
* The service distribution can't be null, otherwise an exception is thrown. * The service distribution can't be null, otherwise an exception is thrown.
@@ -37,7 +37,7 @@ public class ServerNode {
if (servers <= 0) if (servers <= 0)
servers = 1; servers = 1;
if (spawn < 0) if (spawn < 0)
spawn = 0; spawn = -1;
if (queue < servers) if (queue < servers)
queue = servers; queue = servers;
@@ -205,26 +205,27 @@ public class ServerNode {
/** /**
* Creates a source node with the given name and distribution. * Creates a source node with the given name and distribution.
* It swpawns infinite arrivals (Integer.MAX_VALUE). * It swpawns infinite arrivals (-1).
* *
* @param name The name of the node. * @param name The name of the node.
* @param distribution The distribution of the inter-arrival times. * @param distribution The distribution of the inter-arrival times.
* @return The created source node. * @return The created source node.
*/ */
public static ServerNode source(String name, Distribution distribution) { public static ServerNode source(String name, Distribution distribution) {
return new Builder(name, distribution).spawn(Integer.MAX_VALUE).build(); return new Builder(name, distribution).spawn(-1).build();
} }
/** /**
* Creates a source node with the given name, distribution, and number of * Creates a terminal node with the given name, distribution, and number of
* arrivals to spawn. * arrivals to spawn.
* Once it has finished spawning the arrivals, it will not spawn anymore.
* *
* @param name The name of the node. * @param name The name of the node.
* @param service The distribution of the inter-arrival times. * @param service The distribution of the inter-arrival times.
* @param spawnArrivals The number of arrivals to spawn. * @param spawnArrivals The number of arrivals to spawn.
* @return The created source node. * @return The created source node.
*/ */
public static ServerNode sourceLimited(String name, int spawnArrivals, Distribution service) { public static ServerNode terminal(String name, int spawnArrivals, Distribution service) {
return new Builder(name, service).spawn(spawnArrivals).build(); return new Builder(name, service).spawn(spawnArrivals).build();
} }

View File

@@ -69,7 +69,7 @@ public class ServerNodeState {
* @return True if the node should spawn an arrival, false otherwise. * @return True if the node should spawn an arrival, false otherwise.
*/ */
public boolean shouldSpawnArrival() { public boolean shouldSpawnArrival() {
return this.node.spawnArrivals > this.stats.numArrivals; return this.node.spawnArrivals < 0 || this.node.spawnArrivals > this.stats.numArrivals;
} }
/** /**

View File

@@ -107,29 +107,29 @@ public class TestInteractions {
var net = new NetBuilderInteractive(out, in).run(); var net = new NetBuilderInteractive(out, in).run();
assertEquals("", net.toString()); assertEquals("", net.toString());
inputs = List.of("1", "1", "Source", "1", "1.0", "10000", "5"); inputs = List.of("1", "1", "Source", "1", "1.0", "5");
bytes = String.join("\n", inputs).getBytes(); bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream("1\n1\nSource\n1\n1.0\n1000\n5\n".getBytes()); in = new ByteArrayInputStream("1\n1\nSource\n1\n1.0\n1000\n5\n".getBytes());
net = new NetBuilderInteractive(out, in).run(); net = new NetBuilderInteractive(out, in).run();
assertEquals("Source[servers:1, queue:100, spawn:1000, Exponential(1.0)] -\n", net.toString()); assertEquals("Source[servers:1, queue:100, spawn:-1, Exponential(1.0)] -\n", net.toString());
inputs = List.of("1", "1", "Source", "1", "2.0", "500", inputs = List.of("1", "2", "Terminal", "1", "2.0", "500",
"1", "2", "Queue", "5", "3.2", "0.6", "1", "1", "3", "Queue", "5", "3.2", "0.6", "1",
"5"); "5");
bytes = String.join("\n", inputs).getBytes(); bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream(bytes); in = new ByteArrayInputStream(bytes);
net = new NetBuilderInteractive(out, in).run(); net = new NetBuilderInteractive(out, in).run();
assertEquals("Source[servers:1, queue:100, spawn:500, Exponential(2.0)] -\n" assertEquals("Terminal[servers:1, queue:100, spawn:500, Exponential(2.0)] -\n"
+ "Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)] -\n", net.toString()); + "Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)] -\n", net.toString());
inputs = List.of("1", "1", "Source", "1", "2.0", "500", inputs = List.of("1", "1", "Source", "1", "2.0",
"1", "2", "Queue", "5", "3.2", "0.6", "1", "1", "3", "Queue", "5", "3.2", "0.6", "1",
"2", "Source", "Queue", "1.0", "2", "Source", "Queue", "1.0",
"5"); "5");
bytes = String.join("\n", inputs).getBytes(); bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream(bytes); in = new ByteArrayInputStream(bytes);
net = new NetBuilderInteractive(out, in).run(); net = new NetBuilderInteractive(out, in).run();
assertEquals("Source[servers:1, queue:100, spawn:500, Exponential(2.0)] -> Queue(1.0)\n" assertEquals("Source[servers:1, queue:100, spawn:-1, Exponential(2.0)] -> Queue(1.0)\n"
+ "Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)] -\n", net.toString()); + "Queue[servers:1, queue:100, spawn:0, Normal(3.2, 0.6)] -\n", net.toString());
} }

View File

@@ -46,17 +46,17 @@ public class TestSaveExamplesNet {
private static final Net net2 = new Net(); private static final Net net2 = new Net();
private static final Net net3 = new Net(); private static final Net net3 = new Net();
static { static {
net1.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp0_22)); net1.addNode(ServerNode.Builder.terminal("Source", spawn, exp0_22));
net1.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2)); net1.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2));
net1.addConnection(0, 1, 1.0); net1.addConnection(0, 1, 1.0);
net2.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp0_22)); net2.addNode(ServerNode.Builder.terminal("Source", spawn, exp0_22));
net2.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2)); net2.addNode(ServerNode.Builder.queue("Queue", 1, norm3_2));
net2.addNode(ServerNode.Builder.queue("Queue Wait", 1, norm3_2, unNorm)); net2.addNode(ServerNode.Builder.queue("Queue Wait", 1, norm3_2, unNorm));
net2.addConnection(0, 1, 1.0); net2.addConnection(0, 1, 1.0);
net2.addConnection(1, 2, 1.0); net2.addConnection(1, 2, 1.0);
net3.addNode(ServerNode.Builder.sourceLimited("Source", spawn, exp1_5)); net3.addNode(ServerNode.Builder.terminal("Source", spawn, exp1_5));
net3.addNode(ServerNode.Builder.queue("Service1", 1, exp2)); net3.addNode(ServerNode.Builder.queue("Service1", 1, exp2));
net3.addNode(ServerNode.Builder.queue("Service2", 1, exp3_5, unExp)); net3.addNode(ServerNode.Builder.queue("Service2", 1, exp3_5, unExp));
net3.addConnection(0, 1, 1.0); net3.addConnection(0, 1, 1.0);

View File

@@ -24,7 +24,7 @@ public class TestSimulation {
private static final ServerNode node0; private static final ServerNode node0;
private static final ServerNode node1; private static final ServerNode node1;
static { static {
node0 = ServerNode.Builder.sourceLimited("First", 0, const1); node0 = ServerNode.Builder.terminal("First", 0, const1);
node1 = ServerNode.Builder.queue("Second", 1, const1); node1 = ServerNode.Builder.queue("Second", 1, const1);
simpleNet = new Net(); simpleNet = new Net();
@@ -57,10 +57,10 @@ public class TestSimulation {
node = ServerNode.Builder.source("Source", const1); node = ServerNode.Builder.source("Source", const1);
assertEquals("Source", node.name); assertEquals("Source", node.name);
assertEquals(1, node.maxServers); assertEquals(1, node.maxServers);
assertEquals(Integer.MAX_VALUE, node.spawnArrivals); assertEquals(-1, node.spawnArrivals);
assertEquals(1.0, node.getServiceTime(null), DELTA); assertEquals(1.0, node.getServiceTime(null), DELTA);
node = ServerNode.Builder.sourceLimited("Source", 50, const1); node = ServerNode.Builder.terminal("Source", 50, const1);
assertEquals("Source", node.name); assertEquals("Source", node.name);
assertEquals(1, node.maxServers); assertEquals(1, node.maxServers);
assertEquals(50, node.spawnArrivals); assertEquals(50, node.spawnArrivals);
@@ -197,7 +197,7 @@ public class TestSimulation {
@Test @Test
public void nodeStatsUpdates() { public void nodeStatsUpdates() {
var net = new Net(); var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", 50, const1)); net.addNode(ServerNode.Builder.terminal("Source", 50, const1));
net.addNode(node1); net.addNode(node1);
net.addConnection(0, 1, 1.0); net.addConnection(0, 1, 1.0);
@@ -410,7 +410,7 @@ public class TestSimulation {
// we can use the average time // we can use the average time
var endAllocation = System.nanoTime(); var endAllocation = System.nanoTime();
var time = (endAllocation + start) / 2; var time = (endAllocation + start) / 2;
var diff = 0.5e-6 * (endAllocation - start); // getting the error margin in ms var diff = 1e-6 * (endAllocation - start); // getting the error margin in ms
assertTrue(sim.hasEnded()); assertTrue(sim.hasEnded());
assertEquals(0, sim.getEventsProcessed()); assertEquals(0, sim.getEventsProcessed());
@@ -542,7 +542,7 @@ public class TestSimulation {
@Test @Test
public void simulationStats() { public void simulationStats() {
var net = new Net(); var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", 50, const1)); net.addNode(ServerNode.Builder.terminal("Source", 50, const1));
var sim = new Simulation(net, rigged); var sim = new Simulation(net, rigged);
var result = sim.run(); var result = sim.run();
@@ -594,7 +594,7 @@ public class TestSimulation {
@Test @Test
public void simulationDrop() { public void simulationDrop() {
var net = new Net(); var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", 50, const1)); net.addNode(ServerNode.Builder.terminal("Source", 50, const1));
net.addNode(new ServerNode.Builder("Queue", _ -> 2.0).queue(20).build()); net.addNode(new ServerNode.Builder("Queue", _ -> 2.0).queue(20).build());
net.addConnection(0, 1, 1.0); net.addConnection(0, 1, 1.0);