/// <summary>
 /// Processes an INSERT DATA command
 /// </summary>
 /// <param name="cmd">Insert Data Command</param>
 public void ProcessInsertDataCommand(InsertDataCommand cmd)
 {
     this.ProcessCommand(cmd);
 }
Esempio n. 2
0
        /// <summary>
        /// Processes an INSERT DATA command
        /// </summary>
        /// <param name="cmd">Insert Data Command</param>
        public void ProcessInsertDataCommand(InsertDataCommand cmd)
        {
            if (this._manager is IUpdateableGenericIOManager)
            {
                ((IUpdateableGenericIOManager)this._manager).Update(cmd.ToString());
            }
            else
            {
                //Check IO Behaviour
                //For a insert we either need the ability to Update Delete Triples or to Overwrite Graphs
                //Firstly check behaviour persuant to default graph if applicable
                if (cmd.DataPattern.TriplePatterns.OfType<IConstructTriplePattern>().Any())
                {
                    //Must support notion of default graph
                    if ((this._manager.IOBehaviour & IOBehaviour.HasDefaultGraph) == 0) throw new SparqlUpdateException("The underlying store does not support the notion of an explicit unnamed Default Graph required to process this command");
                    //Must allow either OverwriteDefault or CanUpdateAddTriples
                    if ((this._manager.IOBehaviour & IOBehaviour.CanUpdateAddTriples) == 0 && (this._manager.IOBehaviour & IOBehaviour.OverwriteDefault) == 0) throw new SparqlUpdateException("The underlying store does not support the required IO Behaviour to implement this command");
                }
                //Then check behaviour persuant to named graphs if applicable
                if (cmd.DataPattern.HasChildGraphPatterns)
                {
                    //Must support named graphs
                    if ((this._manager.IOBehaviour & IOBehaviour.HasNamedGraphs) == 0) throw new SparqlUpdateException("The underlying store does not support the notion of named graphs required to process this command");
                    //Must allow either CanUpdateAddTriples or OverwriteNamed
                    if ((this._manager.IOBehaviour & IOBehaviour.CanUpdateAddTriples) == 0 && (this._manager.IOBehaviour & IOBehaviour.OverwriteNamed) == 0) throw new SparqlUpdateException("The underlying store does not support the required IO Behaviour to implement this command");
                }

                //Split the Pattern into the set of Graph Patterns
                List<GraphPattern> patterns = new List<GraphPattern>();
                if (cmd.DataPattern.IsGraph)
                {
                    patterns.Add(cmd.DataPattern);
                }
                else if (cmd.DataPattern.TriplePatterns.Count > 0 || cmd.DataPattern.HasChildGraphPatterns)
                {
                    if (cmd.DataPattern.TriplePatterns.Count > 0)
                    {
                        patterns.Add(new GraphPattern());
                        cmd.DataPattern.TriplePatterns.ForEach(tp => patterns[0].AddTriplePattern(tp));
                    }
                    cmd.DataPattern.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 an INSERT DATA command where any of the Triple Patterns are not concrete triples - variables are not permitted");

                    Uri graphUri = null;
                    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");
                        }
                    }

                    Graph g = new Graph();
                    if (!this._manager.UpdateSupported) this._manager.LoadGraph(g, graphUri);

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

                        g.Assert(new Triple(subj, pred, obj));
                    }

                    if (this._manager.UpdateSupported)
                    {
                        this._manager.UpdateGraph(graphUri, g.Triples, Enumerable.Empty<Triple>());
                    }
                    else
                    {
                        this._manager.SaveGraph(g);
                    }
                }
            }
        }
        /// <summary>
        /// Processes an INSERT DATA command
        /// </summary>
        /// <param name="cmd">Insert Data Command</param>
        public void ProcessInsertDataCommand(InsertDataCommand cmd)
        {
            if (this._manager is IUpdateableGenericIOManager)
            {
                ((IUpdateableGenericIOManager)this._manager).Update(cmd.ToString());
            }
            else
            {
                //Split the Pattern into the set of Graph Patterns
                List<GraphPattern> patterns = new List<GraphPattern>();
                if (cmd.DataPattern.IsGraph)
                {
                    patterns.Add(cmd.DataPattern);
                }
                else if (cmd.DataPattern.TriplePatterns.Count > 0 || cmd.DataPattern.HasChildGraphPatterns)
                {
                    if (cmd.DataPattern.TriplePatterns.Count > 0)
                    {
                        patterns.Add(new GraphPattern());
                        cmd.DataPattern.TriplePatterns.ForEach(tp => patterns[0].AddTriplePattern(tp));
                    }
                    cmd.DataPattern.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 an INSERT DATA command where any of the Triple Patterns are not concrete triples - variables are not permitted");

                    Uri graphUri = null;
                    if (pattern.IsGraph)
                    {
                        switch (pattern.GraphSpecifier.TokenType)
                        {
                            case Token.QNAME:
                                throw new NotImplementedException("Graph Specifiers as QNames for INSERT DATA Commands are not supported - please specify an absolute URI instead");
                            case Token.URI:
                                graphUri = new Uri(pattern.GraphSpecifier.Value);
                                break;
                            default:
                                throw new SparqlUpdateException("Cannot evaluate an INSERT DATA Command as the Graph Specifier is not a QName/URI");
                        }
                    }

                    Graph g = new Graph();
                    if (!this._manager.UpdateSupported) this._manager.LoadGraph(g, graphUri);

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

                        g.Assert(new Triple(subj, pred, obj));
                    }

                    if (this._manager.UpdateSupported)
                    {
                        this._manager.UpdateGraph(graphUri, g.Triples, Enumerable.Empty<Triple>());
                    }
                    else
                    {
                        this._manager.SaveGraph(g);
                    }
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Processes an INSERT DATA command
 /// </summary>
 /// <param name="cmd">Insert Data Command</param>
 /// <param name="context">SPARQL Update Evaluation Context</param>
 protected virtual void ProcessInsertDataCommandInternal(InsertDataCommand cmd, SparqlUpdateEvaluationContext context)
 {
     cmd.Evaluate(context);
 }
Esempio n. 5
0
 /// <summary>
 /// Processes an INSERT DATA command
 /// </summary>
 /// <param name="cmd">Insert Data Command</param>
 public void ProcessInsertDataCommand(InsertDataCommand cmd)
 {
     this.ProcessInsertDataCommandInternal(cmd, this.GetContext());
 }
        private SparqlUpdateCommand TryParseInsertDataCommand(SparqlUpdateParserContext context)
        {
            InsertDataCommand cmd;

            SparqlQueryParserContext subContext = new SparqlQueryParserContext(context.Tokens);
            subContext.Query.BaseUri = context.BaseUri;
            subContext.Query.NamespaceMap = context.NamespaceMap;
            subContext.ExpressionParser.NamespaceMap = context.NamespaceMap;
            subContext.ExpressionParser.ExpressionFactories = context.ExpressionFactories;
            subContext.ExpressionFactories = context.ExpressionFactories;
            GraphPattern gp = context.QueryParser.TryParseGraphPattern(subContext, context.Tokens.LastTokenType != Token.LEFTCURLYBRACKET);

            //Validate that the Graph Pattern is simple
            //Check it doesn't contain anything other than Triple Patterns or if it does it just contains a single GRAPH Pattern
            if (gp.IsFiltered)
            {
                throw new RdfParseException("A FILTER Clause cannot occur in a INSERT DATA Command");
            }
            else if (gp.IsOptional)
            {
                throw new RdfParseException("An OPTIONAL Clause cannot occur in a INSERT DATA Command");
            }
            else if (gp.IsUnion)
            {
                throw new RdfParseException("A UNION Clause cannot occur in a INSERT DATA Command");
            }
            else if (gp.HasChildGraphPatterns)
            {
                if (!gp.ChildGraphPatterns.All(p => p.IsGraph || (!p.IsExists && !p.IsMinus && !p.IsNotExists && !p.IsOptional && !p.IsOptional && !p.IsService && !p.IsSubQuery && !p.IsUnion)))
                {
                    throw new RdfParseException("An INSERT DATA Command may only contain a combination of Triple Patterns and GRAPH clauses");
                }
                else if (gp.ChildGraphPatterns.Any(p => p.HasChildGraphPatterns))
                {
                    throw new RdfParseException("An INSERT DATA Command may not contain nested Graph Patterns");
                }
                else if (gp.HasChildGraphPatterns && gp.TriplePatterns.Count > 0)
                {
                    cmd = new InsertDataCommand(gp);
                }
                else if (gp.ChildGraphPatterns.Count == 1 && gp.ChildGraphPatterns[0].IsGraph)
                {
                    cmd = new InsertDataCommand(gp.ChildGraphPatterns[0]);
                }
                else
                {
                    throw new RdfParseException("Nested Graph Patterns cannot occur in a INSERT DATA Command");
                }
            }
            else
            {
                //OK
                cmd = new InsertDataCommand(gp);
            }

            return cmd;
        }