/// <summary>
        /// Try parse RDF from a given text reader into the store.
        /// </summary>
        /// <param name="reader">The text reader to read from.</param>
        /// <param name="graph">The graph to store the read triples.</param>
        /// <param name="format">RDF format to be read.</param>
        public static void TryParse(TextReader reader, IGraph graph, RdfSerializationFormat format)
        {
            switch (format)
            {
            case RdfSerializationFormat.N3:
                new Notation3Parser().Load(graph, reader); break;

            case RdfSerializationFormat.NTriples:
                new NTriplesParser().Load(graph, reader); break;

#if !NET35
            case RdfSerializationFormat.NQuads:
                new NQuadsParser().Load(new GraphHandler(graph), reader); break;
#endif

            case RdfSerializationFormat.Turtle:
                new TurtleParser().Load(graph, reader); break;

            case RdfSerializationFormat.Json:
                new RdfJsonParser().Load(graph, reader); break;

#if !NET35
            case RdfSerializationFormat.JsonLd:
                new JsonLdParser().Load(new GraphHandler(graph), reader); break;
#endif

            default:
            case RdfSerializationFormat.RdfXml:
                new RdfXmlParser().Load(graph, reader); break;
            }
        }
        private Uri ReadQuadFormat(TextReader reader, Uri graph, RdfSerializationFormat format, bool update)
        {
            using (VirtuosoManager manager = new VirtuosoManager(CreateConnectionString()))
            {
                using (VDS.RDF.ThreadSafeTripleStore store = new VDS.RDF.ThreadSafeTripleStore())
                {
                    VDS.RDF.Parsing.TriGParser parser = new TriGParser();

                    parser.Load(store, reader);

                    foreach (var g in store.Graphs)
                    {
                        if (update)
                        {
                            manager.UpdateGraph(g.BaseUri, g.Triples, new Triple[] { });
                        }
                        else
                        {
                            manager.SaveGraph(g);
                        }
                    }
                }
            }

            return(graph);
        }
Exemple #3
0
        /// <summary>
        /// Loads a serialized graph from the given location into the current store. See allowed <see cref="RdfSerializationFormat">formats</see>.
        /// </summary>
        /// <param name="modelUri">Uri of the graph in this store</param>
        /// <param name="url">Location</param>
        /// <param name="format">Allowed formats</param>
        /// <param name="update">Pass false if you want to overwrite the existing data. True if you want to add the new data to the existing.</param>
        /// <returns></returns>
        public override Uri Read(Uri modelUri, Uri url, RdfSerializationFormat format, bool update)
        {
            Stream stream;

            switch (url.Scheme)
            {
            case "file":
            {
                stream = File.OpenRead(url.LocalPath);
                break;
            }

            case "http":
            case "https":
            {
                HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                stream = response.GetResponseStream();
                break;
            }

            default:
            {
                throw new NotSupportedException($"Unsupported URI schema {url.Scheme}");
            }
            }

            return(Read(stream, modelUri, format, update));
        }
Exemple #4
0
        /// <summary>
        /// Reads model contents from a stream. The method supports importing files and other models stored in the local RDF store.
        /// </summary>
        /// <param name="stream">A stream.</param>
        /// <param name="format">Serialization format <see cref="RdfSerializationFormat"/></param>
        /// <param name="update">Pass false if you want to overwrite existing data. True if you want to keep the data and add the new entries.</param>
        /// <returns>True if the contents of the model were imported, False if not.</returns>
        public bool Read(Stream stream, RdfSerializationFormat format, bool update)
        {
            if (format == RdfSerializationFormat.Trig)
            {
                throw new ArgumentException("Quadruple serialization formats are not supported by this method. Use IStore.Read() instead.");
            }

            return(_store.Read(stream, Uri, format, update) != null);
        }
Exemple #5
0
        /// <summary>
        /// Writes a serialized graph to the given stream. See allowed <see cref="RdfSerializationFormat">formats</see>.
        /// </summary>
        /// <param name="stream">Stream to which the content should be written.</param>
        /// <param name="graphUri">Uri fo the graph in this store</param>
        /// <param name="format">Allowed formats</param>
        /// <returns></returns>
        public override void Write(Stream stream, Uri graphUri, RdfSerializationFormat format)
        {
            var query = $"SELECT * {{ GRAPH <{graphUri.AbsoluteUri}> {{ ?s ?p ?o . }} }}";

            var result = ExecuteQuery(query);

            if (result.SparqlResultSet.Result)
            {
                using (var graphEmpty = new Graph())
                    using (var writer = new StreamWriter(stream))
                    {
                        var triples = result.SparqlResultSet.ToTripleCollection(graphEmpty);

                        using (var graph = new Graph(triples))
                        {
                            graph.BaseUri = graphUri;

                            switch (format)
                            {
                            case RdfSerializationFormat.N3:
                                graph.SaveToStream(writer, new Notation3Writer());
                                break;

                            case RdfSerializationFormat.NTriples:
                                graph.SaveToStream(writer, new NTriplesWriter());
                                break;

#if !NET35
                            case RdfSerializationFormat.NQuads:
                                graph.SaveToStream(writer, new NQuadsWriter());
                                break;
#endif

                            case RdfSerializationFormat.Turtle:
                                graph.SaveToStream(writer, new CompressingTurtleWriter());
                                break;

                            case RdfSerializationFormat.Json:
                                graph.SaveToStream(writer, new RdfJsonWriter());
                                break;

#if !NET35
                            case RdfSerializationFormat.JsonLd:
                                graph.SaveToStream(writer, new JsonLdWriter());
                                break;
#endif
                            case RdfSerializationFormat.RdfXml:
                                graph.SaveToStream(writer, new RdfXmlWriter());
                                break;

                            default:
                                throw new ArgumentOutOfRangeException(nameof(format), format, null);
                            }
                        }
                    }
            }
        }
Exemple #6
0
 public IModel Import(Uri graphUri, Uri location, RdfSerializationFormat format)
 {
     lock (ThreadDictionary)
     {
         if (ThreadDictionary.ContainsKey(Thread.CurrentThread))
         {
             Uri t = ThreadDictionary[Thread.CurrentThread].Read(graphUri, location, format, false);
         }
     }
     return(null);
 }
Exemple #7
0
 public void Write(Stream stream, Uri graphUri, RdfSerializationFormat format)
 {
     if (_store.HasGraph(graphUri))
     {
         IGraph graph = _store.Graphs[graphUri];
         using (StreamWriter writer = new StreamWriter(stream))
         {
             graph.SaveToStream(writer, GetWriter(format));
         }
     }
 }
Exemple #8
0
        public void UpdateOntologies(IEnumerable <Semiodesk.Trinity.Configuration.Ontology> ontologies)
        {
            foreach (Semiodesk.Trinity.Configuration.Ontology onto in ontologies)
            {
                Uri path = GetPathFromSource(onto.FileSource);

                RdfSerializationFormat format = GetSerializationFormatFromUri(path);

                _store.Read(onto.Uri, path, format, false);
            }
        }
Exemple #9
0
        private Uri ReadRemoteTripleFormat(Uri graph, Uri location, RdfSerializationFormat format)
        {
            using (VDS.RDF.Storage.VirtuosoManager m = new VDS.RDF.Storage.VirtuosoManager(CreateConnectionString()))
            {
                using (VDS.RDF.Graph g = new VDS.RDF.Graph())
                {
                    UriLoader.Load(g, location);
                    g.BaseUri = graph;
                    m.SaveGraph(g);
                }
            }

            return(graph);
        }
Exemple #10
0
 public Uri Read(Stream stream, Uri graph, RdfSerializationFormat format, bool update)
 {
     using (TextReader reader = new StreamReader(stream))
     {
         if (format == RdfSerializationFormat.Trig)
         {
             return(ReadQuadFormat(reader, graph, format, update));
         }
         else
         {
             return(ReadTripleFormat(reader, graph, format, update));
         }
     }
 }
Exemple #11
0
        public Uri Read(Stream stream, Uri graphUri, RdfSerializationFormat format, bool update)
        {
            TextReader reader = new StreamReader(stream);
            IGraph     graph  = new Graph();
            IRdfReader parser = GetReader(format);

            parser.Load(graph, reader);
            graph.BaseUri = graphUri;
            if (!update)
            {
                _store.Remove(graphUri);
            }
            _store.Add(graph, update);
            return(graphUri);
        }
Exemple #12
0
        public IModel Export(FileInfo target, Uri graphUri, RdfSerializationFormat format)
        {
            if (target.Exists)
            {
                target.Delete();
            }

            FileStream s = target.OpenWrite();

            lock (ThreadDictionary)
            {
                if (ThreadDictionary.ContainsKey(Thread.CurrentThread))
                {
                    ThreadDictionary[Thread.CurrentThread].Write(s, graphUri, format);
                }
            }
            return(null);
        }
        public override Uri Read(Stream stream, Uri graph, RdfSerializationFormat format, bool update)
        {
            using (TextReader reader = new StreamReader(stream))
            {
#if !NET35
                if (format == RdfSerializationFormat.Trig || format == RdfSerializationFormat.NQuads || format == RdfSerializationFormat.JsonLd)
#else
                if (format == RdfSerializationFormat.Trig)
#endif
                {
                    return(ReadQuadFormat(reader, graph, format, update));
                }
                else
                {
                    return(ReadTripleFormat(reader, graph, format, update));
                }
            }
        }
Exemple #14
0
        public static IRdfReader GetReader(RdfSerializationFormat format)
        {
            switch (format)
            {
            case RdfSerializationFormat.N3:
                return(new Notation3Parser());

            case RdfSerializationFormat.NTriples:
                return(new NTriplesParser());

            case RdfSerializationFormat.Turtle:
                return(new TurtleParser());

            default:
            case RdfSerializationFormat.RdfXml:
                return(new RdfXmlParser());
            }
        }
Exemple #15
0
        public static IRdfWriter GetWriter(RdfSerializationFormat format)
        {
            switch (format)
            {
            case RdfSerializationFormat.N3:
                return(new Notation3Writer());

            case RdfSerializationFormat.NTriples:
                return(new NTriplesWriter());

            case RdfSerializationFormat.Turtle:
                return(new CompressingTurtleWriter());

            default:
            case RdfSerializationFormat.RdfXml:
                return(new RdfXmlWriter());
            }
        }
Exemple #16
0
        /// <summary>
        /// This method loads the given ontologies into the provided store.
        /// A model will be created for each ontology. If it already exists, it wil be replaced.
        /// </summary>
        /// <param name="ontologies">A collection of ontologies to be loaded.</param>
        public void UpdateOntologies(IEnumerable <Semiodesk.Trinity.Configuration.IOntologyConfiguration> ontologies)
        {
            foreach (var onto in ontologies)
            {
                if (!string.IsNullOrEmpty(onto.Location))
                {
                    Uri path = GetPathFromLocation(onto.Location);

                    RdfSerializationFormat format = GetSerializationFormatFromUri(path);

                    _store.Read(onto.Uri, path, format, false);
                }
                else
                {
                    throw new ArgumentException(string.Format("The file for the ontology {0} ({1}) could not be found. Please check the configuration file.", onto.Prefix, onto.Uri));
                }
            }
        }
        /// <summary>
        /// Loads a serialized graph from the given stream into the current store. See allowed <see cref="RdfSerializationFormat">formats</see>.
        /// </summary>
        /// <param name="stream">Stream containing a serialized graph</param>
        /// <param name="graphUri">Uri of the graph in this store</param>
        /// <param name="format">Allowed formats</param>
        /// <param name="update">Pass false if you want to overwrite the existing data. True if you want to add the new data to the existing.</param>
        /// <returns></returns>
        public override Uri Read(Stream stream, Uri graphUri, RdfSerializationFormat format, bool update)
        {
            using (TextReader reader = new StreamReader(stream))
            {
                IGraph graph = new Graph();

                TryParse(reader, graph, format);

                graph.BaseUri = graphUri;

                if (!update)
                {
                    _store.Remove(graphUri);
                }

                _store.Add(graph, update);

                return(graphUri);
            }
        }
Exemple #18
0
        public void Write(Stream fs, Uri graph, RdfSerializationFormat format)
        {
            using (VDS.RDF.Storage.VirtuosoManager m = new VDS.RDF.Storage.VirtuosoManager(CreateConnectionString()))
            {
                using (VDS.RDF.Graph g = new VDS.RDF.Graph())
                {
                    m.LoadGraph(g, graph);

                    StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

                    switch (format)
                    {
                    case RdfSerializationFormat.RdfXml:
                    {
                        VDS.RDF.Writing.RdfXmlWriter wr = new VDS.RDF.Writing.RdfXmlWriter();
                        wr.Save(g, sw);
                        break;
                    }
                    }
                }
            }
        }
        /// <summary>
        /// Writes a serialized graph to the given stream. See allowed <see cref="RdfSerializationFormat">formats</see>.
        /// </summary>
        /// <param name="stream">Stream to which the content should be written.</param>
        /// <param name="graphUri">Uri fo the graph in this store</param>
        /// <param name="format">Allowed formats</param>
        /// <returns></returns>
        public override void Write(Stream stream, Uri graphUri, RdfSerializationFormat format)
        {
            if (_store.HasGraph(graphUri))
            {
                IGraph graph = _store.Graphs[graphUri];

                using (StreamWriter writer = new StreamWriter(stream))
                {
                    switch (format)
                    {
                    case RdfSerializationFormat.N3:
                        graph.SaveToStream(writer, new Notation3Writer()); break;

                    case RdfSerializationFormat.NTriples:
                        graph.SaveToStream(writer, new NTriplesWriter()); break;

#if !NET35
                    case RdfSerializationFormat.NQuads:
                        graph.SaveToStream(writer, new NQuadsWriter()); break;
#endif

                    case RdfSerializationFormat.Turtle:
                        graph.SaveToStream(writer, new CompressingTurtleWriter()); break;

                    case RdfSerializationFormat.Json:
                        graph.SaveToStream(writer, new RdfJsonWriter()); break;

#if !NET35
                    case RdfSerializationFormat.JsonLd:
                        graph.SaveToStream(writer, new JsonLdWriter()); break;
#endif

                    default:
                    case RdfSerializationFormat.RdfXml:
                        graph.SaveToStream(writer, new RdfXmlWriter()); break;
                    }
                }
            }
        }
Exemple #20
0
        private Uri ReadTripleFormat(TextReader reader, Uri graphUri, RdfSerializationFormat format, bool update)
        {
            using (VDS.RDF.Storage.VirtuosoManager m = new VDS.RDF.Storage.VirtuosoManager(CreateConnectionString()))
            {
                using (VDS.RDF.Graph graph = new VDS.RDF.Graph())
                {
                    IRdfReader parser = dotNetRDFStore.GetReader(format);
                    parser.Load(graph, reader);
                    graph.BaseUri = graphUri;
                    if (update)
                    {
                        m.UpdateGraph(graphUri, graph.Triples, new Triple[] {});
                    }
                    else
                    {
                        m.SaveGraph(graph);
                    }
                }
            }

            return(graphUri);
        }
        private Uri ReadTripleFormat(TextReader reader, Uri graphUri, RdfSerializationFormat format, bool update)
        {
            using (VirtuosoManager manager = new VirtuosoManager(CreateConnectionString()))
            {
                using (VDS.RDF.Graph graph = new VDS.RDF.Graph())
                {
                    dotNetRDFStore.TryParse(reader, graph, format);

                    graph.BaseUri = graphUri;

                    if (update)
                    {
                        manager.UpdateGraph(graphUri, graph.Triples, new Triple[] { });
                    }
                    else
                    {
                        manager.SaveGraph(graph);
                    }
                }
            }

            return(graphUri);
        }
        public override void Write(Stream fs, Uri graph, RdfSerializationFormat format)
        {
            using (VirtuosoManager manager = new VirtuosoManager(CreateConnectionString()))
            {
                using (VDS.RDF.Graph g = new VDS.RDF.Graph())
                {
                    manager.LoadGraph(g, graph);

                    StreamWriter streamWriter = new StreamWriter(fs, Encoding.UTF8);

                    switch (format)
                    {
                    case RdfSerializationFormat.RdfXml:
                    {
                        VDS.RDF.Writing.RdfXmlWriter xmlWriter = new VDS.RDF.Writing.RdfXmlWriter();

                        xmlWriter.Save(g, streamWriter);

                        break;
                    }
                    }
                }
            }
        }
        public override Uri Read(Uri graph, Uri url, RdfSerializationFormat format, bool update)
        {
            // Note: Accessing the file scheme here throws an exception in case the URL is relative..
            if (url.IsFile)
            {
                string path = GetLocalPathFromUrl(url);

                using (TextReader reader = File.OpenText(path))
                {
                    if (format == RdfSerializationFormat.Trig)
                    {
                        return(ReadQuadFormat(reader, graph, format, update));
                    }
                    else
                    {
                        return(ReadTripleFormat(reader, graph, format, update));
                    }
                }
            }
            else if (url.Scheme == "http")
            {
                if (format == RdfSerializationFormat.Trig)
                {
                    throw new Exception("Loading of remote trig files is not supported yet.");
                }
                else
                {
                    return(ReadRemoteTripleFormat(graph, url, format));
                }
            }
            else
            {
                string msg = string.Format("Unkown URL scheme {0}", url.Scheme);
                throw new ArgumentException(msg);
            }
        }
Exemple #24
0
 public bool Read(Stream stream, RdfSerializationFormat format, bool update)
 {
     throw new NotSupportedException();
 }
Exemple #25
0
 /// <summary>
 /// Loads a serialized graph from the given location into the current store. See allowed <see cref="RdfSerializationFormat">formats</see>.
 /// </summary>
 /// <param name="modelUri">Uri of the graph in this store</param>
 /// <param name="url">Location</param>
 /// <param name="format">Allowed formats</param>
 /// <param name="update">Pass false if you want to overwrite the existing data. True if you want to add the new data to the existing.</param>
 /// <returns></returns>
 public abstract Uri Read(Uri modelUri, Uri url, RdfSerializationFormat format, bool update);
Exemple #26
0
 /// <summary>
 /// Loads a serialized graph from the given stream into the current store. See allowed <see cref="RdfSerializationFormat">formats</see>.
 /// </summary>
 /// <param name="stream">Stream containing a serialized graph</param>
 /// <param name="graphUri">Uri of the graph in this store</param>
 /// <param name="format">Allowed formats</param>
 /// <param name="update">Pass false if you want to overwrite the existing data. True if you want to add the new data to the existing.</param>
 /// <returns></returns>
 public abstract Uri Read(Stream stream, Uri graphUri, RdfSerializationFormat format, bool update);
Exemple #27
0
        /// <summary>
        /// Loads a serialized graph from the given stream into the current store. See allowed <see cref="RdfSerializationFormat">formats</see>.
        /// </summary>
        /// <param name="stream">Stream containing a serialized graph</param>
        /// <param name="modelUri">Uri of the graph in this store</param>
        /// <param name="format">Allowed formats</param>
        /// <param name="update">Pass false if you want to overwrite the existing data. True if you want to add the new data to the existing.</param>
        /// <returns></returns>
        public override Uri Read(Stream stream, Uri modelUri, RdfSerializationFormat format, bool update)
        {
            if (!update)
            {
                // Clear the graph.
                RemoveModel(modelUri);
            }

            // Number of triples to write simultanously.
            const uint bulkSize = 100;

            // Collect the triples that have been read.
            List <Triple> triples = new List <Triple>();

            // A handler which will fill the triples up to the bulk size.
            StardogRdfHandler handler = new StardogRdfHandler()
            {
                OnReadTriple = (e, t) =>
                {
                    if (triples.Count < bulkSize)
                    {
                        triples.Add(t);
                    }
                    else
                    {
                        _connector.UpdateGraph(modelUri, triples, null);

                        triples.Clear();
                    }
                },
                OnReadEnded = (e, ok) =>
                {
                    if (triples.Count > 0)
                    {
                        _connector.UpdateGraph(modelUri, triples, null);
                    }
                }
            };

            using (var reader = new StreamReader(stream))
            {
                switch (format)
                {
                case RdfSerializationFormat.N3:
                    new Notation3Parser().Load(handler, reader);
                    break;

                case RdfSerializationFormat.NTriples:
                    new NTriplesParser().Load(handler, reader);
                    break;

                case RdfSerializationFormat.Trig:
                    new TriGParser().Load(handler, reader);
                    break;

#if !NET35
                case RdfSerializationFormat.NQuads:
                    new NQuadsParser().Load(handler, reader);
                    break;
#endif

                case RdfSerializationFormat.Turtle:
                    new TurtleParser().Load(handler, reader);
                    break;

                case RdfSerializationFormat.Json:
                    new RdfJsonParser().Load(handler, reader);
                    break;

#if !NET35
                case RdfSerializationFormat.JsonLd:
                    new JsonLdParser().Load(handler, reader);
                    break;
#endif

                case RdfSerializationFormat.RdfXml:
                    new RdfXmlParser().Load(handler, reader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(format), format, null);
                }
            }

            return(modelUri);
        }
 public System.IO.MemoryStream Write(Uri graphUri, RdfSerializationFormat format)
 {
     throw new NotSupportedException();
 }
 public Uri Read(Uri graphUri, Uri url, RdfSerializationFormat format)
 {
     throw new NotSupportedException();
 }
Exemple #30
0
 /// <summary>
 /// Writes a serialized graph to the given stream. See allowed <see cref="RdfSerializationFormat">formats</see>.
 /// </summary>
 /// <param name="fs">Stream to which the content should be written.</param>
 /// <param name="graphUri">Uri fo the graph in this store</param>
 /// <param name="format">Allowed formats</param>
 /// <returns></returns>
 public abstract void Write(Stream fs, Uri graphUri, RdfSerializationFormat format);