Ejemplo n.º 1
0
        public List <IResource> AsList()
        {
            List <IResource> result   = new List <IResource>();
            INode            listRoot = this;
            Triple           step     = _model.GetTriplesWithSubjectPredicate(listRoot, RDF.PropertyFirst).FirstOrDefault();

            if (step != null)
            {
                while (step != null)
                {
                    if (!RDFUtil.sameTerm(RDF.Nil, step.Object))
                    {
                        result.Add(Resource.Get(step.Object, _model));
                    }
                    step = _model.GetTriplesWithSubjectPredicate(listRoot, RDF.PropertyRest).FirstOrDefault();
                    if (step != null)
                    {
                        if (RDFUtil.sameTerm(RDF.Nil, step.Object))
                        {
                            break;
                        }
                        listRoot = step.Object;
                        step     = _model.GetTriplesWithSubjectPredicate(listRoot, RDF.PropertyFirst).FirstOrDefault();
                    }
                }
            }
            else
            {
                result.Add(this);
            }
            return(result);
        }
Ejemplo n.º 2
0
        // TODO synchronise this
        internal static void ApplyChanges(this SpinWrappedDataset queryModel)
        {
            IGraph currentDataset = queryModel._configuration;

            if (!currentDataset.GetTriplesWithPredicateObject(RDF.PropertyType, RDFRuntime.ClassUpdateControlledDataset).Any())
            {
                return;
            }
            // TODO check whether transactions are supported by the storage provider
            // Loads the original dataset will will flush into
            IGraph targetDataset = new Graph();

            targetDataset.BaseUri = ((IUriNode)currentDataset.GetTriplesWithSubjectPredicate(queryModel._datasetNode, RDFRuntime.PropertyUpdatesDataset).First().Object).Uri;
            queryModel._storage.LoadGraph(targetDataset, targetDataset.BaseUri);

            if (currentDataset.GetTriplesWithPredicateObject(RDF.PropertyType, SPIN.ClassConstraintViolation).Any())
            {
                throw new SpinException("Unable to flush the dataset while constraints are not satisfied");
            }
            // Flush pending changes to the graphs
            // TODO also handle entailment graphs updates
            foreach (Triple t in currentDataset.GetTriplesWithPredicateObject(RDF.PropertyType, SD.ClassGraph).Union(currentDataset.GetTriplesWithPredicateObject(RDF.PropertyType, RDFRuntime.ClassEntailmentGraph)))
            {
                IUriNode targetGraph = (IUriNode)t.Subject;
                if (currentDataset.GetTriplesWithPredicateObject(RDFRuntime.PropertyRemovesGraph, targetGraph).Any())
                {
                    targetDataset.Retract(currentDataset.GetTriplesWithObject(t.Object).Union(currentDataset.GetTriplesWithSubject(targetGraph)));
                }
                else
                {
                    Triple ucgTriple = currentDataset.GetTriplesWithPredicateObject(RDFRuntime.PropertyUpdatesGraph, targetGraph)
                                       .Union(currentDataset.GetTriplesWithPredicateObject(RDFRuntime.PropertyReplacesGraph, targetGraph))
                                       .FirstOrDefault();
                    if (ucgTriple != null)
                    {
                        IUriNode updateControlledGraph = (IUriNode)ucgTriple.Subject;
                        if (RDFUtil.sameTerm(ucgTriple.Predicate, RDFRuntime.PropertyReplacesGraph))
                        {
                            queryModel._storage.Update("WITH <" + updateControlledGraph.Uri.ToString() + "> DELETE { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p } WHERE { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p }"); // For safety only
                            queryModel._storage.Update("MOVE GRAPH <" + updateControlledGraph.Uri.ToString() + "> TO <" + targetGraph.Uri.ToString() + ">");
                        }
                        else
                        {
                            queryModel._storage.Update("DELETE { GRAPH <" + updateControlledGraph.Uri.ToString() + "> { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p } . GRAPH <" + targetGraph.Uri.ToString() + "> { ?s ?p ?o } } USING <" + updateControlledGraph.Uri.ToString() + "> WHERE { ?s <" + RDFRuntime.PropertyResets.Uri.ToString() + "> ?p }");
                            queryModel._storage.Update("ADD GRAPH <" + updateControlledGraph.Uri.ToString() + "> TO <" + targetGraph.Uri.ToString() + ">");
                        }
                    }
                }
            }
            queryModel._storage.SaveGraph(targetDataset);
            queryModel.DisposeUpdateControlledDataset();
        }
Ejemplo n.º 3
0
        internal static IResource CreateUpdateControlledGraph(this SpinWrappedDataset queryModel, IResource graphNode, INode mode = null)
        {
            IGraph currentDataset = queryModel.CreateUpdateControlledDataset();

            if (RDFUtil.sameTerm(graphNode, queryModel._datasetNode))
            {
                return(graphNode);
            }

            INode updatedGraph = null;

            if (currentDataset.ContainsTriple(new Triple(graphNode, RDF.PropertyType, RDFRuntime.ClassReadOnlyGraph)))
            {
                throw new SpinException("The graph " + graphNode.Uri().ToString() + " is marked as Readonly for the current dataset");
            }
            if (!currentDataset.ContainsTriple(new Triple(graphNode.getSource(), RDF.PropertyType, SD.ClassGraph)))
            {
                currentDataset.Assert(graphNode.getSource(), Tools.CopyNode(RDF.PropertyType, currentDataset), Tools.CopyNode(SD.ClassGraph, currentDataset));
                currentDataset.Assert(graphNode.getSource(), Tools.CopyNode(RDFRuntime.PropertyUpdatesGraph, currentDataset), Tools.CopyNode(graphNode.getSource(), currentDataset));
            }
            updatedGraph = queryModel.GetUpdateControlledGraph(graphNode);
            if (updatedGraph == null)
            {
                if (mode == null)
                {
                    mode = RDFRuntime.PropertyUpdatesGraph;
                }
                currentDataset.Retract(currentDataset.GetTriplesWithObject(graphNode.getSource()).ToList());

                updatedGraph = currentDataset.CreateUriNode(UriFactory.Create(RDFRuntime.GRAPH_NS_URI + Guid.NewGuid().ToString()));
                currentDataset.Assert(updatedGraph, Tools.CopyNode(RDF.PropertyType, currentDataset), Tools.CopyNode(RDFRuntime.ClassUpdateControlGraph, currentDataset));
                currentDataset.Assert(updatedGraph, Tools.CopyNode(mode, currentDataset), Tools.CopyNode(graphNode.getSource(), currentDataset));
                // addition to simplify additional graph mapping constraints patterns
                currentDataset.Assert(updatedGraph, Tools.CopyNode(RDFRuntime.PropertyUpdatesGraph, currentDataset), updatedGraph);
            }
            else if (!RDFUtil.sameTerm(graphNode, updatedGraph))
            {
                updatedGraph = ((IResource)updatedGraph).getSource();
                currentDataset.Retract(graphNode.getSource(), Tools.CopyNode(RDFRuntime.PropertyUpdatesGraph, currentDataset), Tools.CopyNode(graphNode.getSource(), currentDataset));
                currentDataset.Assert(updatedGraph, Tools.CopyNode(mode, currentDataset), Tools.CopyNode(graphNode.getSource(), currentDataset));
            }
            return(Resource.Get(updatedGraph, queryModel.spinProcessor));
        }
Ejemplo n.º 4
0
        private IGraph GetModifiableGraph(Uri graphUri, INode modificationMode)
        {
            if (_modificableGraphs.ContainsKey(graphUri))
            {
                return(_modificableGraphs[graphUri]);
            }
            if (ContainsTriple(new Triple(RDFUtil.CreateUriNode(graphUri), RDF.PropertyType, RDFRuntime.ClassReadOnlyGraph)))
            {
                throw new SpinException("The graph " + graphUri.ToString() + " is marked as Readonly for the current dataset");
            }

            IUriNode sourceGraph        = RDFUtil.CreateUriNode(graphUri);
            IUriNode updateControlGraph = RDFUtil.CreateUriNode(GetUpdateControlUri(graphUri, true));

            this.Retract(RDFUtil.CreateUriNode(BaseUri), RDFRuntime.PropertyRemovesGraph, sourceGraph);
            this.Assert(sourceGraph, RDF.PropertyType, SD.ClassGraph);
            this.Assert(updateControlGraph, RDF.PropertyType, RDFRuntime.ClassUpdateControlGraph);

            if (!RDFUtil.sameTerm(sourceGraph, updateControlGraph))
            {
                this.Assert(updateControlGraph, modificationMode, sourceGraph);
                this.Assert(updateControlGraph, RDFRuntime.PropertyUpdatesGraph, updateControlGraph);
            }
            else
            {
                this.Assert(updateControlGraph, RDFRuntime.PropertyUpdatesGraph, sourceGraph);
            }

            SpinWrappedGraph graph = (SpinWrappedGraph)this[graphUri];

            graph.BaseUri  = graphUri;
            graph.Readonly = false;
            graph.Changed += OnModificableGraphChange;
            graph.Cleared += OnModificableGraphCleared;
            _modificableGraphs[graphUri] = graph;
            return(graph);
        }
Ejemplo n.º 5
0
        internal static string StringForNode(IResource node, INamespaceMapper pm)
        {
            SpinWrappedDataset model = null; // TODO change this for a queryModel
            StringBuilder      sb    = new StringBuilder();

            if (node.canAs(SP.ClassExpression))
            {
                ((IPrintable)SPINFactory.asExpression(node)).print(new BaseSparqlFactory(model, sb));
            }
            else if (node.canAs(SP.ClassVariable))
            {
                ((IPrintable)SPINFactory.asVariable(node)).print(new BaseSparqlFactory(model, sb));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyNot))
            {
                sb.Append("!(");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(")");
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyOr))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(" || ");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyAnd))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(" && ");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyEq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyNeq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("!=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyLt))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("<");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyLeq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append("<=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyGt))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(">");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyGeq))
            {
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(">=");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg2), pm));
            }
            else if (RDFUtil.sameTerm(node.getResource(RDF.PropertyType), SP.PropertyBound))
            {
                sb.Append("bound(");
                sb.Append(StringForNode(node.getObject(SP.PropertyArg1), pm));
                sb.Append(")");
            }
            else if (node.isUri())
            {
                sb.Append("<");
                sb.Append(node.Uri().ToString());
                sb.Append(">");
            }
            else if (node.isLiteral())
            {
                sb.Append(((ILiteralNode)node.getSource()).Value);
            }
            else
            {
                throw new Exception("Missing translation for expression " + node.getResource(RDF.PropertyType).Uri().ToString());
            }
            return(sb.ToString());
        }