Beispiel #1
0
        private static string WriteLiteralNode(ILiteralNode literalnode, Triple t, BaseWriterContext context, bool collapseLiterals)
        {
            // Example output:
            //     "h" [label = "v", shape = box];
            // where h is the hash of the triple containing the literal node
            // and v is value of literal node

            // Literal nodes are identified either by their value or by their containing triple.
            // When identified by value, there will be a single node representing all literals with the same value.
            // When identified by triple, there will be a separate node representing each triple that has an object with that value.
            var idObject = collapseLiterals ? literalnode as object : t as object;
            var nodeId   = idObject.GetHashCode().ToString();

            GraphVizWriter.Prettify(DOT.Tab, context);
            GraphVizWriter.WriteQuoted(nodeId, context);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.OpenSquare);
            context.Output.Write(DOT.Label);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.Equal);
            GraphVizWriter.Prettify(DOT.Space, context);
            GraphVizWriter.WriteLiteralNodeLabel(literalnode, context);
            context.Output.Write(DOT.Comma);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.Shape);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.Equal);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.Box);
            context.Output.Write(DOT.CloseSquare);
            context.Output.Write(DOT.NewLine, context);

            return(nodeId);
        }
Beispiel #2
0
        private static void WriteTriple(Triple triple, BaseWriterContext context, bool collapseLiterals)
        {
            // Output Node lines for Literal Node so we show them as Boxes
            // This is in keeping with Standard Graph representation of RDF
            // Literals are shown in Boxes, Uri Nodes in ellipses (GraphViz's default shape)
            var subjectId = GraphVizWriter.ProcessNode(triple, TripleSegment.Subject, context, collapseLiterals);
            var objectId  = GraphVizWriter.ProcessNode(triple, TripleSegment.Object, context, collapseLiterals);

            // Output the actual lines that state the relationship between the Nodes
            // We use the Predicate as the Label on the relationship
            var predicateLabel = GraphVizWriter.ReduceToQName((triple.Predicate as IUriNode).Uri, context);

            GraphVizWriter.Prettify(DOT.Tab, context);
            GraphVizWriter.WriteQuoted(subjectId, context);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.Arrow);
            GraphVizWriter.Prettify(DOT.Space, context);
            GraphVizWriter.WriteQuoted(objectId, context);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.OpenSquare);
            context.Output.Write(DOT.Label);
            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.Equal);
            GraphVizWriter.Prettify(DOT.Space, context);
            GraphVizWriter.WriteQuoted(predicateLabel, context);
            context.Output.Write(DOT.CloseSquare);
            context.Output.Write(DOT.Semicolon);
            GraphVizWriter.Prettify(DOT.NewLine, context);
        }
Beispiel #3
0
        /// <summary>
        /// Saves a Graph into GraphViz DOT Format
        /// </summary>
        /// <param name="g">Graph to save</param>
        /// <param name="output">Stream to save to</param>
        protected override void SaveInternal(IGraph g, TextWriter output)
        {
            var context = new BaseWriterContext(g, output)
            {
                PrettyPrint = this.PrettyPrintMode
            };

            GraphVizWriter.WriteGraph(context, this.CollapseLiterals);
        }
Beispiel #4
0
        /// <summary>
        /// Generates GraphViz Output for the given Graph.
        /// </summary>
        /// <param name="g">Graph to generated GraphViz Output for.</param>
        /// <param name="filename">File you wish to save the Output to.</param>
        /// <param name="open">Whether you want to open the Output in the default application (according to OS settings) for the filetype after it is Created.</param>
        public void Generate(IGraph g, String filename, bool open)
        {
            // Prepare the Process
            ProcessStartInfo start = new ProcessStartInfo();

            if (!_graphvizdir.Equals(String.Empty))
            {
                start.FileName = _graphvizdir + "dot.exe";
            }
            else
            {
                start.FileName = "dot.exe";
            }
            start.Arguments              = "-T" + _format;
            start.UseShellExecute        = false;
            start.RedirectStandardInput  = true;
            start.RedirectStandardOutput = true;

            // Prepare the GraphVizWriter and Streams
            GraphVizWriter gvzwriter = new GraphVizWriter();

            using (BinaryWriter writer = new BinaryWriter(new FileStream(filename, FileMode.Create)))
            {
                // Start the Process
                Process gvz = new Process();
                gvz.StartInfo = start;
                gvz.Start();

                // Write to the Standard Input
                gvzwriter.Save(g, gvz.StandardInput);

                // Read the Standard Output
                byte[] buffer = new byte[4096];
                using (BinaryReader reader = new BinaryReader(gvz.StandardOutput.BaseStream))
                {
                    while (true)
                    {
                        int read = reader.Read(buffer, 0, buffer.Length);
                        if (read == 0)
                        {
                            break;
                        }
                        writer.Write(buffer, 0, read);
                    }
                    reader.Close();
                }
                writer.Close();
                gvz.Close();
            }

            // Open if requested
            if (open)
            {
                Process.Start(filename);
            }
        }
        public void WritingGraphViz1()
        {
            Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseGraphViz),
                       "Test Config marks GraphViz as unavailable, test cannot be run");

            Graph g = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");

            GraphVizWriter writer = new GraphVizWriter();

            writer.Save(g, "WritingGraphViz1.dot");
        }
        public static void Main(string[] args)
        {
            Graph g = new Graph();
            Graph h = new Graph();
            UriLoader.Load(g, new Uri("http://nottm.ecs.soton.ac.uk/AllAboutThatWeb/profiles/exports/14"));
            UriLoader.Load(h, new Uri("http://nottm.ecs.soton.ac.uk/AllAboutThatWeb/profiles/14"));

            //Output to GraphViz
            GraphVizGenerator gvizgen = new GraphVizGenerator("png");
            gvizgen.Generate(g, "Triple.png", false);
            gvizgen.Generate(h, "AATTriple.png", false);

            GraphVizWriter gvizwriter = new GraphVizWriter();
            gvizwriter.Save(g, "Triple.dot");
            gvizwriter.Save(h, "AATTriple.dot");
        }
        public static void Main(string[] args)
        {
            //Create a new Empty Graph
            Graph g = new Graph();

            //Define Namespaces
            g.NamespaceMap.AddNamespace("pets", new Uri("http://example.org/pets"));
            
            //Create Uri Nodes
            IUriNode dog, fido, rob, owner, name, species, breed, lab;
            dog = g.CreateUriNode("pets:Dog");
            fido = g.CreateUriNode("pets:abc123");
            rob = g.CreateUriNode("pets:def456");
            owner = g.CreateUriNode("pets:hasOwner");
            name = g.CreateUriNode("pets:hasName");
            species = g.CreateUriNode("pets:isAnimal");
            breed = g.CreateUriNode("pets:isBreed");
            lab = g.CreateUriNode("pets:Labrador");

            //Assert Triples
            g.Assert(new Triple(fido, species, dog));
            g.Assert(new Triple(fido, owner, rob));
            g.Assert(new Triple(fido, name, g.CreateLiteralNode("Fido")));
            g.Assert(new Triple(rob, name, g.CreateLiteralNode("Rob")));
            g.Assert(new Triple(fido, breed, lab));

            //Attempt to output GraphViz
            try
            {
                Console.WriteLine("Writing GraphViz DOT file graph_building_example2.dot");
                GraphVizWriter gvzwriter = new GraphVizWriter();
                gvzwriter.Save(g, "graph_building_example2.dot");

                Console.WriteLine("Creating a PNG got this Graph called graph_building_example2.png");
                GraphVizGenerator gvzgen = new GraphVizGenerator("svg", "C:\\Program Files (x86)\\Graphviz2.20\\bin");
                gvzgen.Format = "png";
                gvzgen.Generate(g, "graph_building_example2.png", false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
Beispiel #8
0
        private static string ProcessNode(Triple t, TripleSegment segment, BaseWriterContext context, bool collapseLiterals)
        {
            var node = GraphVizWriter.GetNode(t, segment);

            switch (node)
            {
            case ILiteralNode literalnode:
                return(GraphVizWriter.WriteLiteralNode(literalnode, t, context, collapseLiterals));

            case IUriNode uriNode:
                return(GraphVizWriter.ReduceToQName(uriNode.Uri, context));

            case IBlankNode blankNode:
                return(blankNode.ToString());

            default:
                throw new RdfOutputException("Only Uri nodes, literal nodes and blank nodes can be converted to GraphViz DOT Format.");
            }
        }
Beispiel #9
0
        /// <summary>
        /// Generates GraphViz Output for the given Graph
        /// </summary>
        /// <param name="g">Graph to generated GraphViz Output for</param>
        /// <param name="filename">File you wish to save the Output to</param>
        /// <param name="open">Whether you want to open the Output in the default application (according to OS settings) for the filetype after it is Created</param>
        public void Generate(IGraph g, String filename, bool open)
        {
            //Prepare the Process
            ProcessStartInfo start = new ProcessStartInfo();

            if (!_graphvizdir.Equals(String.Empty))
            {
                start.FileName = this._graphvizdir + "dot.exe";
            }
            else
            {
                start.FileName = "dot.exe";
            }
            start.Arguments              = "-T" + this._format;
            start.UseShellExecute        = false;
            start.RedirectStandardInput  = true;
            start.RedirectStandardOutput = true;

            //Prepare the GraphVizWriter and Streams
            GraphVizWriter gvzwriter = new GraphVizWriter();
            BinaryWriter   output    = new BinaryWriter(new FileStream(filename, FileMode.Create));

            //Start the Process
            Process gvz = new Process();

            gvz.StartInfo = start;
            gvz.Start();

            //Write to the Standard Input
            gvzwriter.Save(g, gvz.StandardInput);

            //Read the Standard Output
            Tools.StreamCopy(gvz.StandardOutput.BaseStream, output.BaseStream);
            output.Close();

            gvz.Close();

            //Open if requested
            if (open)
            {
                Process.Start(filename);
            }
        }
Beispiel #10
0
        private void btnVisualise_Click(object sender, EventArgs e)
        {
            if (this.txtFile.Text.Equals(String.Empty))
            {
                MessageBox.Show("You must enter a filename you wish to visualise the Graph to", "Filename Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                try
                {
                    switch (Path.GetExtension(this.txtFile.Text))
                    {
                        case ".dot":
                        case "dot":
                            GraphVizWriter dotwriter = new GraphVizWriter();
                            dotwriter.Save(this._g, this.txtFile.Text);
                            break;
                        case ".png":
                        case "png":
                            GraphVizGenerator pnggenerator = new GraphVizGenerator("png");
                            pnggenerator.Generate(this._g, this.txtFile.Text, true);
                            break;
                        case ".svg":
                        case "svg":
                        default:
                            GraphVizGenerator svggenerator = new GraphVizGenerator("svg");
                            svggenerator.Generate(this._g, this.txtFile.Text, true);
                            break;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An Error occurred while trying to visualise the selected Graph:\n" + ex.Message, "Visualisation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
Beispiel #11
0
        private static void WriteGraph(BaseWriterContext context, bool collapseLiterals)
        {
            context.Output.Write(DOT.Digraph);

            if (context.Graph.BaseUri != null)
            {
                var graphId = GraphVizWriter.ReduceToQName(context.Graph.BaseUri, context);

                GraphVizWriter.Prettify(DOT.Space, context);
                GraphVizWriter.WriteQuoted(graphId, context);
            }

            GraphVizWriter.Prettify(DOT.Space, context);
            context.Output.Write(DOT.OpenCurly);
            GraphVizWriter.Prettify(DOT.NewLine, context);

            foreach (var t in context.Graph.Triples)
            {
                GraphVizWriter.WriteTriple(t, context, collapseLiterals);
            }

            context.Output.Write(DOT.CloseCurly);
        }
Beispiel #12
0
        private static void WriteLiteralNodeLabel(ILiteralNode literalnode, BaseWriterContext context)
        {
            var nodeValue = GraphVizWriter.Escape(literalnode.Value);

            context.Output.Write(DOT.Quote);
            context.Output.Write(nodeValue);

            if (!string.IsNullOrEmpty(literalnode.Language))
            {
                context.Output.Write("@");
                context.Output.Write(literalnode.Language);
            }

            if (literalnode.DataType != null)
            {
                string datatype = GraphVizWriter.ReduceToQName(literalnode.DataType, context);

                context.Output.Write("^^");
                context.Output.Write(datatype);
            }

            context.Output.Write(DOT.Quote);
        }
Beispiel #13
0
        public void GraphCreation1()
        {
            //Create a new Empty Graph
            Graph g = new Graph();
            Assert.IsNotNull(g);

            //Define Namespaces
            g.NamespaceMap.AddNamespace("vds", new Uri("http://www.vdesign-studios.com/dotNetRDF#"));
            g.NamespaceMap.AddNamespace("ecs", new Uri("http://id.ecs.soton.ac.uk/person/"));

            //Check we set the Namespace OK
            Assert.IsTrue(g.NamespaceMap.HasNamespace("vds"), "Failed to set a Namespace");

            //Set Base Uri
            g.BaseUri = g.NamespaceMap.GetNamespaceUri("vds");
            Assert.IsNotNull(g.BaseUri);
            Assert.AreEqual(g.NamespaceMap.GetNamespaceUri("vds"), g.BaseUri);

            //Create Uri Nodes
            IUriNode rav08r, wh, lac, hcd;
            rav08r = g.CreateUriNode("ecs:11471");
            wh = g.CreateUriNode("ecs:1650");
            hcd = g.CreateUriNode("ecs:46");
            lac = g.CreateUriNode("ecs:60");

            //Create Uri Nodes for some Predicates
            IUriNode supervises, collaborates, advises, has;
            supervises = g.CreateUriNode("vds:supervises");
            collaborates = g.CreateUriNode("vds:collaborates");
            advises = g.CreateUriNode("vds:advises");
            has = g.CreateUriNode("vds:has");

            //Create some Literal Nodes
            ILiteralNode singleLine = g.CreateLiteralNode("Some string");
            ILiteralNode multiLine = g.CreateLiteralNode("This goes over\n\nseveral\n\nlines");
            ILiteralNode french = g.CreateLiteralNode("Bonjour", "fr");
            ILiteralNode number = g.CreateLiteralNode("12", new Uri(g.NamespaceMap.GetNamespaceUri("xsd") + "integer"));

            g.Assert(new Triple(wh, supervises, rav08r));
            g.Assert(new Triple(lac, supervises, rav08r));
            g.Assert(new Triple(hcd, advises, rav08r));
            g.Assert(new Triple(wh, collaborates, lac));
            g.Assert(new Triple(wh, collaborates, hcd));
            g.Assert(new Triple(lac, collaborates, hcd));
            g.Assert(new Triple(rav08r, has, singleLine));
            g.Assert(new Triple(rav08r, has, multiLine));
            g.Assert(new Triple(rav08r, has, french));
            g.Assert(new Triple(rav08r, has, number));

            //Now print all the Statements
            Console.WriteLine("All Statements");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }

            //Get statements about Rob Vesse
            Console.WriteLine();
            Console.WriteLine("Statements about Rob Vesse");
            foreach (Triple t in g.GetTriples(rav08r))
            {
                Console.WriteLine(t.ToString());
            }

            //Get Statements about Collaboration
            Console.WriteLine();
            Console.WriteLine("Statements about Collaboration");
            foreach (Triple t in g.GetTriples(collaborates))
            {
                Console.WriteLine(t.ToString());
            }

            //Attempt to output Turtle for this Graph
            try
            {
                Console.WriteLine("Writing Turtle file graph_building_example.ttl");
                TurtleWriter ttlwriter = new TurtleWriter();
                ttlwriter.Save(g, "graph_building_example.ttl");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            //Attempt to output GraphViz
            try
            {
                Console.WriteLine("Writing GraphViz DOT file graph_building_example.dot");
                GraphVizWriter gvzwriter = new GraphVizWriter();
                gvzwriter.Save(g, "graph_building_example.dot");

                Console.WriteLine("Attempting Live GraphViz Generation as SVG");
                GraphVizGenerator gvzgen = new GraphVizGenerator("svg", "C:\\Program Files (x86)\\Graphviz2.20\\bin");
                gvzgen.Generate(g, "graph_building_example.svg", false);

                Console.WriteLine("Attempting Live GraphViz Generation as PNG");
                gvzgen.Format = "png";
                gvzgen.Generate(g, "graph_building_example.png", false);
            }
            catch (Exception ex)
            {
                TestTools.ReportError("Exception using GraphViz", ex, true);
            }
        }
Beispiel #14
0
        /// <summary>
        /// Generates GraphViz Output for the given Graph
        /// </summary>
        /// <param name="g">Graph to generated GraphViz Output for</param>
        /// <param name="filename">File you wish to save the Output to</param>
        /// <param name="open">Whether you want to open the Output in the default application (according to OS settings) for the filetype after it is Created</param>
        public void Generate(IGraph g, String filename, bool open)
        {
            //Prepare the Process
            ProcessStartInfo start = new ProcessStartInfo();
            if (!_graphvizdir.Equals(String.Empty)) {
                start.FileName = this._graphvizdir + "dot.exe";
            } else {
                start.FileName = "dot.exe";
            }
            start.Arguments = "-T" + this._format;
            start.UseShellExecute = false;
            start.RedirectStandardInput = true;
            start.RedirectStandardOutput = true;

            //Prepare the GraphVizWriter and Streams
            GraphVizWriter gvzwriter = new GraphVizWriter();
            BinaryWriter output = new BinaryWriter(new FileStream(filename, FileMode.Create));

            //Start the Process
            Process gvz = new Process();
            gvz.StartInfo = start;
            gvz.Start();

            //Write to the Standard Input
            gvzwriter.Save(g, gvz.StandardInput);

            //Read the Standard Output
            Tools.StreamCopy(gvz.StandardOutput.BaseStream, output.BaseStream);
            output.Close();

            gvz.Close();

            //Open if requested
            if (open)
            {
                Process.Start(filename);
            }
        }