Example #1
0
        public void Parse(TextReader reader, Action <Triple> sink, IGraph defaultGraph)
        {
            _sinkAction = sink;
            var parser = new VDS.RDF.Parsing.NQuadsParser(NQuadsSyntax.Rdf11);

            parser.Load(this, reader);
        }
Example #2
0
        public void Parse(IEnumerable <string> lines, Action <Triple> sink, IGraph defaultGraph)
        {
            _sinkAction = sink;
            var parser   = new VDS.RDF.Parsing.NQuadsParser(NQuadsSyntax.Rdf11);
            var allLines = string.Join("\n", lines);

            using (var reader = new StringReader(allLines))
            {
                parser.Load(this, reader);
            }
        }
        private void ParsingStoreHandlerNQuadsExplicitActual()
        {
            this.EnsureTestData("test.nq");

            TripleStore store = new TripleStore();
            StreamParams ps = new StreamParams("test.nq", Encoding.ASCII);

            NQuadsParser parser = new NQuadsParser();
            parser.Load(new StoreHandler(store), ps);

            Assert.IsTrue(store.HasGraph(new Uri("http://www.dotnetrdf.org/configuration#")), "Configuration Vocab Graph should have been parsed from Dataset");
            Graph configOrig = new Graph();
            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            IGraph config = store.Graph(new Uri("http://www.dotnetrdf.org/configuration#"));
            Assert.AreEqual(configOrig, config, "Configuration Vocab Graphs should have been equal");

            Assert.IsTrue(store.HasGraph(new Uri("http://www.dotnetrdf.org/leviathan#")), "Leviathan Function Library Graph should have been parsed from Dataset");
            Graph lvnOrig = new Graph();
            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.Functions.LeviathanFunctionLibrary.ttl");
            IGraph lvn = store.Graph(new Uri("http://www.dotnetrdf.org/leviathan#"));
            Assert.AreEqual(lvnOrig, lvn, "Leviathan Function Library Graphs should have been equal");

        }
        private void ParsingStoreHandlerNQuadsCountingActual()
        {
            this.EnsureTestData("test.nq");

            StreamParams ps = new StreamParams("test.nq", Encoding.ASCII);

            NQuadsParser parser = new NQuadsParser();
            StoreCountHandler counter = new StoreCountHandler();
            parser.Load(counter, ps);

            Graph configOrig = new Graph();
            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            Graph lvnOrig = new Graph();
            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.Functions.LeviathanFunctionLibrary.ttl");

            Assert.AreEqual(2, counter.GraphCount, "Expected 2 Graphs to be counted");
            Assert.AreEqual(configOrig.Triples.Count + lvnOrig.Triples.Count, counter.TripleCount, "Expected Triple Count to be sum of Triple Counts in two input Graphs");
        }
        public void ParsingStoreHandlerBlankNodesNQuadsActual()
        {
            EnsureTestData("test-bnodes.nq");

            NQuadsParser parser = new NQuadsParser();
            TripleStore store = new TripleStore();
            parser.Load(store, new StreamParams("test-bnodes.nq"));

            EnsureTestResults(store);
        }
        public static void Main(String[] args)
        {
            StreamWriter output = new StreamWriter("NQuadsTestSuite.txt");
            Console.SetOut(output);

            Console.WriteLine("## NQuads Test Suite");
            Console.WriteLine();

            try
            {
                //Make sure Test Directory exists
                if (!Directory.Exists("nquads_tests"))
                {
                    Directory.CreateDirectory("nquads_tests");
                }

                //First read in the TriG tests and serialize to NQuads
                Console.WriteLine("# Reading in TriG files and serializing to NQuads");
                foreach (String file in Directory.GetFiles("trig_tests"))
                {
                    if (Path.GetExtension(file) == ".trig")
                    {
                        //Skip the Bad Syntax tests
                        if (Path.GetFileName(file).StartsWith("bad")) continue;

                        Console.WriteLine("Reading File " + Path.GetFileName(file));

                        try
                        {
                            TriGParser parser = new TriGParser();
                            TripleStore store = new TripleStore();
                            parser.Load(store, new StreamParams(file));

                            Console.WriteLine("Parsed OK");

                            NQuadsWriter writer = new NQuadsWriter();
                            String outfile = "nquads_tests/" + Path.GetFileNameWithoutExtension(file) + ".nq";
                            writer.Save(store, new StreamParams(outfile));

                            Console.WriteLine("Serialized OK");

                            NQuadsParser nqparser = new NQuadsParser();
                            nqparser.TraceTokeniser = true;
                            TripleStore store2 = new TripleStore();
                            nqparser.Load(store2, new StreamParams(outfile));

                            Console.WriteLine("Parsed serialized NQuads OK");

                            if (store.Graphs.Count != store2.Graphs.Count)
                            {
                                Console.WriteLine("ERROR: Wrong number of Graphs when parsed back in");
                            }
                            else if (store.Triples.Count() != store2.Triples.Count())
                            {
                                Console.WriteLine("ERROR: Wrong number of Triples when parsed back in");
                            }

                        }
                        catch (RdfParseException parseEx)
                        {
                            HandleError("Parser Error", parseEx);
                        }
                        catch (RdfException rdfEx)
                        {
                            HandleError("RDF Error", rdfEx);
                        }
                        catch (Exception ex)
                        {
                            HandleError("Other Error", ex);
                        }
                        finally
                        {
                            Console.WriteLine();
                        }
                    }
                }
            }
            catch (RdfParseException parseEx)
            {
                HandleError("Parser Error", parseEx);
            }
            catch (Exception ex)
            {
                HandleError("Other Error", ex);
            }
            finally
            {
                output.Close();
            }
        }
        private void TestWriteToStoreDatasetsHandler(IGenericIOManager manager)
        {
            NodeFactory factory = new NodeFactory();
            INode a = factory.CreateUriNode(new Uri("http://example.org/a"));
            INode b = factory.CreateUriNode(new Uri("http://example.org/b"));
            INode c = factory.CreateUriNode(new Uri("http://example.org/c"));
            INode d = factory.CreateUriNode(new Uri("http://example.org/d"));

            Uri graphB = new Uri("http://example.org/graphs/b");
            Uri graphD = new Uri("http://example.org/graphs/d");

            //Try to ensure that the target Graphs do not exist
            if (manager.DeleteSupported)
            {
                manager.DeleteGraph(TestGraphUri);
                manager.DeleteGraph(graphB);
                manager.DeleteGraph(graphD);
            }
            else
            {
                Graph g = new Graph();
                g.BaseUri = TestGraphUri;
                manager.SaveGraph(g);
                g.BaseUri = graphB;
                manager.SaveGraph(g);
                g.BaseUri = graphD;
                manager.SaveGraph(g);
            }

            //Do the parsing and thus the loading
            WriteToStoreHandler handler = new WriteToStoreHandler(manager, TestGraphUri);
            NQuadsParser parser = new NQuadsParser();
            parser.Load(handler, new StreamParams("writetostore.nq"));

            //Load the expected Graphs
            Graph def = new Graph();
            manager.LoadGraph(def, TestGraphUri);
            Graph gB = new Graph();
            manager.LoadGraph(gB, graphB);
            Graph gD = new Graph();
            manager.LoadGraph(gD, graphD);

            Assert.AreEqual(2, def.Triples.Count, "Should be two triples in the default Graph");
            Assert.IsTrue(def.ContainsTriple(new Triple(a, a, a)), "Default Graph should have the a triple");
            Assert.AreEqual(1, gB.Triples.Count, "Should be one triple in the b Graph");
            Assert.IsTrue(gB.ContainsTriple(new Triple(b, b, b)), "b Graph should have the b triple");
            Assert.IsTrue(def.ContainsTriple(new Triple(c, c, c)), "Default Graph should have the c triple");
            Assert.AreEqual(1, gD.Triples.Count, "Should be one triple in the d Graph");
            Assert.IsTrue(gD.ContainsTriple(new Triple(d, d, d)), "d Graph should have the d triple");
        }