/// <summary>
 /// Creates a new Threaded SQL Store Writer Context
 /// </summary>
 /// <param name="store">Triple Store to save</param>
 /// <param name="manager">SQL IO Manager</param>
 /// <param name="threads">Number of threads to use</param>
 /// <param name="clearIfExists">Whether to clear graphs if they already exist</param>
 public ThreadedSqlStoreWriterContext(ITripleStore store, IThreadedSqlIOManager manager, int threads, bool clearIfExists)
 {
     this._store = store;
     this._manager = manager;
     this._writeThreads = threads;
     this._clearIfExists = clearIfExists;
 }
        /// <summary>
        /// Writes the given Triple Store to a String and returns the output in your chosen concrete RDF dataset syntax
        /// </summary>
        /// <param name="store">Triple Store</param>
        /// <param name="writer">Writer to use to generate conrete RDF Syntax</param>
        /// <returns></returns>
        public static String Write(ITripleStore store, IStoreWriter writer)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            writer.Save(store, new TextWriterParams(sw));

            return sw.ToString();
        }
 /// <summary>
 /// Creates a new Folder Store Parser Context
 /// </summary>
 /// <param name="store">Triple Store to parse into</param>
 /// <param name="folder">Folder</param>
 /// <param name="format">Format</param>
 /// <param name="threads">Threads to use</param>
 public FolderStoreParserContext(ITripleStore store, String folder, FolderStoreFormat format, int threads)
     : base(store)
 {
     this._folder = folder;
     this._format = format;
     this._threads = threads;
 }
Exemple #4
0
        /// <summary>
        /// Creates a new SPARQL View
        /// </summary>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <param name="store">Triple Store to query</param>
        public BaseSparqlView(String sparqlQuery, ITripleStore store)
        {
            SparqlQueryParser parser = new SparqlQueryParser();
            this._q = parser.ParseFromString(sparqlQuery);
            this._store = store;

            this._async = new UpdateViewDelegate(this.UpdateViewInternal);
            this.Initialise();
        }
        /// <summary>
        /// Saves a Store in TriX format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            //Try and get the TextWriter to output to
            TextWriter output = null;
            if (parameters is StreamParams)
            {
                output = ((StreamParams)parameters).StreamWriter;
            } 
            else if (parameters is TextWriterParams)
            {
                output = ((TextWriterParams)parameters).TextWriter;
            }

            if (output != null)
            {
                try
                {
                    //Setup the XML document
                    XmlWriter writer = XmlWriter.Create(output, this.GetSettings());
                    writer.WriteStartDocument();
                    writer.WriteStartElement("TriX", TriXParser.TriXNamespaceURI);
                    writer.WriteStartAttribute("xmlns");
                    writer.WriteRaw(TriXParser.TriXNamespaceURI);
                    writer.WriteEndAttribute();

                    //Output Graphs as XML <graph> elements
                    foreach (IGraph g in store.Graphs) 
                    {
                        this.GraphToTriX(g, writer);
                    }

                    //Save the XML to disk
                    writer.WriteEndDocument();
                    writer.Close();
                    output.Close();
                }
                catch
                {
                    try
                    {
                        output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the TriXWriter must be of the type StreamParams/TextWriterParams");
            }
        }
        private static IUriNode FindBlankNodeOwner(this IBlankNode blankNode, ITripleStore tripleStore)
        {
            INode current = blankNode;
            while ((current = tripleStore.Triples.Where(triple => triple.Object.Equals(current)).Select(triple => triple.Subject).FirstOrDefault()) != null)
            {
                if (current is IUriNode)
                {
                    return (IUriNode)current;
                }
            }

            return null;
        }
Exemple #7
0
        /// <summary>
        /// Saves the given Triple Store to the SQL Store that this class was instantiated with
        /// </summary>
        /// <param name="store">Store you wish to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        public virtual void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is ISQLIOParams)
            {
                SqlIOParams writeParams = (SqlIOParams)parameters;
                writeParams.Manager.PreserveState = true;
                SqlWriter writer = new SqlWriter(writeParams.Manager);

                foreach (IGraph g in store.Graphs)
                {
                    writer.Save(g, writeParams.ClearIfExists);
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the SQLStoreWriter must implement the interface ISQLIOParams");
            }
        }
        /// <summary>
        /// Saves the given Triple Store to an arbitrary store
        /// </summary>
        /// <param name="store">Store to Save</param>
        /// <param name="parameters">Parameters for the Store</param>
        /// <remarks>
        /// Parameters must be of type <see cref="GenericIOParams">GenericIOParams</see>
        /// </remarks>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is GenericIOParams)
            {
                //Create the Writer Context
                GenericStoreWriterContext context = new GenericStoreWriterContext(store, (GenericIOParams)parameters);

                //Queue Graphs for Writing
                foreach (IGraph g in store.Graphs)
                {
                    context.Add(g.BaseUri);
                }
                
                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                WriteGraphsDelegate d = new WriteGraphsDelegate(this.WriteGraphs);
                for (int i = 0; i < context.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());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the GenericStoreWriter must be of type GenericIOParams");
            }
        }
Exemple #9
0
 /// <summary>
 /// Creates a new TriG Parser Context with custom settings.
 /// </summary>
 /// <param name="store">Store to parse into.</param>
 /// <param name="tokeniser">Tokeniser to use.</param>
 /// <param name="traceParsing">Whether to trace parsing.</param>
 /// <param name="traceTokeniser">Whether to trace tokenisation.</param>
 public TriGParserContext(ITripleStore store, ITokeniser tokeniser, bool traceParsing, bool traceTokeniser)
     : this(store, tokeniser, TokenQueueMode.SynchronousBufferDuringParsing, traceParsing, traceTokeniser)
 {
 }
Exemple #10
0
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="folder">Folder to write to</param>
 public FolderStoreWriterContext(ITripleStore store, String folder)
 {
     this._store  = store;
     this._folder = folder;
 }
Exemple #11
0
 /// <summary>
 /// Creates a new TriG Parser Context with default settings.
 /// </summary>
 /// <param name="store">Store to parse into.</param>
 /// <param name="tokeniser">Tokeniser to use.</param>
 public TriGParserContext(ITripleStore store, ITokeniser tokeniser)
     : base(store, tokeniser)
 {
 }
Exemple #12
0
        /// <summary>
        /// Saves a Triple Store to CSV Format
        /// </summary>
        /// <param name="store">Triple Store to save</param>
        /// <param name="parameters">A set of <see cref="StreamParams">StreamParams</see></param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter);
            }

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

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

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                SaveGraphsDeletegate d = new SaveGraphsDeletegate(this.SaveGraphs);
                for (int i = 0; i < this._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());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("CSV"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }
                context.Output.Close();

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the CsvStoreWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Exemple #13
0
 /// <summary>
 /// Creates a new set of Triple Store Event Arguments
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="g">Graph</param>
 public TripleStoreEventArgs(ITripleStore store, IGraph g)
     : this(store, new GraphEventArgs(g)) { }
 /// <summary>
 /// Loads a RDF Dataset from an Embedded Resource
 /// </summary>
 /// <param name="store">Store to load into</param>
 /// <param name="resource">Assembly Qualified Name of the Resource to load</param>
 /// <param name="parser">Parser to use (leave null for auto-selection)</param>
 public static void Load(ITripleStore store, String resource, IStoreReader parser)
 {
     if (store == null) throw new RdfParseException("Cannot read RDF Dataset into a null Store");
     EmbeddedResourceLoader.Load(new StoreHandler(store), resource, parser);
 }
Exemple #15
0
        /// <summary>
        /// Parses a raw RDF Dataset String (attempts to auto-detect the format)
        /// </summary>
        /// <param name="store">Store to load into</param>
        /// <param name="data">Raw RDF Dataset String</param>
        /// <remarks>
        /// <p>
        /// Auto-detection is based on testing the string to see if it contains certain keyword constructs which might indicate a particular syntax has been used.  This detection may not always be accurate.
        /// </p>
        /// </remarks>
        public static void ParseDataset(ITripleStore store, String data)
        {
            if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Graph");
            if (data == null) return;

            //Try to guess the format
            String format = "Unknown";
            try
            {
                if (data.Contains("<?xml") && data.Contains("<TriX"))
                {
                    //Probably TriX
                    format = "TriX";
                    ParseDataset(store, data, new TriXParser());
                }
                else if (data.Contains("@prefix") || data.Contains("@base"))
                {
                    //Probably TriG
                    format = "TriG";
                    ParseDataset(store, data, new TriGParser());
                }
                else
                {
                    //Take a stab at it being NQuads
                    //No real way to test as there's nothing particularly distinctive in NQuads
                    format = "NQuads";
                    ParseDataset(store, data, new NQuadsParser());
                }
            }
            catch (RdfParseException parseEx)
            {
                //Wrap the exception in an informational exception about what we guessed
                throw new RdfParseException("StringParser failed to parse the RDF Dataset string correctly, StringParser auto-detection guessed '" + format + "' but this failed to parse.  RDF Dataset string may be malformed or StringParser may have guessed incorrectly", parseEx);
            }
        }
Exemple #16
0
 /// <summary>
 /// Creates a new Folder Store Parser Context
 /// </summary>
 /// <param name="store">Triple Store to parse into</param>
 /// <param name="folder">Folder</param>
 /// <param name="format">Format</param>
 public FolderStoreParserContext(ITripleStore store, String folder, FolderStoreFormat format)
     : this(store, folder, format, 8)
 {
 }
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="writer">Writer to save to</param>
        public void Save(ITripleStore store, TextWriter writer)
        {
            if (store == null)
            {
                throw new RdfOutputException("Cannot output a null Triple Store");
            }
            if (writer == null)
            {
                throw new RdfOutputException("Cannot output to a null writer");
            }

            ThreadedStoreWriterContext context = new ThreadedStoreWriterContext(store, writer, this._prettyPrint, false);

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

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

                    //Start making the async calls
                    List <IAsyncResult> results = new List <IAsyncResult>();
                    SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                    for (int i = 0; i < this._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());
                    RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                    foreach (IAsyncResult result in results)
                    {
                        try
                        {
                            d.EndInvoke(result);
                        }
                        catch (Exception ex)
                        {
                            outputEx.AddException(ex);
                        }
                    }
                    context.Output.Close();

                    //If there were any errors we'll throw an RdfThreadedOutputException now
                    if (outputEx.InnerExceptions.Any())
                    {
                        throw outputEx;
                    }
                }
                else
                {
                    foreach (IGraph g in context.Store.Graphs)
                    {
                        NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                        foreach (Triple t in g.Triples)
                        {
                            context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                        }
                    }
                    context.Output.Close();
                }
            }
            catch
            {
                try
                {
                    context.Output.Close();
                }
                catch
                {
                    //Just cleaning up
                }
                throw;
            }
        }
        public static void LoadTestFile(this ITripleStore store, string fileName)
        {
            string resource = GetResourceName(fileName);

            store.LoadFromEmbeddedResource(resource);
        }
        private void AssertGraphsEqual(IGraph expectedGraph, IGraph actualGraph, ITripleStore expectedStore, ITripleStore actualStore, string testFile)
        {
            var graphsEqual = actualGraph.Equals(expectedGraph, out _);

            if (!graphsEqual)
            {
                var expectedLines = MakeNQuadsList(expectedStore);
                var actualLines   = MakeNQuadsList(actualStore);
                Assert.True(graphsEqual,
                            $"Test failed for input {testFile}.\r\nGraph {expectedGraph.BaseUri} differs in actual output from expected output.\r\nExpected:\r\n{expectedLines}\r\nActual:\r\n{actualLines}");
            }
        }
 /// <summary>
 /// Creates a new Threaded SQL Store Writer Context
 /// </summary>
 /// <param name="store">Triple Store to save</param>
 /// <param name="manager">SQL IO Manager</param>
 public ThreadedSqlStoreWriterContext(ITripleStore store, IThreadedSqlIOManager manager)
     : this(store, manager, 8, false) { }
Exemple #21
0
 /// <summary>
 /// Saves a triple store to an XML writer in GraphML format.
 /// </summary>
 /// <param name="store">The source triple store.</param>
 /// <param name="output">The target XML writer.</param>
 public void Save(ITripleStore store, XmlWriter output)
 {
     GraphMLWriter.WriteGraphML(output, store, this.CollapseLiterals);
 }
 /// <summary>
 /// Creates a new Threaded SQL Store Writer Context
 /// </summary>
 /// <param name="store">Triple Store to save</param>
 /// <param name="params">Threaded SQL IO Parameters</param>
 public ThreadedSqlStoreWriterContext(ITripleStore store, ThreadedSqlIOParams @params)
     : this(store, @params.ThreadedManager, @params.Threads, @params.ClearIfExists) { }
Exemple #23
0
 /// <summary>
 /// Creates a new Generic Store Writer Context
 /// </summary>
 /// <param name="store">Triple Store that will be written to the underlying Store</param>
 /// <param name="parameters">Generic IO Parameters</param>
 public GenericStoreWriterContext(ITripleStore store, GenericIOParams parameters)
 {
     this._store  = store;
     this._params = parameters;
 }
 /// <summary>Sets up the <paramref name="factory"/> with components required to use dotNetRDF and supplies a triple store instance.</summary>
 public static EntityContextFactory WithDotNetRDF(this EntityContextFactory factory, ITripleStore store)
 {
     ((IComponentRegistryFacade)factory).Register(store);
     return(WithDotNetRDF(factory));
 }
Exemple #25
0
 /// <summary>
 /// Creates a new Threaded Store Writer Context with custom settings
 /// </summary>
 /// <param name="store">Store to be written</param>
 /// <param name="output">TextWriter to write to</param>
 /// <param name="prettyPrint">Pretty Print Mode</param>
 /// <param name="hiSpeedAllowed">High Speed Mode</param>
 public ThreadedStoreWriterContext(ITripleStore store, TextWriter output, bool prettyPrint, bool hiSpeedAllowed)
     : base(store, output, prettyPrint, hiSpeedAllowed)
 {
 }
Exemple #26
0
 /// <summary>
 /// Loads the contents of the given File into a Triple Store providing the RDF dataset format can be determined
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="filename">File to load from</param>
 /// <remarks>
 /// <para>
 /// The <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file.  This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports.  If this suceeds then a parser is chosen and used to parse the input file.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, String filename)
 {
     Load(store, filename, null);
 }
        /// <summary>
        /// Saves the given Store to Disk using the settings the Writer was instantiated with
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating where to save to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (parameters is FolderStoreParams)
            {
                //Get the Parameters
                FolderStoreWriterContext context = new FolderStoreWriterContext(store, (FolderStoreParams)parameters);

                //Create the Folder
                if (!Directory.Exists(context.Folder))
                {
                    Directory.CreateDirectory(context.Folder);
                }

                //Write list of Graphs and Queue URIs of Graphs for writing by the async calls
                StreamWriter graphlist = new StreamWriter(Path.Combine(context.Folder,"graphs.fstore"));
                String ext;
                switch (context.Format)
                {
                    case FolderStoreFormat.Turtle:
                        ext = ".ttl";
                        break;
                    case FolderStoreFormat.Notation3:
                        ext = ".n3";
                        break;
                    case FolderStoreFormat.RdfXml:
                        ext = ".rdf";
                        break;
                    default:
                        ext = ".ttl";
                        break;
                }
                graphlist.WriteLine(ext);
                foreach (Uri u in context.Store.Graphs.GraphUris)
                {
                    context.Add(u);
                    graphlist.WriteLine(u.GetSha256Hash() + ext);
                }
                graphlist.Close();

                //Start making the async calls
                List<IAsyncResult> results = new List<IAsyncResult>();
                SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs);
                for (int i = 0; i < context.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());
                RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("Folder Store"));
                foreach (IAsyncResult result in results)
                {
                    try
                    {
                        d.EndInvoke(result);
                    }
                    catch (Exception ex)
                    {
                        outputEx.AddException(ex);
                    }
                }

                //If there were any errors we'll throw an RdfThreadedOutputException now
                if (outputEx.InnerExceptions.Any()) throw outputEx;
            }
            else
            {
                throw new RdfStorageException("Parameters for the FolderStoreWriter must be of type FolderStoreParams");
            }
        }
Exemple #28
0
 /// <summary>
 /// Loads a RDF Dataset from an Embedded Resource.
 /// </summary>
 /// <param name="store">Store to load into.</param>
 /// <param name="resource">Assembly Qualified Name of the Resource to load.</param>
 /// <remarks>
 /// Parser will be auto-selected.
 /// </remarks>
 public static void Load(ITripleStore store, String resource)
 {
     Load(store, resource, null);
 }
Exemple #29
0
 /// <summary>
 /// Creates a new SPARQL View
 /// </summary>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <param name="store">Triple Store to query</param>
 public BaseSparqlView(SparqlParameterizedString sparqlQuery, ITripleStore store)
     : this(sparqlQuery.ToString(), store) { }
Exemple #30
0
 /// <summary>
 /// Loads the contents of the given File into a Triple Store providing the RDF dataset format can be determined
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="filename">File to load from</param>
 /// <param name="parser">Parser to use to parse the given file</param>
 /// <remarks>
 /// <para>
 /// If the <paramref name="parser"/> parameter is set to null then the <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file.  This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports.  If this suceeds then a parser is chosen and used to parse the input file.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, String filename, IStoreReader parser)
 {
     if (store == null) throw new RdfParseException("Cannot read a RDF Dataset into a null Store");
     FileLoader.Load(new StoreHandler(store), filename, parser);
 }
 /// <summary>
 /// Loads Graphs into the store using the settings the Reader was instantiated with
 /// </summary>
 /// <param name="store">Store to load into</param>
 /// <param name="parameters">Parameters indicating where to read from</param>
 public void Load(ITripleStore store, IStoreParams parameters)
 {
     if (store == null) throw new RdfParseException("Cannot read RDF from a Folder Store into a null Triple Store");
     this.Load(new StoreHandler(store), parameters);
 }
Exemple #32
0
 /// <summary>
 /// Loads the contents of the given File into a Triple Store providing the RDF dataset format can be determined
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="filename">File to load from</param>
 /// <remarks>
 /// <para>
 /// The <see cref="FileLoader">FileLoader</see> attempts to select a Store Parser by examining the file extension to select the most likely MIME type for the file.  This assume that the file extension corresponds to one of the recognized file extensions for a RDF dataset format the library supports.  If this suceeds then a parser is chosen and used to parse the input file.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, String filename)
 {
     FileLoader.Load(store, filename, null);
 }
Exemple #33
0
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="folder">Folder to write to</param>
 /// <param name="format">Folder Store Format</param>
 public FolderStoreWriterContext(ITripleStore store, String folder, FolderStoreFormat format)
     : this(store, folder)
 {
     this._format = format;
 }
Exemple #34
0
 /// <summary>
 /// Saves a Store in NQuads format.
 /// </summary>
 /// <param name="store">Store to save.</param>
 /// <param name="writer">Writer to save to.</param>
 public void Save(ITripleStore store, TextWriter writer)
 {
     Save(store, writer, false);
 }
Exemple #35
0
 /// <summary>
 /// Attempts to load a RDF dataset asynchronously from the given URI into the given Triple Store.
 /// </summary>
 /// <param name="store">Triple Store to load into.</param>
 /// <param name="u">URI to attempt to get a RDF dataset from.</param>
 /// <param name="callback">Callback to invoke when the operation completes.</param>
 /// <param name="state">State to pass to the callback.</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// <para>
 /// If the loading completes normally the callback will be invoked normally, if an error occurs it will be invoked and passed an instance of <see cref="AsyncError"/> as the state which contains details of the error and the original state.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, Uri u, TripleStoreCallback callback, Object state)
 {
     Load(store, u, null, callback, state);
 }
Exemple #36
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;
            }
        }
Exemple #37
0
 /// <summary>
 /// Creates a new TrigG Parser Context with custom settings.
 /// </summary>
 /// <param name="store">Store to parse into.</param>
 /// <param name="tokeniser">Tokeniser to use.</param>
 /// <param name="queueMode">Tokeniser Queue Mode.</param>
 public TriGParserContext(ITripleStore store, ITokeniser tokeniser, TokenQueueMode queueMode)
     : base(store, tokeniser, queueMode)
 {
 }
Exemple #38
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;

            if (parameters is StreamParams)
            {
                //Create a new Writer Context
#if !SILVERLIGHT
                ((StreamParams)parameters).Encoding = Encoding.ASCII;
#endif
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyPrint, false);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyPrint, false);
            }

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

                try
                {
                    if (this._multiThreaded)
                    {
                        //Queue the Graphs to be written
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            if (g.BaseUri == null)
                            {
                                context.Add(UriFactory.Create(GraphCollection.DefaultGraphUri));
                            }
                            else
                            {
                                context.Add(g.BaseUri);
                            }
                        }

                        //Start making the async calls
                        List <IAsyncResult> results = new List <IAsyncResult>();
                        SaveGraphsDelegate  d       = new SaveGraphsDelegate(this.SaveGraphs);
                        for (int i = 0; i < this._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());
                        RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (IAsyncResult result in results)
                        {
                            try
                            {
                                d.EndInvoke(result);
                            }
                            catch (Exception ex)
                            {
                                outputEx.AddException(ex);
                            }
                        }
                        context.Output.Close();

                        //If there were any errors we'll throw an RdfThreadedOutputException now
                        if (outputEx.InnerExceptions.Any())
                        {
                            throw outputEx;
                        }
                    }
                    else
                    {
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                            foreach (Triple t in g.Triples)
                            {
                                context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                            }
                        }
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        context.Output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Exemple #39
0
 /// <summary>
 /// Creates a new TriG Parser Context with custom settings.
 /// </summary>
 /// <param name="store">Store to parse into.</param>
 /// <param name="tokeniser">Tokeniser to use.</param>
 /// <param name="queueMode">Tokeniser Queue Mode.</param>
 /// <param name="traceParsing">Whether to trace parsing.</param>
 /// <param name="traceTokeniser">Whether to trace tokenisation.</param>
 public TriGParserContext(ITripleStore store, ITokeniser tokeniser, TokenQueueMode queueMode, bool traceParsing, bool traceTokeniser)
     : base(store, tokeniser, queueMode, traceParsing, traceTokeniser)
 {
 }
Exemple #40
0
 /// <summary>
 /// Attempts to load a RDF dataset from the given URI into the given Triple Store
 /// </summary>
 /// <param name="store">Triple Store to load into</param>
 /// <param name="u">URI to attempt to get a RDF dataset from</param>
 /// <remarks>
 /// <para>
 /// Attempts to select the relevant Store Parser based on the Content Type header returned in the HTTP Response.
 /// </para>
 /// </remarks>
 public static void Load(ITripleStore store, Uri u)
 {
     Load(store, u, null);
 }
Exemple #41
0
 /// <summary>
 /// Creates a new Threaded Store Writer Context with default settings
 /// </summary>
 /// <param name="store">Store to be written</param>
 /// <param name="output">TextWriter to write to</param>
 public ThreadedStoreWriterContext(ITripleStore store, TextWriter output)
     : base(store, output)
 {
 }
Exemple #42
0
 /// <summary>
 /// Creates a new Tokenising Store Parser Context with default settings.
 /// </summary>
 /// <param name="store">Store to parse into.</param>
 /// <param name="tokeniser">Tokeniser to use.</param>
 public TokenisingStoreParserContext(ITripleStore store, ITokeniser tokeniser)
     : base(store)
 {
     _queue = new TokenQueue(tokeniser);
 }
        /// <summary>
        /// Saves a RDF Dataset as GZipped output
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Storage Parameters</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            if (store == null) throw new RdfOutputException("Cannot output a new Triple Store");
            if (parameters == null) throw new RdfOutputException("Cannot output using null parameters");

            if (parameters is StreamParams)
            {
                StreamParams sp = (StreamParams)parameters;
                StreamWriter output = sp.StreamWriter;

                if (output.BaseStream is GZipStream)
                {
                    this._writer.Save(store, sp);
                }
                else
                {
                    this._writer.Save(store, new StreamParams(new GZipStream(output.BaseStream, CompressionMode.Compress)));
                }
            }
            else
            {
                throw new RdfOutputException("GZip Dataset Writers can only write to StreamParams instances");
            }
        }
Exemple #44
0
 /// <summary>
 /// Creates a new Base Store Parser Context.
 /// </summary>
 /// <param name="store">Triple Store.</param>
 public BaseStoreParserContext(ITripleStore store)
     : this(new StoreHandler(store))
 {
 }
 /// <summary>
 /// Loads a RDF Dataset from an Embedded Resource
 /// </summary>
 /// <param name="store">Store to load into</param>
 /// <param name="resource">Assembly Qualified Name of the Resource to load</param>
 /// <remarks>
 /// Parser will be auto-selected
 /// </remarks>
 public static void Load(ITripleStore store, String resource)
 {
     EmbeddedResourceLoader.Load(store, resource, null);
 }
Exemple #46
0
 /// <summary>
 /// Creates a new Base Parser Context.
 /// </summary>
 /// <param name="store">Triple Store.</param>
 /// <param name="traceParsing">Whether to trace parsing.</param>
 public BaseStoreParserContext(ITripleStore store, bool traceParsing)
     : this(new StoreHandler(store), traceParsing)
 {
 }
Exemple #47
0
        /// <summary>
        /// Parses a raw RDF Dataset String using the given Parser
        /// </summary>
        /// <param name="store">Store to load into</param>
        /// <param name="data">Raw RDF Dataset String</param>
        /// <param name="reader">Parser to use</param>
        public static void ParseDataset(ITripleStore store, String data, IStoreReader reader)
        {
            if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Graph");
            if (data == null) return;

            if (reader == null)
            {
                //If no parser supplied then should auto-detect syntax
                ParseDataset(store, data);
            }
            else
            {
                try
                {
                    reader.Load(store, new TextReaderParams(new StringReader(data)));
                }
                catch
                {
                    throw;
                }
            }
        }
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="folder">Folder to write to</param>
 /// <param name="format">Folder Store Format</param>
 public FolderStoreWriterContext(ITripleStore store, String folder, FolderStoreFormat format)
     : this(store, folder)
 {
     this._format = format;
 }
Exemple #49
0
 /// <summary>
 /// Saves a triple store to a text writer in GraphML format.
 /// </summary>
 /// <param name="store">The source triple store.</param>
 /// <param name="output">The target text writer.</param>
 public void Save(ITripleStore store, TextWriter output)
 {
     this.Save(store, output, false);
 }
Exemple #50
0
        /// <summary>
        /// Saves a Store in NQuads format
        /// </summary>
        /// <param name="store">Store to save</param>
        /// <param name="parameters">Parameters indicating a Stream to write to</param>
        public void Save(ITripleStore store, IStoreParams parameters)
        {
            ThreadedStoreWriterContext context = null;
            if (parameters is StreamParams)
            {
                //Create a new Writer Context
            #if !SILVERLIGHT
                ((StreamParams)parameters).Encoding = Encoding.ASCII;
            #endif
                context = new ThreadedStoreWriterContext(store, ((StreamParams)parameters).StreamWriter, this._prettyPrint, false);
            }
            else if (parameters is TextWriterParams)
            {
                context = new ThreadedStoreWriterContext(store, ((TextWriterParams)parameters).TextWriter, this._prettyPrint, false);
            }

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

                try
                {
                    if (this._multiThreaded)
                    {
                        //Queue the Graphs to be written
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            if (g.BaseUri == null)
                            {
                                context.Add(UriFactory.Create(GraphCollection.DefaultGraphUri));
                            }
                            else
                            {
                                context.Add(g.BaseUri);
                            }
                        }

                        //Start making the async calls
                        List<IAsyncResult> results = new List<IAsyncResult>();
                        SaveGraphsDelegate d = new SaveGraphsDelegate(this.SaveGraphs);
                        for (int i = 0; i < this._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());
                        RdfThreadedOutputException outputEx = new RdfThreadedOutputException(WriterErrorMessages.ThreadedOutputFailure("TSV"));
                        foreach (IAsyncResult result in results)
                        {
                            try
                            {
                                d.EndInvoke(result);
                            }
                            catch (Exception ex)
                            {
                                outputEx.AddException(ex);
                            }
                        }
                        context.Output.Close();

                        //If there were any errors we'll throw an RdfThreadedOutputException now
                        if (outputEx.InnerExceptions.Any()) throw outputEx;
                    }
                    else
                    {
                        foreach (IGraph g in context.Store.Graphs)
                        {
                            NTriplesWriterContext graphContext = new NTriplesWriterContext(g, context.Output);
                            foreach (Triple t in g.Triples)
                            {
                                context.Output.WriteLine(this.TripleToNQuads(graphContext, t));
                            }
                        }
                        context.Output.Close();
                    }
                }
                catch
                {
                    try
                    {
                        context.Output.Close();
                    }
                    catch
                    {
                        //Just cleaning up
                    }
                    throw;
                }
            }
            else
            {
                throw new RdfStorageException("Parameters for the NQuadsWriter must be of the type StreamParams/TextWriterParams");
            }
        }
Exemple #51
0
        /// <summary>
        /// Creates a new SPARQL View
        /// </summary>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <param name="store">Triple Store to query</param>
        public BaseSparqlView(SparqlQuery sparqlQuery, ITripleStore store)
        {
            this._q = sparqlQuery;
            this._store = store;

            this._async = new UpdateViewDelegate(this.UpdateViewInternal);
            this.Initialise();
        }
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="params">Folder Store Parameters</param>
 public FolderStoreWriterContext(ITripleStore store, FolderStoreParams @params)
     : this(store, @params.Folder, @params.Format, @params.Threads) { }
        /// <summary>
        /// Loads a RDF Dataset from the NQuads input into the given Triple Store
        /// </summary>
        /// <param name="store">Triple Store to load into</param>
        /// <param name="parameters">Parameters indicating the Stream to read from</param>
        public void Load(ITripleStore store, IStoreParams parameters)
        {
            if (store == null) throw new RdfParseException("Cannot read a RDF dataset into a null Store");

            this.Load(new StoreHandler(store), parameters);
        }
 /// <summary>Initializes a new instance of the <see cref="PersistentTripleStoreSparqlCommandExecutionStrategy" /> class.</summary>
 /// <param name="store">Target in-memory triple store.</param>
 /// <param name="metaGraphUri">Meta-graph uri.</param>
 public PersistentTripleStoreSparqlCommandExecutionStrategy(ITripleStore store, Uri metaGraphUri)
 {
     MetaGraphUri = metaGraphUri;
     Store        = store;
 }
Exemple #55
0
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="params">Folder Store Parameters</param>
 public FolderStoreWriterContext(ITripleStore store, FolderStoreParams @params)
     : this(store, @params.Folder, @params.Format, @params.Threads)
 {
 }
Exemple #56
0
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="folder">Folder to write to</param>
 /// <param name="format">Folder Store Format</param>
 /// <param name="threads">Threads to use</param>
 public FolderStoreWriterContext(ITripleStore store, String folder, FolderStoreFormat format, int threads)
     : this(store, folder, format)
 {
     this._threads = threads;
 }
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="folder">Folder to write to</param>
 /// <param name="format">Folder Store Format</param>
 /// <param name="threads">Threads to use</param>
 public FolderStoreWriterContext(ITripleStore store, String folder, FolderStoreFormat format, int threads)
     : this(store, folder, format)
 {
     this._threads = threads;
 }
        /// <summary>
        /// Tries to load a Triple Store based on information from the Configuration Graph.
        /// </summary>
        /// <param name="g">Configuration Graph.</param>
        /// <param name="objNode">Object Node.</param>
        /// <param name="targetType">Target Type.</param>
        /// <param name="obj">Output Object.</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            ITripleStore store = null;
            INode        subObj;
            Object       temp;

            // Get Property Nodes we need
            INode propStorageProvider = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyStorageProvider)),
                  propAsync           = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyAsync));

            // Check whether to use a specific Graph Collection
            INode collectionNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingGraphCollection)));

            // Instantiate the Store Class
            switch (targetType.FullName)
            {
            case TripleStore:
                if (collectionNode == null)
                {
                    store = new TripleStore();
                }
                else
                {
                    BaseGraphCollection graphCollection = ConfigurationLoader.LoadObject(g, collectionNode) as BaseGraphCollection;
                    if (graphCollection == null)
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the Triple Store identified by the Node '" + objNode.ToString() + "' as the dnr:usingGraphCollection points to an object which cannot be loaded as an instance of the required type BaseGraphCollection");
                    }
                    store = new TripleStore(graphCollection);
                }
                break;

            case WebDemandTripleStore:
                store = new WebDemandTripleStore();
                break;

            case PersistentTripleStore:
                subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propStorageProvider);
                if (subObj == null)
                {
                    return(false);
                }

                temp = ConfigurationLoader.LoadObject(g, subObj);
                if (temp is IStorageProvider)
                {
                    store = new PersistentTripleStore((IStorageProvider)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load a Persistent Triple Store identified by the Node '" + objNode.ToString() + "' as the value given the for dnr:genericManager property points to an Object which could not be loaded as an object which implements the IStorageProvider interface");
                }
                break;
            }

            // Read in additional data to be added to the Store
            if (store != null)
            {
                IEnumerable <INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingGraph)));

                // Read from Graphs
                foreach (INode source in sources)
                {
                    temp = ConfigurationLoader.LoadObject(g, source);
                    if (temp is IGraph)
                    {
                        store.Add((IGraph)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface");
                    }
                }

                // Load from Embedded Resources
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromEmbedded)));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        EmbeddedResourceLoader.Load(store, ((ILiteralNode)source).Value);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required");
                    }
                }

                // Read from Files - we assume these files are Dataset Files
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromFile)));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value));
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required");
                    }
                }

                // Finally we'll apply any reasoners
                if (store is IInferencingTripleStore)
                {
                    IEnumerable <INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyReasoner)));
                    foreach (INode reasoner in reasoners)
                    {
                        temp = ConfigurationLoader.LoadObject(g, reasoner);
                        if (temp is IInferenceEngine)
                        {
                            ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface");
                        }
                    }
                }

                // And as an absolute final step if the store is transactional we'll flush any changes we've made
                if (store is ITransactionalStore)
                {
                    ((ITransactionalStore)store).Flush();
                }
            }

            obj = store;
            return(store != null);
        }
 /// <summary>
 /// Creates a new Writer Context
 /// </summary>
 /// <param name="store">Triple Store</param>
 /// <param name="folder">Folder to write to</param>
 public FolderStoreWriterContext(ITripleStore store, String folder)
 {
     this._store = store;
     this._folder = folder;
 }
 /// <summary>
 /// Creates a new Folder Store Parser Context
 /// </summary>
 /// <param name="store">Triple Store to parse into</param>
 /// <param name="folder">Folder</param>
 /// <param name="format">Format</param>
 public FolderStoreParserContext(ITripleStore store, String folder, FolderStoreFormat format)
     : this(store, folder, format, 8)
 {
 }