Graph implements GraphInterface { static public Graph fromDotty(File dottyFile) throws FileNotFoundException, Exception { Parser p = new Parser(new FileInputStream(dottyFile), System.err); p.parse(); att.grappa.Graph dottyGraph = p.getGraph(); att.grappa.Node[] nodes = dottyGraph.nodeElementsAsArray(); Graph g = new Graph(); Map<att.grappa.Node, Node> nodemap = new HashMap<att.grappa.Node, Node>(); for (att.grappa.Node n: nodes) { Node nn = new Node(n.getName()); Iterator<att.grappa.Attribute> itAttr = n.getLocalAttributePairs(); while (itAttr.hasNext()) { att.grappa.Attribute attr = (att.grappa.Attribute) itAttr.next(); nn.setAttribute(attr.getName(), attr.getStringValue()); } nodemap.put(n, nn); g.addNode(nn); } for (att.grappa.Node n: nodes) { Node N = nodemap.get(n); Iterator<att.grappa.Edge> itEdge = n.inEdgeElements(); while (itEdge.hasNext()) { att.grappa.Edge e = (att.grappa.Edge) itEdge.next(); Edge ee = new Edge(nodemap.get(e.getTail()), nodemap.get(e.getHead())); Iterator<att.grappa.Attribute> itAttr = e.getLocalAttributePairs(); while (itAttr.hasNext()) { att.grappa.Attribute attr = (att.grappa.Attribute) itAttr.next(); ee.setAttribute(attr.getName(), attr.getStringValue()); } N.addInEdge(ee); } itEdge = n.outEdgeElements(); while (itEdge.hasNext()) { att.grappa.Edge e = (att.grappa.Edge) itEdge.next(); Edge ee = new Edge(nodemap.get(e.getTail()), nodemap.get(e.getHead())); Iterator<att.grappa.Attribute> itAttr = e.getLocalAttributePairs(); while (itAttr.hasNext()) { att.grappa.Attribute attr = (att.grappa.Attribute) itAttr.next(); ee.setAttribute(attr.getName(), attr.getStringValue()); } N.addOutEdge(ee); } } return g; } Graph(); static Graph fromDotty(File dottyFile); void addNode(NodeInterface n); Set<NodeInterface> getAllNodes(); Set<EdgeInterface> getAllEdges(); @Override String toString(); @Override String toDot(); String toXML(); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public final void testFromDotty() { Graph g = null; try { g = Graph.fromDotty(new File("resources/testGraph.dotty")); } catch (FileNotFoundException e) { fail("File not found"); } catch (Exception e) { e.printStackTrace(); fail("Exception thrown"); } System.out.println(testGraph.toDot()); System.out.println(g.toDot()); assertTrue(testGraph.equals(g)); }