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

            TripleStore store = new TripleStore();

            NQuadsParser parser = new NQuadsParser();

            parser.Load(store, "test.nq");

            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[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.LeviathanFunctionLibrary.ttl");
            IGraph lvn = store[new Uri("http://www.dotnetrdf.org/leviathan#")];

            Assert.AreEqual(lvnOrig, lvn, "Leviathan Function Library Graphs should have been equal");
        }
Beispiel #2
0
        public void ParsingStoreHandlerNQuadsExplicit()
        {
            this.EnsureTestData("test.nq");

            TripleStore store = new TripleStore();

            NQuadsParser parser = new NQuadsParser();

            parser.Load(new StoreHandler(store), "test.nq");

            Assert.True(store.HasGraph(new Uri("http://graphs/1")), "Configuration Vocab Graph should have been parsed from Dataset");
            Graph configOrig = new Graph();

            configOrig.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            IGraph config = store[new Uri("http://graphs/1")];

            Assert.Equal(configOrig, config);

            Assert.True(store.HasGraph(new Uri("http://graphs/2")), "Leviathan Function Library Graph should have been parsed from Dataset");
            Graph lvnOrig = new Graph();

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            IGraph lvn = store[new Uri("http://graphs/2")];

            Assert.Equal(lvnOrig, lvn);
        }
        public bool ApplyDiff(IDiffResult diffResult)
        {
            // TODO: Make async
            Log.LogTrace("ApplyDiff: {0}", diffResult.ToString());
            var store = GetStore();

            if (!diffResult.Success)
            {
                Log.LogTrace("ApplyDiff: Aborting attempt to apply a failed diff command result");
                return(false);
            }
            var updateGraph = new NonIndexedGraph {
                BaseUri = new Uri("http://example.org/")
            };
            var parser = new NQuadsParser();

            Log.LogTrace("ApplyDiff: Applying deletes");
            parser.Parse(diffResult.FileDiffs.SelectMany(diff => diff.Deleted),
                         triple => store.Retract(triple.Subject, triple.Predicate, triple.Object, triple.GraphUri),
                         updateGraph);
            updateGraph.Clear();
            Log.LogTrace("ApplyDiff: Applying inserts");
            parser.Parse(diffResult.FileDiffs.SelectMany(diff => diff.Inserted),
                         triple => store.Assert(triple.Subject, triple.Predicate, triple.Object, triple.GraphUri),
                         updateGraph);
            Log.LogTrace("ApplyDiff: Flushing changes");
            store.Flush();
            Log.LogTrace("ApplyDiff: Completed");
            return(true);
        }
        public void JsonLdParserTests(string inputPath, string contextPath, string expectedOutputPath, string baseIri,
                                      string processorMode, string expandContextPath, bool compactArrays)
        {
            var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath,
                                                        compactArrays);
            var contextJson    = contextPath == null ? null : File.ReadAllText(contextPath);
            var contextElement = contextJson == null ? null : JToken.Parse(contextJson);
            var nqParser       = new NQuadsParser(NQuadsSyntax.Rdf11);
            var expectedStore  = new TripleStore();

            nqParser.Load(expectedStore, expectedOutputPath);
            FixStringLiterals(expectedStore);
            var jsonldParser = new JsonLdParser(processorOptions);
            var actualStore  = new TripleStore();

            jsonldParser.Load(actualStore, inputPath);
            Assert.True(expectedStore.Graphs.Count.Equals(actualStore.Graphs.Count),
                        $"Test failed for input {Path.GetFileName(inputPath)}.\r\nActual graph count {actualStore.Graphs.Count} does not match expected graph count {expectedStore.Graphs.Count}.");
            foreach (var expectGraph in expectedStore.Graphs)
            {
                Assert.True(actualStore.HasGraph(expectGraph.BaseUri),
                            $"Test failed for input {Path.GetFileName(inputPath)}.\r\nCould not find expected graph {expectGraph.BaseUri}");
                var actualGraph  = actualStore.Graphs[expectGraph.BaseUri];
                var bNodeMapping = new Dictionary <INode, INode>();
                var graphsEqual  = actualGraph.Equals(expectGraph, out bNodeMapping);
                if (!graphsEqual)
                {
                    var    ser           = new NQuadsWriter();
                    string expectedLines = MakeNQuadsList(expectedStore);
                    string actualLines   = MakeNQuadsList(actualStore);
                    Assert.True(graphsEqual,
                                $"Test failed for input {Path.GetFileName(inputPath)}.\r\nGraph {expectGraph.BaseUri} differs in actual output from expected output.\r\nExpected:\r\n{expectedLines}\r\nActual:\r\n{actualLines}");
                }
            }
        }
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void JsonLdWriterTests(string inputPath, string contextPath, string expectedOutputPath, bool useNativeTypes, bool useRdfType)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var nqParser = new NQuadsParser(NQuadsSyntax.Rdf11);
            var input    = new TripleStore();

            nqParser.Load(input, inputPath);
            FixStringLiterals(input);
            var expectedOutputJson = File.ReadAllText(expectedOutputPath);
            var expectedOutput     = JToken.Parse(expectedOutputJson);
            var jsonLdWriter       =
                new JsonLdWriter(new JsonLdWriterOptions {
                UseNativeTypes = useNativeTypes, UseRdfType = useRdfType
            });
            var actualOutput = jsonLdWriter.SerializeStore(input);

            try
            {
                Assert.True(DeepEquals(expectedOutput, actualOutput, true, true),
                            $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}");
            }
            catch (DeepEqualityFailure ex)
            {
                Assert.True(false,
                            $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}\nMatch Failured: {ex}");
            }
        }
Beispiel #6
0
        private IGraph CreateGraph(string s)
        {
            var nqr = new NQuadsParser();
            var ts  = new TripleStore();

            nqr.Load(ts, new StringReader(s));
            return(ts.Graphs.FirstOrDefault());
        }
        private void TestWriteToStoreDatasetsHandler(IStorageProvider 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 StreamReader("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");
        }
        public void Import(string testDataFile)
        {
            var defaultGraph = new Graph();
            var parser       = new NQuadsParser();

            using (var reader = File.OpenText(testDataFile))
            {
                parser.Parse(reader, t => Store.Assert(t.Subject, t.Predicate, t.Object, t.GraphUri), defaultGraph);
            }
            Store.Flush();
        }
Beispiel #9
0
        public IEnumerable <Edge> Triples()
        {
            var stream = new Antlr4.Runtime.AntlrInputStream(_reader);
            var lexer  = new NQuadsLexer(stream);
            var parser = new NQuadsParser(new Antlr4.Runtime.CommonTokenStream(lexer));

            foreach (var child in parser.nquadsDoc().statement())
            {
                yield return(null);
            }
        }
        protected void ParsingStoreHandlerBlankNodesNQuadsActual()
        {
            EnsureTestData("test-bnodes.nq");

            NQuadsParser parser = new NQuadsParser();
            TripleStore  store  = new TripleStore();

            parser.Load(store, "test-bnodes.nq");

            EnsureTestResults(store);
        }
Beispiel #11
0
        public void CanGenerateRdfFromRepository()
        {
            var repoDir = Path.Combine(_tmpDir, "quince");

            Directory.CreateDirectory(repoDir);
            var repo         = new DynamicFileStore(repoDir, 100);
            var defaultGraph = new Graph();
            var parser       = new NQuadsParser();

            using (var reader = File.OpenText(Path.Join("data", "test1.nq")))
            {
                parser.Parse(reader, t => repo.Assert(t.Subject, t.Predicate, t.Object, t.GraphUri), defaultGraph);
            }

            repo.Flush();
            var mockQuinceFactory = new Mock <IQuinceStoreFactory>();

            mockQuinceFactory.Setup(x => x.MakeQuinceStore(It.IsAny <string>())).Returns(repo);
            var rdfGeneratorMock = new Mock <ITripleCollectionHandler>();

            rdfGeneratorMock.Setup(x => x.HandleTripleCollection(It.Is <IList <Triple> >(c =>
                                                                                         c.All(t => (t.Subject as IUriNode).Uri.ToString().Equals("http://example.org/id/resource/s/s0"))))).Verifiable();
            var fileGeneratorFactoryMock = new Mock <IFileGeneratorFactory>();

            fileGeneratorFactoryMock.Setup(x => x.MakeRdfFileGenerator(It.IsAny <IResourceFileMapper>(),
                                                                       It.IsAny <IEnumerable <Uri> >(), It.IsAny <IProgressLog>(), It.IsAny <int>())).Returns(rdfGeneratorMock.Object);

            var rdfResourceFileMapper = new ResourceFileMapper(
                new ResourceMapEntry(new Uri("http://example.org/id/"), "data"));
            var htmlResourceFileMapper = new ResourceFileMapper(
                new ResourceMapEntry(new Uri("http://example.org/id/"), "doc"));
            var ddRepository = new DataDockRepository(
                _tmpDir, new Uri("http://example.org/"), new MockProgressLog(),
                mockQuinceFactory.Object, fileGeneratorFactoryMock.Object,
                rdfResourceFileMapper, htmlResourceFileMapper, _uriService);

            ddRepository.GenerateRdf(new[] { new Uri("http://example.org/g/g1") });

            // Should be invoked to generate files for subject IRIs
            rdfGeneratorMock.Verify(x => x.HandleTripleCollection(It.Is <IList <Triple> >(c =>
                                                                                          c.All(t => (t.Subject as IUriNode).Uri.ToString().Equals("http://example.org/id/resource/s/s0")))),
                                    Times.Once);
            // Should not be invoked to generate files for object IRIs
            rdfGeneratorMock.Verify(x => x.HandleTripleCollection(It.Is <IList <Triple> >(
                                                                      c => c.Any(t => (t.Subject as IUriNode).Uri.ToString().Equals("http://example.org/id/resource/o/o0")))), Times.Never);
        }
Beispiel #12
0
        public void ParsingStoreHandlerNQuadsCounting()
        {
            this.EnsureTestData("test.nq");

            NQuadsParser      parser  = new NQuadsParser();
            StoreCountHandler counter = new StoreCountHandler();

            parser.Load(counter, "test.nq");

            Graph configOrig = new Graph();

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

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");

            Assert.Equal(2, counter.GraphCount);
            Assert.Equal(configOrig.Triples.Count + lvnOrig.Triples.Count, counter.TripleCount);
        }
        private void ParsingStoreHandlerNQuadsCountingActual()
        {
            this.EnsureTestData("test.nq");

            NQuadsParser      parser  = new NQuadsParser();
            StoreCountHandler counter = new StoreCountHandler();

            parser.Load(counter, "test.nq");

            Graph configOrig = new Graph();

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

            lvnOrig.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.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 virtual void JsonLdParserTests(string testId, JsonLdTestType testType, string inputPath, string contextPath,
                                              string expectedOutputPath, JsonLdErrorCode expectedErrorCode, string baseIri,
                                              string processorMode, string expandContextPath, bool compactArrays, string rdfDirection)
        {
            var processorOptions = MakeProcessorOptions(inputPath, baseIri, processorMode, expandContextPath,
                                                        compactArrays, rdfDirection);
            var jsonldParser = new JsonLdParser(processorOptions);
            var actualStore  = new TripleStore();

            switch (testType)
            {
            case JsonLdTestType.PositiveEvaluationTest:
                var nqParser      = new NQuadsParser(NQuadsSyntax.Rdf11);
                var expectedStore = new TripleStore();
                nqParser.Load(expectedStore, expectedOutputPath);
                FixStringLiterals(expectedStore);
                jsonldParser.Load(actualStore, inputPath);
                Assert.True(expectedStore.Graphs.Count.Equals(actualStore.Graphs.Count) ||
                            (expectedStore.Graphs.Count == 0 && actualStore.Graphs.Count == 1 &&
                             actualStore.Graphs[null].IsEmpty),
                            $"Test failed for input {Path.GetFileName(inputPath)}.\r\nActual graph count {actualStore.Graphs.Count} does not match expected graph count {expectedStore.Graphs.Count}.");
                AssertStoresEqual(expectedStore, actualStore, Path.GetFileName(inputPath));
                break;

            case JsonLdTestType.NegativeEvaluationTest:
                var exception =
                    Assert.Throws <JsonLdProcessorException>(() => jsonldParser.Load(actualStore, inputPath));
                Assert.Equal(expectedErrorCode, exception.ErrorCode);
                break;

            case JsonLdTestType.PositiveSyntaxTest:
                // Positive syntax test should load input file without raising any exceptions
                jsonldParser.Load(actualStore, inputPath);
                break;

            default:
                Assert.True(false, $"Test type {testType} is not currently supported for the JSON-LD Parser tests");
                break;
            }
        }
        public static JToken FromRdf(IEnumerable <string> statements)
        {
            var stringBuilder = new StringBuilder();

            foreach (var item in statements)
            {
                stringBuilder.AppendLine(item);
            }
            var reader = new StringReader(stringBuilder.ToString());

            var store  = new TripleStore();
            var parser = new NQuadsParser(NQuadsSyntax.Rdf11);

            parser.Load(store, reader);

            var ldWriter     = new JsonLdWriter();
            var stringWriter = new System.IO.StringWriter();

            ldWriter.Save(store, stringWriter);

            return(JToken.Parse(stringWriter.ToString()));
        }
        public void ParseDiff(IDiffResult diffResult, QuinceDiff quinceDiff)
        {
            // TODO: Make async
            Log.LogTrace("ParseDiff: {0}", diffResult.ToString());
            if (!diffResult.Success)
            {
                Log.LogTrace("ParseDiff: Aborting attempt to apply a failed diff command result");
                return;
            }
            var diffGraph = new NonIndexedGraph {
                BaseUri = new Uri("http://example.org/")
            };
            var parser = new NQuadsParser();

            Log.LogTrace("ParseDiff: Parsing deletes");
            parser.Parse(diffResult.FileDiffs.Where(fd => fd.FilePath.StartsWith("_s")).SelectMany(diff => diff.Deleted),
                         quinceDiff.Deleted,
                         diffGraph);
            diffGraph.Clear();
            Log.LogTrace("ParseDiff: Parsing inserts");
            parser.Parse(diffResult.FileDiffs.Where(fd => fd.FilePath.StartsWith("_s")).SelectMany(diff => diff.Inserted),
                         quinceDiff.Inserted,
                         diffGraph);
        }
Beispiel #17
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="writer">Writer to save to</param>
        /// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the store is written</param>
        public void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            var context = new ThreadedStoreWriterContext(store, writer, PrettyPrintMode, false);

            // Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                if (!leaveOpen)
                {
                    context.Output.Close();
                }
                return;
            }

            try
            {
                if (UseMultiThreadedWriting)
                {
                    // Queue the Graphs to be written
                    foreach (var g in context.Store.Graphs)
                    {
                        context.Add(g.BaseUri);
                    }

                    // Start making the async calls
                    var results = new List <IAsyncResult>();
                    var d       = new SaveGraphsDelegate(SaveGraphs);
                    for (var i = 0; i < _threads; i++)
                    {
                        results.Add(d.BeginInvoke(context, null, null));
                    }

                    // Wait for all the async calls to complete
                    WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());
                    var outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                    foreach (var result in results)
                    {
                        try
                        {
                            d.EndInvoke(result);
                        }
                        catch (Exception ex)
                        {
                            outputEx.AddException(ex);
                        }
                    }
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }

                    // If there were any errors we'll throw an RdfThreadedOutputException now
                    if (outputEx.InnerExceptions.Any())
                    {
                        throw outputEx;
                    }
                }
                else
                {
                    foreach (var g in context.Store.Graphs)
                    {
                        var graphContext = new NTriplesWriterContext(g, context.Output, NQuadsParser.AsNTriplesSyntax(Syntax));
                        foreach (var t in g.Triples)
                        {
                            context.Output.WriteLine(TripleToNQuads(graphContext, t, g.BaseUri));
                        }
                    }
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
            }
            catch
            {
                try
                {
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
                catch
                {
                    // Just cleaning up
                }
                throw;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadMatchedState"/> class.
 /// </summary>
 public QuadMatchedState(NQuadsParser parser, int currentLine, Quad quad) : base(parser, currentLine)
 {
     _quad = quad;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SubjectMatchedState"/> class.
 /// </summary>
 public SubjectMatchedState(NQuadsParser parser, int currentLine, Node subjectNode)
     : base(parser, currentLine)
 {
     _subjectNode = subjectNode;
 }
 /// <summary>
 /// Creates a new NQuads formatter
 /// </summary>
 /// <param name="syntax">NQuads syntax to output</param>
 /// <param name="formatName">Format Name</param>
 public NQuadsFormatter(NQuadsSyntax syntax, String formatName)
     : base(NQuadsParser.AsNTriplesSyntax(syntax), formatName)
 {
 }
Beispiel #21
0
        /// <summary>
        /// Thread Worker method which writes Graphs to the output.
        /// </summary>
        /// <param name="globalContext">Context for writing the Store.</param>
        private void SaveGraphs(ThreadedStoreWriterContext globalContext)
        {
            try
            {
                while (globalContext.TryGetNextUri(out var u))
                {
                    // Get the Graph from the Store
                    var g = globalContext.Store.Graphs[u];

                    // Generate the Graph Output and add to Stream
                    var context      = new NTriplesWriterContext(g, new System.IO.StringWriter(), NQuadsParser.AsNTriplesSyntax(Syntax), globalContext.PrettyPrint, globalContext.HighSpeedModePermitted);
                    var graphContent = GraphToNQuads(globalContext, context);
                    if (!graphContent.Equals(String.Empty))
                    {
                        try
                        {
                            Monitor.Enter(globalContext.Output);
                            globalContext.Output.WriteLine(graphContent);
                            globalContext.Output.Flush();
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            Monitor.Exit(globalContext.Output);
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                // We've been terminated, don't do anything
                Thread.ResetAbort();
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Beispiel #22
0
        /// <summary>
        /// Saves a Store in NQuads format.
        /// </summary>
        /// <param name="store">Store to save.</param>
        /// <param name="writer">Writer to save to.</param>
        /// <param name="leaveOpen">Boolean flag indicating if <paramref name="writer"/> should be left open after the store is written.</param>
        public void Save(ITripleStore store, TextWriter writer, bool leaveOpen)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            var context = new ThreadedStoreWriterContext(store, writer, PrettyPrintMode, false);

            // Check there's something to do
            if (context.Store.Graphs.Count == 0)
            {
                if (!leaveOpen)
                {
                    context.Output.Close();
                }
                return;
            }

            try
            {
                if (UseMultiThreadedWriting)
                {
                    // Queue the Graphs to be written
                    foreach (var g in context.Store.Graphs)
                    {
                        context.Add(g.BaseUri);
                    }

                    // Start making the async calls
                    //var results = new List<IAsyncResult>();
                    var workers = new Task[_threads];
                    for (var i = 0; i < _threads; i++)
                    {
                        workers[i] = Task.Factory.StartNew(() => SaveGraphs(context));
                    }

                    try
                    {
                        Task.WaitAll(workers);
                    }
                    catch (AggregateException ex)
                    {
                        var outputException =
                            new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (var innerException in ex.InnerExceptions)
                        {
                            outputException.AddException(innerException);
                        }
                    }
                    finally
                    {
                        if (!leaveOpen)
                        {
                            context.Output.Close();
                        }
                    }
                }
                else
                {
                    foreach (var g in context.Store.Graphs)
                    {
                        var graphContext = new NTriplesWriterContext(g, context.Output, NQuadsParser.AsNTriplesSyntax(Syntax));
                        foreach (var t in g.Triples)
                        {
                            context.Output.WriteLine(TripleToNQuads(graphContext, t, g.BaseUri));
                        }
                    }
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
            }
            catch
            {
                try
                {
                    if (!leaveOpen)
                    {
                        context.Output.Close();
                    }
                }
                catch
                {
                    // Just cleaning up
                }
                throw;
            }
        }
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public virtual void JsonLdWriterTests(string testId, JsonLdTestType testType, string inputPath, string contextPath,
                                              string expectedOutputPath, JsonLdErrorCode expectErrorCode, bool useNativeTypes, bool useRdfType, bool ordered, string rdfDirection)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            var nqParser = new NQuadsParser(NQuadsSyntax.Rdf11);
            var input    = new TripleStore();

            nqParser.Load(input, inputPath);
            FixStringLiterals(input);
            var writerOptions = new JsonLdWriterOptions
            {
                UseNativeTypes = useNativeTypes, UseRdfType = useRdfType, Ordered = ordered
            };

            if (rdfDirection != null)
            {
                switch (rdfDirection)
                {
                case "i18n-datatype":
                    writerOptions.RdfDirection = JsonLdRdfDirectionMode.I18NDatatype;
                    break;

                case "compound-literal":
                    writerOptions.RdfDirection = JsonLdRdfDirectionMode.CompoundLiteral;
                    break;

                default:
                    throw new Exception($"Test {testId} specifies an unrecognized value for the rdfDirection option: {rdfDirection}.");
                }
            }
            var jsonLdWriter = new JsonLdWriter(writerOptions);

            switch (testType)
            {
            case JsonLdTestType.PositiveEvaluationTest:
            {
                var actualOutput       = jsonLdWriter.SerializeStore(input);
                var expectedOutputJson = File.ReadAllText(expectedOutputPath);
                var expectedOutput     = JToken.Parse(expectedOutputJson);

                try
                {
                    Assert.True(DeepEquals(expectedOutput, actualOutput, true, true),
                                $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}");
                }
                catch (DeepEqualityFailure ex)
                {
                    Assert.True(false,
                                $"Test failed for input {Path.GetFileName(inputPath)}\nExpected:\n{expectedOutput}\nActual:\n{actualOutput}\nMatch Failured: {ex}");
                }

                break;
            }

            case JsonLdTestType.NegativeEvaluationTest:
            {
                var exception = Assert.Throws <JsonLdProcessorException>(() => jsonLdWriter.SerializeStore(input));
                Assert.Equal(expectErrorCode, exception.ErrorCode);
                break;
            }

            case JsonLdTestType.PositiveSyntaxTest:
                var _ = jsonLdWriter.SerializeStore(input);
                break;

            case JsonLdTestType.NegativeSyntaxTest:
                Assert.ThrowsAny <JsonLdProcessorException>(() => jsonLdWriter.SerializeStore(input));
                break;
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PredicateMatchedState"/> class.
 /// </summary>
 public PredicateMatchedState(NQuadsParser parser, int currentLine, Node subjectNode, IriNode predicateNode)
     : base(parser, currentLine)
 {
     _subjectNode = subjectNode;
     _predicateNode = predicateNode;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TripleMatchedState"/> class.
 /// </summary>
 public TripleMatchedState(NQuadsParser parser, int currentLine, Triple triple) : base(parser, currentLine)
 {
     _triple = triple;
 }
Beispiel #26
0
        /// <summary>
        /// Thread Worker method which writes Graphs to the output
        /// </summary>
        /// <param name="globalContext">Context for writing the Store</param>
        private void SaveGraphs(ThreadedStoreWriterContext globalContext)
        {
            try
            {
                Uri u = null;
                while (globalContext.TryGetNextUri(out u))
                {
                    //Get the Graph from the Store
                    IGraph g = globalContext.Store.Graphs[u];

                    //Generate the Graph Output and add to Stream
                    NTriplesWriterContext context = new NTriplesWriterContext(g, new System.IO.StringWriter(), NQuadsParser.AsNTriplesSyntax(this.Syntax), globalContext.PrettyPrint, globalContext.HighSpeedModePermitted);
                    String graphContent           = this.GraphToNQuads(globalContext, context);
                    if (!graphContent.Equals(String.Empty))
                    {
                        try
                        {
                            Monitor.Enter(globalContext.Output);
                            globalContext.Output.WriteLine(graphContent);
                            globalContext.Output.Flush();
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            Monitor.Exit(globalContext.Output);
                        }
                    }
                }
            }
#if !PORTABLE // PCL doesn't provide Thread.Abort() or ThreadAbortException
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
#if !SILVERLIGHT
                Thread.ResetAbort();
#endif
            }
#endif
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }