/// <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); }
// TODO pass the internal static IGraph RunConstructors(this SpinWrappedDataset dataset, List <Resource> usedClasses) { IGraph outputGraph = new ThreadSafeGraph(); if (usedClasses.Count > 0) { dataset.spinProcessor.SortClasses(usedClasses); SpinWrappedDataset.QueryMode currentExecutionMode = dataset.QueryExecutionMode; dataset.QueryExecutionMode = SpinWrappedDataset.QueryMode.SpinInferencing; foreach (Resource classResource in usedClasses) { IEnumerable <IUpdate> constructors = dataset.spinProcessor.GetConstructorsForClass(classResource); if (constructors.Count() > 0) { outputGraph.Assert(dataset.ExecuteUpdate(constructors).Triples); } } dataset.QueryExecutionMode = currentExecutionMode; } return(outputGraph); }
// TODO Replace IEnumerable<IUpdate> with a SparqlUpdateCommandSet /// <summary> /// TODO find a way to compile the global changes so ExecutionContexts can be set globally for Rules processing or Constraints checking. /// </summary> /// <param name="spinUpdateCommandSet"></param> internal IGraph ExecuteUpdate(IEnumerable <IUpdate> spinUpdateCommandSet) { QueryMode currentQueryMode = QueryExecutionMode; Uri currentExecutionGraphUri = RDFRuntime.NewTempGraphUri(); IGraph remoteChanges = new ThreadSafeGraph(); remoteChanges.BaseUri = currentExecutionGraphUri; try { foreach (IUpdate update in spinUpdateCommandSet) { if (update != null) { UpdateInternal(update, currentExecutionGraphUri); } } Storage.LoadGraph(remoteChanges, currentExecutionGraphUri); List <Resource> newTypes = new List <Resource>(); foreach (Triple t in remoteChanges.Triples) { if (RDFUtil.sameTerm(RDF.PropertyType, t.Predicate)) { // if remoteChanges contains an already checked rdf:type triple that means we have a infinite loop case in the SPIN processing pipeline so we stop execution if (_loopPreventionChecks.Contains(t)) { // TODO document better the exception cause throw new SpinException("Infinite loop encountered. Execution is canceled"); } _loopPreventionChecks.Add(t); newTypes.Add(AsResource(t.Object)); } else if (RDFUtil.sameTerm(RDFRuntime.PropertyHasChanged, t.Predicate)) { // mark the resource as updated for the next global CheckConstraints or Apply rules _changedResources.Add(t.Object); } else if (RDFUtil.sameTerm(RDFRuntime.PropertyResets, t.Predicate)) { // TODO log the property resets for filtering pattern extensions in the subsequent SPARQL execution } } CurrentExecutionContext = currentExecutionGraphUri; // run the constructors remoteChanges.Assert(this.RunConstructors(newTypes).Triples); } catch (Exception any) { // for cleanliness sake on exception cases foreach (Uri graphUri in Configuration.GetTriplesRemovalsGraphs().Union(Configuration.GetTriplesAdditionsGraphs())) { Storage.DeleteGraph(graphUri); } throw new Exception("", any); } finally { Storage.DeleteGraph(currentExecutionGraphUri); _queryExecutionMode = currentQueryMode; ResetExecutionContext(); } return(remoteChanges); }