Exemplo n.º 1
0
        public void WritingBlankNodeOutput()
        {
            //Create a Graph and add a couple of Triples which when serialized have
            //potentially colliding IDs

            Graph g = new Graph();

            g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org"));
            IUriNode   subj = g.CreateUriNode("ex:subject");
            IUriNode   pred = g.CreateUriNode("ex:predicate");
            IUriNode   name = g.CreateUriNode("ex:name");
            IBlankNode b1   = g.CreateBlankNode("autos1");
            IBlankNode b2   = g.CreateBlankNode("1");

            g.Assert(subj, pred, b1);
            g.Assert(b1, name, g.CreateLiteralNode("First Triple"));
            g.Assert(subj, pred, b2);
            g.Assert(b2, name, g.CreateLiteralNode("Second Triple"));

            TurtleWriter ttlwriter = new TurtleWriter();

            ttlwriter.Save(g, "bnode-output-test.ttl");

            TestTools.ShowGraph(g);

            TurtleParser ttlparser = new TurtleParser();
            Graph        h         = new Graph();

            ttlparser.Load(h, "bnode-output-test.ttl");

            TestTools.ShowGraph(h);

            Assert.AreEqual(g.Triples.Count, h.Triples.Count, "Expected same number of Triples after serialization and reparsing");
        }
Exemplo n.º 2
0
        public void WritingBlankNodeOutput()
        {
            //Create a Graph and add a couple of Triples which when serialized have
            //potentially colliding IDs

            Graph g = new Graph();
            g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org"));
            IUriNode subj = g.CreateUriNode("ex:subject");
            IUriNode pred = g.CreateUriNode("ex:predicate");
            IUriNode name = g.CreateUriNode("ex:name");
            IBlankNode b1 = g.CreateBlankNode("autos1");
            IBlankNode b2 = g.CreateBlankNode("1");

            g.Assert(subj, pred, b1);
            g.Assert(b1, name, g.CreateLiteralNode("First Triple"));
            g.Assert(subj, pred, b2);
            g.Assert(b2, name, g.CreateLiteralNode("Second Triple"));

            TurtleWriter ttlwriter = new TurtleWriter();
            ttlwriter.Save(g, "bnode-output-test.ttl");

            TestTools.ShowGraph(g);

            TurtleParser ttlparser = new TurtleParser();
            Graph h = new Graph();
            ttlparser.Load(h, "bnode-output-test.ttl");

            TestTools.ShowGraph(h);

            Assert.AreEqual(g.Triples.Count, h.Triples.Count, "Expected same number of Triples after serialization and reparsing");

        }
Exemplo n.º 3
0
        public void WritingOwlCharEscaping()
        {
            Graph g = new Graph();

            FileLoader.Load(g, "charescaping.owl");

            Console.WriteLine("Original Triples");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
            Console.WriteLine();

            TurtleWriter ttlwriter  = new TurtleWriter();
            String       serialized = VDS.RDF.Writing.StringWriter.Write(g, ttlwriter);

            Graph h = new Graph();

            StringParser.Parse(h, serialized);

            Console.WriteLine("Serialized Triples");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
            Console.WriteLine();

            Assert.AreEqual(g, h, "Graphs should have been equal");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Internal Method which performs multi-threaded writing of data
        /// </summary>
        private void SaveGraphs(FolderStoreWriterContext context)
        {
            //Create the relevant Writer
            IRdfWriter writer;
            switch (context.Format)
            {
                case FolderStoreFormat.Turtle:
                    writer = new TurtleWriter();
                    break;
                case FolderStoreFormat.Notation3:
                    writer = new Notation3Writer();
                    break;
                case FolderStoreFormat.RdfXml:
#if !NO_XMLDOM
                    writer = new FastRdfXmlWriter();
#else
                    writer = new RdfXmlWriter();
#endif
                    break;
                default:
                    writer = new TurtleWriter();
                    break;
            }

            try
            {
                Uri u = context.GetNextURI();
                while (u != null)
                {
                    //Get the Graph from the Store
                    IGraph g = context.Store.Graphs[u];

                    //Write to Disk
                    String destFile = Path.Combine(context.Folder, u.GetSha256Hash());
                    switch (context.Format)
                    {
                        case FolderStoreFormat.Turtle:
                            destFile += ".ttl";
                            break;
                        case FolderStoreFormat.Notation3:
                            destFile += ".n3";
                            break;
                        case FolderStoreFormat.RdfXml:
                            destFile += ".rdf";
                            break;
                        default:
                            destFile += ".ttl";
                            break;
                    }
                    writer.Save(g, destFile);

                    //Get the Next Uri
                    u = context.GetNextURI();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
#if !SILVERLIGHT
                Thread.ResetAbort();
#endif
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
        public void WritingOwlCharEscaping()
        {
            Graph g = new Graph();
            FileLoader.Load(g, "charescaping.owl");

            Console.WriteLine("Original Triples");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
            Console.WriteLine();               

            TurtleWriter ttlwriter = new TurtleWriter();
            String serialized = StringWriter.Write(g, ttlwriter);

            Graph h = new Graph();
            StringParser.Parse(h, serialized);

            Console.WriteLine("Serialized Triples");
            foreach (Triple t in g.Triples)
            {
                Console.WriteLine(t.ToString());
            }
            Console.WriteLine();

            Assert.AreEqual(g, h, "Graphs should have been equal");
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            //Going to create a Graph and assert some stuff into it
            Graph g = new Graph();

            //Try to read from a file
            TurtleParser parser = new TurtleParser();
            parser.TraceTokeniser = true;
            parser.TraceParsing = true;
            try
            {
                StreamReader input = new StreamReader("test.n3");
                parser.Load(g, input);
            }
            catch (RDFException rdfEx)
            {
                reportError("RDF Exception", rdfEx);
            }
            catch (IOException ioEx)
            {
                reportError("IO Exception", ioEx);
            }
            catch (Exception ex)
            {
               reportError("Other Exception", ex);
            }

            Console.WriteLine();
            Console.WriteLine();

            //Show Namespaces
            Console.WriteLine("All Namespaces");
            foreach (String pre in g.NamespaceMap.Prefixes)
            {
                Console.WriteLine(pre + " = " + g.NamespaceMap.GetNamespaceURI(pre));
            }

            Console.WriteLine();

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


            System.Threading.Thread.Sleep(60000);
            return;



            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/"));
            //g.BaseURI = g.NamespaceMap.GetNamespaceURI("vds");

            URINode rav08r, wh, lac, hcd;
            rav08r = g.CreateURINode("ecs:11471");
            wh = g.CreateURINode("ecs:1650");
            hcd = g.CreateURINode("ecs:46");
            lac = g.CreateURINode("ecs:60");

            BlankNode blank = g.CreateBlankNode();
            URINode a, b, c, d, has;
            a = g.CreateURINode("vds:someRel");
            b = g.CreateURINode("vds:someOtherRel");
            c = g.CreateURINode("vds:someObj");
            d = g.CreateURINode("vds:someOtherObj");
            has = g.CreateURINode("vds:has");

            URINode supervises, collaborates, advises;
            supervises = g.CreateURINode("vds:supervises");
            collaborates = g.CreateURINode("vds:collaborates");
            advises = g.CreateURINode("vds:advises");

            LiteralNode singleLine = g.CreateLiteralNode("Some string");
            LiteralNode multiLine = g.CreateLiteralNode("This goes over\n\nseveral\n\nlines");
            LiteralNode french = g.CreateLiteralNode("Bonjour", "fr");

            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, blank, c));
            //g.Assert(new Triple(rav08r, blank, d));
            g.Assert(new Triple(rav08r, has, singleLine));
            g.Assert(new Triple(rav08r, has, multiLine));
            g.Assert(new Triple(rav08r, has, french));


            //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());
            }

            //Show Namespaces for URINodes
            Console.WriteLine();
            Console.WriteLine("Namespaces for URI Nodes");
            foreach (URINode u in g.Nodes.URINodes)
            {
                Console.WriteLine(u.Namespace + " = " + u.URI);
            }

            //Attempt to output Notation 3 for this Graph
            try
            {
                TurtleWriter n3writer = new TurtleWriter();
                n3writer.Save(g, "test.n3");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            

            System.Threading.Thread.Sleep(30000);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Internal Method which performs multi-threaded writing of data
        /// </summary>
        private void SaveGraphs(FolderStoreWriterContext context)
        {
            //Create the relevant Writer
            IRdfWriter writer;

            switch (context.Format)
            {
            case FolderStoreFormat.Turtle:
                writer = new TurtleWriter();
                break;

            case FolderStoreFormat.Notation3:
                writer = new Notation3Writer();
                break;

            case FolderStoreFormat.RdfXml:
#if !NO_XMLDOM
                writer = new FastRdfXmlWriter();
#else
                writer = new RdfXmlWriter();
#endif
                break;

            default:
                writer = new TurtleWriter();
                break;
            }

            try
            {
                Uri u = context.GetNextURI();
                while (u != null)
                {
                    //Get the Graph from the Store
                    IGraph g = context.Store.Graphs[u];

                    //Write to Disk
                    String destFile = Path.Combine(context.Folder, u.GetSha256Hash());
                    switch (context.Format)
                    {
                    case FolderStoreFormat.Turtle:
                        destFile += ".ttl";
                        break;

                    case FolderStoreFormat.Notation3:
                        destFile += ".n3";
                        break;

                    case FolderStoreFormat.RdfXml:
                        destFile += ".rdf";
                        break;

                    default:
                        destFile += ".ttl";
                        break;
                    }
                    writer.Save(g, destFile);

                    //Get the Next Uri
                    u = context.GetNextURI();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
#if !SILVERLIGHT
                Thread.ResetAbort();
#endif
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Exemplo n.º 9
0
        public void Dispose()
        {
            //Create all of the directories required for the file
            var f = new FileInfo(_outputPath);
            f.Directory.Create();

            if (this._outputFormat == ERdfFormat.RdfXml)
            {
                using (XmlTextWriter xmlWriter = new XmlTextWriter(_outputPath, new UTF8Encoding(false)))
                    //Set encoding
                {
                    _output.Save(xmlWriter);
                }
            }
            else if (this._outputFormat == ERdfFormat.TriG)
            {
                string fileNameAsTrig = GetFilePathBasedOnFormat();
                var outparams = new StreamParams(fileNameAsTrig);
                outparams.Encoding = Encoding.UTF8;
                var writer = new TriGWriter();

                if (_store == null)
                {
                    var g = GetXmlDocumentAsGraph();
                    _store = new TripleStore();
                    _store.Add(g, true);
                }

                writer.Save(_store, outparams);
            }
            else if (this._outputFormat == ERdfFormat.Turtle)
            {
                var g = GetXmlDocumentAsGraph();
                string filePathForFormat = GetFilePathBasedOnFormat();
                var writer = new TurtleWriter(TurtleSyntax.W3C);
                writer.Save(g, filePathForFormat);
            }
            else if (this._outputFormat == ERdfFormat.NTriples)
            {
                var g = GetXmlDocumentAsGraph();
                string filePathForFormat = GetFilePathBasedOnFormat();
                var writer = new NTriplesWriter();
                writer.Save(g, filePathForFormat);
            }
            else if (this._outputFormat == ERdfFormat.N3)
            {
                var g = GetXmlDocumentAsGraph();
                string filePathForFormat = GetFilePathBasedOnFormat();
                var writer = new Notation3Writer();
                writer.Save(g, filePathForFormat);
            }
            else if (this._outputFormat == ERdfFormat.NQuads)
            {
                string filePathForFormat = GetFilePathBasedOnFormat();
                var outparams = new StreamParams(filePathForFormat);
                outparams.Encoding = Encoding.UTF8;

                if (_store == null)
                {
                    var g = GetXmlDocumentAsGraph();
                    _store = new TripleStore();
                    _store.Add(g, true);
                }

                var writer = new NQuadsWriter();
                writer.Save(_store, outparams);
            }

            //make sure it's not null - can happen if no graphs have yet to be asserted!!
            if (_store != null)
            {
                foreach (var graph in _store.Graphs)
                {
                    graph.Dispose();
                }
                _store.Dispose();
                GC.Collect();
            }
        }