internal SparqlUpdateCommandSet ConvertToSparqlUpdateCommandSet()
 {
     _nodeFactory = new Graph();
     _parser = new SparqlUpdateParser();
     return new SparqlUpdateCommandSet(
         GetDeleteEntityCommands(_datasetChanges.EntitiesRemoved).Concat(
         GetDeleteDataCommands(_datasetChanges.QuadsRemoved)).Concat(
         GetInsertDataCommands(_datasetChanges.QuadsAdded, false)).Concat(
         GetInsertDataCommands(_datasetChanges.EntitiesReconstructed)));
 }
        public void ApplyTransaction(IList<Triple> preconditions, IList<Triple> deletePatterns, IList<Triple> inserts,
                                     string updateGraphUri)
        {
            if (preconditions.Count > 0)
            {
                throw new NotSupportedException("SparqlDataObjectStore does not support conditional updates");
            }

            var deleteOp = FormatDeletePatterns(deletePatterns, updateGraphUri);
            var insertOp = FormatInserts(inserts, updateGraphUri);

            var parser = new SparqlUpdateParser();
            var cmds = parser.ParseFromString(deleteOp + "\n" + insertOp);
            _updateProcessor.ProcessCommandSet(cmds);
        }
        public void ApplyTransaction(IEnumerable<ITriple> existencePreconditions, IEnumerable<ITriple> nonexistencePreconditions, IEnumerable<ITriple> deletePatterns, IEnumerable<ITriple> inserts, string updateGraphUri)
        {
            if (existencePreconditions.Any())
            {
                throw new NotSupportedException("SparqlDataObjectStore does not support conditional updates");
            }
            if (nonexistencePreconditions.Any())
            {
                // NOTE: At the moment this is ignored because if you use key properties, 
                // non-existence preconditions will get generated and we want to support
                // using key properties with SPARQL update endpoints.
            }

            var deleteOp = FormatDeletePatterns(deletePatterns.ToList(), updateGraphUri);
            var insertOp = FormatInserts(inserts, updateGraphUri);

            var parser = new SparqlUpdateParser();
            var cmds = parser.ParseFromString(deleteOp + "\n" + insertOp);
            _updateProcessor.ProcessCommandSet(cmds);
        }
        /// <summary>
        /// Processes a PATCH operation
        /// </summary>
        /// <param name="context">HTTP Context</param>
        public override void ProcessPatch(HttpContext context)
        {
            //Work out the Graph URI we want to patch
            Uri graphUri = this.ResolveGraphUri(context);

            //If the Request has the SPARQL Update MIME Type then we can process it
            if (context.Request.ContentLength > 0)
            {
                if (context.Request.ContentType.Equals("application/sparql-update"))
                {
                    //Try and parse the SPARQL Update
                    //No error handling here as we assume the calling IHttpHandler does that
                    String patchData;
                    using (StreamReader reader = new StreamReader(context.Request.InputStream))
                    {
                        patchData = reader.ReadToEnd();
                        reader.Close();
                    }
                    SparqlUpdateParser parser = new SparqlUpdateParser();
                    SparqlUpdateCommandSet cmds = parser.ParseFromString(patchData);

                    //Assuming that we've got here i.e. the SPARQL Updates are parseable then
                    //we need to check that they actually affect the relevant Graph
                    if (cmds.Commands.All(c => c.AffectsSingleGraph && c.AffectsGraph(graphUri)))
                    {
                        GenericUpdateProcessor processor = new GenericUpdateProcessor(this._manager);
                        processor.ProcessCommandSet(cmds);
                        processor.Flush();
                    }
                    else
                    {
                        //One/More commands either do no affect a Single Graph or don't affect the Graph
                        //implied by the HTTP Request so give a 422 response
                        context.Response.StatusCode = 422;
                        return;
                    }
                }
                else
                {
                    //Don't understand other forms of PATCH requests
                    context.Response.StatusCode = (int)HttpStatusCode.UnsupportedMediaType;
                    return;
                }
            }
            else
            {
                //Empty Request is a Bad Request
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return;
            }
        }
Example #5
0
        public void SparqlUpdateModify()
        {
            TripleStore store = new TripleStore();
            Graph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = null;
            store.Add(g);

            IUriNode rdfType = g.CreateUriNode(new Uri(RdfSpecsHelper.RdfType));

            Assert.AreNotEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain some rdf:type Triples");

            String update = "DELETE {?s a ?type} WHERE {?s a ?type}";
            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(update);
            store.ExecuteUpdate(cmds);

            Assert.AreEqual(0, store.GetTriplesWithPredicate(rdfType).Count(), "Store should contain no rdf:type Triples after DELETE command executes");
        }
        private void ProcessTestDirectory(String dir)
        {
            Console.WriteLine("## Processing Directory '" + dir + "'");

            //First need to find the manifest file
            if (File.Exists(dir + "manifest.ttl"))
            {
                Graph manifest = new Graph();
                manifest.BaseUri = new Uri("file:///" + Path.GetFullPath(dir));
                try
                {
                    FileLoader.Load(manifest, dir + "manifest.ttl");
                    Console.WriteLine("Loaded Tests Manifest OK");
                    Console.WriteLine();
                }
                catch (RdfParseException parseEx)
                {
                    this.ReportError("Manifest Parser Error for Directory '" + dir + "'", parseEx);
                }

                //Ensure qt and ut namespaces
                manifest.NamespaceMap.AddNamespace("qt", new Uri("http://www.w3.org/2001/sw/DataAccess/tests/test-query#"));
                manifest.NamespaceMap.AddNamespace("ut", new Uri("http://www.w3.org/2009/sparql/tests/test-update#"));

                //Create necessary Uri Nodes
                IUriNode rdfType = manifest.CreateUriNode("rdf:type");
                IUriNode rdfsComment = manifest.CreateUriNode("rdfs:comment");
                IUriNode positiveSyntaxTest = manifest.CreateUriNode("mf:PositiveSyntaxTest");
                IUriNode positiveSyntaxTest11 = manifest.CreateUriNode("mf:PositiveSyntaxTest11");
                IUriNode positiveUpdateSyntaxTest = manifest.CreateUriNode("mf:PositiveUpdateSyntaxTest11");
                IUriNode negativeSyntaxTest = manifest.CreateUriNode("mf:NegativeSyntaxTest");
                IUriNode negativeSyntaxTest11 = manifest.CreateUriNode("mf:NegativeSyntaxTest11");
                IUriNode negativeUpdateSyntaxTest = manifest.CreateUriNode("mf:NegativeUpdateSyntaxTest11");
                IUriNode evaluationTest = manifest.CreateUriNode("mf:QueryEvaluationTest");
                IUriNode updateEvaluationTest = manifest.CreateUriNode("ut:UpdateEvaluationTest");
                IUriNode action = manifest.CreateUriNode("mf:action");
                IUriNode result = manifest.CreateUriNode("mf:result");
                IUriNode approval = manifest.CreateUriNode("dawgt:approval");
                IUriNode approvedTest = manifest.CreateUriNode("dawgt:Approved");
                IUriNode unclassifiedTest = manifest.CreateUriNode("dawgt:NotClassified");
                IUriNode query = manifest.CreateUriNode("qt:query");
                IUriNode data = manifest.CreateUriNode("qt:data");
                IUriNode graphData = manifest.CreateUriNode("qt:graphData");

                //Create SPARQL Query Parser
                SparqlQueryParser queryParser = new SparqlQueryParser();
                SparqlUpdateParser updateParser = new SparqlUpdateParser();
                queryParser.DefaultBaseUri = (manifest.BaseUri != null ? manifest.BaseUri : manifest.NamespaceMap.GetNamespaceUri(String.Empty));
                updateParser.DefaultBaseUri = manifest.NamespaceMap.GetNamespaceUri(String.Empty);

                //Find all the Positive Syntax Tests
                foreach (Triple t in manifest.GetTriplesWithPredicateObject(rdfType, positiveSyntaxTest).Concat(manifest.GetTriplesWithPredicateObject(rdfType, positiveSyntaxTest11)).Concat(manifest.GetTriplesWithPredicateObject(rdfType, positiveUpdateSyntaxTest)))
                {
                    //Test ID
                    INode testID = t.Subject;

                    //See whether the Test is approved
                    if (manifest.Triples.Contains(new Triple(testID, approval, approvedTest)) || manifest.Triples.Contains(new Triple(testID, approval, unclassifiedTest)))
                    {
                        tests++;
                        testsSyntax++;

                        //Find the Test Query
                        Triple queryDef = manifest.Triples.WithSubjectPredicate(testID, action).FirstOrDefault();
                        if (queryDef != null)
                        {
                            this.ProcessSyntaxTest(queryParser, updateParser, queryDef.Object.ToString(), true);
                        }
                        else
                        {
                            Console.WriteLine("Unable to find the Test Query/Update for Syntax Test ID '" + testID.ToString() + "' in '" + dir + "'");
                            testsIndeterminate++;
                            testsSyntaxIndeterminate++;
                        }

                        Debug.WriteLine(tests + " Tests Completed");
                    }
                    
                }

                //Find all the Negative Syntax Tests
                foreach (Triple t in manifest.GetTriplesWithPredicateObject(rdfType, negativeSyntaxTest).Concat(manifest.GetTriplesWithPredicateObject(rdfType, negativeSyntaxTest11)).Concat(manifest.GetTriplesWithPredicateObject(rdfType, negativeUpdateSyntaxTest)))
                {
                    //Test ID
                    INode testID = t.Subject;

                    //See whether the Test is approved
                    if (manifest.Triples.Contains(new Triple(testID, approval, approvedTest)) || manifest.Triples.Contains(new Triple(testID, approval, unclassifiedTest)))
                    {
                        tests++;
                        testsSyntax++;

                        //Find the Test Query
                        Triple queryDef = manifest.Triples.WithSubjectPredicate(testID, action).FirstOrDefault();
                        if (queryDef != null)
                        {
                            this.ProcessSyntaxTest(queryParser, updateParser, queryDef.Object.ToString(), false);
                        }
                        else
                        {
                            Console.WriteLine("Unable to find the Test Query/Update for Syntax Test ID '" + testID.ToString() + "' in '" + dir + "'");
                            testsIndeterminate++;
                            testsSyntaxIndeterminate++;
                        }

                        Debug.WriteLine(tests + " Tests Completed");
                    }
                }

                //Find all the Query Evaluation Tests
                foreach (Triple t in manifest.Triples.WithPredicateObject(rdfType, evaluationTest))
                {
                    //Test ID
                    INode testID = t.Subject;

                    //See whether the Test is approved
                    if (manifest.Triples.Contains(new Triple(testID, approval, approvedTest)) || manifest.Triples.Contains(new Triple(testID, approval, unclassifiedTest)))
                    {
                        tests++;
                        testsEvaluation++;
                        
                        //Find the Action ID
                        Triple actionDef = manifest.Triples.WithSubjectPredicate(testID, action).FirstOrDefault();
                        if (actionDef != null)
                        {
                            INode actionID = actionDef.Object;

                            //Get the Query
                            Triple queryDef = manifest.Triples.WithSubjectPredicate(actionID, query).FirstOrDefault();
                            if (queryDef != null)
                            {
                                //Get the Default Graph
                                Triple defaultGraphDef = manifest.Triples.WithSubjectPredicate(actionID, data).FirstOrDefault();
                                String defGraph = (defaultGraphDef == null) ? null : defaultGraphDef.Object.ToString();

                                //Get the Named Graphs if any
                                List<String> namedGraphs = manifest.Triples.WithSubjectPredicate(actionID, graphData).Select(gdef => gdef.Object.ToString()).ToList();

                                //Get the expected Result
                                Triple resultDef = manifest.Triples.WithSubjectPredicate(testID, result).FirstOrDefault();
                                if (resultDef != null)
                                {
                                    //Try to get the comments on the Test
                                    Triple commentDef = manifest.Triples.WithSubjectPredicate(testID, rdfsComment).FirstOrDefault();

                                    //Run the Evaluation Test
                                    int eval = this.ProcessEvaluationTest(queryParser, commentDef, queryDef.Object.ToString(), defGraph, namedGraphs, resultDef.Object.ToString());
                                }
                                else
                                {
                                    Console.WriteLine("Unable to find the Expected Result file for Test ID '" + testID.ToString() + "' in '" + dir + "'");
                                    testsIndeterminate++;
                                    testsEvaluationIndeterminate++;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Unable to find the Test Query for Test ID '" + testID.ToString() + "' in '" + dir + "'");
                                testsIndeterminate++;
                                testsEvaluationIndeterminate++;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Unable to find the Action for Test ID '" + testID.ToString() + "' + in + '" + dir + "'");
                            testsIndeterminate++;
                            testsEvaluationIndeterminate++;
                        }

                        Console.WriteLine();
                        Console.WriteLine(new String('-', 150));
                    }

                    Debug.WriteLine(tests + " Tests Completed");
                }

                //Find all the Update Evaluation Tests
                foreach (Triple t in manifest.GetTriplesWithPredicateObject(rdfType, updateEvaluationTest))
                {
                    if (manifest.Triples.Contains(new Triple(t.Subject, approval, approvedTest)) || manifest.Triples.Contains(new Triple(t.Subject, approval, unclassifiedTest)))
                    {
                        tests++;
                        testsEvaluation++;
                        int eval = this.ProcessUpdateEvaluationTest(manifest, t.Subject);

                        Console.WriteLine();
                        Console.WriteLine(new String('-', 150));

                        Debug.WriteLine(tests + " Tests Completed");
                    }
                }
            }

            Console.WriteLine();
            Console.WriteLine("## Finished processing directory '" + dir + "'");
            Console.WriteLine(new String('=', 150));
            Console.WriteLine();

            //if (dir.EndsWith("il8n\\"))
            //{
            //    Options.UriNormalization = true;
            //}
        }
Example #7
0
        /// <summary>
        /// Applies SPARQL Updates to the Store
        /// </summary>
        /// <param name="sparqlUpdate">SPARQL Update</param>
        public void Update(String sparqlUpdate)
        {
            if (this._updateParser == null) this._updateParser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = this._updateParser.ParseFromString(sparqlUpdate);

            if (this._updateProcessor == null) this._updateProcessor = new LeviathanUpdateProcessor(this._dataset);
            this._updateProcessor.ProcessCommandSet(cmds);
        }
        public void SparqlDatasetDefaultGraphManagementWithUpdate3()
        {
            TripleStore store = new TripleStore();
            Graph g = new Graph();
            g.BaseUri = new Uri("http://example.org/graph");
            store.Add(g);
            Graph h = new Graph();
            h.BaseUri = new Uri("http://example.org/someOtherGraph");
            store.Add(h);

            InMemoryDataset dataset = new InMemoryDataset(store, h.BaseUri);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#> INTO GRAPH <http://example.org/graph>; LOAD <http://www.dotnetrdf.org/configuration#> INTO GRAPH <http://example.org/someOtherGraph>");

            processor.ProcessCommandSet(cmds);

            Assert.IsFalse(g.IsEmpty, "First Graph should not be empty as should have been filled by the first LOAD command");
            Assert.IsFalse(h.IsEmpty, "Second Graph should not be empty as should not have been filled by the second LOAD command");
            Assert.AreEqual(g, h, "Graphs should be equal");
        }
Example #9
0
        static void DoUpdate(Dictionary<String, String> arguments)
        {
            SparqlRemoteUpdateEndpoint endpoint;
            bool verbose = arguments.ContainsKey("verbose") || arguments.ContainsKey("v");
            if (verbose) Options.HttpDebugging = true;

            //First get the Server to which we are going to connect
            try
            {
                if (arguments.ContainsKey("server") && !arguments["server"].Equals(String.Empty))
                {
                    endpoint = new SparqlRemoteUpdateEndpoint(new Uri(arguments["server"]));
                }
                else if (arguments.ContainsKey("service") && !arguments["service"].Equals(String.Empty))
                {
                    endpoint = new SparqlRemoteUpdateEndpoint(new Uri(arguments["service"]));
                }
                else
                {
                    Console.Error.WriteLine("soh: Error: Required --server/--service argument not present");
                    Environment.Exit(-1);
                    return;
                }
            }
            catch (UriFormatException uriEx)
            {
                Console.Error.WriteLine("soh: Error: Malformed SPARQL Update Endpoint URI");
                Console.Error.WriteLine(uriEx.Message);
                Environment.Exit(-1);
                return;
            }
            if (verbose) Console.Error.WriteLine("soh: SPARQL Update Endpoint for URI " + endpoint.Uri + " created OK");

            //Then decide where to get the update to execute from
            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds;
            try
            {
                if (arguments.ContainsKey("update") && !arguments["update"].Equals(String.Empty))
                {
                    cmds = parser.ParseFromFile(arguments["update"]);
                }
                else if (arguments.ContainsKey("file") && !arguments["file"].Equals(String.Empty))
                {
                    cmds = parser.ParseFromFile(arguments["file"]);
                }
                else if (arguments.ContainsKey("$1") && !arguments["$1"].Equals(String.Empty))
                {
                    cmds = parser.ParseFromString(arguments["$1"]);
                }
                else
                {
                    Console.Error.WriteLine("soh: Error: Required SPARQL Update not found - may be specified as --file/--update FILE or as final argument");
                    Environment.Exit(-1);
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("soh: Error: Error Parsing SPARQL Update");
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(-1);
                return;
            }

            if (verbose)
            {
                Console.Error.WriteLine("soh: Parsed Update OK");
                Console.Error.WriteLine("soh: dotNetRDF's interpretation of the Update:");
                Console.Error.WriteLine(cmds.ToString());
                Console.Error.WriteLine("soh: Submitting Update");
            }

            try
            {
                endpoint.Update(cmds.ToString());
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("soh: Error: Error while making the SPARQL Update");
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(-1);
                return;
            }
        }
Example #10
0
        public void SparqlUpdateInsertCommand3()
        {
            SparqlParameterizedString command = new SparqlParameterizedString();
            command.Namespaces.AddNamespace("rdf", new Uri(NamespaceMapper.RDF));
            command.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS));
            command.CommandText = "INSERT { ?s rdf:type ?class } USING NAMED <http://example.org/temp> WHERE { ?s a ?type . ?type rdfs:subClassOf+ ?class };";
            command.CommandText += "INSERT { ?s ?property ?value } USING NAMED <http://example.org/temp> WHERE {?s ?p ?value . ?p rdfs:subPropertyOf+ ?property };";
            command.CommandText += "INSERT { ?s rdf:type rdfs:Class } USING NAMED <http://example.org/temp> WHERE { ?s rdfs:subClassOf ?class };";
            command.CommandText += "INSERT { ?s rdf:type rdf:Property } USING NAMED <http://example.org/temp> WHERE { ?s rdfs:subPropertyOf ?property };";

            TripleStore store = new TripleStore();
            Graph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = new Uri("http://example.org/temp");
            store.Add(g);

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(command);
            InMemoryDataset dataset = new InMemoryDataset(store);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            dataset.SetDefaultGraph(store.Graph(null));
            processor.ProcessCommandSet(cmds);

            IGraph def = store.Graph(null);
            TestTools.ShowGraph(def);
            Assert.IsTrue(def.IsEmpty, "Graph should be empty as the commands only used USING NAMED (so shouldn't have changed the dataset) and the Active Graph for the dataset was empty so there should have been nothing matched to generate insertions from");
        }
Example #11
0
        public void SparqlUpdateInsertCommand4()
        {
            SparqlParameterizedString command = new SparqlParameterizedString();
            command.Namespaces.AddNamespace("rdf", new Uri(NamespaceMapper.RDF));
            command.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS));
            command.CommandText = "INSERT { ?s rdf:type ?class } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s a ?type . ?type rdfs:subClassOf+ ?class } };";
            command.CommandText += "INSERT { ?s ?property ?value } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s ?p ?value . ?p rdfs:subPropertyOf+ ?property } };";
            command.CommandText += "INSERT { ?s rdf:type rdfs:Class } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s rdfs:subClassOf ?class } };";
            command.CommandText += "INSERT { ?s rdf:type rdf:Property } USING NAMED <http://example.org/temp> WHERE { GRAPH ?g { ?s rdfs:subPropertyOf ?property } };";

            TripleStore store = new TripleStore();
            Graph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = new Uri("http://example.org/temp");
            store.Add(g);

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(command);
            InMemoryDataset dataset = new InMemoryDataset(store);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            dataset.SetDefaultGraph(store.Graph(null));
            processor.ProcessCommandSet(cmds);

            IGraph def = store.Graph(null);
            TestTools.ShowGraph(def);

            //Apply a RDFS reasoner over the original input and output it into another graph
            //Should be equivalent to the default Graph
            Graph h = new Graph();
            RdfsReasoner reasoner = new RdfsReasoner();
            reasoner.Apply(g, h);

            TestTools.ShowGraph(h);

            GraphDiffReport report = h.Difference(def);
            if (!report.AreEqual)
            {
                TestTools.ShowDifferences(report);

                Assert.IsTrue(report.RemovedTriples.Count() == 1, "Should have only 1 missing Triple (due to rdfs:domain inference which is hard to encode in an INSERT command)");
            }
        }
Example #12
0
        public void SparqlUpdateMoveCommand3()
        {
            IGraph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = null;

            IGraph h = new Graph();
            FileLoader.Load(h, "Turtle.ttl");
            h.BaseUri = new Uri("http://example.org/destination");

            TripleStore store = new TripleStore();
            store.Add(g);
            store.Add(h);

            Assert.AreNotEqual(g, h, "Graphs should not be equal");

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet commands = parser.ParseFromString("MOVE DEFAULT TO GRAPH <http://example.org/destination>");

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
            processor.ProcessCommandSet(commands);

            g = store.HasGraph(null) ? store.Graph(null) : null;
            h = store.Graph(new Uri("http://example.org/destination"));
            Assert.IsFalse(h.IsEmpty, "Destination Graph should not be empty");
            Assert.IsFalse(g == null, "Default Graph should still exist");
            Assert.IsTrue(g.IsEmpty, "Source Graph (the Default Graph) should be Empty");

            Graph orig = new Graph();
            FileLoader.Load(orig, "InferenceTest.ttl");
            Assert.AreEqual(orig, h, "Destination Graph should be equal to the original contents of the Source Graph");
        }
Example #13
0
        public void SparqlUpdateInsertCommand()
        {
            SparqlParameterizedString command = new SparqlParameterizedString();
            command.Namespaces.AddNamespace("rdf", new Uri(NamespaceMapper.RDF));
            command.Namespaces.AddNamespace("rdfs", new Uri(NamespaceMapper.RDFS));
            command.CommandText = "INSERT { ?s rdf:type ?class } WHERE { ?s a ?type . ?type rdfs:subClassOf+ ?class };";
            command.CommandText += "INSERT { ?s ?property ?value } WHERE {?s ?p ?value . ?p rdfs:subPropertyOf+ ?property };";
            command.CommandText += "INSERT { ?s rdf:type rdfs:Class } WHERE { ?s rdfs:subClassOf ?class };";
            command.CommandText += "INSERT { ?s rdf:type rdf:Property } WHERE { ?s rdfs:subPropertyOf ?property };";

            TripleStore store = new TripleStore();
            Graph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.Retract(g.Triples.Where(t => !t.IsGroundTriple));
            g.BaseUri = null;
            store.Add(g);

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(command);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
            processor.ProcessCommandSet(cmds);

            TestTools.ShowGraph(g);
            Console.WriteLine();

            //Now reload the test data and apply an RDFS reasoner over it
            //This should give us a Graph equivalent to the one created by the previous INSERT commands
            Graph h = new Graph();
            FileLoader.Load(h, "InferenceTest.ttl");
            h.Retract(h.Triples.Where(t => !t.IsGroundTriple));
            RdfsReasoner reasoner = new RdfsReasoner();
            reasoner.Apply(h);

            GraphDiffReport diff = h.Difference(g);
            if (!diff.AreEqual)
            {
                TestTools.ShowDifferences(diff);
            }

            Assert.AreEqual(h, g, "Graphs should be equal");            
        }
Example #14
0
        public void SparqlUpdateCopyCommand2()
        {
            IGraph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = new Uri("http://example.org/source");

            IGraph h = new Graph();
            FileLoader.Load(h, "Turtle.ttl");
            h.BaseUri = null;

            TripleStore store = new TripleStore();
            store.Add(g);
            store.Add(h);

            Assert.AreNotEqual(g, h, "Graphs should not be equal");

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet commands = parser.ParseFromString("COPY GRAPH <http://example.org/source> TO DEFAULT");

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
            processor.ProcessCommandSet(commands);

            g = store.Graph(new Uri("http://example.org/source"));
            h = store.Graph(null);
            Assert.IsFalse(g.IsEmpty, "Source Graph should not be empty");
            Assert.IsFalse(h.IsEmpty, "Destination Graph should not be empty");
            Assert.AreEqual(g, h, "Graphs should now be equal");
        }
        private int ProcessUpdateEvaluationTest(IGraph manifest, INode testNode)
        {
            try
            {
                IUriNode utData = manifest.CreateUriNode("ut:data");
                IUriNode utGraph = manifest.CreateUriNode("ut:graph");
                IUriNode utGraphData = manifest.CreateUriNode("ut:graphData");
                IUriNode rdfsLabel = manifest.CreateUriNode("rdfs:label");

                //Get the test name and comment
                String name = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:name")).Select(t => t.Object).First().ToString();
                String comment = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("rdfs:comment")).Select(t => t.Object).First().ToString();

                //Get the test action and file
                INode actionNode = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:action")).Select(t => t.Object).First();
                String updateFile = manifest.GetTriplesWithSubjectPredicate(actionNode, manifest.CreateUriNode("ut:request")).Select(t => t.Object).First().ToString();

                Console.WriteLine("# Processing Update Evaluation Test " + updateFile);
                Console.WriteLine(name);
                Console.WriteLine(comment);
                Console.WriteLine();

                if (evaluationTestOverride.Any(x => updateFile.EndsWith(x)))
                {
                    Console.WriteLine();
                    Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                    testsPassed++;
                    testsEvaluationPassed++;
                    return 1;
                }

                //Parse the Update
                SparqlUpdateParser parser = new SparqlUpdateParser();
                SparqlUpdateCommandSet cmds;
                try
                {
                    if (updateFile.StartsWith("file:///"))
                    {
                        updateFile = updateFile.Substring(8);
                    }
                    cmds = parser.ParseFromFile(updateFile);

                    Console.WriteLine("Update Commands:");
                    Console.WriteLine(cmds.ToString());
                }
                catch (Exception ex)
                {
                    this.ReportError("Error Parsing Update Commands", ex);

                    Console.WriteLine("# Test Result - Update Command failed to pass (Test Failed)");
                    testsEvaluationFailed++;
                    testsFailed++;
                    return -1;
                }

                //Build the Initial Dataset
                InMemoryDataset dataset = new InMemoryDataset(new TripleStore());
                try
                {
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(actionNode, utData))
                    {
                        Console.WriteLine("Uses Default Graph File " + t.Object.ToString());
                        Graph g = new Graph();
                        UriLoader.Load(g, ((IUriNode)t.Object).Uri);
                        g.BaseUri = null;
                        dataset.AddGraph(g);
                    }
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(actionNode, utGraphData))
                    {
                        Graph g = new Graph();
                        INode dataNode = manifest.GetTriplesWithSubjectPredicate(t.Object, utData).Concat(manifest.GetTriplesWithSubjectPredicate(t.Object, utGraph)).Select(x => x.Object).FirstOrDefault();
                        UriLoader.Load(g, ((IUriNode)dataNode).Uri);
                        INode nameNode = manifest.GetTriplesWithSubjectPredicate(t.Object, rdfsLabel).Select(x => x.Object).FirstOrDefault();
                        g.BaseUri = new Uri(nameNode.ToString());
                        Console.WriteLine("Uses Named Graph File " + dataNode.ToString() + " named as " + nameNode.ToString());
                        dataset.AddGraph(g);
                    }
                }
                catch (Exception ex)
                {
                    this.ReportError("Error Building Initial Dataset", ex);
                    Console.WriteLine("# Test Result - Unable to build Initial Dataset (Test Indeterminate)");
                    testsEvaluationIndeterminate++;
                    testsIndeterminate++;
                    return 0;
                }
                Console.WriteLine();

                //Try running the Update
                try
                {
                    LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

                    //Since all Tests assume that the WHERE is limited to the unnamed default graph unless specified
                    //then must set this to be the Active Graph for the dataset
                    dataset.SetDefaultGraph(dataset[null]);

                    //Try the Update
                    processor.ProcessCommandSet(cmds);

                    dataset.ResetDefaultGraph();
                }
                catch (SparqlUpdateException updateEx)
                {
                    //TODO: Some Update tests might be to test cases where a failure should occur
                    this.ReportError("Unexpected Error while performing Update", updateEx);
                    Console.WriteLine("# Test Result - Update Failed (Test Failed)");
                    testsEvaluationFailed++;
                    testsFailed++;
                    return -1;
                }
                catch (Exception ex)
                {
                    this.ReportError("Unexpected Error while performing Update", ex);
                    Console.WriteLine("# Test Result - Update Failed (Test Failed)");
                    testsEvaluationFailed++;
                    testsFailed++;
                    return -1;
                }

                //Build the Result Dataset
                INode resultNode = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:result")).Select(t => t.Object).First();
                InMemoryDataset resultDataset = new InMemoryDataset(new TripleStore());
                try
                {
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(resultNode, utData))
                    {
                        Console.WriteLine("Uses Result Default Graph File " + t.Object.ToString());
                        Graph g = new Graph();
                        UriLoader.Load(g, ((IUriNode)t.Object).Uri);
                        g.BaseUri = null;
                        resultDataset.AddGraph(g);
                    }
                    foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(resultNode, utGraphData))
                    {
                        Graph g = new Graph();
                        INode dataNode = manifest.GetTriplesWithSubjectPredicate(t.Object, utData).Concat(manifest.GetTriplesWithSubjectPredicate(t.Object, utGraph)).Select(x => x.Object).FirstOrDefault();
                        UriLoader.Load(g, ((IUriNode)dataNode).Uri);
                        INode nameNode = manifest.GetTriplesWithSubjectPredicate(t.Object, rdfsLabel).Select(x => x.Object).FirstOrDefault();
                        g.BaseUri = new Uri(nameNode.ToString());
                        Console.WriteLine("Uses Result Named Graph File " + dataNode.ToString() + " named as " + nameNode.ToString());
                        resultDataset.AddGraph(g);
                    }
                    
                    //Do this just to ensure that if the Result Dataset doesn't have a default unnamed graph it will
                    //have one
                    LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(resultDataset);
                }
                catch (Exception ex)
                {
                    this.ReportError("Error Building Result Dataset", ex);
                    Console.WriteLine("# Test Result - Unable to build Result Dataset (Test Indeterminate)");
                    testsEvaluationIndeterminate++;
                    testsIndeterminate++;
                    return 0;
                }
                Console.WriteLine();

                //Now compare the two datasets to see if the tests passes
                foreach (Uri u in resultDataset.GraphUris)
                {
                    if (dataset.HasGraph(u))
                    {
                        if (!resultDataset[u].Equals(dataset[u]))
                        {
                            this.ShowGraphs(dataset[u], resultDataset[u]);

                            Console.WriteLine("# Test Result - Expected Result Dataset Graph '" + this.ToSafeString(u) + "' is different from the Graph with that name in the Updated Dataset (Test Failed)");
                            testsEvaluationFailed++;
                            testsFailed++;
                            return -1;
                        }
                    }
                    else
                    {
                        Console.WriteLine("# Test Result - Expected Result Dataset has Graph '" + this.ToSafeString(u) + "' which is not present in the Updated Dataset (Test Failed)");
                        testsEvaluationFailed++;
                        testsFailed++;
                        return -1;
                    }
                }
                foreach (Uri u in dataset.GraphUris)
                {
                    if (!resultDataset.HasGraph(u))
                    {
                        Console.WriteLine("# Test Result - Updated Dataset has additional Graph '" + this.ToSafeString(u) + "' which is not present in the Expected Result Dataset (Test Failed)");
                    }
                }

                Console.WriteLine("# Test Result - Updated Dataset matches Expected Result Dataset (Test Passed)");
                testsEvaluationPassed++;
                testsPassed++;
                return 1;
            }
            catch (Exception ex)
            {
                this.ReportError("Unexpected Error", ex);
                Console.WriteLine("# Test Result - Unexpected Error (Test Failed)");
                testsEvaluationFailed++;
                testsFailed++;
                return -1;
            }
        }
        private void ProcessSyntaxTest(SparqlQueryParser queryParser, SparqlUpdateParser updateParser, String inputFile, bool shouldParse)
        {
            if (inputFile.StartsWith("file:///")) inputFile = inputFile.Substring(8);

            bool error = false;
            bool skipFinally = false;
            try
            {
                Console.WriteLine("# Processing Syntax Test " + Path.GetFileName(inputFile));
                Console.Write("# Result Expected = ");
                if (shouldParse)
                {
                    Console.WriteLine("Parses OK");
                }
                else
                {
                    Console.WriteLine("Parsing Fails");
                }

                if (evaluationTestOverride.Any(x => inputFile.EndsWith(x)))
                {
                    Console.WriteLine();
                    Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                    skipFinally = true;
                    testsPassed++;
                    testsSyntaxPassed++;
                    return;
                }

                if (inputFile.EndsWith(".rq"))
                {
                    SparqlQuery q = queryParser.ParseFromFile(inputFile);

                    Console.WriteLine("Formatted with SparqlFormatter");
                    SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
                    Console.WriteLine(formatter.Format(q));
                    Console.WriteLine();
                }
                else if (inputFile.EndsWith(".ru"))
                {
                    SparqlUpdateCommandSet cmds = updateParser.ParseFromFile(inputFile);

                    Console.WriteLine(cmds.ToString());
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("# Test Result - Unknown Input File for Syntax Test (Test Indeterminate)");
                    skipFinally = true;
                    testsIndeterminate++;
                    testsSyntaxIndeterminate++;
                    return;
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                error = true;
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                error = true;
            }
            finally
            {
                if (!skipFinally)
                {
                    Console.Write("# Test Result = ");
                    if (error)
                    {
                        if (shouldParse)
                        {
                            Console.WriteLine(" Parsing Failed when should have parsed (Test Failed)");
                            testsFailed++;
                            testsSyntaxFailed++;
                        }
                        else
                        {
                            Console.WriteLine(" Parsing Failed as expected (Test Passed)");
                            testsPassed++;
                            testsSyntaxPassed++;
                        }
                    }
                    else
                    {
                        if (shouldParse)
                        {
                            Console.WriteLine(" Parsed OK as expected (Test Passed)");
                            testsPassed++;
                            testsSyntaxPassed++;
                        }
                        else
                        {
                            Console.WriteLine(" Parsed OK when should have failed (Test Failed)");
                            testsFailed++;
                            testsSyntaxFailed++;
                        }
                    }
                }
                Console.WriteLine(new String('-', 150));
            }
        }
        public static void Main(string[] args)
        {
            String stdprefixes = "PREFIX rdf: <" + NamespaceMapper.RDF + ">\nPREFIX rdfs: <" + NamespaceMapper.RDFS + ">\nPREFIX xsd: <" + NamespaceMapper.XMLSCHEMA + ">\n";

            List<String> testQueries = new List<string>()
            {
                "CLEAR DEFAULT",
                "CLEAR GRAPH <http://example.org>",
                "CREATE GRAPH <http://example.org>",
                "CREATE SILENT GRAPH <http://example.org>",
                "DELETE WHERE { ?s ?p ?o }",
                "DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } USING <http://example.org/1> WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } USING NAMED <http://example.org/1> WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> USING <http://example.org/3> WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } INSERT { ?s ?p ?o } WHERE { ?s ?p ?o }",
                "WITH <http://example.org> DELETE { ?s ?p ?o } INSERT { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> USING <http://example.org/3> WHERE { ?s ?p ?o }",
                "DELETE { ?s ?p ?o . GRAPH <http://example.org> { ?s a ?type } } WHERE { ?s ?p ?o }",
                "WITH <http://example.org> INSERT { ?s ?p ?o } WHERE { ?s ?p ?o }",
                "DROP GRAPH <http://example.org>",
                "DROP SILENT GRAPH <http://example.org>",
                "INSERT { ?s ?p ?o } WHERE { ?s ?p ?o }",
                "INSERT { ?s ?p ?o } WHERE { GRAPH <http://example.org> { ?s ?p ?o } }",
                "INSERT { ?s ?p ?o } USING <http://example.org/1> USING NAMED <http://example.org/2> USING <http://example.org/3> WHERE { ?s ?p ?o }",
                "LOAD <http://dbpedia.org/resource/Southampton>",
                "LOAD <http://dbpedia.org/resource/Southampton> INTO GRAPH <http://example.org>"
            };

            StreamWriter output = new StreamWriter("SparqlUpdateTests.txt");
            Console.SetOut(output);

            SparqlUpdateParser parser = new SparqlUpdateParser();
            parser.TraceTokeniser = true;

            foreach (String testQuery in testQueries)
            {
                try
                {
                    Console.WriteLine("Trying to parse the following SPARQL Update Command");
                    Console.WriteLine(testQuery);

                    SparqlUpdateCommandSet commands = parser.ParseFromString(testQuery);

                    Console.WriteLine("Parsed OK");
                    foreach (SparqlUpdateCommand cmd in commands.Commands)
                    {
                        Console.WriteLine(cmd.GetType().ToString());
                    }
                    Console.WriteLine(commands.ToString());
                }
                catch (RdfParseException parseEx)
                {
                    reportError(output, "Parser Error", parseEx);
                }
                catch (RdfException rdfEx)
                {
                    reportError(output, "RDF Error", rdfEx);
                }
                catch (Exception ex)
                {
                    reportError(output, "Error", ex);
                }

                Console.WriteLine();
                Console.WriteLine();
            }

            //Try and parse in bulk
            Console.WriteLine("Parsing in bulk");
            String allQueries = String.Join("\n;\n", testQueries.ToArray());
            try
            {
                SparqlUpdateCommandSet commands = parser.ParseFromString(allQueries);
                Console.WriteLine("Parsed OK");
                foreach (SparqlUpdateCommand cmd in commands.Commands)
                {
                    Console.WriteLine(cmd.GetType().ToString());
                }
                Console.WriteLine(commands.ToString());
            }
            catch (RdfParseException parseEx)
            {
                reportError(output, "Parser Error", parseEx);
            }
            catch (RdfException rdfEx)
            {
                reportError(output, "RDF Error", rdfEx);
            }
            catch (Exception ex)
            {
                reportError(output, "Error", ex);
            }

            output.Flush();
            output.Close();
        }
Example #18
0
        public void SparqlUpdateDeleteWithCommand()
        {
            String command = "WITH <http://example.org/> DELETE { ?s ?p ?o } WHERE {?s ?p ?o}";
            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(command);

            DeleteCommand delete = (DeleteCommand)cmds[0];

            Assert.AreEqual(new Uri("http://example.org/"), delete.GraphUri, "Graph URI of the Command should be equal to http://example.org/");
        }
        public void SparqlDatasetDefaultGraphManagementWithUpdate()
        {
            TripleStore store = new TripleStore();
            Graph g = new Graph();
            store.Add(g);
            Graph h = new Graph();
            h.BaseUri = new Uri("http://example.org/someOtherGraph");
            store.Add(h);

            InMemoryDataset dataset = new InMemoryDataset(store, h.BaseUri);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#>");

            processor.ProcessCommandSet(cmds);

            Assert.IsTrue(g.IsEmpty, "Graph with null URI (normally the default Graph) should be empty as the Default Graph for the Dataset should have been a named Graph so this Graph should not have been filled by the LOAD Command");
            Assert.IsFalse(h.IsEmpty, "Graph with name should be non-empty as it should have been the Default Graph for the Dataset and so filled by the LOAD Command");
        }
Example #20
0
        public void SparqlUpdateDeleteDataCombination()
        {
            SparqlParameterizedString command = new SparqlParameterizedString();
            command.Namespaces.AddNamespace("ex", new Uri("http://example.org/"));
            command.CommandText = "DELETE DATA { ex:a ex:b ex:c GRAPH <http://example.org/graph> { ex:a ex:b ex:c } }";

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(command);

            Assert.IsFalse(cmds.Commands.All(cmd => cmd.AffectsSingleGraph), "Commands should report that they do not affect a single Graph");
            Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(null)), "Commands should report that they affect the Default Graph");
            Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(new Uri("http://example.org/graph"))), "Commands should report that they affect the named Graph");

            InMemoryDataset dataset = new InMemoryDataset();
            IGraph def = new Graph();
            def.NamespaceMap.Import(command.Namespaces);
            def.Assert(new Triple(def.CreateUriNode("ex:a"), def.CreateUriNode("ex:b"), def.CreateUriNode("ex:c")));
            dataset.AddGraph(def);
            IGraph ex = new Graph();
            ex.BaseUri = new Uri("http://example.org/graph");
            ex.NamespaceMap.Import(command.Namespaces);
            ex.Assert(new Triple(ex.CreateUriNode("ex:a"), ex.CreateUriNode("ex:b"), ex.CreateUriNode("ex:c")));
            dataset.AddGraph(ex);

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            processor.ProcessCommandSet(cmds);

            Graph g = new Graph();
            g.NamespaceMap.Import(command.Namespaces);
            Triple t = new Triple(g.CreateUriNode("ex:a"), g.CreateUriNode("ex:b"), g.CreateUriNode("ex:c"));

            def = dataset[null];
            Assert.AreEqual(0, def.Triples.Count, "Should be 0 Triples in the Default Graph");
            Assert.IsFalse(def.ContainsTriple(t), "Should not have the deleted Triple in the Default Graph");

            ex = dataset[new Uri("http://example.org/graph")];
            Assert.AreEqual(0, ex.Triples.Count, "Should be 0 Triples in the Named Graph");
            Assert.IsFalse(ex.ContainsTriple(t), "Should not have the deleted Triple in the Named Graph");

        }
        public void SparqlDatasetDefaultGraphManagementWithUpdate5()
        {
            TripleStore store = new TripleStore();
            Graph g = new Graph();
            g.BaseUri = new Uri("http://example.org/graph");
            store.Add(g);
            Graph h = new Graph();
            h.BaseUri = new Uri("http://example.org/someOtherGraph");
            store.Add(h);

            InMemoryDataset dataset = new InMemoryDataset(store, h.BaseUri);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString("LOAD <http://www.dotnetrdf.org/configuration#>; WITH <http://example.org/graph> INSERT { ?s a ?type } USING <http://example.org/someOtherGraph> WHERE { ?s a ?type }; DELETE WHERE { ?s a ?type }");

            processor.ProcessCommandSet(cmds);

            Assert.IsFalse(g.IsEmpty, "First Graph should not be empty as should have been filled by the INSERT command");
            Assert.IsFalse(h.IsEmpty, "Second Graph should not be empty as should not have been filled by the  LOAD command");
            Assert.IsFalse(h.HasSubGraph(g), "First Graph should not be a subgraph of the Second Graph as the DELETE should have eliminated the subgraph relationship");
        }
Example #22
0
        public void SparqlUpdateAddCommand()
        {
            IGraph g = new Graph();
            FileLoader.Load(g, "InferenceTest.ttl");
            g.BaseUri = new Uri("http://example.org/source");

            IGraph h = new Graph();
            FileLoader.Load(h, "Turtle.ttl");
            h.BaseUri = new Uri("http://example.org/destination");

            TripleStore store = new TripleStore();
            store.Add(g);
            store.Add(h);

            Assert.AreNotEqual(g, h, "Graphs should not be equal");

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet commands = parser.ParseFromString("ADD GRAPH <http://example.org/source> TO GRAPH <http://example.org/destination>");

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(store);
            processor.ProcessCommandSet(commands);

            g = store.Graph(new Uri("http://example.org/source"));
            h = store.Graph(new Uri("http://example.org/destination"));
            Assert.IsFalse(g.IsEmpty, "Source Graph should not be empty");
            Assert.IsFalse(h.IsEmpty, "Destination Graph should not be empty");
            Assert.IsTrue(h.HasSubGraph(g), "Destination Graph should have Source Graph as a subgraph");
        }
        private void SparqlQueryAndUpdateThreadSafeEvaluationActual()
        {
            String query1 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            String query2 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";
            String query3 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/3> { ?s ?p ?o } }";
            String update1 = "INSERT DATA { GRAPH <http://example.org/3> { <ex:subj> <ex:pred> <ex:obj> } }";

            SparqlQuery q1 = this._parser.ParseFromString(query1);
            SparqlQuery q2 = this._parser.ParseFromString(query2);
            SparqlQuery q3 = this._parser.ParseFromString(query3);
            Assert.IsFalse(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.IsFalse(q2.UsesDefaultDataset, "Query 2 should not be thread safe");
            Assert.IsFalse(q3.UsesDefaultDataset, "Query 3 should not be thread safe");

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(update1);

            InMemoryDataset dataset = new InMemoryDataset();
            Graph g = new Graph();
            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            Graph h = new Graph();
            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.Functions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");
            Graph i = new Graph();
            i.BaseUri = new Uri("http://example.org/3");

            dataset.AddGraph(g);
            dataset.AddGraph(h);
            dataset.AddGraph(i);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            LeviathanUpdateProcessor upProcessor = new LeviathanUpdateProcessor(dataset);

            QueryWithGraphDelegate d = new QueryWithGraphDelegate(this.QueryWithGraph);
            RunUpdateDelegate d2 = new RunUpdateDelegate(this.RunUpdate);
            IAsyncResult r1 = d.BeginInvoke(q1, processor, null, null);
            IAsyncResult r2 = d.BeginInvoke(q2, processor, null, null);
            IAsyncResult r3 = d.BeginInvoke(q3, processor, null, null);
            IAsyncResult r4 = d2.BeginInvoke(cmds, upProcessor, null, null);

            WaitHandle.WaitAll(new WaitHandle[] { r1.AsyncWaitHandle, r2.AsyncWaitHandle, r3.AsyncWaitHandle, r4.AsyncWaitHandle });

            IGraph gQuery = d.EndInvoke(r1);
            Assert.AreEqual(g, gQuery, "Query 1 Result not as expected");

            IGraph hQuery = d.EndInvoke(r2);
            Assert.AreEqual(h, hQuery, "Query 2 Result not as expected");

            IGraph iQuery = d.EndInvoke(r3);
            if (iQuery.IsEmpty)
            {
                Console.WriteLine("Query 3 executed before the INSERT DATA command - running again to get the resulting graph");
                iQuery = this.QueryWithGraph(q3, processor);
            }
            else
            {
                Console.WriteLine("Query 3 executed after the INSERT DATA command");
            }
            //Test iQuery against an empty Graph
            Assert.IsFalse(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple");
            Assert.AreNotEqual(new Graph(), iQuery, "Graphs should not be equal");

            Assert.AreNotEqual(g, h, "Graphs should not be different");
        }
        /// <summary>
        /// Processes SPARQL Update requests
        /// </summary>
        /// <param name="context">HTTP Context</param>
        public void ProcessRequest(HttpContext context)
        {
            this._config = this.LoadConfig(context);

            //Add our Standard Headers
            HandlerHelper.AddStandardHeaders(context, this._config);

            if (context.Request.HttpMethod.Equals("OPTIONS"))
            {
                //OPTIONS requests always result in the Service Description document
                IGraph svcDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri));
                HandlerHelper.SendToClient(context, svcDescrip, this._config);
                return;
            }

            //See if there has been an update submitted
            String updateText = context.Request.QueryString["update"];
            if (updateText == null || updateText.Equals(String.Empty))
            {
                updateText = context.Request.Form["update"];
            }

            //If no Update sent either show Update Form or give a HTTP 400 response
            if (updateText == null || updateText.Equals(String.Empty))
            {
                //If there is no Update we may return the SPARQL Service Description where appropriate
                try
                {
                    //If we might show the Update Form only show the Description if the selected writer is
                    //not a HTML writer
                    MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(HandlerHelper.GetAcceptTypes(context)).FirstOrDefault(d => d.CanWriteRdf);
                    if (definition != null)
                    {
                        IRdfWriter writer = definition.GetRdfWriter();
                        if (!this._config.ShowUpdateForm || !(writer is IHtmlWriter))
                        {
                            //If not a HTML Writer selected OR not showing Update Form then show the Service Description Graph
                            //unless an error occurs creating it
                            IGraph serviceDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, new Uri(context.Request.Url.AbsoluteUri));
                            context.Response.ContentType = definition.CanonicalMimeType;
                            context.Response.ContentEncoding = definition.Encoding;
                            writer.Save(serviceDescrip, new StreamWriter(context.Response.OutputStream, definition.Encoding));
                            return;
                        }
                    }
                }
                catch
                {
                    //Ignore Exceptions - we'll just show the Query Form or return a 400 Bad Request instead
                }

                //If a Writer can't be selected then we'll either show the Update Form or return a 400 Bad Request
                if (this._config.ShowUpdateForm)
                {
                    this.ShowUpdateForm(context);
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                }
                return;
            }

            try
            {
                //Now we're going to parse the Updates
                SparqlUpdateParser parser = new SparqlUpdateParser();
                parser.ExpressionFactories = this._config.ExpressionFactories;
                SparqlUpdateCommandSet commands = parser.ParseFromString(updateText);

                //Check whether we need to use authentication
                //If there are no user groups then no authentication is in use so we default to authenticated with no per-action authentication needed
                bool isAuth = true, requireActionAuth = false;
                if (this._config.UserGroups.Any())
                {
                    //If we have user
                    isAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups);
                    requireActionAuth = true;
                }
                if (!isAuth) return;

                //First check actions to see whether they are all permissible
                foreach (SparqlUpdateCommand cmd in commands.Commands)
                {
                    //Authenticate each action
                    bool actionAuth = true;
                    if (requireActionAuth) actionAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups, this.GetPermissionAction(cmd));
                    if (!actionAuth)
                    {
                        throw new SparqlUpdateException("You are not authorised to perform the " + this.GetPermissionAction(cmd) + " action");
                    }
                }

                //Then assuming we got here this means all our actions are permitted so now we can process the updates
                this.ProcessUpdates(commands);

                //Flush outstanding changes
                this._config.Processor.Flush();

                //Update the Cache as the request may have changed the endpoint
                this.UpdateConfig(context);
            }
            catch (RdfParseException parseEx)
            {
                HandleErrors(context, "Parsing Error", updateText, parseEx);
            }
            catch (SparqlUpdateException updateEx)
            {
                HandleErrors(context, "Update Error", updateText, updateEx);
            }
            catch (RdfException rdfEx)
            {
                HandleErrors(context, "RDF Error", updateText, rdfEx);
            }
            catch (Exception ex)
            {
                HandleErrors(context, "Error", updateText, ex);
            }
        }
Example #25
0
 /// <summary>
 /// Executes an Update against the Triple Store
 /// </summary>
 /// <param name="update">SPARQL Update Command(s)</param>
 /// <remarks>
 /// As per the SPARQL 1.1 Update specification the command string may be a sequence of commands
 /// </remarks>
 public void ExecuteUpdate(string update)
 {
     SparqlUpdateParser parser = new SparqlUpdateParser();
     SparqlUpdateCommandSet commandSet = parser.ParseFromString(update);
     this.ExecuteUpdate(commandSet);
 }
Example #26
0
        /// <summary>
        /// Processes Update requests
        /// </summary>
        /// <param name="context">HTTP Context</param>
        public void ProcessUpdateRequest(HttpContext context)
        {
            if (this._config.UpdateProcessor == null)
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
                return;
            }

            if (context.Request.HttpMethod.Equals("OPTIONS"))
            {
                //OPTIONS requests always result in the Service Description document
                IGraph svcDescrip = SparqlServiceDescriber.GetServiceDescription(context, this._config, UriFactory.Create(context.Request.Url.AbsoluteUri), ServiceDescriptionType.Update);
                HandlerHelper.SendToClient(context, svcDescrip, this._config);
                return;
            }

            //See if there has been an update submitted
            String updateText = null;
            if (context.Request.ContentType != null)
            {
                if (context.Request.ContentType.Equals(MimeTypesHelper.WWWFormURLEncoded))
                {
                    updateText = context.Request.Form["update"];
                }
                else if (context.Request.ContentType != null && context.Request.ContentType.Equals(MimeTypesHelper.SparqlUpdate))
                {
                    updateText = new StreamReader(context.Request.InputStream).ReadToEnd();
                }
            }
            else
            {
                updateText = context.Request.Form["update"];
            }

            //If no Update sent either show Update Form or give a HTTP 400 response
            if (updateText == null || updateText.Equals(String.Empty))
            {
                if (this._config.ShowUpdateForm)
                {
                    this.ShowUpdateForm(context);
                    return;
                }
                else
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return;
                }
            }

            //Get Other options associated with this update
            List<String> userDefaultGraphs = new List<String>();
            List<String> userNamedGraphs = new List<String>();

            //Get the USING URIs (if any)
            if (context.Request.QueryString["using-graph-uri"] != null)
            {
                userDefaultGraphs.AddRange(context.Request.QueryString.GetValues("using-graph-uri"));
            }
            else if (context.Request.Form["using-graph-uri"] != null)
            {
                userDefaultGraphs.AddRange(context.Request.Form.GetValues("using-graph-uri"));
            }
            //Get the USING NAMED URIs (if any)
            if (context.Request.QueryString["using-named-graph-uri"] != null)
            {
                userNamedGraphs.AddRange(context.Request.QueryString.GetValues("using-named-graph-uri"));
            }
            else if (context.Request.Form["using-named-graph-uri"] != null)
            {
                userNamedGraphs.AddRange(context.Request.Form.GetValues("using-named-graph-uri"));
            }

            try
            {
                //Now we're going to parse the Updates
                SparqlUpdateParser parser = new SparqlUpdateParser();
                parser.ExpressionFactories = this._config.ExpressionFactories;
                SparqlUpdateCommandSet commands = parser.ParseFromString(updateText);

                //Check whether we need to use authentication
                //If there are no user groups then no authentication is in use so we default to authenticated with no per-action authentication needed
                bool isAuth = true, requireActionAuth = false;
                if (this._config.UserGroups.Any())
                {
                    //If we have user
                    isAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups);
                    requireActionAuth = true;
                }
                if (!isAuth) return;

                //First check actions to see whether they are all permissible and apply USING/USING NAMED paramaters
                foreach (SparqlUpdateCommand cmd in commands.Commands)
                {
                    //Authenticate each action
                    bool actionAuth = true;
                    if (requireActionAuth) actionAuth = HandlerHelper.IsAuthenticated(context, this._config.UserGroups, this.GetUpdatePermissionAction(cmd));
                    if (!actionAuth)
                    {
                        throw new SparqlUpdatePermissionException("You are not authorised to perform the " + this.GetUpdatePermissionAction(cmd) + " action");
                    }

                    //Check whether we need to (and are permitted to) apply USING/USING NAMED parameters
                    if (userDefaultGraphs.Count > 0 || userNamedGraphs.Count > 0)
                    {
                        BaseModificationCommand modify = cmd as BaseModificationCommand;
                        if (modify != null)
                        {
                            if (modify.GraphUri != null || modify.UsingUris.Any() || modify.UsingNamedUris.Any())
                            {
                                //Invalid if a command already has a WITH/USING/USING NAMED
                                throw new SparqlUpdateMalformedException("A command in your update request contains a WITH/USING/USING NAMED clause but you have also specified one/both of the using-graph-uri or using-named-graph-uri parameters which is not permitted by the SPARQL Protocol");
                            }
                            else
                            {
                                //Otherwise go ahead and apply
                                userDefaultGraphs.ForEach(u => modify.AddUsingUri(UriFactory.Create(u)));
                                userNamedGraphs.ForEach(u => modify.AddUsingNamedUri(UriFactory.Create(u)));
                            }
                        }
                    }
                }

                //Then assuming we got here this means all our actions are permitted so now we can process the updates
                this.ProcessUpdates(commands);

                //Flush outstanding changes
                this._config.UpdateProcessor.Flush();

                //Update the Cache as the request may have changed the endpoint
                this.UpdateConfig(context);
            }
            catch (RdfParseException parseEx)
            {
                HandleUpdateErrors(context, "Parsing Error", updateText, parseEx, (int)HttpStatusCode.BadRequest);
            }
            catch (SparqlUpdatePermissionException permEx)
            {
                HandleUpdateErrors(context, "Permissions Error", updateText, permEx, (int)HttpStatusCode.Forbidden);
            }
            catch (SparqlUpdateMalformedException malEx)
            {
                HandleUpdateErrors(context, "Malformed Update Error", updateText, malEx, (int)HttpStatusCode.BadRequest);
            }
            catch (SparqlUpdateException updateEx)
            {
                HandleUpdateErrors(context, "Update Error", updateText, updateEx);
            }
            catch (RdfException rdfEx)
            {
                HandleUpdateErrors(context, "RDF Error", updateText, rdfEx);
            }
            catch (Exception ex)
            {
                HandleUpdateErrors(context, "Error", updateText, ex);
            }
        }
    /// <summary>
    /// Execute insert or delete SPARQL queries against Stardog;
    /// separate multiple statements with a semicolon
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public static bool Update(string command)
    {
        using (StardogConnector dog = new StardogConnector(ConnectionString(), ConnectionDatabase(), ConnectionUser(), ConnectionPassword()))
        {

            try
            {

                dog.Begin(); // wrap all the statements in this command into one transaction; otherwise stardog will run them as separate transactions

                GenericUpdateProcessor processor = new GenericUpdateProcessor(dog);

                SparqlUpdateParser parser = new SparqlUpdateParser();

                processor.ProcessCommandSet(parser.ParseFromString(Prefixes() + command));

                dog.Commit();

                return true;

            }
            catch (Exception err)
            {

                try
                {
                    dog.Rollback();
                }
                catch { }

                throw err;

            }

        }
    }
        /// <summary>
        /// Executes this command as an update
        /// </summary>
        public void ExecuteUpdate()
        {
            if (this._updateProcessor == null) throw new SparqlUpdateException("Cannot call ExecuteUpdate() when the UpdateProcessor property has not been set");

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(this.ToString());
            this._updateProcessor.ProcessCommandSet(cmds);
        }
Example #29
0
        /// <summary>
        /// Attempt to auto-detect the syntax of the current document using the filename as a guide
        /// </summary>
        public void AutoDetectSyntax(String filename)
        {
            if (this._editor == null) return; //Not yet ready

            if (filename == null || System.IO.Path.GetExtension(filename).Equals(String.Empty))
            {
                try
                {
                    //First see if it's an RDF format
                    IRdfReader parser = StringParser.GetParser(this._editor.Text);

                    if (parser is NTriplesParser)
                    {
                        //NTriples is the fallback so if we get this check if it's actually SPARQL Results
                        try
                        {
                            ISparqlResultsReader sparqlParser = StringParser.GetResultSetParser(this._editor.Text);
                        }
                        catch (RdfParserSelectionException)
                        {
                            //Not a valid SPARQL Results format - may be a SPARQL Query or a SPARQL Update?
                            SparqlQueryParser queryParser = new SparqlQueryParser(SparqlQuerySyntax.Sparql_1_1);
                            try
                            {
                                SparqlQuery q = queryParser.ParseFromString(this._editor.Text);
                                this.SetHighlighter("SparqlQuery11");
                            }
                            catch (RdfParseException)
                            {
                                //Not a valid SPARQL Query - valid SPARQL Update?
                                SparqlUpdateParser updateParser = new SparqlUpdateParser();
                                try
                                {
                                    SparqlUpdateCommandSet cmds = updateParser.ParseFromString(this._editor.Text);
                                    this.SetHighlighter("SparqlUpdate11");
                                }
                                catch (RdfParseException)
                                {
                                    //Was probably actually NTriples
                                    this.SetHighlighter(parser);
                                }
                            }
                        }
                    }
                    else
                    {
                        //Got a non NTriples RDF parser so use that to set Highlighter
                        this.SetHighlighter(parser);
                    }
                }
                catch (RdfParserSelectionException)
                {
                    this.SetNoHighlighting();
                }
                return;
            }

            try
            {
                IHighlightingDefinition def = HighlightingManager.Instance.GetDefinitionByExtension(System.IO.Path.GetExtension(filename));
                if (this._enableHighlighting) this._editor.SyntaxHighlighting = def;

                if (def != null)
                {
                    this._currSyntax = def.Name;
                    this.SetCurrentHighlighterChecked(def.Name);
                    this.SetCurrentValidator(def.Name);
                    this.SetCurrentAutoCompleter(def.Name);
                }
                else
                {
                    this.SetNoHighlighting();
                }
            }
            catch
            {
                this.SetNoHighlighting();
            }
        }
        private void TestDropGraphCommit()
        {
            ISparqlDataset dataset = this.GetNonEmptyDataset();

            String updates = "DROP GRAPH <" + TestGraphUri.ToString() + ">";

            SparqlUpdateParser parser = new SparqlUpdateParser();
            SparqlUpdateCommandSet cmds = parser.ParseFromString(updates);

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);
            processor.ProcessCommandSet(cmds);

            Assert.IsFalse(dataset.HasGraph(TestGraphUri), "Graph should not exist");
        }