summaryrefslogtreecommitdiff
path: root/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java
diff options
context:
space:
mode:
authorRené den Hertog2017-10-26 17:34:51 +0200
committerRené den Hertog2017-10-26 17:34:51 +0200
commitac914c719d401c44cb2633127952b022bf563ff3 (patch)
treef7d2399b2ab6cddb86aa996096581a397ff407b9 /assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java
parentFinish assignment 2, part 1 (diff)
'Bounded Retransmission Protocol' Blob
Diffstat (limited to 'assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java')
-rwxr-xr-xassignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java101
1 files changed, 101 insertions, 0 deletions
diff --git a/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java b/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java
new file mode 100755
index 0000000..49c29c7
--- /dev/null
+++ b/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/brpcompare/GraphvizParser.java
@@ -0,0 +1,101 @@
+package brpcompare;
+
+import com.google.common.collect.Lists;
+import net.automatalib.automata.transout.impl.compact.CompactMealy;
+import net.automatalib.util.automata.builders.AutomatonBuilders;
+import net.automatalib.util.automata.builders.MealyBuilder;
+import net.automatalib.words.Alphabet;
+import net.automatalib.words.impl.Alphabets;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.*;
+
+/**
+ * A class which is just able to parse the output generated by LearnLib (why is this not included in LearnLib itself?
+ * I have no clue.) It is *not* a general graphviz parser, it only parses the things I needed for the mealy machines.
+ */
+public class GraphvizParser {
+ public static class Edge {
+ public final String from;
+ public final String to;
+ public final String label;
+ Edge(String b, String e, String l){
+ from = b;
+ to = e;
+ label = l;
+ }
+ }
+
+ public final Set<String> nodes;
+ public final Set<Edge> edges;
+ public String initialState;
+
+ GraphvizParser(Path filename) throws IOException {
+ File file = new File(filename.toString());
+ if (!file.exists()) {
+ throw new RuntimeException("File '" + filename + "' does not exist");
+ }
+ if (file.isDirectory()) {
+ throw new RuntimeException("File " + filename + "' is a directory");
+ }
+
+ nodes = new HashSet<>();
+ edges = new HashSet<>();
+
+ Scanner s = new Scanner(filename);
+ while(s.hasNextLine()){
+ String line = s.nextLine();
+
+ if(!line.contains("label")) continue;
+
+ if(line.contains("->")){
+ int e1 = line.indexOf('-');
+ int e2 = line.indexOf('[');
+ int b3 = line.indexOf('"');
+ int e3 = line.lastIndexOf('"');
+
+ String from = line.substring(0, e1).trim();
+ String to = line.substring(e1+2, e2).trim();
+ String label = line.substring(b3+1, e3).trim();
+
+ // First read state will be the initial one.
+ if(initialState == null) initialState = from;
+
+ nodes.add(from);
+ nodes.add(to);
+ edges.add(new Edge(from, to, label));
+ } else {
+ int end = line.indexOf('[');
+ if(end <= 0) continue;
+ String node = line.substring(0, end).trim();
+
+ nodes.add(node);
+ }
+ }
+ }
+
+ CompactMealy<String, String> createMachine(){
+ Set<String> inputs = new HashSet<>();
+ for(Edge e : edges){
+ String[] io = e.label.split("/");
+ inputs.add(io[0].trim());
+ }
+
+ List<String> inputList = Lists.newArrayList(inputs.iterator());
+ Alphabet<String> alphabet = new HashAlphabet<>(Alphabets.fromList(inputList));
+
+ MealyBuilder<?, String, ?, String, CompactMealy<String, String>>.MealyBuilder__1 builder = AutomatonBuilders.<String, String>newMealy(alphabet).withInitial(initialState);
+
+ for(Edge e : edges){
+ String[] io = e.label.split("/");
+
+ builder.from(e.from).on(io[0].trim()).withOutput(io[1].trim()).to(e.to);
+ }
+
+ return builder.create();
+ }
+
+}
+