/// <summary> /// Returns the actual Query/Update String with parameter and variable values inserted /// </summary> /// <returns></returns> public override string ToString() { StringBuilder output = new StringBuilder(); // First prepend Base declaration if (BaseUri != null) { output.AppendLine("BASE <" + _formatter.FormatUri(BaseUri) + ">"); } // Next prepend any Namespace Declarations foreach (String prefix in _nsmap.Prefixes) { output.AppendLine("PREFIX " + prefix + ": <" + _formatter.FormatUri(_nsmap.GetNamespaceUri(prefix)) + ">"); } // Then append the text with variable and parameters replaced by their values if set INode value = null; for (int i = 0, l = _commandText.Count; i < l; i++) { String segment = _commandText[i]; switch (segment[0]) { case '@': String paramName = segment.Substring(1); _parameters.TryGetValue(paramName, out value); if (value != null) { output.Append(_formatter.Format(value)); } else { output.Append(segment); } break; case '?': case '$': String varName = segment.Substring(1); _variables.TryGetValue(varName, out value); if (value != null) { output.Append(_formatter.Format(value)); } else { output.Append(segment); } break; default: output.Append(_commandText[i]); break; } } return(output.ToString()); }
/// <summary> /// Deletes a graph from the store. /// </summary> /// <param name="graphUri">URI of the graph to delete.</param> public override void DeleteGraph(Uri graphUri) { if (graphUri == null) { Update("DROP DEFAULT"); } else { Update("DROP GRAPH <" + _formatter.FormatUri(graphUri) + ">"); } }
/// <summary> /// Applies reasoning on the Input Graph materialising the generated Triples in the Output Graph. /// </summary> /// <param name="input">Input Graph.</param> /// <param name="output">Output Graph.</param> public void Apply(IGraph input, IGraph output) { TripleStore store = new TripleStore(); store.Add(input); if (!ReferenceEquals(input, output)) { store.Add(output, true); } // Apply each rule in turn foreach (String[] rule in _rules) { // Build the final version of the rule text for the given input and output StringBuilder ruleText = new StringBuilder(); // If there's a Base URI on the Output Graph need a WITH clause if (output.BaseUri != null) { ruleText.AppendLine("WITH <" + _formatter.FormatUri(output.BaseUri) + ">"); } ruleText.AppendLine(rule[0]); // If there's a Base URI on the Input Graph need a USING clause if (input.BaseUri != null) { ruleText.AppendLine("USING <" + _formatter.FormatUri(input.BaseUri) + ">"); } ruleText.AppendLine(rule[1]); ISyntaxValidationResults results = _validator.Validate(ruleText.ToString()); if (results.IsValid) { store.ExecuteUpdate((SparqlUpdateCommandSet)results.Result); } } }
private string GetSparqlQuery(string graph, IEnumerable <Triple> additions) { var stringBuilder = new StringBuilder() .AppendLine("INSERT DATA {") .AppendLine("GRAPH <" + formatter.FormatUri(graph) + "> {"); foreach (var addition in additions) { var formattedTriple = formatter.Format(addition); stringBuilder.AppendLine(formattedTriple); } return(stringBuilder .AppendLine("}") .AppendLine("}") .ToString()); }
/// <summary> /// Updates a Graph in the Fuseki store /// </summary> /// <param name="graphUri">URI of the Graph to update</param> /// <param name="additions">Triples to be added</param> /// <param name="removals">Triples to be removed</param> public override void UpdateGraph(string graphUri, IEnumerable <Triple> additions, IEnumerable <Triple> removals) { try { String graph = (graphUri != null && !graphUri.Equals(String.Empty)) ? "GRAPH <" + _formatter.FormatUri(graphUri) + "> {" : String.Empty; StringBuilder update = new StringBuilder(); if (additions != null) { if (additions.Any()) { update.AppendLine("INSERT DATA {"); if (!graph.Equals(String.Empty)) { update.AppendLine(graph); } foreach (Triple t in additions) { update.AppendLine(_formatter.Format(t)); } if (!graph.Equals(String.Empty)) { update.AppendLine("}"); } update.AppendLine("}"); } } if (removals != null) { if (removals.Any()) { if (update.Length > 0) { update.AppendLine(";"); } update.AppendLine("DELETE DATA {"); if (!graph.Equals(String.Empty)) { update.AppendLine(graph); } foreach (Triple t in removals) { update.AppendLine(_formatter.Format(t)); } if (!graph.Equals(String.Empty)) { update.AppendLine("}"); } update.AppendLine("}"); } } if (update.Length > 0) { // Make the SPARQL Update Request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_updateUri); request.Method = "POST"; request.ContentType = "application/sparql-update"; request = ApplyRequestOptions(request); Tools.HttpDebugRequest(request); StreamWriter writer = new StreamWriter(request.GetRequestStream()); writer.Write(update.ToString()); writer.Close(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Tools.HttpDebugResponse(response); // If we get here without erroring then the request was OK response.Close(); } } } catch (WebException webEx) { throw StorageHelper.HandleHttpError(webEx, "updating a Graph in"); } }