Beispiel #1
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            try
            {
                if (context.Data.HasGraph(this._sourceUri))
                {
                    //Get the Source Graph
                    IGraph source = context.Data.GetModifiableGraph(this._sourceUri);

                    //Create/Delete/Clear the Destination Graph
                    IGraph dest;
                    if (context.Data.HasGraph(this._destUri))
                    {
                        if (this._destUri == null)
                        {
                            dest = context.Data.GetModifiableGraph(this._destUri);
                            dest.Clear();
                        }
                        else
                        {
                            context.Data.RemoveGraph(this._destUri);
                            dest = new Graph();
                            dest.BaseUri = this._destUri;
                            context.Data.AddGraph(dest);
                            dest = context.Data.GetModifiableGraph(this._destUri);
                        }
                    }
                    else
                    {
                        dest = new Graph();
                        dest.BaseUri = this._destUri;
                        context.Data.AddGraph(dest);
                    }

                    //Move data from the Source into the Destination
                    dest.Merge(source);
                }
                else
                {
                    //Only show error if not Silent
                    if (!this._silent)
                    {
                        if (this._sourceUri != null)
                        {
                            throw new SparqlUpdateException("Cannot COPY from Graph <" + this._sourceUri.ToString() + "> as it does not exist");
                        }
                        else
                        {
                            //This would imply a more fundamental issue with the Dataset not understanding that null means default graph
                            throw new SparqlUpdateException("Cannot COPY from the Default Graph as it does not exist");
                        }
                    }
                }
            }
            catch
            {
                //If not silent throw the exception upwards
                if (!this._silent) throw;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Executes a set of Update Commands against the Triple Store
        /// </summary>
        /// <param name="updates">SPARQL Update Command Set</param>
        public void ExecuteUpdate(SparqlUpdateCommandSet updates)
        {
            SparqlUpdateEvaluationContext context = new SparqlUpdateEvaluationContext(new InMemoryDataset(this));

            for (int i = 0; i < updates.CommandCount; i++)
            {
                updates[i].Evaluate(context);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            try
            {
                if (context.Data.HasGraph(this._sourceUri))
                {
                    //Get the Source Graph
                    IGraph source = context.Data.GetModifiableGraph(this._sourceUri);

                    //Get the Destination Graph
                    IGraph dest;
                    if (context.Data.HasGraph(this._destUri))
                    {
                        dest = context.Data.GetModifiableGraph(this._destUri);
                    }
                    else
                    {
                        dest         = new Graph();
                        dest.BaseUri = this._destUri;
                    }

                    //Move data from the Source into the Destination
                    dest.Merge(source);
                }
                else
                {
                    //Only show error if not Silent
                    if (!this._silent)
                    {
                        if (this._sourceUri != null)
                        {
                            throw new SparqlUpdateException("Cannot ADD from Graph <" + this._sourceUri.ToString() + "> as it does not exist");
                        }
                        else
                        {
                            //This would imply a more fundamental issue with the Dataset not understanding that null means default graph
                            throw new SparqlUpdateException("Cannot ADD from the Default Graph as it does not exist");
                        }
                    }
                }
            }
            catch
            {
                //If not silent throw the exception upwards
                if (!this._silent)
                {
                    throw;
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Evaluates the Command in the given Context
 /// </summary>
 /// <param name="context">Update Evaluation Context</param>
 public override void Evaluate(SparqlUpdateEvaluationContext context)
 {
     if (context.Data.HasGraph(this._graphUri))
     {
         if (!this._silent)
         {
             throw new SparqlUpdateException("Cannot create a Named Graph with URI '" + this._graphUri.ToString() + "' since a Graph with this URI already exists in the Store");
         }
     }
     else
     {
         Graph g = new Graph();
         g.BaseUri = this._graphUri;
         context.Data.AddGraph(g);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            //Q: Does LOAD into a named Graph require that Graph to be pre-existing?
            //if (this._graphUri != null)
            //{
            //    //When adding to specific Graph need to ensure that Graph exists
            //    //In the case when we're adding to the default graph we'll create it if it doesn't exist
            //    if (!context.Data.HasGraph(this._graphUri))
            //    {
            //        throw new RdfUpdateException("Cannot LOAD into a Graph that does not exist in the Store");
            //    }
            //}

            try
            {
                //Load from the URI
                Graph g = new Graph();
#if SILVERLIGHT
                throw new PlatformNotSupportedException("The SPARQL LOAD command is not currently supported under Silverlight/Windows Phone 7");
#else
                UriLoader.Load(g, this._sourceUri);
#endif

                if (context.Data.HasGraph(this._graphUri))
                {
                    //Merge the Data into the existing Graph
                    context.Data.GetModifiableGraph(this._graphUri).Merge(g);
                }
                else
                {
                    //Add New Graph to the Dataset
                    g.BaseUri = this._graphUri;
                    context.Data.AddGraph(g);
                }
            }
            catch
            {
                if (!this._silent)
                {
                    throw;
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Evaluates the Command in the given Context.
        /// </summary>
        /// <param name="context">Evaluation Context.</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            try
            {
                switch (_mode)
                {
                case ClearMode.Graph:
                case ClearMode.Default:
                    if (context.Data.HasGraph(_graphUri))
                    {
                        context.Data.GetModifiableGraph(_graphUri).Clear();
                    }
                    break;

                case ClearMode.Named:
                    foreach (Uri u in context.Data.GraphUris)
                    {
                        if (u != null)
                        {
                            context.Data.GetModifiableGraph(u).Clear();
                        }
                    }
                    break;

                case ClearMode.All:
                    foreach (Uri u in context.Data.GraphUris)
                    {
                        context.Data.GetModifiableGraph(u).Clear();
                    }
                    break;
                }
            }
            catch
            {
                if (!_silent)
                {
                    throw;
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            // Q: Does LOAD into a named Graph require that Graph to be pre-existing?
            // if (this._graphUri != null)
            // {
            //    //When adding to specific Graph need to ensure that Graph exists
            //    //In the case when we're adding to the default graph we'll create it if it doesn't exist
            //    if (!context.Data.HasGraph(this._graphUri))
            //    {
            //        throw new RdfUpdateException("Cannot LOAD into a Graph that does not exist in the Store");
            //    }
            // }

            try
            {
                // Load from the URI
                Graph g = new Graph();
                UriLoader.Load(g, _sourceUri);

                if (context.Data.HasGraph(_graphUri))
                {
                    // Merge the Data into the existing Graph
                    context.Data.GetModifiableGraph(_graphUri).Merge(g);
                }
                else
                {
                    // Add New Graph to the Dataset
                    g.BaseUri = _graphUri;
                    context.Data.AddGraph(g);
                }
            }
            catch
            {
                if (!_silent)
                {
                    throw;
                }
            }
        }
Beispiel #8
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            //Split the Pattern into the set of Graph Patterns
            List <GraphPattern> patterns = new List <GraphPattern>();

            if (this._pattern.IsGraph)
            {
                patterns.Add(this._pattern);
            }
            else if (this._pattern.TriplePatterns.Count > 0 || this._pattern.HasChildGraphPatterns)
            {
                if (this._pattern.TriplePatterns.Count > 0)
                {
                    patterns.Add(new GraphPattern());
                    this._pattern.TriplePatterns.ForEach(tp => patterns[0].AddTriplePattern(tp));
                }
                this._pattern.ChildGraphPatterns.ForEach(gp => patterns.Add(gp));
            }
            else
            {
                //If no Triple Patterns and No Child Graph Patterns nothing to do
                return;
            }

            ConstructContext constructContext = new ConstructContext(null, null, false);

            foreach (GraphPattern pattern in patterns)
            {
                if (!this.IsValidDataPattern(pattern, false))
                {
                    throw new SparqlUpdateException("Cannot evaluate a INSERT DATA command where any of the Triple Patterns are not concrete triples (variables are not permitted) or any of the GRAPH clauses have nested Graph Patterns");
                }

                //Get the Target Graph
                IGraph target;
                Uri    graphUri;
                if (pattern.IsGraph)
                {
                    switch (pattern.GraphSpecifier.TokenType)
                    {
                    case Token.QNAME:
                        throw new NotSupportedException("Graph Specifiers as QNames for INSERT DATA Commands are not supported - please specify an absolute URI instead");

                    case Token.URI:
                        graphUri = UriFactory.Create(pattern.GraphSpecifier.Value);
                        break;

                    default:
                        throw new SparqlUpdateException("Cannot evaluate an INSERT DATA Command as the Graph Specifier is not a QName/URI");
                    }
                }
                else
                {
                    graphUri = null;
                }
                if (context.Data.HasGraph(graphUri))
                {
                    target = context.Data.GetModifiableGraph(graphUri);
                }
                else
                {
                    //If the Graph does not exist then it must be created
                    target         = new Graph();
                    target.BaseUri = graphUri;
                    context.Data.AddGraph(target);
                }

                //Insert the actual Triples
                foreach (IConstructTriplePattern p in pattern.TriplePatterns.OfType <IConstructTriplePattern>())
                {
                    INode subj = p.Subject.Construct(constructContext);
                    INode pred = p.Predicate.Construct(constructContext);
                    INode obj  = p.Object.Construct(constructContext);

                    target.Assert(new Triple(subj, pred, obj, graphUri));
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Executes a single Update Command against the Triple Store
        /// </summary>
        /// <param name="update">SPARQL Update Command</param>
        public void ExecuteUpdate(SparqlUpdateCommand update)
        {
            SparqlUpdateEvaluationContext context = new SparqlUpdateEvaluationContext(new InMemoryDataset(this));

            update.Evaluate(context);
        }
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            bool datasetOk = false;
            bool defGraphOk = false;

            try
            {
                //First evaluate the WHERE pattern to get the affected bindings
                ISparqlAlgebra where = this._wherePattern.ToAlgebra();

                //Set Active Graph for the WHERE
                //Don't bother if there are USING URIs as these would override any Active Graph we set here
                //so we can save ourselves the effort of doing this
                if (!this.UsingUris.Any())
                {
                    if (this._graphUri != null)
                    {
                        context.Data.SetActiveGraph(this._graphUri);
                        defGraphOk = true;
                    }
                    else
                    {
                        context.Data.SetActiveGraph((Uri)null);
                        defGraphOk = true;
                    }
                }

                //We need to make a dummy SparqlQuery object since if the Command has used any 
                //USING NAMEDs along with GRAPH clauses then the algebra needs to have the
                //URIs available to it which it gets from the Query property of the Context
                //object
                SparqlQuery query = new SparqlQuery();
                foreach (Uri u in this.UsingNamedUris)
                {
                    query.AddNamedGraph(u);
                }
                SparqlEvaluationContext queryContext = new SparqlEvaluationContext(query, context.Data);
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs set the Active Graph to be formed of the Graphs with those URIs
                    context.Data.SetActiveGraph(this._usingUris);
                    datasetOk = true;
                }
                BaseMultiset results = where.Evaluate(queryContext);
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs reset the Active Graph afterwards
                    //Also flag the dataset as no longer being OK as this flag is used in the finally 
                    //block to determine whether the Active Graph needs resetting which it may do if the
                    //evaluation of the 
                    context.Data.ResetActiveGraph();
                    datasetOk = false;
                }

                //Reset Active Graph for the WHERE
                if (defGraphOk)
                {
                    context.Data.ResetActiveGraph();
                    defGraphOk = false;
                }

                //TODO: Need to detect when we create a Graph for Insertion but then fail to insert anything since in this case the Inserted Graph should be removed

                //Get the Graph to which we are inserting
                //HashSet<Uri> insertedGraphs = new HashSet<Uri>();
                IGraph g;
                if (context.Data.HasGraph(this._graphUri))
                {
                    g = context.Data.GetModifiableGraph(this._graphUri);
                }
                else
                {
                    //insertedGraphs.Add(this._graphUri);
                    g = new Graph();
                    g.BaseUri = this._graphUri;
                    context.Data.AddGraph(g);
                    g = context.Data.GetModifiableGraph(this._graphUri);
                }

                //Insert the Triples for each Solution
                foreach (Set s in queryContext.OutputMultiset.Sets)
                {
                    List<Triple> insertedTriples = new List<Triple>();

                    //Triples from raw Triple Patterns
                    try
                    {
                        ConstructContext constructContext = new ConstructContext(g, s, true);
                        foreach (IConstructTriplePattern p in this._insertPattern.TriplePatterns.OfType<IConstructTriplePattern>())
                        {
                            try
                            {
                                insertedTriples.Add(p.Construct(constructContext));
                            }
                            catch (RdfQueryException)
                            {
                                //If we throw an error this means we couldn't construct a specific Triple
                                //so we continue anyway
                            }
                        }
                        g.Assert(insertedTriples);
                    }
                    catch (RdfQueryException)
                    {
                        //If we throw an error this means we couldn't construct for this solution so the
                        //solution is ignored for this graph
                    }

                    //Triples from GRAPH clauses
                    foreach (GraphPattern gp in this._insertPattern.ChildGraphPatterns)
                    {
                        insertedTriples.Clear();
                        try
                        {
                            String graphUri;
                            switch (gp.GraphSpecifier.TokenType)
                            {
                                case Token.URI:
                                    graphUri = gp.GraphSpecifier.Value;
                                    break;
                                case Token.VARIABLE:
                                    if (s.ContainsVariable(gp.GraphSpecifier.Value))
                                    {
                                        INode temp = s[gp.GraphSpecifier.Value.Substring(1)];
                                        if (temp == null)
                                        {
                                            //If the Variable is not bound then skip
                                            continue;
                                        }
                                        else if (temp.NodeType == NodeType.Uri)
                                        {
                                            graphUri = temp.ToSafeString();
                                        }
                                        else
                                        {
                                            //If the Variable is not bound to a URI then skip
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        //If the Variable is not bound for this solution then skip
                                        continue;
                                    }
                                    break;
                                default:
                                    //Any other Graph Specifier we have to ignore this solution
                                    continue;
                            }

                            //Ensure the Graph we're inserting to exists in the dataset creating it if necessary
                            IGraph h;
                            Uri destUri = new Uri(graphUri);
                            if (context.Data.HasGraph(destUri))
                            {
                                h = context.Data.GetModifiableGraph(destUri);
                            }
                            else
                            {
                                //insertedGraphs.Add(destUri);
                                h = new Graph();
                                h.BaseUri = destUri;
                                context.Data.AddGraph(h);
                                h = context.Data.GetModifiableGraph(destUri);
                            }

                            //Do the actual Insertions
                            ConstructContext constructContext = new ConstructContext(h, s, true);
                            foreach (IConstructTriplePattern p in gp.TriplePatterns.OfType<IConstructTriplePattern>())
                            {
                                try
                                {
                                    insertedTriples.Add(p.Construct(constructContext));
                                }
                                catch (RdfQueryException)
                                {
                                    //If we throw an error this means we couldn't construct a specific Triple
                                    //so we continue anyway
                                }
                            }
                            h.Assert(insertedTriples);
                        }
                        catch (RdfQueryException)
                        {
                            //If we throw an error this means we couldn't construct for this solution so the
                            //solution is ignored for this Graph
                        }
                    }
                }
            }
            finally
            {
                //If the Dataset was set and an error occurred in doing the WHERE clause then
                //we'll need to Reset the Active Graph
                if (datasetOk) context.Data.ResetActiveGraph();
                if (defGraphOk) context.Data.ResetActiveGraph();
            }
        }
Beispiel #11
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            //Q: Does LOAD into a named Graph require that Graph to be pre-existing?
            //if (this._graphUri != null)
            //{
            //    //When adding to specific Graph need to ensure that Graph exists
            //    //In the case when we're adding to the default graph we'll create it if it doesn't exist
            //    if (!context.Data.HasGraph(this._graphUri))
            //    {
            //        throw new RdfUpdateException("Cannot LOAD into a Graph that does not exist in the Store");
            //    }
            //}

            try
            {
                //Load from the URI
                Graph g = new Graph();
                UriLoader.Load(g, this._sourceUri);

                if (context.Data.HasGraph(this._graphUri))
                {
                    //Merge the Data into the existing Graph
                    context.Data.GetModifiableGraph(this._graphUri).Merge(g);
                }
                else
                {
                    //Add New Graph to the Dataset
                    g.BaseUri = this._graphUri;
                    context.Data.AddGraph(g);
                }
            }
            catch
            {
                if (!this._silent) throw;
            }
        }
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            bool datasetOk  = false;
            bool defGraphOk = false;

            try
            {
                //First evaluate the WHERE pattern to get the affected bindings
                ISparqlAlgebra where = this._wherePattern.ToAlgebra();
                if (context.Commands != null)
                {
                    where = context.Commands.ApplyAlgebraOptimisers(where);
                }

                //Set Active Graph for the WHERE
                //Don't bother if there are USING URIs as these would override any Active Graph we set here
                //so we can save ourselves the effort of doing this
                if (!this.UsingUris.Any())
                {
                    if (this._graphUri != null)
                    {
                        context.Data.SetActiveGraph(this._graphUri);
                        defGraphOk = true;
                    }
                    else
                    {
                        context.Data.SetActiveGraph((Uri)null);
                        defGraphOk = true;
                    }
                }

                //We need to make a dummy SparqlQuery object since if the Command has used any
                //USING NAMEDs along with GRAPH clauses then the algebra needs to have the
                //URIs available to it which it gets from the Query property of the Context
                //object
                SparqlQuery query = new SparqlQuery();
                foreach (Uri u in this.UsingUris)
                {
                    query.AddDefaultGraph(u);
                }
                foreach (Uri u in this.UsingNamedUris)
                {
                    query.AddNamedGraph(u);
                }
                SparqlEvaluationContext queryContext = new SparqlEvaluationContext(query, context.Data, context.QueryProcessor);
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs set the Active Graph to be formed of the Graphs with those URIs
                    context.Data.SetActiveGraph(this._usingUris);
                    datasetOk = true;
                }
                BaseMultiset results = queryContext.Evaluate(where);
                if (results is IdentityMultiset)
                {
                    queryContext.OutputMultiset = new SingletonMultiset(results.Variables);
                }
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs reset the Active Graph afterwards
                    //Also flag the dataset as no longer being OK as this flag is used in the finally
                    //block to determine whether the Active Graph needs resetting which it may do if the
                    //evaluation of the query fails for any reason
                    context.Data.ResetActiveGraph();
                    datasetOk = false;
                }

                //Reset Active Graph for the WHERE
                if (defGraphOk)
                {
                    context.Data.ResetActiveGraph();
                    defGraphOk = false;
                }

                //TODO: Need to detect when we create a Graph for Insertion but then fail to insert anything since in this case the Inserted Graph should be removed

                //Get the Graph to which we are inserting Triples with no explicit Graph clause
                IGraph g = null;
                if (this._insertPattern.TriplePatterns.Count > 0)
                {
                    if (context.Data.HasGraph(this._graphUri))
                    {
                        g = context.Data.GetModifiableGraph(this._graphUri);
                    }
                    else
                    {
                        //insertedGraphs.Add(this._graphUri);
                        g         = new Graph();
                        g.BaseUri = this._graphUri;
                        context.Data.AddGraph(g);
                        g = context.Data.GetModifiableGraph(this._graphUri);
                    }
                }

                //Keep a record of graphs to which we insert
                MultiDictionary <Uri, IGraph> graphs = new MultiDictionary <Uri, IGraph>(u => (u != null ? u.GetEnhancedHashCode() : 0), true, new UriComparer(), MultiDictionaryMode.AVL);

                //Insert the Triples for each Solution
                foreach (ISet s in queryContext.OutputMultiset.Sets)
                {
                    List <Triple> insertedTriples = new List <Triple>();

                    try
                    {
                        //Create a new Construct Context for each Solution
                        ConstructContext constructContext = new ConstructContext(null, s, true);

                        //Triples from raw Triple Patterns
                        if (this._insertPattern.TriplePatterns.Count > 0)
                        {
                            foreach (IConstructTriplePattern p in this._insertPattern.TriplePatterns.OfType <IConstructTriplePattern>())
                            {
                                try
                                {
                                    insertedTriples.Add(p.Construct(constructContext));
                                }
                                catch (RdfQueryException)
                                {
                                    //If we throw an error this means we couldn't construct a specific Triple
                                    //so we continue anyway
                                }
                            }
                            g.Assert(insertedTriples);
                        }

                        //Triples from GRAPH clauses
                        foreach (GraphPattern gp in this._insertPattern.ChildGraphPatterns)
                        {
                            insertedTriples.Clear();
                            try
                            {
                                String graphUri;
                                switch (gp.GraphSpecifier.TokenType)
                                {
                                case Token.URI:
                                    graphUri = gp.GraphSpecifier.Value;
                                    break;

                                case Token.VARIABLE:
                                    String graphVar = gp.GraphSpecifier.Value.Substring(1);
                                    if (s.ContainsVariable(graphVar))
                                    {
                                        INode temp = s[graphVar];
                                        if (temp == null)
                                        {
                                            //If the Variable is not bound then skip
                                            continue;
                                        }
                                        else if (temp.NodeType == NodeType.Uri)
                                        {
                                            graphUri = temp.ToSafeString();
                                        }
                                        else
                                        {
                                            //If the Variable is not bound to a URI then skip
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        //If the Variable is not bound for this solution then skip
                                        continue;
                                    }
                                    break;

                                default:
                                    //Any other Graph Specifier we have to ignore this solution
                                    continue;
                                }

                                //Ensure the Graph we're inserting to exists in the dataset creating it if necessary
                                IGraph h;
                                Uri    destUri = UriFactory.Create(graphUri);
                                if (graphs.ContainsKey(destUri))
                                {
                                    h = graphs[destUri];
                                }
                                else
                                {
                                    if (context.Data.HasGraph(destUri))
                                    {
                                        h = context.Data.GetModifiableGraph(destUri);
                                    }
                                    else
                                    {
                                        //insertedGraphs.Add(destUri);
                                        h         = new Graph();
                                        h.BaseUri = destUri;
                                        context.Data.AddGraph(h);
                                        h = context.Data.GetModifiableGraph(destUri);
                                    }
                                    graphs.Add(destUri, h);
                                }

                                //Do the actual Insertions
                                foreach (IConstructTriplePattern p in gp.TriplePatterns.OfType <IConstructTriplePattern>())
                                {
                                    try
                                    {
                                        insertedTriples.Add(p.Construct(constructContext));
                                    }
                                    catch (RdfQueryException)
                                    {
                                        //If we throw an error this means we couldn't construct a specific Triple
                                        //so we continue anyway
                                    }
                                }
                                h.Assert(insertedTriples);
                            }
                            catch (RdfQueryException)
                            {
                                //If we throw an error this means we couldn't construct for this solution so the
                                //solution is ignored for this Graph
                            }
                        }
                    }
                    catch (RdfQueryException)
                    {
                        //If we throw an error this means we couldn't construct for this solution so the
                        //solution is ignored for this graph
                    }
                }
            }
            finally
            {
                //If the Dataset was set and an error occurred in doing the WHERE clause then
                //we'll need to Reset the Active Graph
                if (datasetOk)
                {
                    context.Data.ResetActiveGraph();
                }
                if (defGraphOk)
                {
                    context.Data.ResetActiveGraph();
                }
            }
        }
Beispiel #13
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            try
            {
                switch (_mode)
                {
                case ClearMode.Default:
                case ClearMode.Graph:
                    if (!context.Data.HasGraph(_graphUri))
                    {
                        if (!_silent)
                        {
                            throw new SparqlUpdateException("Cannot remove a Named Graph with URI '" + _graphUri.AbsoluteUri + "' since a Graph with this URI does not exist in the Store");
                        }
                    }
                    else
                    {
                        if (_mode == ClearMode.Graph)
                        {
                            context.Data.RemoveGraph(_graphUri);
                        }
                        else
                        {
                            // DROPing the DEFAULT graph only results in clearing it
                            // This is because removing the default graph may cause errors in later commands/queries
                            // which rely on it existing
                            context.Data.GetModifiableGraph(_graphUri).Clear();
                        }
                    }
                    break;

                case ClearMode.Named:
                    foreach (Uri u in context.Data.GraphUris.ToList())
                    {
                        if (u != null)
                        {
                            context.Data.RemoveGraph(u);
                        }
                    }
                    break;

                case ClearMode.All:
                    foreach (Uri u in context.Data.GraphUris.ToList())
                    {
                        if (u != null)
                        {
                            context.Data.RemoveGraph(u);
                        }
                        else
                        {
                            // DROPing the DEFAULT graph only results in clearing it
                            // This is because removing the default graph may cause errors in later commands/queries
                            // which rely on it existing
                            context.Data.GetModifiableGraph(u).Clear();
                        }
                    }
                    break;
                }
            }
            catch
            {
                if (!_silent)
                {
                    throw;
                }
            }
        }
Beispiel #14
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            try
            {
                switch (this._mode)
                {
                    case ClearMode.Default:
                    case ClearMode.Graph:
                        if (!context.Data.HasGraph(this._graphUri))
                        {
                            if (!this._silent) throw new SparqlUpdateException("Cannot remove a Named Graph with URI '" + this._graphUri.ToString() + "' since a Graph with this URI does not exist in the Store");
                        }
                        else
                        {
                            if (this._mode == ClearMode.Graph)
                            {
                                context.Data.RemoveGraph(this._graphUri);
                            }
                            else
                            {
                                //DROPing the DEFAULT graph only results in clearing it
                                //This is because removing the default graph may cause errors in later commands/queries
                                //which rely on it existing
                                context.Data.GetModifiableGraph(this._graphUri).Clear();
                            }
                        }
                        break;

                    case ClearMode.Named:
                        foreach (Uri u in context.Data.GraphUris.ToList())
                        {
                            if (u != null)
                            {
                                context.Data.RemoveGraph(u);
                            }
                        }
                        break;
                    case ClearMode.All:
                        foreach (Uri u in context.Data.GraphUris.ToList())
                        {
                            if (u != null)
                            {
                                context.Data.RemoveGraph(u);
                            }
                            else
                            {
                                //DROPing the DEFAULT graph only results in clearing it
                                //This is because removing the default graph may cause errors in later commands/queries
                                //which rely on it existing
                                context.Data.GetModifiableGraph(u).Clear();
                            }
                        }
                        break;
                }
            }
            catch
            {
                if (!this._silent) throw;
            }
        }
Beispiel #15
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            bool defGraphOk = false;
            bool datasetOk  = false;

            try
            {
                //First evaluate the WHERE pattern to get the affected bindings
                ISparqlAlgebra where = this._wherePattern.ToAlgebra();
                if (context.Commands != null)
                {
                    where = context.Commands.ApplyAlgebraOptimisers(where);
                }

                //Set Active Graph for the WHERE based upon the WITH clause
                //Don't bother if there are USING URIs as these would override any Active Graph we set here
                //so we can save ourselves the effort of doing this
                if (!this.UsingUris.Any())
                {
                    if (this._graphUri != null)
                    {
                        context.Data.SetActiveGraph(this._graphUri);
                        defGraphOk = true;
                    }
                    else
                    {
                        context.Data.SetActiveGraph((Uri)null);
                        defGraphOk = true;
                    }
                }

                //We need to make a dummy SparqlQuery object since if the Command has used any
                //USING/USING NAMEDs along with GRAPH clauses then the algebra needs to have the
                //URIs available to it which it gets from the Query property of the Context
                //object
                SparqlQuery query = new SparqlQuery();
                foreach (Uri u in this.UsingUris)
                {
                    query.AddDefaultGraph(u);
                }
                foreach (Uri u in this.UsingNamedUris)
                {
                    query.AddNamedGraph(u);
                }
                SparqlEvaluationContext queryContext = new SparqlEvaluationContext(query, context.Data, context.QueryProcessor);
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs set the Active Graph to be formed of the Graphs with those URIs
                    context.Data.SetActiveGraph(this._usingUris);
                    datasetOk = true;
                }
                BaseMultiset results = queryContext.Evaluate(where);
                if (results is IdentityMultiset)
                {
                    results = new SingletonMultiset(results.Variables);
                }
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs reset the Active Graph afterwards
                    //Also flag the dataset as no longer being OK as this flag is used in the finally
                    //block to determine whether the Active Graph needs resetting which it may do if the
                    //evaluation of the
                    context.Data.ResetActiveGraph();
                    datasetOk = false;
                }

                //Reset Active Graph for the WHERE
                if (defGraphOk)
                {
                    context.Data.ResetActiveGraph();
                    defGraphOk = false;
                }

                //Get the Graph from which we are deleting
                IGraph g = context.Data.GetModifiableGraph(this._graphUri);

                //Delete the Triples for each Solution
                foreach (ISet s in results.Sets)
                {
                    List <Triple> deletedTriples = new List <Triple>();

                    //Triples from raw Triple Patterns
                    try
                    {
                        ConstructContext constructContext = new ConstructContext(g, s, true);
                        foreach (IConstructTriplePattern p in this._deletePattern.TriplePatterns.OfType <IConstructTriplePattern>())
                        {
                            try
                            {
                                deletedTriples.Add(p.Construct(constructContext));
                            }
                            catch (RdfQueryException)
                            {
                                //If we get an error here then we couldn't construct a specific Triple
                                //so we continue anyway
                            }
                        }
                        g.Retract(deletedTriples);
                    }
                    catch (RdfQueryException)
                    {
                        //If we throw an error this means we couldn't construct for this solution so the
                        //solution is ignored this graph
                    }

                    //Triples from GRAPH clauses
                    foreach (GraphPattern gp in this._deletePattern.ChildGraphPatterns)
                    {
                        deletedTriples.Clear();
                        try
                        {
                            String graphUri;
                            switch (gp.GraphSpecifier.TokenType)
                            {
                            case Token.URI:
                                graphUri = gp.GraphSpecifier.Value;
                                break;

                            case Token.VARIABLE:
                                String graphVar = gp.GraphSpecifier.Value.Substring(1);
                                if (s.ContainsVariable(graphVar))
                                {
                                    INode temp = s[graphVar];
                                    if (temp == null)
                                    {
                                        //If the Variable is not bound then skip
                                        continue;
                                    }
                                    else if (temp.NodeType == NodeType.Uri)
                                    {
                                        graphUri = temp.ToSafeString();
                                    }
                                    else
                                    {
                                        //If the Variable is not bound to a URI then skip
                                        continue;
                                    }
                                }
                                else
                                {
                                    //If the Variable is not bound for this solution then skip
                                    continue;
                                }
                                break;

                            default:
                                //Any other Graph Specifier we have to ignore this solution
                                continue;
                            }

                            //If the Dataset doesn't contain the Graph then no need to do the Deletions
                            if (!context.Data.HasGraph(UriFactory.Create(graphUri)))
                            {
                                continue;
                            }

                            //Do the actual Deletions
                            IGraph           h = context.Data.GetModifiableGraph(UriFactory.Create(graphUri));
                            ConstructContext constructContext = new ConstructContext(h, s, true);
                            foreach (IConstructTriplePattern p in gp.TriplePatterns.OfType <IConstructTriplePattern>())
                            {
                                try
                                {
                                    deletedTriples.Add(p.Construct(constructContext));
                                }
                                catch (RdfQueryException)
                                {
                                    //If we get an error here then we couldn't construct a specific
                                    //triple so we continue anyway
                                }
                            }
                            h.Retract(deletedTriples);
                        }
                        catch (RdfQueryException)
                        {
                            //If we get an error here this means we couldn't construct for this solution so the
                            //solution is ignored for this graph
                        }
                    }
                }
            }
            finally
            {
                //If the Dataset was set and an error occurred in doing the WHERE clause then
                //we'll need to Reset the Active Graph
                if (datasetOk)
                {
                    context.Data.ResetActiveGraph();
                }
                if (defGraphOk)
                {
                    context.Data.ResetActiveGraph();
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// Evaluates the Command in the given Context.
        /// </summary>
        /// <param name="context">Evaluation Context.</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            try
            {
                if (context.Data.HasGraph(_sourceUri))
                {
                    // If Source and Destination are same this is a no-op
                    if (EqualityHelper.AreUrisEqual(_sourceUri, _destUri))
                    {
                        return;
                    }

                    // Get the Source Graph
                    IGraph source = context.Data.GetModifiableGraph(_sourceUri);

                    // Create/Delete/Clear the Destination Graph
                    IGraph dest;
                    if (context.Data.HasGraph(_destUri))
                    {
                        if (_destUri == null)
                        {
                            dest = context.Data.GetModifiableGraph(_destUri);
                            dest.Clear();
                        }
                        else
                        {
                            context.Data.RemoveGraph(_destUri);
                            dest         = new Graph();
                            dest.BaseUri = _destUri;
                            context.Data.AddGraph(dest);
                            dest = context.Data.GetModifiableGraph(_destUri);
                        }
                    }
                    else
                    {
                        dest         = new Graph();
                        dest.BaseUri = _destUri;
                        context.Data.AddGraph(dest);
                    }

                    // Move data from the Source into the Destination
                    dest.Merge(source);
                }
                else
                {
                    // Only show error if not Silent
                    if (!_silent)
                    {
                        if (_sourceUri != null)
                        {
                            throw new SparqlUpdateException("Cannot COPY from Graph <" + _sourceUri.AbsoluteUri + "> as it does not exist");
                        }
                        else
                        {
                            // This would imply a more fundamental issue with the Dataset not understanding that null means default graph
                            throw new SparqlUpdateException("Cannot COPY from the Default Graph as it does not exist");
                        }
                    }
                }
            }
            catch
            {
                // If not silent throw the exception upwards
                if (!_silent)
                {
                    throw;
                }
            }
        }
 public override void Evaluate(SparqlUpdateEvaluationContext context)
 {
     throw new NotImplementedException();
 }
Beispiel #18
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            //Split the Pattern into the set of Graph Patterns
            List<GraphPattern> patterns = new List<GraphPattern>();
            if (this._pattern.IsGraph)
            {
                patterns.Add(this._pattern);
            }
            else if (this._pattern.TriplePatterns.Count > 0 || this._pattern.HasChildGraphPatterns)
            {
                if (this._pattern.TriplePatterns.Count > 0)
                {
                    patterns.Add(new GraphPattern());
                    this._pattern.TriplePatterns.ForEach(tp => patterns[0].AddTriplePattern(tp));
                }
                this._pattern.ChildGraphPatterns.ForEach(gp => patterns.Add(gp));
            }
            else
            {
                //If no Triple Patterns and No Child Graph Patterns nothing to do
                return;
            }

            foreach (GraphPattern pattern in patterns)
            {
                if (!this.IsValidDataPattern(pattern, false)) throw new SparqlUpdateException("Cannot evaluate a INSERT DATA command where any of the Triple Patterns are not concrete triples (variables are not permitted) or any of the GRAPH clauses have nested Graph Patterns");

                //Get the Target Graph
                IGraph target;
                Uri graphUri;
                if (pattern.IsGraph)
                {
                    switch (pattern.GraphSpecifier.TokenType)
                    {
                        case Token.QNAME:
                            throw new NotSupportedException("Graph Specifiers as QNames for INSERT DATA Commands are not supported - please specify an absolute URI instead");
                        case Token.URI:
                            graphUri = UriFactory.Create(pattern.GraphSpecifier.Value);
                            break;
                        default:
                            throw new SparqlUpdateException("Cannot evaluate an INSERT DATA Command as the Graph Specifier is not a QName/URI");
                    }
                }
                else
                {
                    graphUri = null;
                }
                if (context.Data.HasGraph(graphUri))
                {
                    target = context.Data.GetModifiableGraph(graphUri);
                }
                else
                {
                    //If the Graph does not exist then it must be created
                    target = new Graph();
                    target.BaseUri = graphUri;
                    context.Data.AddGraph(target);
                }

                //Insert the actual Triples
                INode subj, pred, obj;
                ConstructContext constructContext = new ConstructContext(target, null, true);
                foreach (IConstructTriplePattern p in pattern.TriplePatterns.OfType<IConstructTriplePattern>())
                {
                    subj = p.Subject.Construct(constructContext);
                    pred = p.Predicate.Construct(constructContext);
                    obj = p.Object.Construct(constructContext);

                    target.Assert(new Triple(subj, pred, obj));
                }
            }
        }
Beispiel #19
0
 /// <summary>
 /// Evaluates the Command in the given Context
 /// </summary>
 /// <param name="context">Evaluation Context</param>
 public override void Evaluate(SparqlUpdateEvaluationContext context)
 {
     try
     {
         switch (this._mode)
         {
             case ClearMode.Graph:
             case ClearMode.Default:
                 if (context.Data.HasGraph(this._graphUri))
                 {
                     context.Data.GetModifiableGraph(this._graphUri).Clear();
                 }
                 break;
             case ClearMode.Named:
                 foreach (Uri u in context.Data.GraphUris)
                 {
                     if (u != null)
                     {
                         context.Data.GetModifiableGraph(u).Clear();
                     }
                 }
                 break;
             case ClearMode.All:
                 foreach (Uri u in context.Data.GraphUris)
                 {
                         context.Data.GetModifiableGraph(u).Clear();
                 }
                 break;
         }
     }
     catch
     {
         if (!this._silent) throw;
     }
 }
Beispiel #20
0
        /// <summary>
        /// Evaluates the Command in the given Context
        /// </summary>
        /// <param name="context">Evaluation Context</param>
        public override void Evaluate(SparqlUpdateEvaluationContext context)
        {
            bool defGraphOk = false;
            bool datasetOk = false;

            try
            {
                //First evaluate the WHERE pattern to get the affected bindings
                ISparqlAlgebra where = this._wherePattern.ToAlgebra();
                if (context.Commands != null)
                {
                    where = context.Commands.ApplyAlgebraOptimisers(where);
                }

                //Set Active Graph for the WHERE based upon the WITH clause
                //Don't bother if there are USING URIs as these would override any Active Graph we set here
                //so we can save ourselves the effort of doing this
                if (!this.UsingUris.Any())
                {
                    if (this._graphUri != null)
                    {
                        context.Data.SetActiveGraph(this._graphUri);
                        defGraphOk = true;
                    }
                    else
                    {
                        context.Data.SetActiveGraph((Uri)null);
                        defGraphOk = true;
                    }
                }

                //We need to make a dummy SparqlQuery object since if the Command has used any
                //USING/USING NAMEDs along with GRAPH clauses then the algebra needs to have the
                //URIs available to it which it gets from the Query property of the Context
                //object
                SparqlQuery query = new SparqlQuery();
                foreach (Uri u in this.UsingUris)
                {
                    query.AddDefaultGraph(u);
                }
                foreach (Uri u in this.UsingNamedUris)
                {
                    query.AddNamedGraph(u);
                }
                SparqlEvaluationContext queryContext = new SparqlEvaluationContext(query, context.Data, context.QueryProcessor);
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs set the Active Graph to be formed of the Graphs with those URIs
                    context.Data.SetActiveGraph(this._usingUris);
                    datasetOk = true;
                }
                BaseMultiset results = queryContext.Evaluate(where);
                if (this.UsingUris.Any())
                {
                    //If there are USING URIs reset the Active Graph afterwards
                    //Also flag the dataset as no longer being OK as this flag is used in the finally
                    //block to determine whether the Active Graph needs resetting which it may do if the
                    //evaluation of the
                    context.Data.ResetActiveGraph();
                    datasetOk = false;
                }

                //Reset Active Graph for the WHERE
                if (defGraphOk)
                {
                    context.Data.ResetActiveGraph();
                    defGraphOk = false;
                }

                //Get the Graph from which we are deleting
                IGraph g = context.Data.GetModifiableGraph(this._graphUri);

                //Delete the Triples for each Solution
                foreach (ISet s in queryContext.OutputMultiset.Sets)
                {
                    List<Triple> deletedTriples = new List<Triple>();

                    //Triples from raw Triple Patterns
                    try
                    {
                        ConstructContext constructContext = new ConstructContext(g, s, true);
                        foreach (IConstructTriplePattern p in this._deletePattern.TriplePatterns.OfType<IConstructTriplePattern>())
                        {
                            try
                            {
                                deletedTriples.Add(p.Construct(constructContext));
                            }
                            catch (RdfQueryException)
                            {
                                //If we get an error here then we couldn't construct a specific Triple
                                //so we continue anyway
                            }
                        }
                        g.Retract(deletedTriples);
                    }
                    catch (RdfQueryException)
                    {
                        //If we throw an error this means we couldn't construct for this solution so the
                        //solution is ignored this graph
                    }

                    //Triples from GRAPH clauses
                    foreach (GraphPattern gp in this._deletePattern.ChildGraphPatterns)
                    {
                        deletedTriples.Clear();
                        try
                        {
                            String graphUri;
                            switch (gp.GraphSpecifier.TokenType)
                            {
                                case Token.URI:
                                    graphUri = gp.GraphSpecifier.Value;
                                    break;
                                case Token.VARIABLE:
                                    if (s.ContainsVariable(gp.GraphSpecifier.Value))
                                    {
                                        INode temp = s[gp.GraphSpecifier.Value.Substring(1)];
                                        if (temp == null)
                                        {
                                            //If the Variable is not bound then skip
                                            continue;
                                        }
                                        else if (temp.NodeType == NodeType.Uri)
                                        {
                                            graphUri = temp.ToSafeString();
                                        }
                                        else
                                        {
                                            //If the Variable is not bound to a URI then skip
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        //If the Variable is not bound for this solution then skip
                                        continue;
                                    }
                                    break;
                                default:
                                    //Any other Graph Specifier we have to ignore this solution
                                    continue;
                            }

                            //If the Dataset doesn't contain the Graph then no need to do the Deletions
                            if (!context.Data.HasGraph(UriFactory.Create(graphUri))) continue;

                            //Do the actual Deletions
                            IGraph h = context.Data.GetModifiableGraph(UriFactory.Create(graphUri));
                            ConstructContext constructContext = new ConstructContext(h, s, true);
                            foreach (IConstructTriplePattern p in gp.TriplePatterns.OfType<IConstructTriplePattern>())
                            {
                                try
                                {
                                    deletedTriples.Add(p.Construct(constructContext));
                                }
                                catch (RdfQueryException)
                                {
                                    //If we get an error here then we couldn't construct a specific
                                    //triple so we continue anyway
                                }
                            }
                            h.Retract(deletedTriples);
                        }
                        catch (RdfQueryException)
                        {
                            //If we get an error here this means we couldn't construct for this solution so the
                            //solution is ignored for this graph
                        }
                    }
                }
            }
            finally
            {
                //If the Dataset was set and an error occurred in doing the WHERE clause then
                //we'll need to Reset the Active Graph
                if (datasetOk) context.Data.ResetActiveGraph();
                if (defGraphOk) context.Data.ResetActiveGraph();
            }
        }
 /// <summary>
 /// Evaluates the Command in the given Context
 /// </summary>
 /// <param name="context">Update Evaluation Context</param>
 public override void Evaluate(SparqlUpdateEvaluationContext context)
 {
     if (context.Data.HasGraph(this._graphUri))
     {
         if (!this._silent) throw new SparqlUpdateException("Cannot create a Named Graph with URI '" + this._graphUri.ToString() + "' since a Graph with this URI already exists in the Store");
     }
     else
     {
         Graph g = new Graph();
         g.BaseUri = this._graphUri;
         context.Data.AddGraph(g);
     }
 }