コード例 #1
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);
                    }
                }
            }
        }
コード例 #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
            {
                //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);
                    }
                }
            }
        }