Ejemplo n.º 1
0
            // returns a list of statements because we may need to declare node attribute statments
            // or other things to describe the WME
            public IEnumerable <Statement> GetStatement()
            {
                List <Statement> statements = new List <Statement>();

                if (IsIdentifier())
                {
                    // add an edge statement
                    EdgeStatement statement = EdgeStatement.EdgeBetweenNodes(ID, value);
                    statement.attributes.Add(new StringAttribute("label", attribute.Replace("-", "_")));
                    statements.Add(statement);
                }
                else
                {
                    // get a new id for the value
                    string newID = getNewID();
                    // make edge statement
                    EdgeStatement statement = EdgeStatement.EdgeBetweenNodes(ID, newID);
                    statement.attributes.Add(new StringAttribute("label", attribute.Replace("-", "_")));
                    statements.Add(statement);
                    // add a node attribute statement to label the child
                    NodeStatement nodeStatement = new NodeStatement(newID);
                    nodeStatement.attributes.Add(new StringAttribute("label", value.Replace("-", "_")));
                    statements.Add(nodeStatement);
                }
                return(statements);
            }
Ejemplo n.º 2
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);
            }
        }
Ejemplo n.º 3
0
        private void AddChildren(ref List <IWriteTo> statements, IUIPerson person, IUIPerson partner, NodeStatement point, IEnumerable <IUIPerson> childrenOfCouple)
        {
            childrenOfCouple = childrenOfCouple.Where(x => !x.IsHidden);
            var childrenSubgraphStatements = new List <IWriteTo>();

            foreach (var child in childrenOfCouple)
            {
                var childNodeDict = new Dictionary <Id, Id> {
                    { "label", child.Label }, { "shape", "box" }, { "color", child.IsFemale ? "pink" : "blue" }, { "fillcolor", "white" }, { "style", "filled" }
                }.ToImmutableDictionary();
                var childNode = new NodeStatement(child.Id.ToString(), childNodeDict);

                var childToDotDict = new Dictionary <Id, Id> {
                    { "dir", "none" }
                }.ToImmutableDictionary();
                var childToDotStatement = new EdgeStatement(point.Id.Value, child.Id.ToString(), childToDotDict);

                childrenSubgraphStatements.Add(childNode);

                statements.Add(childToDotStatement);
            }

            var childrenSubgraphStatement = new SubgraphStatement("children" + (person.Id + "z" + partner.Id), childrenSubgraphStatements.ToImmutableList());

            statements.Add(childrenSubgraphStatement);
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 6
0
        private static void AddEdgeStatement(ref List <Statement> statements, int origin, int destination)
        {
            var edge = new EdgeStatement(new NodeId(Convert.ToString(origin)),
                                         new NodeId(Convert.ToString(destination)),
                                         emptyDictionary);

            statements.Add(edge);
        }
Ejemplo n.º 7
0
        private void AddPartner(ref List <IWriteTo> statements, IUIPerson person, NodeStatement personNode, IUIPerson partner)
        {
            if (partner.IsHidden)
            {
                return;
            }

            var edgeDictPoint = new Dictionary <Id, Id> {
                { "shape", "point" }, { "label", "" }
            }.ToImmutableDictionary();
            var point = new NodeStatement("partners" + (partner.Id + "z" + person.Id), edgeDictPoint);

            var partnerNodeDict = new Dictionary <Id, Id> {
                { "label", partner.Label }, { "shape", "box" }, { "color", partner.IsFemale ? "pink" : "blue" }, { "fillcolor", "white" }, { "style", "filled" }
            }.ToImmutableDictionary();
            var partnerNode = new NodeStatement(partner.Id.ToString(), partnerNodeDict);

            var edgeDictLinePersonToPoint = new Dictionary <Id, Id> {
                { "dir", "none" }
            }.ToImmutableDictionary();
            var linePersonToPoint = new EdgeStatement(person.Id.ToString(), point.Id.ToString(), edgeDictLinePersonToPoint);

            var linePartnerToPointDict = new Dictionary <Id, Id> {
                { "dir", "none" }
            }.ToImmutableDictionary();
            var linePartnerToPoint = new EdgeStatement(partner.Id.ToString(), point.Id.ToString(), linePartnerToPointDict);

            var parentSubgraphExists = statements.Where(x => x is SubgraphStatement).Select(y => ((SubgraphStatement)y).Name).Any(z => z.Value == "partners" + (partner.Id + "z" + person.Id) || z.Value == "partners" + (person.Id + "z" + partner.Id));

            if (!parentSubgraphExists)
            {
                var parentSubgraphStatements = new List <IWriteTo>();

                parentSubgraphStatements.Add(point);

                parentSubgraphStatements.Add(partnerNode);

                parentSubgraphStatements.Add(personNode);

                parentSubgraphStatements.Add(linePersonToPoint);

                parentSubgraphStatements.Add(linePartnerToPoint);

                var parentSubgraphStatement = new SubgraphStatement(point.Id.ToString(), parentSubgraphStatements.ToImmutableList());
                statements.Add(parentSubgraphStatement);

                var childrenSubgraphExists = statements.Where(x => x is SubgraphStatement).Select(y => ((SubgraphStatement)y).Name).Any(z => z.Value == "children" + (partner.Id + "z" + person.Id) || z.Value == "children" + (person.Id + "z" + partner.Id));
                if (!childrenSubgraphExists)
                {
                    var childrenOfCouple = person.Children.Where(x => x.Parents.Contains(partner) && x.Parents.Contains(person)).Where(x => !x.IsHidden);
                    childrenOfCouple = childrenOfCouple.OrderByDescending(x => x.GetAllDescendants().Count);
                    this.AddChildren(ref statements, person, partner, point, childrenOfCouple);
                }
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
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;

            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);
            }
        }
Ejemplo n.º 10
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));
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        public static void Test1()
        {
            Graph graph = new Graph(Graph.EGraphType.GT_Digraph, "hellograph");

            // add edge

            /*EdgeStatement edgeStatement = new EdgeStatement();
             * edgeStatement.nodeID = new NodeStatement.NodeID("A");
             * edgeStatement.rhs = new EdgeStatement.EdgeRHS();
             * edgeStatement.rhs.nodeID = new NodeStatement.NodeID("B");
             * graph.statements.Add(edgeStatement);*/
            graph.statements.Add(EdgeStatement.EdgeBetweenNodes("A", "B"));

            string result = graph.Render();

            // display result
            Console.Write(result);

            System.IO.StreamWriter writer = new System.IO.StreamWriter("graph.txt");
            writer.Write(result);
            writer.Close();

            Console.ReadKey();
        }
Ejemplo n.º 13
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);
                    }
                }
            }
        }
Ejemplo n.º 14
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));
            }
        }