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
*/
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 distribution = askDistribution("Service distribution");
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);
if (limit <= 0)
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);
yield ServerNode.Builder.queue(name, servers, distribution, null);
}
case 3 -> {
case 4 -> {
var servers = ask("Number of servers: ", Integer::parseInt);
var unavailable = askDistribution("Unavailable distribution");
yield ServerNode.Builder.queue(name, servers, distribution, unavailable);
@@ -179,7 +181,7 @@ public class NetBuilderInteractive {
try {
var value = parser.apply(line);
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.
* 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
* will be put to the servers number.
* The service distribution can't be null, otherwise an exception is thrown.
@@ -37,7 +37,7 @@ public class ServerNode {
if (servers <= 0)
servers = 1;
if (spawn < 0)
spawn = 0;
spawn = -1;
if (queue < servers)
queue = servers;
@@ -205,26 +205,27 @@ public class ServerNode {
/**
* 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 distribution The distribution of the inter-arrival times.
* @return The created source node.
*/
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.
* Once it has finished spawning the arrivals, it will not spawn anymore.
*
* @param name The name of the node.
* @param service The distribution of the inter-arrival times.
* @param spawnArrivals The number of arrivals to spawn.
* @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();
}

View File

@@ -69,7 +69,7 @@ public class ServerNodeState {
* @return True if the node should spawn an arrival, false otherwise.
*/
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();
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();
in = new ByteArrayInputStream("1\n1\nSource\n1\n1.0\n1000\n5\n".getBytes());
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",
"1", "2", "Queue", "5", "3.2", "0.6", "1",
inputs = List.of("1", "2", "Terminal", "1", "2.0", "500",
"1", "3", "Queue", "5", "3.2", "0.6", "1",
"5");
bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream(bytes);
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());
inputs = List.of("1", "1", "Source", "1", "2.0", "500",
"1", "2", "Queue", "5", "3.2", "0.6", "1",
inputs = List.of("1", "1", "Source", "1", "2.0",
"1", "3", "Queue", "5", "3.2", "0.6", "1",
"2", "Source", "Queue", "1.0",
"5");
bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream(bytes);
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());
}

View File

@@ -46,17 +46,17 @@ public class TestSaveExamplesNet {
private static final Net net2 = new Net();
private static final Net net3 = new Net();
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.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 Wait", 1, norm3_2, unNorm));
net2.addConnection(0, 1, 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("Service2", 1, exp3_5, unExp));
net3.addConnection(0, 1, 1.0);

View File

@@ -24,7 +24,7 @@ public class TestSimulation {
private static final ServerNode node0;
private static final ServerNode node1;
static {
node0 = ServerNode.Builder.sourceLimited("First", 0, const1);
node0 = ServerNode.Builder.terminal("First", 0, const1);
node1 = ServerNode.Builder.queue("Second", 1, const1);
simpleNet = new Net();
@@ -57,10 +57,10 @@ public class TestSimulation {
node = ServerNode.Builder.source("Source", const1);
assertEquals("Source", node.name);
assertEquals(1, node.maxServers);
assertEquals(Integer.MAX_VALUE, node.spawnArrivals);
assertEquals(-1, node.spawnArrivals);
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(1, node.maxServers);
assertEquals(50, node.spawnArrivals);
@@ -197,7 +197,7 @@ public class TestSimulation {
@Test
public void nodeStatsUpdates() {
var net = new Net();
net.addNode(ServerNode.Builder.sourceLimited("Source", 50, const1));
net.addNode(ServerNode.Builder.terminal("Source", 50, const1));
net.addNode(node1);
net.addConnection(0, 1, 1.0);
@@ -410,7 +410,7 @@ public class TestSimulation {
// we can use the average time
var endAllocation = System.nanoTime();
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());
assertEquals(0, sim.getEventsProcessed());
@@ -542,7 +542,7 @@ public class TestSimulation {
@Test
public void simulationStats() {
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 result = sim.run();
@@ -594,7 +594,7 @@ public class TestSimulation {
@Test
public void simulationDrop() {
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.addConnection(0, 1, 1.0);