public void ApplyTransaction(IList<Triple> preconditions, IList<Triple> deletePatterns, IList<Triple> inserts, string updateGraphUri) { if (preconditions.Count > 0) { throw new NotSupportedException("SparqlDataObjectStore does not support conditional updates"); } var deleteOp = FormatDeletePatterns(deletePatterns, updateGraphUri); var insertOp = FormatInserts(inserts, updateGraphUri); var parser = new SparqlUpdateParser(); var cmds = parser.ParseFromString(deleteOp + "\n" + insertOp); _updateProcessor.ProcessCommandSet(cmds); }
public void ApplyTransaction(IEnumerable<ITriple> existencePreconditions, IEnumerable<ITriple> nonexistencePreconditions, IEnumerable<ITriple> deletePatterns, IEnumerable<ITriple> inserts, string updateGraphUri) { if (existencePreconditions.Any()) { throw new NotSupportedException("SparqlDataObjectStore does not support conditional updates"); } if (nonexistencePreconditions.Any()) { // NOTE: At the moment this is ignored because if you use key properties, // non-existence preconditions will get generated and we want to support // using key properties with SPARQL update endpoints. } var deleteOp = FormatDeletePatterns(deletePatterns.ToList(), updateGraphUri); var insertOp = FormatInserts(inserts, updateGraphUri); var parser = new SparqlUpdateParser(); var cmds = parser.ParseFromString(deleteOp + "\n" + insertOp); _updateProcessor.ProcessCommandSet(cmds); }
/// <summary> /// Processes Update requests /// </summary> /// <param name="context">HTTP Context</param> public void ProcessUpdateRequest(HttpContext context) { if (this._config.UpdateProcessor == null) { context.Response.StatusCode = (int)HttpStatusCode.NotImplemented; return; } if (context.Request.HttpMethod.Equals("OPTIONS")) { //OPTIONS requests always result in the Service Description document IGraph svcDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri), ServiceDescriptionType.Update); HandlerHelper.SendToClient(context, svcDescrip, this._config); return; } //See if there has been an update submitted String updateText = context.Request.QueryString["update"]; if (updateText == null || updateText.Equals(String.Empty)) { updateText = context.Request.Form["update"]; } //If no Update sent either show Update Form or give a HTTP 400 response if (updateText == null || updateText.Equals(String.Empty)) { if (this._config.ShowUpdateForm) { this.ShowUpdateForm(context); return; } else { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } } try { //Now we're going to parse the Updates SparqlUpdateParser parser = new SparqlUpdateParser(); parser.ExpressionFactories = this._config.ExpressionFactories; SparqlUpdateCommandSet commands = parser.ParseFromString(updateText); //Check whether we need to use authentication //If there are no user groups then no authentication is in use so we default to authenticated with no per-action authentication needed bool isAuth = true, requireActionAuth = false; if (this._config.UserGroups.Any()) { //If we have user isAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups); requireActionAuth = true; } if (!isAuth) return; //First check actions to see whether they are all permissible foreach (SparqlUpdateCommand cmd in commands.Commands) { //Authenticate each action bool actionAuth = true; if (requireActionAuth) actionAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups, this.GetUpdatePermissionAction(cmd)); if (!actionAuth) { throw new SparqlUpdateException("You are not authorised to perform the " + this.GetUpdatePermissionAction(cmd) + " action"); } } //Then assuming we got here this means all our actions are permitted so now we can process the updates this.ProcessUpdates(commands); //Flush outstanding changes this._config.UpdateProcessor.Flush(); //Update the Cache as the request may have changed the endpoint this.UpdateConfig(context); } catch (RdfParseException parseEx) { HandleUpdateErrors(context, "Parsing Error", updateText, parseEx); } catch (SparqlUpdateException updateEx) { HandleUpdateErrors(context, "Update Error", updateText, updateEx); } catch (RdfException rdfEx) { HandleUpdateErrors(context, "RDF Error", updateText, rdfEx); } catch (Exception ex) { HandleUpdateErrors(context, "Error", updateText, ex); } }
/// <summary> /// Processes SPARQL Update requests /// </summary> /// <param name="context">HTTP Context</param> public void ProcessRequest(HttpContext context) { this._config = this.LoadConfig(context); //Add our Standard Headers HandlerHelper.AddStandardHeaders(context, this._config); if (context.Request.HttpMethod.Equals("OPTIONS")) { //OPTIONS requests always result in the Service Description document IGraph svcDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri)); HandlerHelper.SendToClient(context, svcDescrip, this._config); return; } //See if there has been an update submitted String updateText = context.Request.QueryString["update"]; if (updateText == null || updateText.Equals(String.Empty)) { updateText = context.Request.Form["update"]; } //If no Update sent either show Update Form or give a HTTP 400 response if (updateText == null || updateText.Equals(String.Empty)) { //If there is no Update we may return the SPARQL Service Description where appropriate try { //If we might show the Update Form only show the Description if the selected writer is //not a HTML writer MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(HandlerHelper.GetAcceptTypes(context)).FirstOrDefault(d => d.CanWriteRdf); if (definition != null) { IRdfWriter writer = definition.GetRdfWriter(); if (!this._config.ShowUpdateForm || !(writer is IHtmlWriter)) { //If not a HTML Writer selected OR not showing Update Form then show the Service Description Graph //unless an error occurs creating it IGraph serviceDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri)); context.Response.ContentType = definition.CanonicalMimeType; context.Response.ContentEncoding = definition.Encoding; writer.Save(serviceDescrip, new StreamWriter(context.Response.OutputStream, definition.Encoding)); return; } } } catch { //Ignore Exceptions - we'll just show the Query Form or return a 400 Bad Request instead } //If a Writer can't be selected then we'll either show the Update Form or return a 400 Bad Request if (this._config.ShowUpdateForm) { this.ShowUpdateForm(context); } else { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; } return; } try { //Now we're going to parse the Updates SparqlUpdateParser parser = new SparqlUpdateParser(); parser.ExpressionFactories = this._config.ExpressionFactories; SparqlUpdateCommandSet commands = parser.ParseFromString(updateText); //Check whether we need to use authentication //If there are no user groups then no authentication is in use so we default to authenticated with no per-action authentication needed bool isAuth = true, requireActionAuth = false; if (this._config.UserGroups.Any()) { //If we have user isAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups); requireActionAuth = true; } if (!isAuth) return; //First check actions to see whether they are all permissible foreach (SparqlUpdateCommand cmd in commands.Commands) { //Authenticate each action bool actionAuth = true; if (requireActionAuth) actionAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups, this.GetPermissionAction(cmd)); if (!actionAuth) { throw new SparqlUpdateException("You are not authorised to perform the " + this.GetPermissionAction(cmd) + " action"); } } //Then assuming we got here this means all our actions are permitted so now we can process the updates this.ProcessUpdates(commands); //Flush outstanding changes this._config.Processor.Flush(); //Update the Cache as the request may have changed the endpoint this.UpdateConfig(context); } catch (RdfParseException parseEx) { HandleErrors(context, "Parsing Error", updateText, parseEx); } catch (SparqlUpdateException updateEx) { HandleErrors(context, "Update Error", updateText, updateEx); } catch (RdfException rdfEx) { HandleErrors(context, "RDF Error", updateText, rdfEx); } catch (Exception ex) { HandleErrors(context, "Error", updateText, ex); } }
private void SparqlQueryAndUpdateThreadSafeEvaluationActual() { String query1 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }"; String query2 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }"; String query3 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/3> { ?s ?p ?o } }"; String update1 = "INSERT DATA { GRAPH <http://example.org/3> { <ex:subj> <ex:pred> <ex:obj> } }"; SparqlQuery q1 = this._parser.ParseFromString(query1); SparqlQuery q2 = this._parser.ParseFromString(query2); SparqlQuery q3 = this._parser.ParseFromString(query3); Assert.IsFalse(q1.UsesDefaultDataset, "Query 1 should not be thread safe"); Assert.IsFalse(q2.UsesDefaultDataset, "Query 2 should not be thread safe"); Assert.IsFalse(q3.UsesDefaultDataset, "Query 3 should not be thread safe"); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(update1); InMemoryDataset dataset = new InMemoryDataset(); Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); g.BaseUri = new Uri("http://example.org/1"); Graph h = new Graph(); h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.Functions.LeviathanFunctionLibrary.ttl"); h.BaseUri = new Uri("http://example.org/2"); Graph i = new Graph(); i.BaseUri = new Uri("http://example.org/3"); dataset.AddGraph(g); dataset.AddGraph(h); dataset.AddGraph(i); LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset); LeviathanUpdateProcessor upProcessor = new LeviathanUpdateProcessor(dataset); QueryWithGraphDelegate d = new QueryWithGraphDelegate(this.QueryWithGraph); RunUpdateDelegate d2 = new RunUpdateDelegate(this.RunUpdate); IAsyncResult r1 = d.BeginInvoke(q1, processor, null, null); IAsyncResult r2 = d.BeginInvoke(q2, processor, null, null); IAsyncResult r3 = d.BeginInvoke(q3, processor, null, null); IAsyncResult r4 = d2.BeginInvoke(cmds, upProcessor, null, null); WaitHandle.WaitAll(new WaitHandle[] { r1.AsyncWaitHandle, r2.AsyncWaitHandle, r3.AsyncWaitHandle, r4.AsyncWaitHandle }); IGraph gQuery = d.EndInvoke(r1); Assert.AreEqual(g, gQuery, "Query 1 Result not as expected"); IGraph hQuery = d.EndInvoke(r2); Assert.AreEqual(h, hQuery, "Query 2 Result not as expected"); IGraph iQuery = d.EndInvoke(r3); if (iQuery.IsEmpty) { Console.WriteLine("Query 3 executed before the INSERT DATA command - running again to get the resulting graph"); iQuery = this.QueryWithGraph(q3, processor); } else { Console.WriteLine("Query 3 executed after the INSERT DATA command"); } //Test iQuery against an empty Graph Assert.IsFalse(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple"); Assert.AreNotEqual(new Graph(), iQuery, "Graphs should not be equal"); Assert.AreNotEqual(g, h, "Graphs should not be different"); }
public void SparqlDatasetDefaultGraphManagementWithUpdate5() { TripleStore store = new TripleStore(); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/graph"); store.Add(g); Graph h = new Graph(); h.BaseUri = new Uri("http://example.org/someOtherGraph"); store.Add(h); InMemoryDataset dataset = new InMemoryDataset(store, h.BaseUri); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#>; WITH <http://example.org/graph> INSERT { ?s a ?type } USING <http://example.org/someOtherGraph> WHERE { ?s a ?type }; DELETE WHERE { ?s a ?type }"); processor.ProcessCommandSet(cmds); Assert.IsFalse(g.IsEmpty, "First Graph should not be empty as should have been filled by the INSERT command"); Assert.IsFalse(h.IsEmpty, "Second Graph should not be empty as should not have been filled by the LOAD command"); Assert.IsFalse(h.HasSubGraph(g), "First Graph should not be a subgraph of the Second Graph as the DELETE should have eliminated the subgraph relationship"); }
public void SparqlDatasetDefaultGraphManagementWithUpdate() { TripleStore store = new TripleStore(); Graph g = new Graph(); store.Add(g); Graph h = new Graph(); h.BaseUri = new Uri("http://example.org/someOtherGraph"); store.Add(h); InMemoryDataset dataset = new InMemoryDataset(store, h.BaseUri); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#>"); processor.ProcessCommandSet(cmds); Assert.IsTrue(g.IsEmpty, "Graph with null URI (normally the default Graph) should be empty as the Default Graph for the Dataset should have been a named Graph so this Graph should not have been filled by the LOAD Command"); Assert.IsFalse(h.IsEmpty, "Graph with name should be non-empty as it should have been the Default Graph for the Dataset and so filled by the LOAD Command"); }
/// <summary> /// Processes a PATCH operation /// </summary> /// <param name="context">HTTP Context</param> public override void ProcessPatch(HttpContext context) { //Work out the Graph URI we want to patch Uri graphUri = this.ResolveGraphUri(context); //If the Request has the SPARQL Update MIME Type then we can process it if (context.Request.ContentLength > 0) { if (context.Request.ContentType.Equals("application/sparql-update")) { //Try and parse the SPARQL Update //No error handling here as we assume the calling IHttpHandler does that String patchData; using (StreamReader reader = new StreamReader(context.Request.InputStream)) { patchData = reader.ReadToEnd(); reader.Close(); } SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(patchData); //Assuming that we've got here i.e. the SPARQL Updates are parseable then //we need to check that they actually affect the relevant Graph if (cmds.Commands.All(c => c.AffectsSingleGraph && c.AffectsGraph(graphUri))) { GenericUpdateProcessor processor = new GenericUpdateProcessor(this._manager); processor.ProcessCommandSet(cmds); processor.Flush(); } else { //One/More commands either do no affect a Single Graph or don't affect the Graph //implied by the HTTP Request so give a 422 response context.Response.StatusCode = 422; return; } } else { //Don't understand other forms of PATCH requests context.Response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType; return; } } else { //Empty Request is a Bad Request context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } }
public void SparqlUpdateCopyCommand2() { IGraph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/source"); IGraph h = new Graph(); FileLoader.Load(h, "Turtle.ttl"); h.BaseUri = null; TripleStore store = new TripleStore(); store.Add(g); store.Add(h); Assert.AreNotEqual(g, h, "Graphs should not be equal"); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet commands = parser.ParseFromString("COPY GRAPH <http://example.org/source> TO DEFAULT"); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store); processor.ProcessCommandSet(commands); g = store.Graph(new Uri("http://example.org/source")); h = store.Graph(null); Assert.IsFalse(g.IsEmpty, "Source Graph should not be empty"); Assert.IsFalse(h.IsEmpty, "Destination Graph should not be empty"); Assert.AreEqual(g, h, "Graphs should now be equal"); }
/// <summary> /// Executes this command as an update /// </summary> public void ExecuteUpdate() { if (this._updateProcessor == null) throw new SparqlUpdateException("Cannot call ExecuteUpdate() when the UpdateProcessor property has not been set"); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(this.ToString()); this._updateProcessor.ProcessCommandSet(cmds); }
/// <summary> /// Processes Update requests /// </summary> /// <param name="context">HTTP Context</param> public void ProcessUpdateRequest(HttpContext context) { if (this._config.UpdateProcessor == null) { context.Response.StatusCode = (int)HttpStatusCode.NotImplemented; return; } if (context.Request.HttpMethod.Equals("OPTIONS")) { //OPTIONS requests always result in the Service Description document IGraph svcDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, UriFactory.Create(context.Request.Url.AbsoluteUri), ServiceDescriptionType.Update); HandlerHelper.SendToClient(context, svcDescrip, this._config); return; } //See if there has been an update submitted String updateText = null; if (context.Request.ContentType != null) { if (context.Request.ContentType.Equals(MimeTypesHelper.WWWFormURLEncoded)) { updateText = context.Request.Form["update"]; } else if (context.Request.ContentType != null && context.Request.ContentType.Equals(MimeTypesHelper.SparqlUpdate)) { updateText = new StreamReader(context.Request.InputStream).ReadToEnd(); } } else { updateText = context.Request.Form["update"]; } //If no Update sent either show Update Form or give a HTTP 400 response if (updateText == null || updateText.Equals(String.Empty)) { if (this._config.ShowUpdateForm) { this.ShowUpdateForm(context); return; } else { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; return; } } //Get Other options associated with this update List<String> userDefaultGraphs = new List<String>(); List<String> userNamedGraphs = new List<String>(); //Get the USING URIs (if any) if (context.Request.QueryString["using-graph-uri"] != null) { userDefaultGraphs.AddRange(context.Request.QueryString.GetValues("using-graph-uri")); } else if (context.Request.Form["using-graph-uri"] != null) { userDefaultGraphs.AddRange(context.Request.Form.GetValues("using-graph-uri")); } //Get the USING NAMED URIs (if any) if (context.Request.QueryString["using-named-graph-uri"] != null) { userNamedGraphs.AddRange(context.Request.QueryString.GetValues("using-named-graph-uri")); } else if (context.Request.Form["using-named-graph-uri"] != null) { userNamedGraphs.AddRange(context.Request.Form.GetValues("using-named-graph-uri")); } try { //Now we're going to parse the Updates SparqlUpdateParser parser = new SparqlUpdateParser(); parser.ExpressionFactories = this._config.ExpressionFactories; SparqlUpdateCommandSet commands = parser.ParseFromString(updateText); //Check whether we need to use authentication //If there are no user groups then no authentication is in use so we default to authenticated with no per-action authentication needed bool isAuth = true, requireActionAuth = false; if (this._config.UserGroups.Any()) { //If we have user isAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups); requireActionAuth = true; } if (!isAuth) return; //First check actions to see whether they are all permissible and apply USING/USING NAMED paramaters foreach (SparqlUpdateCommand cmd in commands.Commands) { //Authenticate each action bool actionAuth = true; if (requireActionAuth) actionAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups, this.GetUpdatePermissionAction(cmd)); if (!actionAuth) { throw new SparqlUpdatePermissionException("You are not authorised to perform the " + this.GetUpdatePermissionAction(cmd) + " action"); } //Check whether we need to (and are permitted to) apply USING/USING NAMED parameters if (userDefaultGraphs.Count > 0 || userNamedGraphs.Count > 0) { BaseModificationCommand modify = cmd as BaseModificationCommand; if (modify != null) { if (modify.GraphUri != null || modify.UsingUris.Any() || modify.UsingNamedUris.Any()) { //Invalid if a command already has a WITH/USING/USING NAMED throw new SparqlUpdateMalformedException("A command in your update request contains a WITH/USING/USING NAMED clause but you have also specified one/both of the using-graph-uri or using-named-graph-uri parameters which is not permitted by the SPARQL Protocol"); } else { //Otherwise go ahead and apply userDefaultGraphs.ForEach(u => modify.AddUsingUri(UriFactory.Create(u))); userNamedGraphs.ForEach(u => modify.AddUsingNamedUri(UriFactory.Create(u))); } } } } //Then assuming we got here this means all our actions are permitted so now we can process the updates this.ProcessUpdates(commands); //Flush outstanding changes this._config.UpdateProcessor.Flush(); //Update the Cache as the request may have changed the endpoint this.UpdateConfig(context); } catch (RdfParseException parseEx) { HandleUpdateErrors(context, "Parsing Error", updateText, parseEx, (int)HttpStatusCode.BadRequest); } catch (SparqlUpdatePermissionException permEx) { HandleUpdateErrors(context, "Permissions Error", updateText, permEx, (int)HttpStatusCode.Forbidden); } catch (SparqlUpdateMalformedException malEx) { HandleUpdateErrors(context, "Malformed Update Error", updateText, malEx, (int)HttpStatusCode.BadRequest); } catch (SparqlUpdateException updateEx) { HandleUpdateErrors(context, "Update Error", updateText, updateEx); } catch (RdfException rdfEx) { HandleUpdateErrors(context, "RDF Error", updateText, rdfEx); } catch (Exception ex) { HandleUpdateErrors(context, "Error", updateText, ex); } }
public void SparqlParameterizedString() { String test = @"INSERT DATA { GRAPH @graph { <http://uri> <http://uri> <http://uri> }}"; SparqlParameterizedString cmdString = new SparqlParameterizedString(test); cmdString.SetUri("graph", new Uri("http://example.org/graph")); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(cmdString); cmds.ToString(); }
/// <summary> /// Executes a SPARQL Update on the native Quad Store /// </summary> /// <param name="sparqlUpdate">SPARQL Update to execute</param> /// <remarks> /// <para> /// This method will first attempt to parse the updat einto a <see cref="SparqlUpdateCommandSet">SparqlUpdateCommandSet</see> object. If this succeeds then each command in the command set will be issued to Virtuoso. /// </para> /// <para> /// If the parsing fails then the update will be executed anyway using Virtuoso's SPASQL (SPARQL + SQL) syntax. Parsing can fail because Virtuoso supports various SPARQL extensions which the library does not support. /// </para> /// </remarks> /// <exception cref="SparqlUpdateException">Thrown if an error occurs in making the update</exception> public void Update(String sparqlUpdate) { try { this.Open(true); //Try and parse the SPARQL Update String SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet commands = parser.ParseFromString(sparqlUpdate); //Process each Command individually foreach (SparqlUpdateCommand command in commands.Commands) { //Make the Update against Virtuoso VirtuosoCommand cmd = this._db.CreateCommand(); cmd.CommandText = "SPARQL " + command.ToString(); cmd.ExecuteNonQuery(); } this.Close(true); } catch (RdfParseException) { //Ignore failed parsing and attempt to execute anyway VirtuosoCommand cmd = this._db.CreateCommand(); cmd.CommandText = "SPARQL " + sparqlUpdate; cmd.ExecuteNonQuery(); this.Close(true); } catch (SparqlUpdateException) { this.Close(true, true); throw; } catch (Exception ex) { //Wrap in a SPARQL Update Exception this.Close(true, true); throw new SparqlUpdateException("An error occurred while trying to perform the SPARQL Update with Virtuoso. Note that Virtuoso primarily supports SPARUL (the precursor to SPARQL Update) and many valid SPARQL Update Commands are not yet supported by Virtuoso.", ex); } }
private void TestDropGraphCommit() { ISparqlDataset dataset = this.GetNonEmptyDataset(); String updates = "DROP GRAPH <" + TestGraphUri.ToString() + ">"; SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(updates); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Assert.IsFalse(dataset.HasGraph(TestGraphUri), "Graph should not exist"); }
public void SparqlUpdateMoveCommand3() { IGraph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = null; IGraph h = new Graph(); FileLoader.Load(h, "Turtle.ttl"); h.BaseUri = new Uri("http://example.org/destination"); TripleStore store = new TripleStore(); store.Add(g); store.Add(h); Assert.AreNotEqual(g, h, "Graphs should not be equal"); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet commands = parser.ParseFromString("MOVE DEFAULT TO GRAPH <http://example.org/destination>"); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store); processor.ProcessCommandSet(commands); g = store.HasGraph(null) ? store.Graph(null) : null; h = store.Graph(new Uri("http://example.org/destination")); Assert.IsFalse(h.IsEmpty, "Destination Graph should not be empty"); Assert.IsFalse(g == null, "Default Graph should still exist"); Assert.IsTrue(g.IsEmpty, "Source Graph (the Default Graph) should be Empty"); Graph orig = new Graph(); FileLoader.Load(orig, "InferenceTest.ttl"); Assert.AreEqual(orig, h, "Destination Graph should be equal to the original contents of the Source Graph"); }
public void SparqlUpdateModify() { TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = null; store.Add(g); IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType)); Assert.AreNotEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain some rdf:type Triples"); String update = "DELETE {?s a ?type} WHERE {?s a ?type}"; SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(update); store.ExecuteUpdate(cmds); Assert.AreEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain no rdf:type Triples after DELETE command executes"); }
public void SparqlUpdateInsertCommand() { SparqlParameterizedString command = new SparqlParameterizedString(); command.Namespaces.AddNamespace("rdf", new Uri(NamespaceMapper.RDF)); command.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS)); command.CommandText = "INSERT { ?s rdf:type ?class } WHERE { ?s a ?type . ?type rdfs:subClassOf+ ?class };"; command.CommandText += "INSERT { ?s ?property ?value } WHERE {?s ?p ?value . ?p rdfs:subPropertyOf+ ?property };"; command.CommandText += "INSERT { ?s rdf:type rdfs:Class } WHERE { ?s rdfs:subClassOf ?class };"; command.CommandText += "INSERT { ?s rdf:type rdf:Property } WHERE { ?s rdfs:subPropertyOf ?property };"; TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.Retract(g.Triples.Where(t => !t.IsGroundTriple)); g.BaseUri = null; store.Add(g); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(command); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store); processor.ProcessCommandSet(cmds); TestTools.ShowGraph(g); Console.WriteLine(); //Now reload the test data and apply an RDFS reasoner over it //This should give us a Graph equivalent to the one created by the previous INSERT commands Graph h = new Graph(); FileLoader.Load(h, "InferenceTest.ttl"); h.Retract(h.Triples.Where(t => !t.IsGroundTriple)); RdfsReasoner reasoner = new RdfsReasoner(); reasoner.Apply(h); GraphDiffReport diff = h.Difference(g); if (!diff.AreEqual) { TestTools.ShowDifferences(diff); } Assert.AreEqual(h, g, "Graphs should be equal"); }
public static void Main(string[] args) { String stdprefixes = "PREFIX rdf: <" + NamespaceMapper.RDF + ">\nPREFIX rdfs: <" + NamespaceMapper.RDFS + ">\nPREFIX xsd: <" + NamespaceMapper.XMLSCHEMA + ">\n"; List<String> testQueries = new List<string>() { "CLEAR DEFAULT", "CLEAR GRAPH <http://example.org>", "CREATE GRAPH <http://example.org>", "CREATE SILENT GRAPH <http://example.org>", "DELETE WHERE { ?s ?p ?o }", "DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } USING <http://example.org/1> WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } USING NAMED <http://example.org/1> WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> USING <http://example.org/3> WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } INSERT { ?s ?p ?o } WHERE { ?s ?p ?o }", "WITH <http://example.org> DELETE { ?s ?p ?o } INSERT { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> USING <http://example.org/3> WHERE { ?s ?p ?o }", "DELETE { ?s ?p ?o . GRAPH <http://example.org> { ?s a ?type } } WHERE { ?s ?p ?o }", "WITH <http://example.org> INSERT { ?s ?p ?o } WHERE { ?s ?p ?o }", "DROP GRAPH <http://example.org>", "DROP SILENT GRAPH <http://example.org>", "INSERT { ?s ?p ?o } WHERE { ?s ?p ?o }", "INSERT { ?s ?p ?o } WHERE { GRAPH <http://example.org> { ?s ?p ?o } }", "INSERT { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> USING <http://example.org/3> WHERE { ?s ?p ?o }", "LOAD <http://dbpedia.org/resource/Southampton>", "LOAD <http://dbpedia.org/resource/Southampton> INTO GRAPH <http://example.org>" }; StreamWriter output = new StreamWriter("SparqlUpdateTests.txt"); Console.SetOut(output); SparqlUpdateParser parser = new SparqlUpdateParser(); parser.TraceTokeniser = true; foreach (String testQuery in testQueries) { try { Console.WriteLine("Trying to parse the following SPARQL Update Command"); Console.WriteLine(testQuery); SparqlUpdateCommandSet commands = parser.ParseFromString(testQuery); Console.WriteLine("Parsed OK"); foreach (SparqlUpdateCommand cmd in commands.Commands) { Console.WriteLine(cmd.GetType().ToString()); } Console.WriteLine(commands.ToString()); } catch (RdfParseException parseEx) { reportError(output, "Parser Error", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Error", rdfEx); } catch (Exception ex) { reportError(output, "Error", ex); } Console.WriteLine(); Console.WriteLine(); } //Try and parse in bulk Console.WriteLine("Parsing in bulk"); String allQueries = String.Join("\n;\n", testQueries.ToArray()); try { SparqlUpdateCommandSet commands = parser.ParseFromString(allQueries); Console.WriteLine("Parsed OK"); foreach (SparqlUpdateCommand cmd in commands.Commands) { Console.WriteLine(cmd.GetType().ToString()); } Console.WriteLine(commands.ToString()); } catch (RdfParseException parseEx) { reportError(output, "Parser Error", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Error", rdfEx); } catch (Exception ex) { reportError(output, "Error", ex); } output.Flush(); output.Close(); }
public void SparqlUpdateInsertCommand3() { SparqlParameterizedString command = new SparqlParameterizedString(); command.Namespaces.AddNamespace("rdf", new Uri(NamespaceMapper.RDF)); command.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS)); command.CommandText = "INSERT { ?s rdf:type ?class } USING NAMED <http://example.org/temp> WHERE { ?s a ?type . ?type rdfs:subClassOf+ ?class };"; command.CommandText += "INSERT { ?s ?property ?value } USING NAMED <http://example.org/temp> WHERE {?s ?p ?value . ?p rdfs:subPropertyOf+ ?property };"; command.CommandText += "INSERT { ?s rdf:type rdfs:Class } USING NAMED <http://example.org/temp> WHERE { ?s rdfs:subClassOf ?class };"; command.CommandText += "INSERT { ?s rdf:type rdf:Property } USING NAMED <http://example.org/temp> WHERE { ?s rdfs:subPropertyOf ?property };"; TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/temp"); store.Add(g); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(command); InMemoryDataset dataset = new InMemoryDataset(store); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); dataset.SetDefaultGraph(store.Graph(null)); processor.ProcessCommandSet(cmds); IGraph def = store.Graph(null); TestTools.ShowGraph(def); Assert.IsTrue(def.IsEmpty, "Graph should be empty as the commands only used USING NAMED (so shouldn't have changed the dataset) and the Active Graph for the dataset was empty so there should have been nothing matched to generate insertions from"); }
public void SparqlDatasetDefaultGraphManagementWithUpdate3() { TripleStore store = new TripleStore(); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/graph"); store.Add(g); Graph h = new Graph(); h.BaseUri = new Uri("http://example.org/someOtherGraph"); store.Add(h); InMemoryDataset dataset = new InMemoryDataset(store, h.BaseUri); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#> INTO GRAPH <http://example.org/graph>; LOAD <http://www.dotnetrdf.org/configuration#> INTO GRAPH <http://example.org/someOtherGraph>"); processor.ProcessCommandSet(cmds); Assert.IsFalse(g.IsEmpty, "First Graph should not be empty as should have been filled by the first LOAD command"); Assert.IsFalse(h.IsEmpty, "Second Graph should not be empty as should not have been filled by the second LOAD command"); Assert.AreEqual(g, h, "Graphs should be equal"); }
public void SparqlUpdateInsertCommand4() { SparqlParameterizedString command = new SparqlParameterizedString(); command.Namespaces.AddNamespace("rdf", new Uri(NamespaceMapper.RDF)); command.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS)); command.CommandText = "INSERT { ?s rdf:type ?class } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s a ?type . ?type rdfs:subClassOf+ ?class } };"; command.CommandText += "INSERT { ?s ?property ?value } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s ?p ?value . ?p rdfs:subPropertyOf+ ?property } };"; command.CommandText += "INSERT { ?s rdf:type rdfs:Class } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s rdfs:subClassOf ?class } };"; command.CommandText += "INSERT { ?s rdf:type rdf:Property } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s rdfs:subPropertyOf ?property } };"; TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/temp"); store.Add(g); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(command); InMemoryDataset dataset = new InMemoryDataset(store); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); dataset.SetDefaultGraph(store.Graph(null)); processor.ProcessCommandSet(cmds); IGraph def = store.Graph(null); TestTools.ShowGraph(def); //Apply a RDFS reasoner over the original input and output it into another graph //Should be equivalent to the default Graph Graph h = new Graph(); RdfsReasoner reasoner = new RdfsReasoner(); reasoner.Apply(g, h); TestTools.ShowGraph(h); GraphDiffReport report = h.Difference(def); if (!report.AreEqual) { TestTools.ShowDifferences(report); Assert.IsTrue(report.RemovedTriples.Count() == 1, "Should have only 1 missing Triple (due to rdfs:domain inference which is hard to encode in an INSERT command)"); } }
static void DoUpdate(Dictionary<String, String> arguments) { SparqlRemoteUpdateEndpoint endpoint; bool verbose = arguments.ContainsKey("verbose") || arguments.ContainsKey("v"); if (verbose) Options.HttpDebugging = true; //First get the Server to which we are going to connect try { if (arguments.ContainsKey("server") && !arguments["server"].Equals(String.Empty)) { endpoint = new SparqlRemoteUpdateEndpoint(new Uri(arguments["server"])); } else if (arguments.ContainsKey("service") && !arguments["service"].Equals(String.Empty)) { endpoint = new SparqlRemoteUpdateEndpoint(new Uri(arguments["service"])); } else { Console.Error.WriteLine("soh: Error: Required --server/--service argument not present"); Environment.Exit(-1); return; } } catch (UriFormatException uriEx) { Console.Error.WriteLine("soh: Error: Malformed SPARQL Update Endpoint URI"); Console.Error.WriteLine(uriEx.Message); Environment.Exit(-1); return; } if (verbose) Console.Error.WriteLine("soh: SPARQL Update Endpoint for URI " + endpoint.Uri + " created OK"); //Then decide where to get the update to execute from SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds; try { if (arguments.ContainsKey("update") && !arguments["update"].Equals(String.Empty)) { cmds = parser.ParseFromFile(arguments["update"]); } else if (arguments.ContainsKey("file") && !arguments["file"].Equals(String.Empty)) { cmds = parser.ParseFromFile(arguments["file"]); } else if (arguments.ContainsKey("$1") && !arguments["$1"].Equals(String.Empty)) { cmds = parser.ParseFromString(arguments["$1"]); } else { Console.Error.WriteLine("soh: Error: Required SPARQL Update not found - may be specified as --file/--update FILE or as final argument"); Environment.Exit(-1); return; } } catch (Exception ex) { Console.Error.WriteLine("soh: Error: Error Parsing SPARQL Update"); Console.Error.WriteLine(ex.Message); Environment.Exit(-1); return; } if (verbose) { Console.Error.WriteLine("soh: Parsed Update OK"); Console.Error.WriteLine("soh: dotNetRDF's interpretation of the Update:"); Console.Error.WriteLine(cmds.ToString()); Console.Error.WriteLine("soh: Submitting Update"); } try { endpoint.Update(cmds.ToString()); } catch (Exception ex) { Console.Error.WriteLine("soh: Error: Error while making the SPARQL Update"); Console.Error.WriteLine(ex.Message); Environment.Exit(-1); return; } }
public void SparqlUpdateDeleteWithCommand() { String command = "WITH <http://example.org/> DELETE { ?s ?p ?o } WHERE {?s ?p ?o}"; SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(command); DeleteCommand delete = (DeleteCommand)cmds[0]; Assert.AreEqual(new Uri("http://example.org/"), delete.GraphUri, "Graph URI of the Command should be equal to http://example.org/"); }
/// <summary> /// Executes an Update against the Triple Store /// </summary> /// <param name="update">SPARQL Update Command(s)</param> /// <remarks> /// As per the SPARQL 1.1 Update specification the command string may be a sequence of commands /// </remarks> public void ExecuteUpdate(string update) { SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet commandSet = parser.ParseFromString(update); this.ExecuteUpdate(commandSet); }
public void SparqlUpdateDeleteDataCombination() { SparqlParameterizedString command = new SparqlParameterizedString(); command.Namespaces.AddNamespace("ex", new Uri("http://example.org/")); command.CommandText = "DELETE DATA { ex:a ex:b ex:c GRAPH <http://example.org/graph> { ex:a ex:b ex:c } }"; SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(command); Assert.IsFalse(cmds.Commands.All(cmd => cmd.AffectsSingleGraph), "Commands should report that they do not affect a single Graph"); Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(null)), "Commands should report that they affect the Default Graph"); Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(new Uri("http://example.org/graph"))), "Commands should report that they affect the named Graph"); InMemoryDataset dataset = new InMemoryDataset(); IGraph def = new Graph(); def.NamespaceMap.Import(command.Namespaces); def.Assert(new Triple(def.CreateUriNode("ex:a"), def.CreateUriNode("ex:b"), def.CreateUriNode("ex:c"))); dataset.AddGraph(def); IGraph ex = new Graph(); ex.BaseUri = new Uri("http://example.org/graph"); ex.NamespaceMap.Import(command.Namespaces); ex.Assert(new Triple(ex.CreateUriNode("ex:a"), ex.CreateUriNode("ex:b"), ex.CreateUriNode("ex:c"))); dataset.AddGraph(ex); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Graph g = new Graph(); g.NamespaceMap.Import(command.Namespaces); Triple t = new Triple(g.CreateUriNode("ex:a"), g.CreateUriNode("ex:b"), g.CreateUriNode("ex:c")); def = dataset[null]; Assert.AreEqual(0, def.Triples.Count, "Should be 0 Triples in the Default Graph"); Assert.IsFalse(def.ContainsTriple(t), "Should not have the deleted Triple in the Default Graph"); ex = dataset[new Uri("http://example.org/graph")]; Assert.AreEqual(0, ex.Triples.Count, "Should be 0 Triples in the Named Graph"); Assert.IsFalse(ex.ContainsTriple(t), "Should not have the deleted Triple in the Named Graph"); }
/// <summary> /// Execute insert or delete SPARQL queries against Stardog; /// separate multiple statements with a semicolon /// </summary> /// <param name="command"></param> /// <returns></returns> public static bool Update(string command) { using (StardogConnector dog = new StardogConnector(ConnectionString(), ConnectionDatabase(), ConnectionUser(), ConnectionPassword())) { try { dog.Begin(); // wrap all the statements in this command into one transaction; otherwise stardog will run them as separate transactions GenericUpdateProcessor processor = new GenericUpdateProcessor(dog); SparqlUpdateParser parser = new SparqlUpdateParser(); processor.ProcessCommandSet(parser.ParseFromString(Prefixes() + command)); dog.Commit(); return true; } catch (Exception err) { try { dog.Rollback(); } catch { } throw err; } } }
public void SparqlUpdateAddCommand() { IGraph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/source"); IGraph h = new Graph(); FileLoader.Load(h, "Turtle.ttl"); h.BaseUri = new Uri("http://example.org/destination"); TripleStore store = new TripleStore(); store.Add(g); store.Add(h); Assert.AreNotEqual(g, h, "Graphs should not be equal"); SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet commands = parser.ParseFromString("ADD GRAPH <http://example.org/source> TO GRAPH <http://example.org/destination>"); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store); processor.ProcessCommandSet(commands); g = store.Graph(new Uri("http://example.org/source")); h = store.Graph(new Uri("http://example.org/destination")); Assert.IsFalse(g.IsEmpty, "Source Graph should not be empty"); Assert.IsFalse(h.IsEmpty, "Destination Graph should not be empty"); Assert.IsTrue(h.HasSubGraph(g), "Destination Graph should have Source Graph as a subgraph"); }
/// <summary> /// Attempt to auto-detect the syntax of the current document using the filename as a guide /// </summary> public void AutoDetectSyntax(String filename) { if (this._editor == null) return; //Not yet ready if (filename == null || System.IO.Path.GetExtension(filename).Equals(String.Empty)) { try { //First see if it's an RDF format IRdfReader parser = StringParser.GetParser(this._editor.Text); if (parser is NTriplesParser) { //NTriples is the fallback so if we get this check if it's actually SPARQL Results try { ISparqlResultsReader sparqlParser = StringParser.GetResultSetParser(this._editor.Text); } catch (RdfParserSelectionException) { //Not a valid SPARQL Results format - may be a SPARQL Query or a SPARQL Update? SparqlQueryParser queryParser = new SparqlQueryParser(SparqlQuerySyntax.Sparql_1_1); try { SparqlQuery q = queryParser.ParseFromString(this._editor.Text); this.SetHighlighter("SparqlQuery11"); } catch (RdfParseException) { //Not a valid SPARQL Query - valid SPARQL Update? SparqlUpdateParser updateParser = new SparqlUpdateParser(); try { SparqlUpdateCommandSet cmds = updateParser.ParseFromString(this._editor.Text); this.SetHighlighter("SparqlUpdate11"); } catch (RdfParseException) { //Was probably actually NTriples this.SetHighlighter(parser); } } } } else { //Got a non NTriples RDF parser so use that to set Highlighter this.SetHighlighter(parser); } } catch (RdfParserSelectionException) { this.SetNoHighlighting(); } return; } try { IHighlightingDefinition def = HighlightingManager.Instance.GetDefinitionByExtension(System.IO.Path.GetExtension(filename)); if (this._enableHighlighting) this._editor.SyntaxHighlighting = def; if (def != null) { this._currSyntax = def.Name; this.SetCurrentHighlighterChecked(def.Name); this.SetCurrentValidator(def.Name); this.SetCurrentAutoCompleter(def.Name); } else { this.SetNoHighlighting(); } } catch { this.SetNoHighlighting(); } }
/// <summary> /// Execute insert or delete SPARQL queries against Stardog; /// separate multiple statements with a semicolon /// </summary> /// <param name="command"></param> /// <returns></returns> public static bool Update(string command) { using (StardogConnector dog = new StardogConnector(ConnectionString(), ConnectionDatabase(), ConnectionUser(), ConnectionPassword())) { GenericUpdateProcessor processor = new GenericUpdateProcessor(dog); SparqlUpdateParser parser = new SparqlUpdateParser(); processor.ProcessCommandSet(parser.ParseFromString(Prefixes() + command)); } return true; }