Add example network files and update interactive console commands for improved usability

This commit is contained in:
2025-03-16 16:32:11 +01:00
parent 6cb87be89b
commit 60ef40c0ab
9 changed files with 46 additions and 40 deletions

View File

@@ -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: 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.\ 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. Una volta scelta la rete è necessario salvarla in un file per la successiva simulazione e analisi.
* `java -jar upo-valpre.jar simulation -net <file> [other]`\ * `java -jar upo-valpre.jar simulation -net <file> [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:\ 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) ![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 ### 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. 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.

View File

@@ -39,12 +39,11 @@ public class InteractiveConsole {
/** /**
* Run the interactive net builder. * Run the interactive net builder.
*/ */
public Net runNetBuilder() { public Net run() {
while (true) { while (true) {
try { try {
var choice = choose("Choose the next step to do:", var choice = choose(this.net + "\nChoose the next step to do:",
"Add a node", "Add a connection", "Print Nodes", "Save the net", "Load net", "Clear", "Add a node", "Add a connection", "Save the net", "Load net", "Clear", "Exit");
"Exit");
switch (choice) { switch (choice) {
case 1 -> { case 1 -> {
var node = this.buildNode(); var node = this.buildNode();
@@ -58,8 +57,9 @@ public class InteractiveConsole {
var targetNode = this.net.getNode(target); var targetNode = this.net.getNode(target);
this.net.addConnection(sourceNode, targetNode, weight); this.net.addConnection(sourceNode, targetNode, weight);
} }
case 3 -> this.out.println(this.net); case 3 -> this.net.save(ask("Enter the filename: "));
case 4 -> this.net.save(ask("Enter the filename: ")); case 4 -> this.net = Net.load(ask("Enter the filename: "));
case 5 -> this.net = new Net();
default -> { default -> {
this.scanner.close(); this.scanner.close();
return this.net; return this.net;

View File

@@ -31,7 +31,7 @@ public class Main {
var plot = new Plot(csv); var plot = new Plot(csv);
plot.show(); plot.show();
} }
case "net" -> new InteractiveConsole().runNetBuilder(); case "interactive" -> new InteractiveConsole().run();
default -> exit("Invalid program!"); default -> exit("Invalid program!");
} }
} catch (Exception e) { } catch (Exception e) {
@@ -83,11 +83,11 @@ public class Main {
var uri = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI(); var uri = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI();
var name = new File(uri).getName(); var name = new File(uri).getName();
System.err.println(message); 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 <net> [-csv <csv>] [-runs <runs>] [-seed <seed>]" System.out.println("simulation args: -net <net> [-csv <csv>] [-runs <runs>] [-seed <seed>]"
+ "[-p] [-end <end>] [-i <indices>]"); + "[-p] [-end <end>] [-i <indices>]");
System.out.println("plot args: -csv <csv>"); System.out.println("plot args: -csv <csv>");
System.out.println("net args: none"); System.out.println("interactive: no args needed");
System.exit(1); System.exit(1);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -5,7 +5,6 @@ import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.List;
import org.junit.Test; import org.junit.Test;
@@ -99,38 +98,50 @@ public class TestInteractions {
@Test(timeout = 1000) @Test(timeout = 1000)
public void netBuilderInteractive() { public void netBuilderInteractive() {
var out = new PrintStream(OutputStream.nullOutputStream());
var inputs = List.of("5"); // Test the interactive console EXIT
var bytes = String.join("\n", inputs).getBytes(); var net = runInteraction("6");
var in = new ByteArrayInputStream(bytes);
var net = new InteractiveConsole(out, in).runNetBuilder();
assertEquals("", net.toString()); assertEquals("", net.toString());
inputs = List.of("1", "1", "Source", "1", "1.0", "7"); // Test the interactive console ADD NODE
bytes = String.join("\n", inputs).getBytes(); net = runInteraction("1", "1", "Source", "1", "1.0", "6");
in = new ByteArrayInputStream("1\n1\nSource\n1\n1.0\n1000\n5\n".getBytes());
net = new InteractiveConsole(out, in).runNetBuilder();
assertEquals("Source[servers:1, queue:100, spawn:-1, Exponential(1.0)] -\n", net.toString()); 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", "1", "3", "Queue", "5", "3.2", "0.6", "1",
"7"); "6");
bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream(bytes);
net = new InteractiveConsole(out, in).runNetBuilder();
assertEquals("Terminal[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", // Test the interactive console ADD CONNECTION
net = runInteraction("1", "1", "Source", "1", "2.0",
"1", "3", "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",
"7"); "6");
bytes = String.join("\n", inputs).getBytes();
in = new ByteArrayInputStream(bytes);
net = new InteractiveConsole(out, in).runNetBuilder();
assertEquals("Source[servers:1, queue:100, spawn:-1, 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());
// 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 source node: Source
* - Enter the target node: Queue * - Enter the target node: Queue
* - Enter the weight: 1.0 * - Enter the weight: 1.0
* 3. Print Nodes * 3. Save the net
* 4. Save the net * 4. Load net
* 5. Load net * 5. Clear
* 6. Clear * 6. Exit
* 7. Exit
*/ */
} }

View File

@@ -21,7 +21,7 @@ import net.berack.upo.valpre.sim.stats.NodeStats;
public class TestSaveExamplesNet { 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 netFile1 = path.formatted(1, "net");
private static final String netFile2 = path.formatted(2, "net"); private static final String netFile2 = path.formatted(2, "net");
private static final String csv1 = path.formatted(1, "csv"); private static final String csv1 = path.formatted(1, "csv");

View File

Can't render this file because it is too large.