예제 #1
0
        /// <summary>
        /// Iterate over graph to create graphviz visualization.
        /// </summary>
        private void DFSSearch(BinaryTreeNode parent)
        {
            var parentId = parent.Content + " " + counter;

            if (parent.Id == 0)
            {
                parent.Id = counter;
            }

            parent.Visited = true;

            counter++;
            if (parent.Left != null && !parent.Left.Visited)
            {
                var leftId = parent.Left.Content + " " + counter;

                parent.Left.Id = counter;
                graphVizEdges.Add(EdgeStatement.For(parentId, leftId));
                DFSSearch(parent.Left);
            }
            if (parent.Right != null && !parent.Right.Visited)
            {
                var rightId = parent.Right.Content + " " + counter;

                parent.Right.Id = counter;
                graphVizEdges.Add(EdgeStatement.For(parentId, rightId));
                DFSSearch(parent.Right);
            }
        }
예제 #2
0
        private void DrawNode(SyntaxNode node)
        {
            var nodeStatement = CreateNode(node);

            _graph = _graph.Add(nodeStatement);

            foreach (var childNode in node.ChildNodes)
            {
                DrawNode(childNode);

                _graph = _graph.Add(EdgeStatement.For(_idsDict[node], _idsDict[childNode]));
            }
            //Set all children on same level
            if (node.ChildNodes.Count > 2)
            {
                _graph = _graph.Add(new SameRankStatement(node.ChildNodes.ConvertAll(snode => _idsDict[snode])));
            }

            if (node.Sibling == null)
            {
                return;
            }
            DrawNode(node.Sibling);

            _graph = _graph.Add(EdgeStatement.For(_idsDict[node], _idsDict[node.Sibling]).Set("headport", "w").Set("tailport", "e"));
            _graph = _graph.Add(new SameRankStatement(new List <NodeId> {
                _idsDict[node], _idsDict[node.Sibling]
            }));
        }
        public void BasicTest()
        {
            Graph graph = Graph.Undirected
                          .Add(EdgeStatement.For("a", "b"))
                          .Add(EdgeStatement.For("b", "c"))
                          .Add(EdgeStatement.For("a", "a"));
            string    graphVizBin = @"C:\Program Files (x86)\Graphviz2.38\bin";
            IRenderer renderer    = new Renderer(graphVizBin);

            var    basedir   = AppDomain.CurrentDomain.BaseDirectory;
            string directory = basedir + @"\GRaphs\";
            string fileName  = @"balancedBoundingTreeTests.RotateLeft.png";

            Directory.CreateDirectory(directory);
            using (Stream file = File.Create(directory + fileName))
            {
                var task = renderer.RunAsync(
                    graph, file,
                    RendererLayouts.Dot,
                    RendererFormats.Png,
                    CancellationToken.None);

                task.Wait();
                Console.WriteLine("file://" + directory + fileName);

                Assert.IsTrue(task.IsCompleted);
                Assert.IsTrue(task.Exception == null);
            }
        }
예제 #4
0
        public async Task CancellationWorks()
        {
            var cts = new CancellationTokenSource();

            cts.Cancel();
            var graph = Graph.Undirected
                        .Add(EdgeStatement.For("a", "b"))
                        .Add(EdgeStatement.For("a", "c"));

            using (var outputStream = new MemoryStream())
            {
                await Renderer.RunAsync(graph, outputStream, RendererLayouts.Dot, RendererFormats.Png, cts.Token);
            }
        }
예제 #5
0
파일: Parser.cs 프로젝트: snaekier/CompiB
        /// <summary>
        /// Iterate over graph to create graphviz visualization.
        /// </summary>
        private void DFSSearch(BinaryTreeNode parent)
        {
            var parentId = parent.Content + " " + counter;


            if (parent.Id == 0)
            {
                parent.Id = counter;
            }

            parent.Visited = true;

            var parentNode = Shields.GraphViz.Models.NodeStatement.For(parentId);

            parentNode = parentNode.Set("label", parent.Content);
            graphVizNodes.Add(parentNode);


            counter++;
            if (parent.Left != null && !parent.Left.Visited)
            {
                var leftId = parent.Left.Content + " " + counter;

                parent.Left.Id = counter;

                var leftNode = Shields.GraphViz.Models.NodeStatement.For(leftId);
                leftNode = leftNode.Set("label", parent.Left.Content);
                graphVizNodes.Add(leftNode);


                graphVizEdges.Add(EdgeStatement.For(parentId, leftId));
                DFSSearch(parent.Left);
            }
            if (parent.Right != null && !parent.Right.Visited)
            {
                var rightId = parent.Right.Content + " " + counter;

                parent.Right.Id = counter;

                var rightNode = Shields.GraphViz.Models.NodeStatement.For(rightId);
                rightNode = rightNode.Set("label", parent.Right.Content);

                graphVizNodes.Add(rightNode);

                graphVizEdges.Add(EdgeStatement.For(parentId, rightId));
                DFSSearch(parent.Right);
            }
        }
예제 #6
0
        //[TestMethod]
        public async Task DotProducesCorrectPng()
        {
            var graph = Graph.Undirected
                        .Add(EdgeStatement.For("a", "b"))
                        .Add(EdgeStatement.For("a", "c"));

            using (var outputStream = new MemoryStream())
            {
                await Renderer.RunAsync(graph, outputStream, RendererLayouts.Dot, RendererFormats.Png, CancellationToken.None);

                var output         = outputStream.ToArray();
                var expectedOutput = await ReadAllBytesAsync(Assembly.GetExecutingAssembly().GetManifestResourceStream("Shields.GraphViz.Tests.Resources.Graph1.png"));

                Assert.IsTrue(output.SequenceEqual(expectedOutput));
            }
        }
예제 #7
0
        private static Graph AddToGraph <TBounds, TPayload>(this BalancedBoundingTree <TBounds, TPayload> .BalancedBoundingNode node, Graph graph, int depth)
            where TBounds : IComparable <TBounds>, IEquatable <TBounds>
        {
            if (!ReferenceEquals(node.LowerTree, BalancedBoundingTree <TBounds, TPayload> .Nil))
            {
                graph = graph.Add(EdgeStatement.For(node.ToString(), node.LowerTree.ToString()));
                if (depth > 0)
                {
                    graph = node.LowerTree.AddToGraph(graph, depth - 1);
                }
            }

            if (!ReferenceEquals(node.UpperTree, BalancedBoundingTree <TBounds, TPayload> .Nil))
            {
                graph = graph.Add(EdgeStatement.For(node.ToString(), node.UpperTree.ToString()));
                if (depth > 0)
                {
                    graph = node.UpperTree.AddToGraph(graph, depth - 1);
                }
            }

            return(graph);
        }
예제 #8
0
        private void ConvertButton_Click(object sender, EventArgs e)
        {
            statements.Clear();

            if (InputBox.Text == "")
            {
                return;
            }
            if (vanExpressie.Checked)
            {
                try
                {
                    Expressie expressie = new Expressie(InputBox.Text);
                    _outputNDFA = expressie.ToNDFA();

                    if (ToDFA.Checked)
                    {
                        OutputBox.Text = _outputNDFA.ToDFA().ToString();
                    }
                    if (ToNDFA.Checked)
                    {
                        OutputBox.Text = _outputNDFA.ToString();
                    }
                    if (ToReguliereGrammatica.Checked)
                    {
                        OutputBox.Text = _outputNDFA.ToReguliereGrammatica().ToString();
                    }

                    foreach (var t in _outputNDFA.eindToestanden)
                    {
                        //output += "node [shape = doublecircle]; " + t + " ;\n";
                    }

                    foreach (var t in _outputNDFA.toestanden)
                    {
                        EdgeStatement statement = EdgeStatement.For(t.vorigeToestand, t.volgendeToestand.Item1).Set("label", t.volgendeToestand.Item2.ToString());
                        statements.Add(statement);
                    }
                }
                catch (Exception exception)
                {
                    OutputBox.Text += "Het is niet gelukt\n" + exception.ToString();
                }
            }
            else if (vanDFA.Checked)
            {
                NDFA <char> ndfa = new NDFA <char>();
                for (int x = 0; x < InputBox.Lines.Count(); x++)
                {
                    string temp = InputBox.Lines[x];
                    if (temp.StartsWith("begin"))
                    {
                        ndfa.startSymbolen.Add(temp.Last().ToString());
                    }
                    else if (temp.StartsWith("eind"))
                    {
                        ndfa.eindToestanden.Add(temp.Last().ToString());
                    }
                    else
                    {
                        ndfa.toestanden.Add(Toestand <char> .CreateToestand(temp));
                    }
                    foreach (var t in ndfa.toestanden)
                    {
                        ndfa.invoerSymbolen.Add(t.volgendeToestand.Item2);
                    }
                }
                if (ToDFA.Checked)
                {
                    OutputBox.Text = ndfa.ToDFA().ToString();
                }
                if (ToNDFA.Checked)
                {
                    OutputBox.Text = ndfa.ToString();
                }
                if (ToReguliereGrammatica.Checked)
                {
                    OutputBox.Text = ndfa.ToReguliereGrammatica().ToString();
                }
                _outputNDFA = ndfa;

                foreach (var t in ndfa.toestanden)
                {
                    EdgeStatement statement = EdgeStatement.For(t.vorigeToestand, t.volgendeToestand.Item1).Set("label", t.volgendeToestand.Item2.ToString());
                    statements.Add(statement);
                }
            }
            else if (vanGrammatica.Checked)
            {
                if (ToNDFA.Checked)
                {
                    HashSet <ProductieRegel <char> > set = new HashSet <ProductieRegel <char> >();

                    for (int x = 0; x < InputBox.Lines.Count(); x++)
                    {
                        string[] temp         = InputBox.Lines[x].Split('-');
                        string   startSymbool = temp[0];
                        string   line         = temp[2].Substring(1);
                        string[] toestanden   = line.Split('|');

                        foreach (string s in toestanden)
                        {
                            string[] camelcase = SplitCamelCase(s);

                            if (camelcase.Length > 1)
                            {
                                set.Add(new ProductieRegel <char>(startSymbool, Convert.ToChar(camelcase[0]), camelcase[1]));
                            }
                            else
                            {
                                set.Add(new ProductieRegel <char>(startSymbool, Convert.ToChar(camelcase[0]), ""));
                            }
                        }
                    }

                    Grammatica <char> gr = new Grammatica <char>(InputBox.Lines.First().Split('-')[0], set);

                    OutputBox.Text = gr.TransformToNDFA().ToString();

                    foreach (var t in gr.TransformToNDFA().toestanden)
                    {
                        EdgeStatement statement = EdgeStatement.For(t.vorigeToestand, t.volgendeToestand.Item1).Set("label", t.volgendeToestand.Item2.ToString());
                        statements.Add(statement);
                    }
                }
            }
        }
예제 #9
0
        static void PlotBrackets(IAsyncComparator <T> comparator, Node topMostRoot)
        {
            Graph graph = Graph.Directed;

            Dictionary <Node, NodeId> nodeIdCache = new Dictionary <Node, NodeId>();
            int bracketCounter = 0;

            string NodeToString(Node node)
            {
                if (node is LeafNode)
                {
                    return(((LeafNode)node).Item.ToString());
                }
                else
                {
                    return($"B{++bracketCounter}");
                }
            }

            NodeId NodeToNodeId(Node node)
            {
                if (!nodeIdCache.ContainsKey(node))
                {
                    nodeIdCache[node] = new NodeId(NodeToString(node));
                }
                return(nodeIdCache[node]);
            }

            void Recurse(Node root)
            {
                if (root is BracketNode)
                {
                    BracketNode node = root as BracketNode;

                    Debug.Assert(node.CompetitorA.Parent == node);
                    Debug.Assert(node.CompetitorB.Parent == node);

                    graph = graph.Add(EdgeStatement.For(
                                          NodeToNodeId(node),
                                          NodeToNodeId(node.CompetitorA)));
                    graph = graph.Add(EdgeStatement.For(
                                          NodeToNodeId(node),
                                          NodeToNodeId(node.CompetitorB)));

                    Recurse(node.CompetitorA);
                    Recurse(node.CompetitorB);
                }
            }

            Recurse(topMostRoot);

            IRenderer renderer = new Renderer(@"C:\Program Files (x86)\Graphviz2.38\bin");

            using (Stream file = File.Create("graph.png"))
            {
                AsyncHelper.RunSync(async() => await renderer.RunAsync(
                                        graph, file,
                                        RendererLayouts.Dot,
                                        RendererFormats.Png,
                                        CancellationToken.None));
            }
        }