private void printVertex(GraphComposite<string, Attributed> c)
        {
            string nodeId;
            string indent = "";

            if (_useHashCodeForId)
                nodeId = Convert.ToString(c.GetHashCode());
            else
                nodeId = c.value.attribs["label"];

            for (int i = 0; i < _depth; i++)
                indent += "   ";

            foreach (GraphComposite<string, Attributed> dest in c.outgoing)
                _discoveredEdges.Add(
                    new KeyValuePair<GraphComposite<string, Attributed>, GraphComposite<string, Attributed>>(c, dest));

            if (!c.IsGraph)
                _sw.WriteLine(indent + nodeId + "[label=\"" + convertLabelForDotty(c.value.attribs["label"]) + "\"];");
        }
        private void openGraphSection(GraphComposite<string, Attributed> c)
        {
            string id;
            string val;
            string indent = "";

            if (_useHashCodeForId)
                val = Convert.ToString(c.GetHashCode());
            else
                val = c.value.attribs["label"];

            for (int i = 0; i < _depth; i++)
                indent += "   ";

            if (_depth == 0)
            {
                _sw.WriteLine("digraph cluster_" + val + " {");
                _sw.WriteLine("   compound=true;");
                foreach (string key in _mainGraphNodeAttribs.attribs.Keys)
                    _sw.WriteLine(indent + "   node [" + key + "=" + _mainGraphNodeAttribs[key] + "];");
                foreach (string key in _mainGraphAttribs.attribs.Keys)
                    _sw.WriteLine(indent + "   " + key + "=" + _mainGraphAttribs[key] + ";");
            }
            else
            {
                _sw.WriteLine(indent + "subgraph cluster_" + val + " {");
                foreach (string key in _subgraphNodeAttribs.attribs.Keys)
                    _sw.WriteLine(indent + "   node [" + key + "=" + _subgraphNodeAttribs[key] + "];");
                foreach (string key in _subgraphAttribs.attribs.Keys)
                    _sw.WriteLine(indent + "   " + key + "=" + _subgraphAttribs[key] + ";");
            }

            _sw.WriteLine(indent + "   " + "label=\"" + convertLabelForDotty(c.value.attribs["label"]) + "\";");
            // add a dotty node that will act as the edge snap-point for this subgraph
            _sw.WriteLine(indent + "   " + val + " [style=invis];");

            _depth++;
        }
        private void closeGraphSection(GraphComposite<string, Attributed> c)
        {
            _depth--;

            // if we're closing the main graph, print all the edges
            if (_depth == 0)
            {
                foreach (KeyValuePair<GraphComposite<string, Attributed>, GraphComposite<string, Attributed>> edge
                         in _discoveredEdges)
                {
                    string start, end;
                    if (_useHashCodeForId)
                    {
                        start = Convert.ToString(edge.Key.GetHashCode());
                        end = Convert.ToString(edge.Value.GetHashCode());
                    }
                    else
                    {
                        start = edge.Key.value.attribs["label"];
                        end = edge.Value.value.attribs["label"];
                    }
                    _sw.WriteLine("   " + start + " -> " + end + addTailHeadInfoForEdge(edge.Key, edge.Value) + ";");
                }
            }

            string indent = "";
            for (int i = 0; i < _depth; i++)
                indent += "   ";

            _sw.WriteLine(indent + "}");
        }
        private string addTailHeadInfoForEdge(GraphComposite<string, Attributed> start, GraphComposite<string, Attributed> end)
        {
            string result = "";
            string c1Id, c2Id;

            if (_useHashCodeForId)
            {
                c1Id = Convert.ToString(start.GetHashCode());
                c2Id = Convert.ToString(end.GetHashCode());
            }
            else
            {
                c1Id = start.value.attribs["label"];
                c2Id = end.value.attribs["label"];
            }

            if (start.IsGraph)
            {
                result += " [ltail=cluster_" + c1Id;
                if (end.IsGraph)
                    result += ", lhead=cluster_" + c2Id;
                result += "]";
            }
            else if (end.IsGraph)
            {
                result += " [lhead=cluster_" + c2Id + "]";
            }
            return result;
        }
        public void dottyPrint(GraphComposite<string, Attributed> c, string filePath)
        {
            if (File.Exists(filePath))
                File.Delete(filePath);
            _sw = File.CreateText(filePath);

            _depth = 0;
            _discoveredEdges.Clear();

            GraphCompositeTraversal<string, Attributed>.traverseSubgraphs(c, printVertex, openGraphSection, closeGraphSection);

            _sw.Close();
        }
 public static void TraverSubgraphsClassInitialize(TestContext tc)
 {
     GraphCompositeBuilder<int, string> gcb = new GraphCompositeBuilder<int, string>(EqualityComparer<int>.Default, 0, "root");
     gcb.AddNode(0, 1, "apple", true);
     gcb.AddNode(1, 2, "orange", false);
     gcb.AddNode(0, 3, "kiwi", true);
     gcb.AddNode(3, 4, "potato", false);
     gcb.AddNode(3, 5, "carrot", false);
     gcb.AddEdge(1, 2);
     gcb.AddEdge(3, 4);
     gcb.AddEdge(3, 5);
     gcb.AddEdge(4, 5);
     gc = gcb.GenerateCopy();
 }