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 nodes; public final Set 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 createMachine(){ Set inputs = new HashSet<>(); for(Edge e : edges){ String[] io = e.label.split("/"); inputs.add(io[0].trim()); } List inputList = Lists.newArrayList(inputs.iterator()); Alphabet alphabet = new HashAlphabet<>(Alphabets.fromList(inputList)); MealyBuilder>.MealyBuilder__1 builder = AutomatonBuilders.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(); } }