public void StorageVirtuosoBlankNodeInsert() { VirtuosoManager manager = VirtuosoTest.GetConnection(); try { Graph g = new Graph(); Triple t = new Triple(g.CreateBlankNode(), g.CreateUriNode("rdf:type"), g.CreateUriNode(UriFactory.Create("http://example.org/object"))); manager.UpdateGraph("http://localhost/insertBNodeTest", t.AsEnumerable(), null); Object results = manager.Query("ASK WHERE { GRAPH <http://localhost/insertBNodeTest> { ?s a <http:///example.org/object> } }"); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.IsTrue(((SparqlResultSet)results).Result, "Expected a true result"); } else { Assert.Fail("Didn't get a SPARQL Result Set as expected"); } } finally { if (manager != null) { manager.Dispose(); } } }
/// <summary> /// Creates a new store based on the given template /// </summary> /// <param name="template">Template</param> /// <param name="callback">Callback</param> /// <param name="state">State to pass to the callback</param> /// <remarks> /// <para> /// Template must inherit from <see cref="BaseSesameTemplate"/> /// </para> /// </remarks> public virtual void CreateStore(IStoreTemplate template, AsyncStorageCallback callback, object state) { if (template is BaseSesameTemplate) { // First we need to store the template as a new context in the SYSTEM repository Dictionary <String, String> createParams = new Dictionary <string, string>(); BaseSesameTemplate sesameTemplate = (BaseSesameTemplate)template; if (template.Validate().Any()) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("Template is not valid, call Validate() on the template to see the list of errors")), state); return; } IGraph g = sesameTemplate.GetTemplateGraph(); createParams.Add("context", sesameTemplate.ContextNode.ToString()); HttpWebRequest request = this.CreateRequest(this._repositoriesPrefix + SesameServer.SystemRepositoryID + "/statements", "*/*", "POST", createParams); request.ContentType = MimeTypesHelper.NTriples[0]; NTriplesWriter ntwriter = new NTriplesWriter(); this.EnsureSystemConnection(); this._sysConnection.SaveGraphAsync(request, ntwriter, g, (sender, args, st) => { if (args.WasSuccessful) { // Then we need to declare that said Context is of type rep:RepositoryContext Triple repoType = new Triple(sesameTemplate.ContextNode, g.CreateUriNode("rdf:type"), g.CreateUriNode("rep:RepositoryContext")); this._sysConnection.UpdateGraph(String.Empty, repoType.AsEnumerable(), null, (sender2, args2, st2) => { if (args.WasSuccessful) { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template), state); } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleError(args.Error, "creating a new Store in")), state); } }, st); } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template, StorageHelper.HandleError(args.Error, "creating a new Store in")), state); } }, state); } else { callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template, new RdfStorageException("Invalid template, templates must derive from BaseSesameTemplate")), state); } }
private IEnumerable<Triple> GetTriples(Resource r, Property p, RDFNode rdfn) { if (r == null) { if (p == null) { //Object specified return this._g.GetTriplesWithObject(JenaConverter.FromJenaNode(rdfn, this._mapping)); } else if (rdfn == null) { //Predicate specified return this._g.GetTriplesWithPredicate(JenaConverter.FromJenaProperty(p, this._mapping)); } else { //Object and Predicate specified return this._g.GetTriplesWithPredicateObject(JenaConverter.FromJenaProperty(p, this._mapping), JenaConverter.FromJenaNode(rdfn, this._mapping)); } } else if (p == null) { if (rdfn == null) { //Subject specified return this._g.GetTriplesWithSubject(JenaConverter.FromJenaResource(r, this._mapping)); } else { //Subject and Object specified return this._g.GetTriplesWithSubjectObject(JenaConverter.FromJenaResource(r, this._mapping), JenaConverter.FromJenaNode(rdfn, this._mapping)); } } else if (rdfn == null) { //Subject and Predicate specified return this._g.GetTriplesWithSubjectPredicate(JenaConverter.FromJenaResource(r, this._mapping), JenaConverter.FromJenaProperty(p, this._mapping)); } else { Triple t = new Triple(JenaConverter.FromJenaResource(r, this._mapping), JenaConverter.FromJenaProperty(p, this._mapping), JenaConverter.FromJenaNode(rdfn, this._mapping)); if (this._g.ContainsTriple(t)) { return t.AsEnumerable(); } else { return Enumerable.Empty<Triple>(); } } }
private IEnumerable <Triple> GetTriples(Resource r, Property p, RDFNode rdfn) { if (r == null) { if (p == null) { //Object specified return(this._g.GetTriplesWithObject(JenaConverter.FromJenaNode(rdfn, this._mapping))); } else if (rdfn == null) { //Predicate specified return(this._g.GetTriplesWithPredicate(JenaConverter.FromJenaProperty(p, this._mapping))); } else { //Object and Predicate specified return(this._g.GetTriplesWithPredicateObject(JenaConverter.FromJenaProperty(p, this._mapping), JenaConverter.FromJenaNode(rdfn, this._mapping))); } } else if (p == null) { if (rdfn == null) { //Subject specified return(this._g.GetTriplesWithSubject(JenaConverter.FromJenaResource(r, this._mapping))); } else { //Subject and Object specified return(this._g.GetTriplesWithSubjectObject(JenaConverter.FromJenaResource(r, this._mapping), JenaConverter.FromJenaNode(rdfn, this._mapping))); } } else if (rdfn == null) { //Subject and Predicate specified return(this._g.GetTriplesWithSubjectPredicate(JenaConverter.FromJenaResource(r, this._mapping), JenaConverter.FromJenaProperty(p, this._mapping))); } else { Triple t = new Triple(JenaConverter.FromJenaResource(r, this._mapping), JenaConverter.FromJenaProperty(p, this._mapping), JenaConverter.FromJenaNode(rdfn, this._mapping)); if (this._g.ContainsTriple(t)) { return(t.AsEnumerable()); } else { return(Enumerable.Empty <Triple>()); } } }
/// <summary> /// Creates a new Store based on the given template /// </summary> /// <param name="template">Template</param> /// <returns></returns> /// <remarks> /// <para> /// Templates must inherit from <see cref="BaseSesameTemplate"/> /// </para> /// </remarks> public virtual bool CreateStore(IStoreTemplate template) { if (template is BaseSesameTemplate) { try { Dictionary <String, String> createParams = new Dictionary <string, string>(); BaseSesameTemplate sesameTemplate = (BaseSesameTemplate)template; if (template.Validate().Any()) { throw new RdfStorageException("Template is not valid, call Validate() on the template to see the list of errors"); } IGraph g = sesameTemplate.GetTemplateGraph(); // Firstly we need to save the Repository Template as a new Context to Sesame createParams.Add("context", sesameTemplate.ContextNode.ToString()); HttpWebRequest request = this.CreateRequest(this._repositoriesPrefix + SesameServer.SystemRepositoryID + "/statements", "*/*", "POST", createParams); request.ContentType = MimeTypesHelper.NTriples[0]; NTriplesWriter ntwriter = new NTriplesWriter(); ntwriter.Save(g, new StreamWriter(request.GetRequestStream())); Tools.HttpDebugRequest(request); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); // If we get then it was OK response.Close(); } // Then we need to declare that said Context is of type rep:RepositoryContext Triple repoType = new Triple(sesameTemplate.ContextNode, g.CreateUriNode("rdf:type"), g.CreateUriNode("rep:RepositoryContext")); this.EnsureSystemConnection(); this._sysConnection.UpdateGraph(String.Empty, repoType.AsEnumerable(), null); return(true); } catch (WebException webEx) { throw StorageHelper.HandleHttpError(webEx, "creating a new Store in"); } } else { throw new RdfStorageException("Invalid template, templates must derive from BaseSesameTemplate"); } }
public void StorageVirtuosoBlankNodeDelete() { //First ensure data is present in the store VirtuosoManager manager = VirtuosoTest.GetConnection(); try { manager.DeleteGraph("http://localhost/deleteBNodeTest"); Graph g = new Graph(); Triple t = new Triple(g.CreateBlankNode(), g.CreateUriNode("rdf:type"), g.CreateUriNode(UriFactory.Create("http://example.org/object"))); g.Assert(t); manager.UpdateGraph("http://localhost/deleteBNodeTest", t.AsEnumerable(), null); Object results = manager.Query("ASK WHERE { GRAPH <http://localhost/deleteBNodeTest> { ?s a <http://example.org/object> } }"); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.IsTrue(((SparqlResultSet)results).Result, "Expected a true result"); //Now we've ensured data is present we can first load the graph and then try to delete the given triple Graph h = new Graph(); manager.LoadGraph(h, "http://localhost/deleteBNodeTest"); Assert.AreEqual(g, h, "Graphs should be equal"); //Then we can go ahead and delete the triples from this graph manager.UpdateGraph("http://localhost/deleteBNodeTest", null, h.Triples); Graph i = new Graph(); manager.LoadGraph(i, "http://localhost/deleteBNodeTest"); Assert.IsTrue(i.IsEmpty, "Graph should be empty"); Assert.AreNotEqual(h, i); Assert.AreNotEqual(g, i); } else { Assert.Fail("Didn't get a SPARQL Result Set as expected"); } } finally { if (manager != null) { manager.Dispose(); } } }
protected internal override bool RemoveQuad(Uri graphUri, Triple t) { this._storage.UpdateGraph(graphUri, null, t.AsEnumerable()); return(true); }
public java.util.Iterator match(dotSesame.Resource r, dotSesame.URI uri, dotSesame.Value v, params dotSesame.Resource[] rarr) { INode s = (r != null) ? SesameConverter.FromSesameResource(r, this._mapping) : null; INode p = (uri != null) ? SesameConverter.FromSesameUri(uri, this._mapping) : null; INode o = (v != null) ? SesameConverter.FromSesameValue(v, this._mapping) : null; //Contexts are Ignored for matches on a single Graph IEnumerable<Triple> ts; if (s != null) { if (p != null) { if (o != null) { //Matching on Subject Predicate and Object Triple t = new Triple(s, p, o); if (this._g.ContainsTriple(t)) { ts = t.AsEnumerable(); } else { ts = Enumerable.Empty<Triple>(); } } else { //Just matching on Subject Predicate ts = this._g.GetTriplesWithSubjectPredicate(s, p); } } else { if (o != null) { //Matching on Subject Object ts = this._g.GetTriplesWithSubjectObject(s, o); } else { //Just Matching on Subject ts = this._g.GetTriplesWithSubject(s); } } } else if (p != null) { if (o != null) { //Matching on Predicate Object ts = this._g.GetTriplesWithPredicateObject(p, o); } else { //Just Matching on Predicate ts = this._g.GetTriplesWithPredicate(p); } } else if (o != null) { //Matching on Object only ts = this._g.GetTriplesWithObject(o); } else { //Matching anything ts = this._g.Triples; } return new DotNetEnumerableWrapper(ts.Select(t => SesameConverter.ToSesame(t, this._mapping))); }
/// <summary> /// Converts a SemWeb Statement Template to an IEnumerable of Triples /// </summary> /// <param name="template">Statement Template</param> /// <returns></returns> private IEnumerable <Triple> TemplateToEnumerable(Statement template) { INode s, p, o; if (template.Subject is Variable) { if (template.Predicate is Variable) { if (template.Object is Variable) { //All three things are variables so this just checks that some Triple(s) are present return(this._g.Triples); } else { //Subject & Predicate are Variables //Convert the Object and do a WithObject().Any() call o = SemWebConverter.FromSemWeb(template.Object, this._mapping); return(this._g.GetTriplesWithObject(o)); } } else if (template.Object is Variable) { //Subject & Object are variables //Convert the Predicate and do a WithPredicate() call p = SemWebConverter.FromSemWeb(template.Predicate, this._mapping); return(this._g.GetTriplesWithPredicate(p)); } else { //Subject is a Variable //Convert the Predicate and Object and do a WithPredicateObject() call p = SemWebConverter.FromSemWeb(template.Predicate, this._mapping); o = SemWebConverter.FromSemWeb(template.Object, this._mapping); return(this._g.GetTriplesWithPredicateObject(p, o)); } } else if (template.Predicate is Variable) { if (template.Object is Variable) { //Predicate & Object are Variables //Convert the Subject and do a WithSubject() call s = SemWebConverter.FromSemWeb(template.Subject, this._mapping); return(this._g.GetTriplesWithSubject(s)); } else { //Predicate is a Variable //Convert the Subject and Object and do a WithSubjectObject() call s = SemWebConverter.FromSemWeb(template.Subject, this._mapping); o = SemWebConverter.FromSemWeb(template.Object, this._mapping); return(this._g.GetTriplesWithSubjectObject(s, o)); } } else if (template.Object is Variable) { //Object is a Variable //Convert the Subject and Predicate and do a WithSubjectPredicate() call s = SemWebConverter.FromSemWeb(template.Subject, this._mapping); p = SemWebConverter.FromSemWeb(template.Predicate, this._mapping); return(this._g.GetTriplesWithSubjectPredicate(s, p)); } else { //Just convert the Triple and do a Contains() call Triple t = SemWebConverter.FromSemWeb(template, this._mapping); if (this._g.ContainsTriple(t)) { return(t.AsEnumerable()); } else { return(Enumerable.Empty <Triple>()); } } }
/// <summary> /// Converts a SemWeb Statement Template to an IEnumerable of Triples /// </summary> /// <param name="template">Statement Template</param> /// <param name="g">Graph the Template should be created for</param> /// <returns></returns> private IEnumerable <Triple> TemplateToEnumerable(Statement template, IGraph g) { INode s, p, o; int hash; if (g.BaseUri == null) { hash = new Uri(GraphCollection.DefaultGraphUri).GetEnhancedHashCode(); } else { hash = g.BaseUri.GetEnhancedHashCode(); } if (template.Subject is Variable) { if (template.Predicate is Variable) { if (template.Object is Variable) { //All three things are variables so this just checks that some Triple(s) are present return(g.Triples); } else { //Subject & Predicate are Variables //Convert the Object and do a WithObject().Any() call o = SemWebConverter.FromSemWeb(template.Object, this.GetMapping(hash, g)); return(g.GetTriplesWithObject(o)); } } else if (template.Object is Variable) { //Subject & Object are variables //Convert the Predicate and do a WithPredicate() call p = SemWebConverter.FromSemWeb(template.Predicate, this.GetMapping(hash, g)); return(g.GetTriplesWithPredicate(p)); } else { //Subject is a Variable //Convert the Predicate and Object and do a WithPredicateObject() call p = SemWebConverter.FromSemWeb(template.Predicate, this.GetMapping(hash, g)); o = SemWebConverter.FromSemWeb(template.Object, this.GetMapping(hash, g)); return(g.GetTriplesWithPredicateObject(p, o)); } } else if (template.Predicate is Variable) { if (template.Object is Variable) { //Predicate & Object are Variables //Convert the Subject and do a WithSubject() call s = SemWebConverter.FromSemWeb(template.Subject, this.GetMapping(hash, g)); return(g.GetTriplesWithSubject(s)); } else { //Predicate is a Variable //Convert the Subject and Object and do a WithSubjectObject() call s = SemWebConverter.FromSemWeb(template.Subject, this.GetMapping(hash, g)); o = SemWebConverter.FromSemWeb(template.Object, this.GetMapping(hash, g)); return(g.GetTriplesWithSubjectObject(s, o)); } } else if (template.Object is Variable) { //Object is a Variable //Convert the Subject and Predicate and do a WithSubjectPredicate() call s = SemWebConverter.FromSemWeb(template.Subject, this.GetMapping(hash, g)); p = SemWebConverter.FromSemWeb(template.Predicate, this.GetMapping(hash, g)); return(g.GetTriplesWithSubjectPredicate(s, p)); } else { //Just convert the Triple and do a Contains() call Triple t = SemWebConverter.FromSemWeb(template, this.GetMapping(hash, g)); if (g.ContainsTriple(t)) { return(t.AsEnumerable()); } else { return(Enumerable.Empty <Triple>()); } } }
public java.util.Iterator match(dotSesame.Resource r, dotSesame.URI uri, dotSesame.Value v, params dotSesame.Resource[] rarr) { INode s = (r != null) ? SesameConverter.FromSesameResource(r, this._mapping) : null; INode p = (uri != null) ? SesameConverter.FromSesameUri(uri, this._mapping) : null; INode o = (v != null) ? SesameConverter.FromSesameValue(v, this._mapping) : null; //Contexts are Ignored for matches on a single Graph IEnumerable <Triple> ts; if (s != null) { if (p != null) { if (o != null) { //Matching on Subject Predicate and Object Triple t = new Triple(s, p, o); if (this._g.ContainsTriple(t)) { ts = t.AsEnumerable(); } else { ts = Enumerable.Empty <Triple>(); } } else { //Just matching on Subject Predicate ts = this._g.GetTriplesWithSubjectPredicate(s, p); } } else { if (o != null) { //Matching on Subject Object ts = this._g.GetTriplesWithSubjectObject(s, o); } else { //Just Matching on Subject ts = this._g.GetTriplesWithSubject(s); } } } else if (p != null) { if (o != null) { //Matching on Predicate Object ts = this._g.GetTriplesWithPredicateObject(p, o); } else { //Just Matching on Predicate ts = this._g.GetTriplesWithPredicate(p); } } else if (o != null) { //Matching on Object only ts = this._g.GetTriplesWithObject(o); } else { //Matching anything ts = this._g.Triples; } return(new DotNetEnumerableWrapper(ts.Select(t => SesameConverter.ToSesame(t, this._mapping)))); }