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 1d2eba7..83127eb 100644 --- a/src/main/java/net/berack/upo/valpre/sim/ServerNode.java +++ b/src/main/java/net/berack/upo/valpre/sim/ServerNode.java @@ -54,13 +54,24 @@ 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 distribution can't be null, otherwise an exception is thrown. * - * @param name The name of the node. - * @param maxServers The maximum number of servers in the queue. - * @param distribution The distribution of the service times. - * @param spawnArrivals The number of arrivals to spawn. + * @param name The name of the node. + * @param maxServers The maximum number of servers in the queue. + * @param distribution The distribution of the service times. + * @param spawnArrivals The number of arrivals to spawn. + * @throws NullPointerException if the distribution is null */ public ServerNode(String name, int maxServers, Distribution distribution, int spawnArrivals) { + if (distribution == null) + throw new NullPointerException("Distribution can't be null"); + if (maxServers <= 0) + maxServers = 1; + if (spawnArrivals < 0) + spawnArrivals = 0; + this.name = name; this.maxServers = maxServers; this.distribution = distribution; @@ -91,6 +102,6 @@ public class ServerNode { * @return True if the node should spawn an arrival, false otherwise. */ public boolean shouldSpawnArrival(double numArrivals) { - return this.spawnArrivals > numArrivals; + return this.spawnArrivals > Math.max(0, numArrivals); } } diff --git a/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java b/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java index 434d60a..12cbacb 100644 --- a/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java +++ b/src/main/java/net/berack/upo/valpre/sim/SimulationMultiple.java @@ -15,6 +15,10 @@ import net.berack.upo.valpre.sim.stats.Result; public class SimulationMultiple { private final Net net; + /** + * TODO + * @param net + */ public SimulationMultiple(Net net) { this.net = net; } diff --git a/src/main/java/net/berack/upo/valpre/sim/stats/Statistics.java b/src/main/java/net/berack/upo/valpre/sim/stats/Statistics.java index b800c81..34bde62 100644 --- a/src/main/java/net/berack/upo/valpre/sim/stats/Statistics.java +++ b/src/main/java/net/berack/upo/valpre/sim/stats/Statistics.java @@ -88,7 +88,7 @@ public class Statistics { } /** - * + * TODO * * @param save * @param val1 diff --git a/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java b/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java new file mode 100644 index 0000000..2c55f32 --- /dev/null +++ b/src/test/java/net/berack/upo/valpre/sim/TestSimulation.java @@ -0,0 +1,133 @@ +package net.berack.upo.valpre.sim; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; + +import net.berack.upo.valpre.rand.Distribution; +import net.berack.upo.valpre.rand.Rng; + +public class TestSimulation { + + private static double DELTA = 0.0000001; + private static Rng rigged = new RiggedRng(); + private static Distribution const0 = new Constant(0.0); + private static Distribution const1 = new Constant(0.0); + + private final static class RiggedRng extends Rng { + @Override + public double random() { + return 0.5; + } + } + + private final static class Constant implements Distribution { + public final double value; + + public Constant(double value) { + this.value = value; + } + + @Override + public double sample(Rng rng) { + return this.value; + } + } + + @Test + public void serverNode() { + var node = new ServerNode("Nodo", 0, const1, 0); + assertEquals("Nodo", node.name); + assertEquals(1, node.maxServers); + assertFalse(node.shouldSpawnArrival(0)); + assertFalse(node.shouldSpawnArrival(50)); + assertFalse(node.shouldSpawnArrival(1000)); + assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE)); + assertFalse(node.shouldSpawnArrival(-1)); + assertEquals(1.0, node.getPositiveSample(null), DELTA); + + node = ServerNode.createQueue("Queue", 50, const1); + assertEquals("Queue", node.name); + assertEquals(50, node.maxServers); + assertFalse(node.shouldSpawnArrival(0)); + assertFalse(node.shouldSpawnArrival(50)); + assertFalse(node.shouldSpawnArrival(1000)); + assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE)); + assertFalse(node.shouldSpawnArrival(-1)); + assertEquals(1.0, node.getPositiveSample(null), DELTA); + + node = ServerNode.createSource("Source", const1); + assertEquals("Source", node.name); + assertEquals(Integer.MAX_VALUE, node.maxServers); + assertTrue(node.shouldSpawnArrival(0)); + assertTrue(node.shouldSpawnArrival(50)); + assertTrue(node.shouldSpawnArrival(1000)); + assertTrue(node.shouldSpawnArrival(Integer.MAX_VALUE - 1)); + assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE)); + assertTrue(node.shouldSpawnArrival(-1)); + assertEquals(1.0, node.getPositiveSample(null), DELTA); + + node = ServerNode.createLimitedSource("Source", const1, 50); + assertEquals("Source", node.name); + assertEquals(Integer.MAX_VALUE, node.maxServers); + assertTrue(node.shouldSpawnArrival(0)); + assertTrue(node.shouldSpawnArrival(49)); + assertFalse(node.shouldSpawnArrival(50)); + assertFalse(node.shouldSpawnArrival(1000)); + assertFalse(node.shouldSpawnArrival(Integer.MAX_VALUE)); + assertTrue(node.shouldSpawnArrival(-1)); + assertEquals(1.0, node.getPositiveSample(null), DELTA); + } + + @Test + public void event() { + var node = ServerNode.createSource("Source", const0); + var event = Event.newType(node, 0, Event.Type.ARRIVAL); + assertEquals(node, event.node); + assertEquals(0.0, event.time, 0.000000000001); + assertEquals(Event.Type.ARRIVAL, event.type); + + var event2 = Event.newArrival(node, 1.0); + assertEquals(node, event2.node); + assertEquals(1.0, event2.time, 0.000000000001); + assertEquals(Event.Type.ARRIVAL, event2.type); + + var event3 = Event.newDeparture(node, 5.0); + assertEquals(node, event3.node); + assertEquals(5.0, event3.time, 0.000000000001); + assertEquals(Event.Type.DEPARTURE, event3.type); + + assertEquals(0, event2.compareTo(event2)); + assertEquals(1, event2.compareTo(event)); + assertEquals(-1, event2.compareTo(event3)); + } + + @Test + public void net() { + // TODO + var net = new Net(); + net.addNode(null); + } + + @Test + public void criteria() { + // TODO + + var criteria = new EndCriteria.MaxTime(5.0); + criteria.shouldEnd(null); + } + + @Test + public void simulation() { + // TODO + var sim = new Simulation(null, rigged); + sim.endSimulation(); + } + + @Test + public void multipleSim() { + // TODO + } +}