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:
* `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 <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:\
![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.

View File

@@ -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;

View File

@@ -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 <net> [-csv <csv>] [-runs <runs>] [-seed <seed>]"
+ "[-p] [-end <end>] [-i <indices>]");
System.out.println("plot args: -csv <csv>");
System.out.println("net args: none");
System.out.println("interactive: no args needed");
System.exit(1);
} catch (URISyntaxException e) {
e.printStackTrace();

View File

@@ -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
*/
}

View File

@@ -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");

View File

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