diff --git a/README.md b/README.md index 7316daa..cac7061 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Il risultato è la creazione in una libreria per la simulazione di eventi discre Il JAR viene invocato tramite il classico comando java: `java -jar upo-valpre.jar` al quale si aggiungono vari argomenti successivi in base a cosa si vuole fare: -* `java -jar upo-valpre.jar net`\ +* `java -jar upo-valpre.jar interactive`\ Usato per avviare una sessione interattiva per la creazione di una rete. Da usare se la rete è relativamente breve da descrivere, altrimenti è più comodo usare il codice della libreria per la generazione della rete.\ Una volta scelta la rete è necessario salvarla in un file per la successiva simulazione e analisi. * `java -jar upo-valpre.jar simulation -net [other]`\ @@ -29,10 +29,6 @@ Esistono vari tipi di argomenti per scegliere come fare la simulazione: Mostra (con un ambiente grafico) una finestra nella quale si può scegliere quale nodo vedere e ogni statistica associata ad esso. Di seguito un'immagine di esempio:\ ![1738603552417](image/README/1738603552417.png) -Esistono dei file prefatti per vedere eventuali simulazioni che, nel caso vengano passati come parametri, automaticamente vengono usati: -* `example1.net`, `example2.net` e `example3.net` per `simulation -net` -* `example1.csv`, `example2.csv` e `example3.csv` per `plot -csv` - ### Classi Interne Esistono molteplici classi interne che vengono usate per supportare la simulazione e/o mostrare i risultati. In generale le classi dentro il percorso [net.berack.upo.valpre](https://github.com/Berack96/upo-valpre/tree/main/src/main/java/net/berack/upo/valpre) sono usate per l'utilizzo del jar e quindi non sono essenziali per la simulazione. diff --git a/src/main/java/net/berack/upo/valpre/InteractiveConsole.java b/src/main/java/net/berack/upo/valpre/InteractiveConsole.java index 1fdc8e1..efb5eaf 100644 --- a/src/main/java/net/berack/upo/valpre/InteractiveConsole.java +++ b/src/main/java/net/berack/upo/valpre/InteractiveConsole.java @@ -39,12 +39,11 @@ public class InteractiveConsole { /** * Run the interactive net builder. */ - public Net runNetBuilder() { + public Net run() { while (true) { try { - var choice = choose("Choose the next step to do:", - "Add a node", "Add a connection", "Print Nodes", "Save the net", "Load net", "Clear", - "Exit"); + var choice = choose(this.net + "\nChoose the next step to do:", + "Add a node", "Add a connection", "Save the net", "Load net", "Clear", "Exit"); switch (choice) { case 1 -> { var node = this.buildNode(); @@ -58,8 +57,9 @@ public class InteractiveConsole { var targetNode = this.net.getNode(target); this.net.addConnection(sourceNode, targetNode, weight); } - case 3 -> this.out.println(this.net); - case 4 -> this.net.save(ask("Enter the filename: ")); + case 3 -> this.net.save(ask("Enter the filename: ")); + case 4 -> this.net = Net.load(ask("Enter the filename: ")); + case 5 -> this.net = new Net(); default -> { this.scanner.close(); return this.net; diff --git a/src/main/java/net/berack/upo/valpre/Main.java b/src/main/java/net/berack/upo/valpre/Main.java index ff0a172..8135468 100644 --- a/src/main/java/net/berack/upo/valpre/Main.java +++ b/src/main/java/net/berack/upo/valpre/Main.java @@ -31,7 +31,7 @@ public class Main { var plot = new Plot(csv); plot.show(); } - case "net" -> new InteractiveConsole().runNetBuilder(); + case "interactive" -> new InteractiveConsole().run(); default -> exit("Invalid program!"); } } catch (Exception e) { @@ -83,11 +83,11 @@ public class Main { var uri = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI(); var name = new File(uri).getName(); System.err.println(message); - System.out.println("Usage: java -jar " + name + ".jar [simulation|plot|net] [args]"); + System.out.println("Usage: java -jar " + name + ".jar [simulation|plot|interactive] [args]"); System.out.println("simulation args: -net [-csv ] [-runs ] [-seed ]" + "[-p] [-end ] [-i ]"); System.out.println("plot args: -csv "); - System.out.println("net args: none"); + System.out.println("interactive: no args needed"); System.exit(1); } catch (URISyntaxException e) { e.printStackTrace(); 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 3f9f2fa..f0f39af 100644 --- a/src/test/java/net/berack/upo/valpre/sim/TestInteractions.java +++ b/src/test/java/net/berack/upo/valpre/sim/TestInteractions.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.OutputStream; import java.io.PrintStream; -import java.util.List; import org.junit.Test; @@ -99,38 +98,50 @@ public class TestInteractions { @Test(timeout = 1000) public void netBuilderInteractive() { - var out = new PrintStream(OutputStream.nullOutputStream()); - var inputs = List.of("5"); - var bytes = String.join("\n", inputs).getBytes(); - var in = new ByteArrayInputStream(bytes); - var net = new InteractiveConsole(out, in).runNetBuilder(); + // Test the interactive console EXIT + var net = runInteraction("6"); assertEquals("", net.toString()); - inputs = List.of("1", "1", "Source", "1", "1.0", "7"); - bytes = String.join("\n", inputs).getBytes(); - in = new ByteArrayInputStream("1\n1\nSource\n1\n1.0\n1000\n5\n".getBytes()); - net = new InteractiveConsole(out, in).runNetBuilder(); + // Test the interactive console ADD NODE + net = runInteraction("1", "1", "Source", "1", "1.0", "6"); assertEquals("Source[servers:1, queue:100, spawn:-1, Exponential(1.0)] -\n", net.toString()); - inputs = List.of("1", "2", "Terminal", "1", "2.0", "500", + // Test the interactive console ADD SECOND NODE + net = runInteraction("1", "2", "Terminal", "1", "2.0", "500", "1", "3", "Queue", "5", "3.2", "0.6", "1", - "7"); - bytes = String.join("\n", inputs).getBytes(); - in = new ByteArrayInputStream(bytes); - net = new InteractiveConsole(out, in).runNetBuilder(); + "6"); 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", + // Test the interactive console ADD CONNECTION + net = runInteraction("1", "1", "Source", "1", "2.0", "1", "3", "Queue", "5", "3.2", "0.6", "1", "2", "Source", "Queue", "1.0", - "7"); - bytes = String.join("\n", inputs).getBytes(); - in = new ByteArrayInputStream(bytes); - net = new InteractiveConsole(out, in).runNetBuilder(); + "6"); 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()); + + // Test the interactive console CLEAR + net = runInteraction("1", "1", "Source", "1", "2.0", + "1", "3", "Queue", "5", "3.2", "0.6", "1", + "2", "Source", "Queue", "1.0", + "2", "Queue", "Queue", "1.0", + "5", "6"); + assertEquals("", net.toString()); + + // Test the interactive console LOAD + net = runInteraction("4", "src/test/resources/example1.net", "6"); + assertEquals("Source[servers:1, queue:100, spawn:10000, Exponential(0.2222222222222222)] -> Queue(1.0)\n" + + "Queue[servers:1, queue:100, spawn:0, NormalBoxMuller(3.2, 0.6)] -\n", net.toString()); + } + + private static Net runInteraction(String... commands) { + var out = new PrintStream(OutputStream.nullOutputStream()); + var inputs = String.join("\n", commands); + var bytes = inputs.getBytes(); + var in = new ByteArrayInputStream(bytes); + return new InteractiveConsole(out, in).run(); } /* @@ -154,10 +165,9 @@ public class TestInteractions { * - Enter the source node: Source * - Enter the target node: Queue * - Enter the weight: 1.0 - * 3. Print Nodes - * 4. Save the net - * 5. Load net - * 6. Clear - * 7. Exit + * 3. Save the net + * 4. Load net + * 5. Clear + * 6. Exit */ } 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 1d73d79..5a2ca09 100644 --- a/src/test/java/net/berack/upo/valpre/sim/TestSaveExamplesNet.java +++ b/src/test/java/net/berack/upo/valpre/sim/TestSaveExamplesNet.java @@ -21,7 +21,7 @@ import net.berack.upo.valpre.sim.stats.NodeStats; public class TestSaveExamplesNet { - private static final String path = "src/main/resources/example%d.%s"; + private static final String path = "src/test/resources/example%d.%s"; private static final String netFile1 = path.formatted(1, "net"); private static final String netFile2 = path.formatted(2, "net"); private static final String csv1 = path.formatted(1, "csv"); diff --git a/src/main/resources/example1.csv b/src/test/resources/example1.csv similarity index 100% rename from src/main/resources/example1.csv rename to src/test/resources/example1.csv diff --git a/src/main/resources/example1.net b/src/test/resources/example1.net similarity index 100% rename from src/main/resources/example1.net rename to src/test/resources/example1.net diff --git a/src/main/resources/example2.csv b/src/test/resources/example2.csv similarity index 100% rename from src/main/resources/example2.csv rename to src/test/resources/example2.csv diff --git a/src/main/resources/example2.net b/src/test/resources/example2.net similarity index 100% rename from src/main/resources/example2.net rename to src/test/resources/example2.net