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}");
                }
            }
        }
示例#2
0
        protected void ImportData(string dataPath, string defaultGraphUri = null)
        {
            var g        = new Graph();
            var importId = Guid.NewGuid();

#if PORTABLE
            using (var s = File.OpenRead(dataPath))
            {
                StreamLoader.Load(g, dataPath, s);
            }
#else
            FileLoader.Load(g, dataPath);
#endif
            _bnodeMappings = new Dictionary <string, string>();
            var sw       = new StringWriter();
            var ntWriter = new NQuadsWriter(sw);
            foreach (var t in g.Triples)
            {
                if (t.Object.NodeType == NodeType.Literal)
                {
                    var litNode = t.Object as LiteralNode;
                    ntWriter.Triple(
                        GetNodeString(t.Subject),
                        false,
                        GetNodeString(t.Predicate),
                        false,
                        litNode.Value,
                        false,
                        true,
                        litNode.DataType == null ? null : litNode.DataType.ToString(),
                        litNode.Language,
                        defaultGraphUri ?? Constants.DefaultGraphUri
                        );
                }
                else
                {
                    ntWriter.Triple(
                        GetNodeString(t.Subject),
                        false,
                        GetNodeString(t.Predicate),
                        false,
                        GetNodeString(t.Object),
                        false,
                        false,
                        null,
                        null,
                        defaultGraphUri ?? Constants.DefaultGraphUri
                        );
                }
            }
            _service.ExecuteTransaction(_storeName, new UpdateTransactionData {
                InsertData = sw.ToString()
            });
            //_store.Commit(Guid.NewGuid());
        }
        private static string MakeNQuadsList(ITripleStore store)
        {
            var ser = new NQuadsWriter();

            using (var expectedTextWriter = new System.IO.StringWriter())
            {
                ser.Save(store, expectedTextWriter);
                var lines = expectedTextWriter.ToString().Split('\n').Select(x => x.Trim()).ToList();
                lines.Sort();
                return(String.Join(Environment.NewLine, lines));
            }
        }
        public static IEnumerable <string> ToRdf(JToken token, JsonLdProcessorOptions options)
        {
            var jsonLdParser = new JsonLdParser(options);
            var store        = new TripleStore();

            jsonLdParser.Load(store, new StringReader(token.ToString(Newtonsoft.Json.Formatting.None)));

            var nqWriter = new NQuadsWriter(NQuadsSyntax.Rdf11);

            using var expectedTextWriter = new System.IO.StringWriter();
            nqWriter.Save(store, expectedTextWriter);
            return(expectedTextWriter.ToString().Split(Environment.NewLine).Where(x => !string.IsNullOrWhiteSpace(x)));
        }
示例#5
0
        private static void GetRecord(string leiCode, TextWriter output, AllegroGraphConnector allegroGraphConnector)
        {
            var g = new Graph();

            allegroGraphConnector.LoadGraph(g, "graph://lei.info/" + leiCode);
            var ts = new TripleStore();

            ts.Add(g);
            var nqw = new NQuadsWriter();
            var s   = VDS.RDF.Writing.StringWriter.Write(ts, nqw);

            output.Write(s);
            output.Flush();
            ts.Remove(g.BaseUri);
            g.Clear();
            ts.Dispose();
            g.Dispose();
        }
示例#6
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();
            }
        }