Example #1
0
 /// <summary>
 /// Removes model from the store.
 /// </summary>
 /// <param name="uri">Uri of the model which is to be removed.</param>
 public override void RemoveModel(Uri uri)
 {
     if (_store.HasGraph(uri))
     {
         _store.Remove(uri);
     }
 }
Example #2
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);
        }
 public void ClearRepository()
 {
     foreach (var g in _repository.Graphs)
     {
         _repository.Remove(g.BaseUri);
     }
 }
Example #4
0
        public void SparqlViewGraphScope1()
        {
            TripleStore store = new TripleStore();
            SparqlView  view  = new SparqlView("CONSTRUCT { ?s ?p ?o } FROM <http://example.org/data> WHERE { ?s ?p ?o . FILTER(IsLiteral(?o)) }", store);

            view.BaseUri = new Uri("http://example.org/view");
            store.Add(view);

            Console.WriteLine("SPARQL View Empty");
            TestTools.ShowGraph(view);
            Console.WriteLine();

            //Load a Graph into the Store to cause the SPARQL View to update
            Graph g = new Graph();

            FileLoader.Load(g, "resources\\InferenceTest.ttl");
            g.BaseUri = new Uri("http://example.org/data");
            store.Add(g);

            Thread.Sleep(500);
            if (view.Triples.Count == 0)
            {
                view.UpdateView();
            }
            int lastCount = view.Triples.Count;

            Console.WriteLine("SPARQL View Populated");
            TestTools.ShowGraph(view);

            Assert.True(view.Triples.Count > 0, "View should have updated to contain some Triples");

            //Load another Graph with a different URI into the Store
            Graph h = new Graph();

            h.BaseUri = new Uri("http://example.org/2");
            FileLoader.Load(h, "resources\\Turtle.ttl");
            store.Add(h);

            Thread.Sleep(500);
            view.UpdateView();

            Assert.True(view.Triples.Count == lastCount, "View should not have changed since the added Graph is not in the set of Graphs over which the query operates");

            //Remove this Graph and check the View still doesn't change
            store.Remove(h.BaseUri);

            Thread.Sleep(500);
            view.UpdateView();

            Assert.True(view.Triples.Count == lastCount, "View should not have changed since the removed Graph is not in the set of Graphs over which the query operates");
        }
Example #5
0
        public void RemoveNonExistentTripleDoesNotDecreaseNodeCountOrResourceCount()
        {
            TripleStore store = MakeNewTripleStore();

            store.Add(new Statement(new BlankNode(), new UriRef("http://example.com/pred"), new BlankNode()));

            int NodeCountBefore     = store.NodeCount;
            int ResourceCountBefore = store.ResourceCount;

            store.Remove(new ResourceStatement(new Resource(), new Resource(), new Resource()));

            Assert.IsTrue(NodeCountBefore <= store.NodeCount, "Node count should not decrease");
            Assert.IsTrue(ResourceCountBefore <= store.ResourceCount, "Resource count should not decrease");
            store.Clear();
        }
Example #6
0
        private static void GetRecord(string leiCode, TextWriter output, AllegroGraphConnector allegroGraphConnector)
        {
            var g = new Graph();

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

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

            output.Write(s);
            output.Flush();
            ts.Remove(g.BaseUri);
            g.Clear();
            ts.Dispose();
            g.Dispose();
        }
Example #7
0
        public void GraphEventBubbling()
        {
            try
            {
                this._graphAdded = false;
                this._graphRemoved = false;
                this._graphChanged = false;

                //Create Store and Graph add attach handlers to Store
                TripleStore store = new TripleStore();
                Graph g = new Graph();
                store.GraphAdded += this.HandleGraphAdded;
                store.GraphRemoved += this.HandleGraphRemoved;
                store.GraphChanged += this.HandleGraphChanged;

                //Add the Graph to the Store which should fire the GraphAdded event
                store.Add(g);
                Assert.IsTrue(this._graphAdded, "GraphAdded event of the Triple Store should have fired");

                //Assert a Triple
                INode s = g.CreateBlankNode();
                INode p = g.CreateUriNode("rdf:type");
                INode o = g.CreateUriNode("rdfs:Class");
                Triple t = new Triple(s, p, o);
                g.Assert(t);
                Assert.IsTrue(this._graphChanged, "GraphChanged event of the Triple Store should have fired");

                //Retract the Triple
                this._graphChanged = false;
                g.Retract(t);
                Assert.IsTrue(this._graphChanged, "GraphChanged event of the Triple Store should have fired");

                //Remove the Graph from the Store which should fire the GraphRemoved event
                store.Remove(g.BaseUri);
                Assert.IsTrue(this._graphRemoved, "GraphRemoved event of the Triple Store should have fired");
            }
            catch (Exception ex)
            {
                TestTools.ReportError("Error", ex, true);
            }
        }
Example #8
0
        public void GetResourceDenotedReturnsSameResourceEvenAfterAllStatementsReferringToResourceHaveBeenRemoved()
        {
            TripleStore store = MakeNewTripleStore();

            BlankNode theSubjectNode   = new BlankNode();
            UriRef    thePredicateNode = new UriRef("http://example.com/pred");
            BlankNode theObjectNode    = new BlankNode();

            store.Add(new Statement(theSubjectNode, thePredicateNode, theObjectNode));

            Resource theSubjectResourceBefore   = store.GetResourceDenotedBy(theSubjectNode);
            Resource thePredicateResourceBefore = store.GetResourceDenotedBy(thePredicateNode);
            Resource theObjectResourceBefore    = store.GetResourceDenotedBy(theObjectNode);

            store.Remove(new ResourceStatement(theSubjectResourceBefore, thePredicateResourceBefore, theObjectResourceBefore));

            Assert.AreEqual(theSubjectResourceBefore, store.GetResourceDenotedBy(theSubjectNode), "Subject resource should be the same");
            Assert.AreEqual(thePredicateResourceBefore, store.GetResourceDenotedBy(thePredicateNode), "Predicate resource should be the same");
            Assert.AreEqual(theObjectResourceBefore, store.GetResourceDenotedBy(theObjectNode), "Object resource should be the same");
            store.Clear();
        }
Example #9
0
        public void RemoveExistingTriple()
        {
            TripleStore store = MakeNewTripleStore();

            BlankNode theSubjectNode   = new BlankNode();
            UriRef    thePredicateNode = new UriRef("http://example.com/pred");
            BlankNode theObjectNode    = new BlankNode();

            store.Add(new Statement(theSubjectNode, thePredicateNode, theObjectNode));

            Resource theSubjectResource   = store.GetResourceDenotedBy(theSubjectNode);
            Resource thePredicateResource = store.GetResourceDenotedBy(thePredicateNode);
            Resource theObjectResource    = store.GetResourceDenotedBy(theObjectNode);

            //store.verbose = true;
            store.Remove(new ResourceStatement(theSubjectResource, thePredicateResource, theObjectResource));

            TripleStoreVerifier verifier = new TripleStoreVerifier();

            Assert.IsTrue(verifier.verify(store));
            store.Clear();
        }
Example #10
0
        public virtual void Replace(ConciseBoundedDescription description)
        {
            Resource  internalSubjectResource = GetResourceDescribedBy(description);
            ArrayList statementsToRemove      = new ArrayList();

            IEnumerator statementEnumerator = itsAssertions.GetStatementEnumerator();

            while (statementEnumerator.MoveNext())
            {
                ResourceStatement statement = (ResourceStatement)statementEnumerator.Current;
                if (statement.GetSubject().Equals(internalSubjectResource))
                {
                    statementsToRemove.Add(statement);
                }
            }

            foreach (ResourceStatement statement in statementsToRemove)
            {
                itsAssertions.Remove(statement);
            }

            Add(description);
        }
Example #11
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));
        }
Example #12
0
        public void SparqlViewGraphScope()
        {
            try
            {
                TripleStore store = new TripleStore();
                SparqlView view = new SparqlView("CONSTRUCT { ?s ?p ?o } FROM <http://example.org/data> WHERE { ?s ?p ?o . FILTER(IsLiteral(?o)) }", store);
                view.BaseUri = new Uri("http://example.org/view");
                store.Add(view);

                Console.WriteLine("SPARQL View Empty");
                TestTools.ShowGraph(view);
                Console.WriteLine();

                //Load a Graph into the Store to cause the SPARQL View to update
                Graph g = new Graph();
                FileLoader.Load(g, "InferenceTest.ttl");
                g.BaseUri = new Uri("http://example.org/data");
                store.Add(g);

                Thread.Sleep(200);
                if (view.Triples.Count == 0) view.UpdateView();
                int lastCount = view.Triples.Count;

                Console.WriteLine("SPARQL View Populated");
                TestTools.ShowGraph(view);

                Assert.IsTrue(view.Triples.Count > 0, "View should have updated to contain some Triples");

                //Load another Graph with a different URI into the Store
                Graph h = new Graph();
                h.BaseUri = new Uri("http://example.org/2");
                FileLoader.Load(h, "Turtle.ttl");
                store.Add(h);

                Thread.Sleep(200);
                view.UpdateView();

                Assert.IsTrue(view.Triples.Count == lastCount, "View should not have changed since the added Graph is not in the set of Graphs over which the query operates");

                //Remove this Graph and check the View still doesn't change
                store.Remove(h.BaseUri);

                Thread.Sleep(200);
                view.UpdateView();

                Assert.IsTrue(view.Triples.Count == lastCount, "View should not have changed since the removed Graph is not in the set of Graphs over which the query operates");

            }
            catch (RdfQueryException queryEx)
            {
                TestTools.ReportError("Query Error", queryEx, true);
            }
            catch (RdfException ex)
            {
                TestTools.ReportError("Error", ex, true);
            }
        }