Ejemplo n.º 1
0
        public void SparqlRemoteEndpointWriteThroughHandler()
        {
            SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint();
            WriteThroughHandler  handler  = new WriteThroughHandler(typeof(NTriplesFormatter), Console.Out, false);

            endpoint.QueryWithResultGraph(handler, "CONSTRUCT WHERE { ?s ?p ?o }");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes the RDF is proper serialization.
        /// </summary>
        protected override void WriteRdf(StreamWriter writer, IEnumerable <Triple> triples)
        {
            IRdfHandler h = new WriteThroughHandler(new NTriples11Formatter(), writer);

            h.StartRdf();

            foreach (var triple in triples)
            {
                h.HandleTriple(triple);
            }

            h.EndRdf(true);
        }
        private void TestWriteThroughHandler(ISparqlQueryProcessor processor, String query)
        {
            NTriplesFormatter formatter = new NTriplesFormatter();
            StringWriter      data      = new StringWriter();

            SparqlQuery q        = this._parser.ParseFromString(query);
            Graph       expected = processor.ProcessQuery(q) as Graph;

            Assert.NotNull(expected);

            WriteThroughHandler handler = new WriteThroughHandler(formatter, data, false);

            processor.ProcessQuery(handler, null, q);
            Console.WriteLine(data.ToString());

            Graph actual = new Graph();

            StringParser.Parse(actual, data.ToString(), new NTriplesParser());

            Assert.Equal(expected, actual);
        }
        private void TestWriteThroughHandler(ISparqlQueryProcessor processor, String query)
        {
            NTriplesFormatter formatter = new NTriplesFormatter();
            StringWriter      data      = new StringWriter();

            SparqlQuery q        = this._parser.ParseFromString(query);
            Graph       expected = processor.ProcessQuery(q) as Graph;

            if (expected == null)
            {
                Assert.Fail("Query did not produce a Graph as expected");
            }

            WriteThroughHandler handler = new WriteThroughHandler(formatter, data, false);

            processor.ProcessQuery(handler, null, q);
            Console.WriteLine(data.ToString());

            Graph actual = new Graph();

            StringParser.Parse(actual, data.ToString(), new NTriplesParser());

            Assert.AreEqual(expected, actual, "Graphs should be equal");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Runs the task
        /// </summary>
        /// <returns></returns>
        protected override TaskResult RunTaskInternal()
        {
            MimeTypeDefinition def = MimeTypesHelper.GetDefinitionsByFileExtension(MimeTypesHelper.GetTrueFileExtension(this._file)).FirstOrDefault(d => d.CanWriteRdfDatasets);

            if (def == null)
            {
                throw new RdfOutputException("Cannot Export the Store to the selected File since dotNetRDF was unable to select a writer to use based on the File Extension");
            }

            IStoreWriter writer = def.GetRdfDatasetWriter();

            if (writer is IMultiThreadedWriter)
            {
                ((IMultiThreadedWriter)writer).UseMultiThreadedWriting = false;
            }

            TripleStore store = new TripleStore();

            if (writer is TriXWriter)
            {
                //For TriX must load all into memory and then write out all at once
                foreach (Uri u in this.ListGraphs())
                {
                    Graph g = new Graph();
                    this._manager.LoadGraph(g, u);
                    g.BaseUri = u;
                    store.Add(g);
                    this.Information = "Loading into memory prior to export, loaded " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) so far...";
                    if (this.HasBeenCancelled)
                    {
                        this.Information = "Export Cancelled";
                        return(new TaskResult(true));
                    }
                }
                this.Information = "Exporting Data all at once, have " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s) to export...";
                writer.Save(store, new StreamWriter(this._file));
                this.Information = "Exported " + store.Graphs.Sum(x => x.Triples.Count) + " Triple(s) in " + store.Graphs.Count + " Graph(s)";
            }
            else
            {
                if (File.Exists(this._file))
                {
                    File.Delete(this._file);
                }

                //For non-TriX formats assume it is safe to append one Graph at a time to the file
                int graphCount = 0, tripleCount = 0;
                foreach (Uri u in this.ListGraphs())
                {
                    using (FileStream stream = new FileStream(this._file, FileMode.Append))
                    {
                        if (writer is IFormatterBasedWriter)
                        {
                            //Stream via a WriteThroughHandler
                            this.Information = "Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default");
                            WriteThroughHandler   handler     = new WriteThroughHandler(((IFormatterBasedWriter)writer).TripleFormatterType, new StreamWriter(stream), true);
                            ExportProgressHandler progHandler = new ExportProgressHandler(handler, this, tripleCount);
                            this._manager.LoadGraph(progHandler, u);
                            graphCount++;
                            tripleCount = progHandler.TripleCount;

                            this.Information = "Finished Stream Exporting Graph " + (u != null ? u.AbsoluteUri : "Default") + ", exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far...";
                        }
                        else
                        {
                            //Load Graph into memory
                            Graph g = new Graph();
                            g.BaseUri        = u;
                            this.Information = "Loading Graph " + (u != null ? u.AbsoluteUri : "Default");
                            this._manager.LoadGraph(g, u);
                            g.BaseUri = u;

                            if (this.HasBeenCancelled)
                            {
                                stream.Close();
                                this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)";
                                return(new TaskResult(true));
                            }

                            graphCount++;
                            tripleCount += g.Triples.Count;

                            //Save it
                            store.Add(g);
                            writer.Save(store, new StreamWriter(stream, def.Encoding));
                            store.Remove(u);

                            this.Information = "Exporting Data graph by graph, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s) so far...";
                        }

                        //Check for cancellation
                        if (this.HasBeenCancelled)
                        {
                            stream.Close();
                            this.Information = "Export Cancelled, exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)";
                            return(new TaskResult(true));
                        }
                    }
                }
                this.Information = "Exported " + tripleCount + " Triple(s) in " + graphCount + " Graph(s)";
            }

            return(new TaskResult(true));
        }
Ejemplo n.º 6
0
        private void LocalImportContinuation(MessageBoxResult dialogResult = MessageBoxResult.Yes)
        {
            try
            {
                if (dialogResult == MessageBoxResult.Yes)
                {
                    var    ext       = MimeTypesHelper.GetTrueFileExtension(_importFileName);
                    bool   isGZipped = ext.EndsWith(MimeTypesHelper.DefaultGZipExtension);
                    string lines;
                    var    fileTypeDefinition =
                        MimeTypesHelper.GetDefinitionsByFileExtension(ext).FirstOrDefault(d => d.CanParseRdf);
                    var rdfReader = fileTypeDefinition == null ? null : fileTypeDefinition.GetRdfParser();
                    if (rdfReader == null || rdfReader is NTriplesParser || rdfReader is NQuadsParser)
                    {
                        // We can't determine the file type, or it is NQuads or NTriples
                        if (isGZipped)
                        {
                            using (var fileStream = new FileStream(_importFileName, FileMode.Open))
                            {
                                var gZipStream = new GZipStream(fileStream, CompressionMode.Decompress);
                                var reader     = new StreamReader(gZipStream);
                                lines = reader.ReadToEnd();
                            }
                        }
                        else
                        {
                            lines = File.ReadAllText(_importFileName);
                        }
                    }
                    else
                    {
                        using (var textWriter = new StringWriter())
                        {
                            try
                            {
                                var nQuadsFormatter     = new NQuadsFormatter();
                                var writeThroughHandler = new WriteThroughHandler(nQuadsFormatter, textWriter);
                                rdfReader.Load(writeThroughHandler, _importFileName);
                                lines = textWriter.ToString();
                            }
                            catch (Exception ex)
                            {
                                Messenger.Default.Send(new ShowDialogMessage(
                                                           Strings.ParseErrorTitle,
                                                           String.Format(Strings.ParseErrorDescription, _importFileName,
                                                                         ex.Message),
                                                           MessageBoxImage.Error,
                                                           MessageBoxButton.OK),
                                                       "MainWindow");
                                return;
                            }
                        }
                    }
                    var client = BrightstarService.GetClient(Store.Source.ConnectionString);

                    String graphUri = !String.IsNullOrWhiteSpace(ImportGraphName)
                        ? ImportGraphName
                        : Constants.DefaultGraphUri;

                    _transactionJob = client.ExecuteTransaction(Store.Location, String.Empty, String.Empty, lines, waitForCompletion: false, defaultGraphUri: graphUri);
                    _dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
                                            new TransactionViewModel.JobMonitorDelegate(CheckJobStatus));
                }
            }
            catch (OutOfMemoryException)
            {
                Messenger.Default.Send(new ShowDialogMessage(Strings.ParseErrorTitle,
                                                             Strings.ImportFileTooLarge,
                                                             MessageBoxImage.Error,
                                                             MessageBoxButton.OK), "MainWindow");
            }
        }
Ejemplo n.º 7
0
        public IRdfHandler ToCache(Uri requestUri, Uri responseUri, String etag)
        {
            IRdfHandler handler = null;

            try
            {
                bool cacheTwice = !requestUri.AbsoluteUri.Equals(responseUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase);

                // Cache the ETag if present
                if (this._canCacheETag && etag != null && !etag.Equals(String.Empty))
                {
                    int  id         = requestUri.GetEnhancedHashCode();
                    bool requireAdd = false;
                    if (this._etags.ContainsKey(id))
                    {
                        if (!this._etags[id].Equals(etag))
                        {
                            // If the ETag has changed remove it and then re-add it
                            this.RemoveETag(requestUri);
                            requireAdd = true;
                        }
                    }
                    else
                    {
                        requireAdd = true;
                    }

                    if (requireAdd)
                    {
                        // Add a New ETag
                        this._etags.Add(id, etag);
                        using (StreamWriter writer = new StreamWriter(File.Open(this._etagFile, FileMode.Append, FileAccess.Write), Encoding.UTF8))
                        {
                            writer.WriteLine(id + "\t" + etag);
                            writer.Close();
                        }
                    }

                    // Cache under the Response URI as well if applicable
                    if (cacheTwice)
                    {
                        id         = responseUri.GetEnhancedHashCode();
                        requireAdd = false;
                        if (this._etags.ContainsKey(id))
                        {
                            if (!this._etags[id].Equals(etag))
                            {
                                // If the ETag has changed remove it and then re-add it
                                this.RemoveETag(responseUri);
                                requireAdd = true;
                            }
                        }
                        else
                        {
                            requireAdd = true;
                        }

                        if (requireAdd)
                        {
                            using (StreamWriter writer = new StreamWriter(File.Open(_etagFile, FileMode.Append, FileAccess.Write), Encoding.UTF8))
                            {
                                writer.WriteLine(id + "\t" + etag);
                                writer.Close();
                            }
                        }
                    }
                }

                // Then if we are caching Graphs return WriteThroughHandlers to do the caching for us
                if (this._canCacheGraphs)
                {
                    String graph = Path.Combine(this._graphDir, requestUri.GetSha256Hash());
                    handler = new WriteThroughHandler(_formatterType, new StreamWriter(File.Open(graph, FileMode.Append)));

                    if (cacheTwice)
                    {
                        graph   = Path.Combine(this._graphDir, responseUri.GetSha256Hash());
                        handler = new MultiHandler(new IRdfHandler[] { handler, new WriteThroughHandler(_formatterType, new StreamWriter(File.Open(graph, FileMode.Append, FileAccess.Write)), true) });
                    }
                }
            }
            catch (IOException)
            {
                // Ignore - if we get an IO Exception we failed to cache somehow
            }
            catch (RdfOutputException)
            {
                // Ignore - if we get an RDF Output Exception then we failed to cache
            }
            return(handler);
        }
Ejemplo n.º 8
0
 protected override void StartRdfInternal()
 {
     this._handler = new WriteThroughHandler(this._formatterType, new StreamWriter(this._file, false, this._encoding));
     this._handler.StartRdf();
 }
Ejemplo n.º 9
0
 protected override void StartRdfInternal()
 {
     _handler = new WriteThroughHandler(_formatterType, new StringWriter(_buffer));
     _handler.StartRdf();
 }