|
|
@ -459,4 +459,41 @@ public abstract class FiniteAutomaton<Symbol, E extends Edge<Symbol>> implements |
|
|
|
|
|
|
|
|
return result.toString(); |
|
|
return result.toString(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Generate a representation in GraphViz's dot format. |
|
|
|
|
|
* States are black, red when accepting, blue when initial and violet when both. |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public String toDot() { |
|
|
|
|
|
StringBuffer result = new StringBuffer(); |
|
|
|
|
|
result.append("digraph G {\n"); |
|
|
|
|
|
|
|
|
|
|
|
for(State state : states) { |
|
|
|
|
|
String color = "black"; |
|
|
|
|
|
String stateLabel = "\"" + state + "\""; |
|
|
|
|
|
|
|
|
|
|
|
if (isAcceptingState(state)) { color = "red"; } |
|
|
|
|
|
if (isInitialState(state)) { color = "blue"; } |
|
|
|
|
|
if (isAcceptingState(state) && isInitialState(state)) { color = "violet"; } |
|
|
|
|
|
|
|
|
|
|
|
result.append(stateLabel |
|
|
|
|
|
+ "[shape=box, color=" + color + "," |
|
|
|
|
|
+ " label=" + stateLabel + "]\n"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for(Edge<Symbol> edge : edges) { |
|
|
|
|
|
String sourceLabel = "\"" + edge.getSource() + "\""; |
|
|
|
|
|
String symLabel = "\"" + edge.getLabel() + "\""; |
|
|
|
|
|
for(State sink : edge.getSinks()) { |
|
|
|
|
|
String sinkLabel = "\"" + sink + "\""; |
|
|
|
|
|
result.append(sourceLabel |
|
|
|
|
|
+ " -> " + sinkLabel |
|
|
|
|
|
+ "[label=" + symLabel + "];\n"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
result.append("}"); |
|
|
|
|
|
return result.toString(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |