/// <summary> /// Builds a graph of rdf:type triples to restrict subsequent SPIN Constructors, Rules or Constraint checks evaluations /// </summary> /// <param name="resources"></param> internal Uri CreateExecutionContext(IEnumerable <INode> resources) { Uri executionContextUri = null; if (resources != null) { executionContextUri = RDFRuntime.NewTempGraphUri(); SparqlParameterizedString restrictionQuery; IGraph resourceRestrictions = new ThreadSafeGraph(); resourceRestrictions.BaseUri = executionContextUri; INode inputGraphNode = RDFUtil.CreateUriNode(executionContextUri); foreach (INode resource in resources) { resourceRestrictions.Assert(inputGraphNode, RDFRuntime.PropertyExecutionRestrictedTo, resource); } Storage.SaveGraph(resourceRestrictions); restrictionQuery = new SparqlParameterizedString(SparqlTemplates.SetExecutionContext); restrictionQuery.SetUri("resourceRestriction", executionContextUri); StringBuilder sb = new StringBuilder(); foreach (Resource graph in DefaultGraphs) { sb.AppendLine("USING <" + graph.Uri.ToString() + ">"); } restrictionQuery.CommandText = restrictionQuery.CommandText.Replace("@USING_DEFAULT", sb.ToString()); Storage.Update(restrictionQuery.ToString()); } return(executionContextUri); }
protected void printGraphIRIs(ISparqlPrinter p, String keyword) { List <String> graphIRIs = new List <String>(); { IEnumerator <Triple> it = listProperties(SP.PropertyGraphIRI).GetEnumerator(); while (it.MoveNext()) { Triple s = it.Current; if (s.Object is IUriNode) { graphIRIs.Add(((IUriNode)s.Object).Uri.ToString()); } } graphIRIs.Sort(); } foreach (String graphIRI in graphIRIs) { p.print(" "); if (keyword != null) { p.printKeyword(keyword); p.print(" "); } p.printURIResource(Resource.Get(RDFUtil.CreateUriNode(UriFactory.Create(graphIRI)), getModel())); } }
/** * Gets a Template with a given URI in its defining Model. * @param uri the URI of the Template to look up * @param model an (optional) Model that should also be used for look up * @return a Template or null */ public static ITemplate getTemplate(Uri uri, SpinProcessor model) { if (model != null) { IResource r = Resource.Get(RDFUtil.CreateUriNode(uri), model); if (r.hasProperty(RDF.PropertyType, SPIN.ClassTemplate)) { return((ITemplate)r.As(typeof(TemplateImpl))); } } if (templates.ContainsKey(uri)) { return(templates[uri]); } return(null); }
override public void Print(ISparqlPrinter p) { p.printKeyword("SERVICE"); IVariable var = getServiceVariable(); if (var != null) { p.print(" "); p.printVariable(var.getName()); } else { Uri uri = getServiceURI(); if (uri != null) { p.print(" "); p.printURIResource(Resource.Get(RDFUtil.CreateUriNode(uri), getModel())); } } printNestedElementList(p); }
/** * Gets a registered Function with a given URI. * @param uri the URI of the Function to get * @param model an (optional) Model that should also be used to look up * locally defined functions (currently not used) * @return the Function or null if none was found */ public static IFunction getFunction(Uri uri, SpinProcessor model) { IFunction function = null; if (functions.ContainsKey(uri)) { function = functions[uri]; } if (function != null) { return(function); } if (model != null) { function = (IFunction)Resource.Get(RDFUtil.CreateUriNode(uri), model).As(typeof(FunctionImpl)); if (function.hasProperty(RDF.PropertyType, SPIN.ClassFunction)) { return(function); } } return(null); }
// TODO check whether transactions are supported by the storage provider to make those as atomical as possible /// <summary> /// Flushes changes to the dataset /// TODO handle dataset changes as updates instread of overwriting it to make it workable in a concurrent environment. /// </summary> public void Flush() { if (!Configuration.IsChanged) { return; } // TODO check if the updates did not raise any constraint violation, otherwise reject the Flush request // TODO related to the concurrency policy problem : handle any concurrent updates may have happened and succeded between the first modification and here SpinDatasetDescription updatedSourceDataset = new SpinDatasetDescription(Configuration.SourceUri); updatedSourceDataset.Assert(Configuration.GetTriplesWithPredicateObject(RDF.PropertyType, SPIN.ClassLibraryOntology)); updatedSourceDataset.Assert(Configuration.GetTriplesWithPredicateObject(RDF.PropertyType, SD.ClassGraph)); foreach (SpinWrappedGraph g in Configuration.ModificableGraphs) { Uri updatedGraphUri = Configuration.GetUpdateControlUri(g.BaseUri); Uri sourceGraph = g.BaseUri; if (Configuration.IsGraphReplaced(sourceGraph)) { Storage.Update("WITH <" + updatedGraphUri.ToString() + "> DELETE { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p } WHERE { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p }"); // For safety only Storage.Update("MOVE GRAPH <" + updatedGraphUri.ToString() + "> TO <" + sourceGraph.ToString() + ">"); } else if (Configuration.IsGraphUpdated(sourceGraph)) { Storage.Update("DELETE { GRAPH <" + updatedGraphUri.ToString() + "> { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p } . GRAPH <" + sourceGraph.ToString() + "> { ?s ?p ?o } } USING <" + updatedGraphUri.ToString() + "> USING NAMED <" + g.BaseUri.ToString() + "> WHERE { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p . GRAPH <" + g.BaseUri.ToString() + "> { ?s ?p ?o} }"); Storage.Update("ADD GRAPH <" + updatedGraphUri.ToString() + "> TO <" + sourceGraph.ToString() + ">"); } else { updatedSourceDataset.Retract(Configuration.GetTriplesWithSubject(RDFUtil.CreateUriNode(sourceGraph))); } } // TODO update the original dataset instead of overwriting it Storage.SaveGraph(updatedSourceDataset); DisposeUpdateControlledDataset(); }
// TODO handle the defaultGraph case internal static INode ToSpinRdf(this SparqlUpdateCommand query, IGraph g) { INode root = g.CreateBlankNode(); SpinVariableTable varTable = new SpinVariableTable(g); switch (query.CommandType) { case SparqlUpdateCommandType.Add: g.Assert(root, RDF.PropertyType, SP.ClassAdd); AddCommand add = (AddCommand)query; if (add.SourceUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(add.SourceUri)); } if (add.DestinationUri == null) { g.Assert(root, SP.PropertyInto, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyInto, RDFUtil.CreateUriNode(add.DestinationUri)); } break; case SparqlUpdateCommandType.Clear: g.Assert(root, RDF.PropertyType, SP.ClassClear); if (((ClearCommand)query).TargetUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(((ClearCommand)query).TargetUri)); } break; case SparqlUpdateCommandType.Copy: g.Assert(root, RDF.PropertyType, SP.ClassCopy); CopyCommand copy = (CopyCommand)query; if (copy.SourceUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(copy.SourceUri)); } if (copy.DestinationUri == null) { g.Assert(root, SP.PropertyInto, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyInto, RDFUtil.CreateUriNode(copy.DestinationUri)); } break; case SparqlUpdateCommandType.Create: g.Assert(root, RDF.PropertyType, SP.ClassCreate); CreateCommand create = (CreateCommand)query; if (create.TargetUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(create.TargetUri)); } break; case SparqlUpdateCommandType.Delete: g.Assert(root, RDF.PropertyType, SP.ClassModify); DeleteCommand delete = (DeleteCommand)query; if (delete.GraphUri != null) { g.Assert(root, SP.PropertyWith, RDFUtil.CreateUriNode(delete.GraphUri)); } // TODO handle the usings g.Assert(root, SP.PropertyDeletePattern, delete.DeletePattern.ToSpinRdf(g, varTable)); g.Assert(root, SP.PropertyWhere, delete.WherePattern.ToSpinRdf(g, varTable)); break; case SparqlUpdateCommandType.DeleteData: g.Assert(root, RDF.PropertyType, SP.ClassDeleteData); g.Assert(root, SP.PropertyData, ((DeleteDataCommand)query).DataPattern.ToSpinRdf(g, varTable)); break; case SparqlUpdateCommandType.Drop: g.Assert(root, RDF.PropertyType, SP.ClassDrop); DropCommand drop = (DropCommand)query; if (drop.TargetUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(drop.TargetUri)); } g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(((DropCommand)query).TargetUri)); break; case SparqlUpdateCommandType.Insert: g.Assert(root, RDF.PropertyType, SP.ClassModify); InsertCommand insert = (InsertCommand)query; if (insert.GraphUri != null) { g.Assert(root, SP.PropertyWith, RDFUtil.CreateUriNode(insert.GraphUri)); } g.Assert(root, SP.PropertyInsertPattern, insert.InsertPattern.ToSpinRdf(g, varTable)); g.Assert(root, SP.PropertyWhere, insert.WherePattern.ToSpinRdf(g, varTable)); break; case SparqlUpdateCommandType.InsertData: g.Assert(root, RDF.PropertyType, SP.ClassInsertData); g.Assert(root, SP.PropertyData, ((InsertDataCommand)query).DataPattern.ToSpinRdf(g, varTable)); break; case SparqlUpdateCommandType.Load: g.Assert(root, RDF.PropertyType, SP.ClassLoad); LoadCommand load = (LoadCommand)query; if (load.SourceUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(load.SourceUri)); } if (load.TargetUri == null) { g.Assert(root, SP.PropertyInto, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyInto, RDFUtil.CreateUriNode(load.TargetUri)); } break; case SparqlUpdateCommandType.Modify: g.Assert(root, RDF.PropertyType, SP.ClassModify); ModifyCommand modify = (ModifyCommand)query; if (modify.GraphUri != null) { g.Assert(root, SP.PropertyWith, RDFUtil.CreateUriNode(modify.GraphUri)); } if (modify.DeletePattern != null) { g.Assert(root, SP.PropertyDeletePattern, modify.DeletePattern.ToSpinRdf(g, varTable)); } if (modify.InsertPattern != null) { g.Assert(root, SP.PropertyInsertPattern, modify.InsertPattern.ToSpinRdf(g, varTable)); } g.Assert(root, SP.PropertyWhere, modify.WherePattern.ToSpinRdf(g, varTable)); break; case SparqlUpdateCommandType.Move: g.Assert(root, RDF.PropertyType, SP.ClassMove); MoveCommand move = (MoveCommand)query; if (move.SourceUri == null) { g.Assert(root, SP.PropertyGraphIRI, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyGraphIRI, RDFUtil.CreateUriNode(move.SourceUri)); } if (move.DestinationUri == null) { g.Assert(root, SP.PropertyInto, SP.PropertyDefault); } else { g.Assert(root, SP.PropertyInto, RDFUtil.CreateUriNode(move.DestinationUri)); } break; case SparqlUpdateCommandType.Unknown: throw new NotSupportedException("Unkown SPARQL update query encountered " + query.ToString()); } return(root); }