Exemplo n.º 1
0
        /// <summary>
        /// Thread Worker method which writes Graphs to the output
        /// </summary>
        /// <param name="globalContext">Context for writing the Store</param>
        private void SaveGraphs(ThreadedStoreWriterContext globalContext)
        {
            try
            {
                Uri u = globalContext.GetNextUri();
                while (u != null)
                {
                    //Get the Graph from the Store
                    if (WriterHelper.IsDefaultGraph(u) && !globalContext.Store.HasGraph(u)) u = null;
                    IGraph g = globalContext.Store.Graphs[u];

                    //Generate the Graph Output and add to Stream
                    NTriplesWriterContext context = new NTriplesWriterContext(g, new System.IO.StringWriter(), globalContext.PrettyPrint, globalContext.HighSpeedModePermitted);
                    String graphContent = this.GraphToNQuads(globalContext, context);
                    if (!graphContent.Equals(String.Empty))
                    {
                        try
                        {
                            Monitor.Enter(globalContext.Output);
                            globalContext.Output.WriteLine(graphContent);
                            globalContext.Output.Flush();
                        }
                        catch
                        {
                            throw;
                        }
                        finally
                        {
                            Monitor.Exit(globalContext.Output);
                        }
                    }

                    //Get the Next Uri
                    u = globalContext.GetNextUri();
                }
            }
            catch (ThreadAbortException)
            {
                //We've been terminated, don't do anything
            #if !SILVERLIGHT
                Thread.ResetAbort();
            #endif
            }
            catch (Exception ex)
            {
                throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a Triple into relevant NQuads Syntax
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="t">Triple to convert</param>
        /// <returns></returns>
        private String TripleToNQuads(NTriplesWriterContext context, Triple t)
        {
            StringBuilder output = new StringBuilder();
            output.Append(this.NodeToNTriples(context, t.Subject, TripleSegment.Subject));
            output.Append(" ");
            output.Append(this.NodeToNTriples(context, t.Predicate, TripleSegment.Predicate));
            output.Append(" ");
            output.Append(this.NodeToNTriples(context, t.Object, TripleSegment.Object));
            if (t.GraphUri != null && !WriterHelper.IsDefaultGraph(t.GraphUri))
            {
                output.Append(" <");
                output.Append(context.UriFormatter.FormatUri(t.GraphUri));
                output.Append(">");
            }
            output.Append(" .");

            return output.ToString();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts a Node into relevant NTriples Syntax
        /// </summary>
        /// <param name="n">Node to convert</param>
        /// <param name="context">Writer Context</param>
        /// <param name="segment">Triple Segment being written</param>
        /// <returns></returns>
        private String NodeToNTriples(NTriplesWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
                case NodeType.Blank:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("NQuads"));
                    break;
                case NodeType.Literal:
                    if (segment == TripleSegment.Subject) throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("NQuads"));
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("NQuads"));
                    break;
                case NodeType.Uri:
                    break;
                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("NQuads"));
                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("NQuads"));
            }

            return context.NodeFormatter.Format(n);
        }
Exemplo n.º 4
0
        private String GraphToNQuads(ThreadedStoreWriterContext globalContext, NTriplesWriterContext context)
        {
            if (context.Graph.IsEmpty) return String.Empty;
            if (context.PrettyPrint && !WriterHelper.IsDefaultGraph(context.Graph.BaseUri))
            {
                context.Output.WriteLine("# Graph: " + context.Graph.BaseUri.ToString());
            }
            foreach (Triple t in context.Graph.Triples)
            {
                context.Output.WriteLine(this.TripleToNQuads(context, t));
            }
            context.Output.WriteLine();

            return context.Output.ToString();
        }
Exemplo n.º 5
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");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Converts a Triple into relevant NTriples Syntax
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="t">Triple to convert</param>
        /// <returns></returns>
        private String TripleToNTriples(NTriplesWriterContext context, Triple t)
        {
            StringBuilder output = new StringBuilder();
            output.Append(this.NodeToNTriples(context, t.Subject, TripleSegment.Subject));
            output.Append(" ");
            output.Append(this.NodeToNTriples(context, t.Predicate, TripleSegment.Predicate));
            output.Append(" ");
            output.Append(this.NodeToNTriples(context, t.Object, TripleSegment.Object));
            output.Append(" .");

            return output.ToString();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Saves the Graph in NTriples Syntax to the given stream
        /// </summary>
        /// <param name="g">Graph to save</param>
        /// <param name="output">Stream to save to</param>
        public void Save(IGraph g, TextWriter output)
        {
            try
            {
                NTriplesWriterContext context = new NTriplesWriterContext(g, output);
                List<Triple> ts = g.Triples.ToList();
                if (this._sort) ts.Sort();

                foreach (Triple t in ts)
                {
                    output.WriteLine(this.TripleToNTriples(context, t));
                }

                output.Close();
            }
            catch
            {
                //Try and ensure the Stream gets closed
                try
                {
                    output.Close();
                }
                catch
                {
                    //No Catch actions
                }
                throw;
            }
        }