package yannakakis; 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.IOException; import java.nio.file.Path; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * 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 String from; public String to; public String label; Edge(String b, String e, String l){ from = b; to = e; label = l; } } public Set nodes; public Set edges; GraphvizParser(Path filename) throws IOException { 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(); 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(GraphvizParser.Edge e : edges){ String[] io = e.label.split("/"); inputs.add(io[0].trim()); } List inputList = Lists.newArrayList(inputs.iterator()); Alphabet alphabet = Alphabets.fromList(inputList); MealyBuilder>.MealyBuilder__1 builder = AutomatonBuilders.newMealy(alphabet).withInitial("s0"); for(GraphvizParser.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(); } }