public static DirectedSparseGraph buildGraph(String fsm,String name)
{
final Map<String,DeterministicVertex> existingVertices = new HashMap<String,DeterministicVertex>();
final Map<StatePair,DeterministicEdge> existingEdges = new HashMap<StatePair,DeterministicEdge>();
final DirectedSparseGraph g = new DirectedSparseGraph();
g.setUserDatum(JUConstants.TITLE, name,UserData.SHARED);
new TestFSMParser.fsmParser(fsm).parse(new TestFSMParser.TransitionReceiver()
{
public void put(String from, String to, String label, boolean accept) {
DeterministicVertex fromVertex = existingVertices.get(from), toVertex = existingVertices.get(to);
if (fromVertex == null)
{
fromVertex = new DeterministicDirectedSparseGraph.DeterministicVertex(from);
if (existingVertices.isEmpty())
fromVertex.addUserDatum(JUConstants.INITIAL, true, UserData.SHARED);
fromVertex.addUserDatum(JUConstants.ACCEPTED, true, UserData.SHARED);
existingVertices.put(from, fromVertex);
g.addVertex(fromVertex);
}
else
if (!Boolean.valueOf(fromVertex.getUserDatum(JUConstants.ACCEPTED).toString()))
throw new IllegalArgumentException("conflicting acceptance assignment on vertex "+from);
if (from.equals(to))
{
if (!accept) throw new IllegalArgumentException("conflicting acceptance assignment on vertex "+to);
toVertex = fromVertex;
}
else
if (toVertex == null)
{
toVertex = new DeterministicDirectedSparseGraph.DeterministicVertex(to);
toVertex.removeUserDatum(JUConstants.ACCEPTED); // in case we've got a reject loop in the same state
toVertex.addUserDatum(JUConstants.ACCEPTED, accept, UserData.SHARED);
existingVertices.put(to, toVertex);
g.addVertex(toVertex);
}
else
if (DeterministicDirectedSparseGraph.isAccept(toVertex) != accept)
throw new IllegalArgumentException("conflicting acceptance assignment on vertex "+to);
StatePair pair = new StatePair(fromVertex,toVertex);
DeterministicEdge edge = existingEdges.get(pair);
if (edge == null)
{
edge = new DeterministicDirectedSparseGraph.DeterministicEdge(fromVertex,toVertex);
edge.addUserDatum(JUConstants.LABEL, new HashSet<String>(), UserData.CLONE);
g.addEdge(edge);existingEdges.put(pair,edge);
}
Set<String> labels = (Set<String>)edge.getUserDatum(JUConstants.LABEL);
labels.add(label);
}