From 744df75d41a041f02e75337112d4ae5d33aa5cbe Mon Sep 17 00:00:00 2001 From: Berack96 Date: Wed, 19 Feb 2025 11:49:31 +0100 Subject: [PATCH] Rename sourceLimited method to terminal and update related logic for improved clarity; adjust spawn behavior to use -1 for infinite arrivals --- .../berack/upo/valpre/NetBuilderInteractive.java | 14 ++++++++------ .../net/berack/upo/valpre/sim/ServerNode.java | 13 +++++++------ .../berack/upo/valpre/sim/ServerNodeState.java | 2 +- .../berack/upo/valpre/sim/TestInteractions.java | 16 ++++++++-------- .../upo/valpre/sim/TestSaveExamplesNet.java | 6 +++--- .../berack/upo/valpre/sim/TestSimulation.java | 14 +++++++------- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/berack/upo/valpre/NetBuilderInteractive.java b/src/main/java/net/berack/upo/valpre/NetBuilderInteractive.java index a1259a2..8af26a1 100644 --- a/src/main/java/net/berack/upo/valpre/NetBuilderInteractive.java +++ b/src/main/java/net/berack/upo/valpre/NetBuilderInteractive.java @@ -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) { } } } diff --git a/src/main/java/net/berack/upo/valpre/sim/ServerNode.java b/src/main/java/net/berack/upo/valpre/sim/ServerNode.java index b7ee450..75a1dea 100644 --- a/src/main/java/net/berack/upo/valpre/sim/ServerNode.java +++ b/src/main/java/net/berack/upo/valpre/sim/ServerNode.java @@ -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(); } diff --git a/src/main/java/net/berack/upo/valpre/sim/ServerNodeState.java b/src/main/java/net/berack/upo/valpre/sim/ServerNodeState.java index 6734e10..adc6ec3 100644 --- a/src/main/java/net/berack/upo/valpre/sim/ServerNodeState.java +++ b/src/main/java/net/berack/upo/valpre/sim/ServerNodeState.java @@ -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; } /** diff --git a/src/test/java/net/berack/upo/valpre/sim/TestInteractions.java b/src/test/java/net/berack/upo/valpre/sim/TestInteractions.java index 7f776a8..103e21e 100644 --- a/src/test/java/net/berack/upo/valpre/sim/TestInteractions.java +++ b/src/test/java/net/berack/upo/valpre/sim/TestInteractions.java @@ -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()); } diff --git a/src/test/java/net/berack/upo/valpre/sim/TestSaveExamplesNet.java b/src/test/java/net/berack/upo/valpre/sim/TestSaveExamplesNet.java index b37e333..e652ce8 100644 --- a/src/test/java/net/berack/upo/valpre/sim/TestSaveExamplesNet.java +++ b/src/test/java/net/berack/upo/valpre/sim/TestSaveExamplesNet.java @@ -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); diff --git a/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java b/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java index 5f2c317..f9eaeb5 100644 --- a/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java +++ b/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java @@ -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);