Exemple #1
0
        /// <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);
        }
Exemple #2
0
        /**
         * Attempts to load a graph with a given URI.
         * In the default implementation, this uses the Jena
         * OntDocumentManager and default loading mechanisms.
         * Subclasses can override this.
         * @param uri  the base URI of the graph to load
         * @return the Graph or null to ignore this
         * @throws IOException
         */
        public virtual IGraph getImportedGraph(Uri uri, IRdfReader rdfReader = null)
        {
            if (_registeredImports.ContainsKey(uri))
            {
                return(_registeredImports[uri]);
            }
            IGraph importGraph = new ThreadSafeGraph();

            importGraph.BaseUri = uri;
            try
            {
                if (rdfReader == null)
                {
                    importGraph.LoadFromUri(uri);
                }
                else
                {
                    importGraph.LoadFromUri(uri, rdfReader);
                }
            }
            catch (Exception any)
            {
            }
            RegisterGraph(importGraph);
            return(importGraph);
        }
Exemple #3
0
        // TODO Replace IUpdate with a SparqlUpdateCommand
        private void UpdateInternal(IUpdate spinQuery, Uri outputGraphUri)
        {
            ISparqlPrinter sparqlFactory = new BaseSparqlPrinter(this);
            // TODO handle the current Execution Context while rewriting queries
            SparqlParameterizedString command = sparqlFactory.GetCommandText(spinQuery);

            command.SetUri("datasetUri", Configuration.BaseUri);
            command.SetUri("outputGraph", outputGraphUri);

            this.SaveConfiguration();
            if (!(spinQuery is IInsertData || spinQuery is IDeleteData))
            {
                CommandCalls.Add(command.ToString());
                Storage.Update(command.ToString());
                UpdateInternal(new DeleteDataImpl(RDF.Nil, spinProcessor), outputGraphUri);
                UpdateInternal(new InsertDataImpl(RDF.Nil, spinProcessor), outputGraphUri);
                ResetActiveGraph();
                return;
            }
            SaveInMemoryChanges();

            HashSet <Uri> dataGraphs = new HashSet <Uri>(RDFUtil.uriComparer);

            dataGraphs.UnionWith(spinQuery is IDeleteData ? Configuration.GetTriplesRemovalsGraphs() : Configuration.GetTriplesAdditionsGraphs());
            try
            {
                Storage.Update(command.ToString());
                _ignoreMonitoredChangeEvents = true;
                // TODO if needed : postpone this until full update execution is done to alleviate I/O
                foreach (IGraph synced in _synchronizedGraphs)
                {
                    Uri changesGraphUri = spinQuery is IDeleteData?Configuration.GetTripleRemovalsMonitorUri(synced.BaseUri) : Configuration.GetTripleAdditionsMonitorUri(synced.BaseUri);

                    IGraph changes = new ThreadSafeGraph();
                    Storage.LoadGraph(changes, changesGraphUri);
                    if (spinQuery is IDeleteData)
                    {
                        synced.Retract(changes.Triples);
                    }
                    else
                    {
                        synced.Assert(changes.Triples);
                    }
                }
                _ignoreMonitoredChangeEvents = false;
            }
            finally
            {
                foreach (Uri graphUri in dataGraphs)
                {
                    Storage.DeleteGraph(graphUri);
                }
            }
        }
Exemple #4
0
        private ITripleStore CreateTripleStore()
        {
            var metaGraphUri = ConfigurationSectionHandler.Default.Factories[DescriptionConfigurationSection.Default.DefaultStoreFactoryName].MetaGraphUri;
            var store        = new ThreadSafeTripleStore();
            var metaGraph    = new ThreadSafeGraph()
            {
                BaseUri = metaGraphUri
            };

            store.Add(metaGraph);
            return(store);
        }
Exemple #5
0
        internal static IGraph FindOrCreate(this ITripleStore tripleStore, Uri uri)
        {
            var graph = tripleStore.Graphs.FirstOrDefault(item => AbsoluteUriComparer.Default.Equals(item.BaseUri, uri));

            if (graph != null)
            {
                return(graph);
            }

            graph = new ThreadSafeGraph()
            {
                BaseUri = uri
            };
            tripleStore.Add(graph);
            return(graph);
        }
Exemple #6
0
        internal IGraph ApplyInference(IGraph g)
        {
            IGraph inferedTriples;

            if (_inferenceGraphs.ContainsKey(g))
            {
                inferedTriples = _inferenceGraphs[g];
                inferedTriples.Clear();
            }
            else
            {
                inferedTriples = new ThreadSafeGraph();
            }
            _reasoner.Apply(g, inferedTriples);
            _inferenceGraphs[g] = inferedTriples;
            return(inferedTriples);
        }
Exemple #7
0
        internal void Initialise()
        {
            IEnumerable <Uri> localGraphs = Storage.ListGraphs();

            foreach (IUriNode spinLibrary in Configuration.GetTriplesWithPredicateObject(RDF.PropertyType, SPIN.ClassLibraryOntology).Select(t => t.Subject))
            {
                // TODO maybe clean this to use SPINImports instead
                if (localGraphs.Contains(spinLibrary.Uri))
                {
                    IGraph library = new ThreadSafeGraph();
                    library.BaseUri = spinLibrary.Uri;
                    Storage.LoadGraph(library, spinLibrary.Uri);
                    spinProcessor.Initialise(library);
                }
                else
                {
                    spinProcessor.Initialise(spinLibrary.Uri);
                }
            }
            // TODO explore the dataset to add spin:import triples
            Configuration.Changed += OnDatasetDescriptionChanged;
        }
Exemple #8
0
        // 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);
        }
Exemple #9
0
        // 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);
        }