|
|
|
@ -172,4 +172,39 @@ public class DeterministicFiniteAutomaton<Symbol> extends FiniteAutomaton<Symbol |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 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"; |
|
|
|
|
|
|
|
if (isAcceptingState(state)) { color = "red"; } |
|
|
|
if (isInitialState(state)) { color = "blue"; } |
|
|
|
if (isAcceptingState(state) && isInitialState(state)) { color = "violet"; } |
|
|
|
|
|
|
|
result.append(state |
|
|
|
+ "[shape=box, color=" + color + "," |
|
|
|
+ " label=" + state + "]\n"); |
|
|
|
} |
|
|
|
|
|
|
|
for(SingleEdge<Symbol> edge : edges) { |
|
|
|
String sourceLabel = edge.getSource().toString(); |
|
|
|
String symLabel = "\"" + edge.getLabel() + "\""; |
|
|
|
State sink = edge.getSink(); |
|
|
|
String sinkLabel = "\"" + sink + "\""; |
|
|
|
result.append(sourceLabel |
|
|
|
+ " -> " + sinkLabel |
|
|
|
+ "[label=" + symLabel + "];\n"); |
|
|
|
} |
|
|
|
|
|
|
|
result.append("}"); |
|
|
|
return result.toString(); |
|
|
|
} |
|
|
|
} |