public void SparqlGraphClause4()
        {
            String            query  = "SELECT * WHERE { GRAPH ?g { ?s ?p ?o } }";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(query);

            InMemoryDataset dataset = new InMemoryDataset(false);
            IGraph          ex      = new Graph();

            FileLoader.Load(ex, "resources\\InferenceTest.ttl");
            ex.BaseUri = new Uri("http://example.org/graph");
            dataset.AddGraph(ex);

            IGraph def = new Graph();

            dataset.AddGraph(def);

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            Object results = processor.ProcessQuery(q);

            if (results is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)results;
                TestTools.ShowResults(rset);
                Assert.Equal(ex.Triples.Count, rset.Count);
            }
            else
            {
                Assert.True(false, "Did not get a SPARQL Result Set as expected");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="spinGraph"></param>
        /// <returns></returns>
        internal IGraph Initialise(IGraph spinGraph)
        {
            if (_spinConfiguration.GraphUris.Contains(spinGraph.BaseUri))
            {
                _spinConfiguration.RemoveGraph(spinGraph.BaseUri);
            }
            String ontQuery = "CONSTRUCT { ?graphUri a <" + SPIN.ClassLibraryOntology + ">} WHERE { {?s (<" + SPIN.PropertyImports + ">|<" + OWL.PropertyImports + ">) ?graphUri} UNION {?graphUri a <" + SPIN.ClassLibraryOntology + ">} }";
            IGraph imports  = (Graph)spinGraph.ExecuteQuery(ontQuery);

            // Explore for subsequent imports
            foreach (Triple t in imports.GetTriplesWithPredicateObject(RDF.PropertyType, SPIN.ClassLibraryOntology))
            {
                Uri importUri = ((IUriNode)t.Subject).Uri;
                if (!_spinConfiguration.GraphUris.Contains(importUri) && !RDFUtil.sameTerm(importUri, spinGraph.BaseUri))
                {
                    Initialise(importUri);
                }
            }
            _spinConfiguration.AddGraph(spinGraph);
            _reasoner.Initialise(spinGraph);

            IGraph inferenceGraph = ApplyInference(spinGraph);

            _spinConfiguration.AddGraph(inferenceGraph);
            _reasoner.Initialise(inferenceGraph);

            return(inferenceGraph);
        }
Ejemplo n.º 3
0
        public void SparqlUpdateDeleteCommandWithNoMatchingGraphAndChildGraphPatterns()
        {
            var dataset        = new InMemoryDataset();
            var updateGraphUri = new Uri("http://example.org/g2");
            var updateGraph    = new Graph {
                BaseUri = updateGraphUri
            };

            updateGraph.Assert(new Triple(updateGraph.CreateUriNode(new Uri("http://example.org/s")),
                                          updateGraph.CreateUriNode(new Uri("http://example.org/p")),
                                          updateGraph.CreateUriNode(new Uri("http://example.org/o"))));
            dataset.AddGraph(updateGraph);

            var egGraph = new Uri("http://example.org/graph");

            dataset.HasGraph(egGraph).Should().BeFalse();
            var processor = new LeviathanUpdateProcessor(dataset);
            var cmdSet    = new SparqlUpdateParser().ParseFromString(
                "WITH <" + egGraph +
                "> DELETE { GRAPH <http://example.org/g2> { <http://example.org/s> <http://example.org/p> <http://example.org/o> }} WHERE {}");

            processor.ProcessCommandSet(cmdSet);

            dataset.HasGraph(egGraph).Should().BeFalse(because: "Update command should not create an empty graph for a DELETE operation");
            dataset[updateGraphUri].IsEmpty.Should()
            .BeTrue("Triples should be deleted by child graph pattern");
        }
Ejemplo n.º 4
0
        public void SparqlUpdateDeleteWithGraphClause2()
        {
            Graph g = new Graph();

            g.Assert(g.CreateUriNode(UriFactory.Create("http://subject")), g.CreateUriNode(UriFactory.Create("http://predicate")), g.CreateUriNode(UriFactory.Create("http://object")));
            Graph h = new Graph();

            h.Merge(g);
            h.BaseUri = UriFactory.Create("http://subject");

            InMemoryDataset dataset = new InMemoryDataset(g);

            dataset.AddGraph(h);
            dataset.Flush();

            Assert.Equal(2, dataset.GraphUris.Count());

            String updates = "DELETE { GRAPH ?s { ?s ?p ?o } } INSERT { GRAPH ?o { ?s ?p ?o } } WHERE { ?s ?p ?o }";
            SparqlUpdateCommandSet commands = new SparqlUpdateParser().ParseFromString(updates);

            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

            processor.ProcessCommandSet(commands);

            Assert.Equal(3, dataset.GraphUris.Count());
            Assert.True(dataset.HasGraph(UriFactory.Create("http://subject")));
            Assert.True(dataset.HasGraph(UriFactory.Create("http://object")));
            Assert.Equal(0, dataset[UriFactory.Create("http://subject")].Triples.Count);
        }
        private static InMemoryDataset CreateDataset(int numberOfTriples)
        {
            var dataset = new InMemoryDataset(new TripleStore(), true);

            dataset.AddGraph(CreateGraph(numberOfTriples));
            return(dataset);
        }
Ejemplo n.º 6
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");
        }
Ejemplo n.º 7
0
        private static ISparqlDataset GetTestData()
        {
            InMemoryDataset dataset = new InMemoryDataset();
            Graph           g       = new Graph();

            g.LoadFromFile("resources\\InferenceTest.ttl");
            dataset.AddGraph(g);

            return(dataset);
        }
Ejemplo n.º 8
0
        private ISparqlDataset GetTestData()
        {
            InMemoryDataset dataset = new InMemoryDataset();
            Graph           g       = new Graph();

            FileLoader.Load(g, "InferenceTest.ttl");
            dataset.AddGraph(g);

            return(dataset);
        }
Ejemplo n.º 9
0
        public RdfHandler()
        {
            _testGraphUri = new Uri("http://example.org/graph");
            var dataset = new InMemoryDataset(new TripleStore(), _testGraphUri);

            dataset.AddGraph(new Graph {
                BaseUri = _testGraphUri
            });

            _rdfQueryWrapper = new RdfQueryWrapper(dataset);
        }
Ejemplo n.º 10
0
        protected virtual ISparqlDataset GetNonEmptyDataset()
        {
            InMemoryDataset dataset = new InMemoryDataset();

            Graph g = new Graph();

            g.BaseUri = TestGraphUri;
            dataset.AddGraph(g);

            return(dataset);
        }
Ejemplo n.º 11
0
        private void SparqlQueryThreadSafeEvaluationActual()
        {
            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 } }";

            SparqlQuery q1 = this._parser.ParseFromString(query1);
            SparqlQuery q2 = this._parser.ParseFromString(query2);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");

            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.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");

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

            QueryWithGraphDelegate d  = new QueryWithGraphDelegate(this.QueryWithGraph);
            IAsyncResult           r1 = d.BeginInvoke(q1, processor, null, null);
            IAsyncResult           r2 = d.BeginInvoke(q2, processor, null, null);

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

            IGraph gQuery = d.EndInvoke(r1);

            Assert.Equal(g, gQuery);

            IGraph hQuery = d.EndInvoke(r2);

            Assert.Equal(h, hQuery);

            Assert.NotEqual(g, h);
        }
        private void SparqlQueryThreadSafeEvaluationActual()
        {
            var query1 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            var query2 = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";

            var q1 = _parser.ParseFromString(query1);
            var q2 = _parser.ParseFromString(query2);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");

            var dataset = new InMemoryDataset();
            var g       = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            var h = new Graph();

            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");

            dataset.AddGraph(g);
            dataset.AddGraph(h);
            var processor = new LeviathanQueryProcessor(dataset);

            var t1 = Task.Factory.StartNew(() => QueryWithGraph(q1, processor));
            var t2 = Task.Factory.StartNew(() => QueryWithGraph(q2, processor));

            Task.WaitAll(t1, t2);
            var gQuery = t1.Result;
            var hQuery = t2.Result;

            Assert.Equal(g, gQuery);
            Assert.Equal(h, hQuery);
            Assert.NotEqual(g, h);
        }
        public void SparqlDescribeDefaultGraphHandling1()
        {
            InMemoryDataset dataset = new InMemoryDataset();

            IGraph g = new Graph();

            g.Assert(g.CreateUriNode(UriFactory.Create("http://subject")), g.CreateUriNode(UriFactory.Create("http://predicate")), g.CreateUriNode(UriFactory.Create("http://object")));
            g.BaseUri = UriFactory.Create("http://graph");
            dataset.AddGraph(g);

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            IGraph description = processor.ProcessQuery(this._parser.ParseFromString("DESCRIBE ?s FROM <http://graph> WHERE { ?s ?p ?o }")) as IGraph;

            Assert.NotNull(description);
            Assert.False(description.IsEmpty);
        }
Ejemplo n.º 14
0
        public void SparqlUpdateChangesNotReflectedInOriginalGraph1()
        {
            //Test Case originally submitted by Tomasz Pluskiewicz

            // given
            IGraph sourceGraph = new Graph();

            sourceGraph.LoadFromString(@"@prefix ex: <http://www.example.com/> .
ex:Subject ex:hasObject ex:Object .");

            IGraph expectedGraph = new Graph();

            expectedGraph.LoadFromString(@"@prefix ex: <http://www.example.com/> .
ex:Subject ex:hasBlank [ ex:hasObject ex:Object ] .");


            //IMPORTANT - Because we create the dataset without an existing store it
            //creates a new triple store internally which has a single unnamed graph
            //Then when we add another unnamed graph it merges into that existing graph,
            //this is why the update does not apply to the added graph but rather to
            //the merged graph that is internal to the dataset
            ISparqlDataset dataset = new InMemoryDataset(true);

            dataset.AddGraph(sourceGraph);

            // when
            var command = new SparqlParameterizedString
            {
                CommandText = @"PREFIX ex: <http://www.example.com/>
DELETE { ex:Subject ex:hasObject ex:Object . }
INSERT { ex:Subject ex:hasBlank [ ex:hasObject ex:Object ] . }
WHERE { ?s ?p ?o . }"
            };
            SparqlUpdateCommandSet   cmds      = new SparqlUpdateParser().ParseFromString(command);
            LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset);

            processor.ProcessCommandSet(cmds);

            Assert.Single(dataset.Graphs);

            IGraph g = dataset.Graphs.First();

            Assert.Equal(2, g.Triples.Count);
            Assert.False(ReferenceEquals(g, sourceGraph), "Result Graph should not be the Source Graph");
            Assert.Equal(1, sourceGraph.Triples.Count);
            Assert.NotEqual(expectedGraph, sourceGraph);
        }
        private void EnsureTestData()
        {
            if (_dataset == null)
            {
                _dataset = new InMemoryDataset();
                Graph g = new Graph();
                g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
                g.Retract(g.Triples.Where(t => !t.IsGroundTriple).ToList());
                if (g.Triples.Count > TripleLimit)
                {
                    g.Retract(g.Triples.Skip(TripleLimit).ToList());
                }
                _dataset.AddGraph(g);
                _dataset.SetDefaultGraph(g.BaseUri);

                _processor = new LeviathanQueryProcessor(_dataset);
            }
        }
        private void SparqlQueryAndUpdateThreadSafeEvaluationActual()
        {
            var query1  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/1> { ?s ?p ?o } }";
            var query2  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/2> { ?s ?p ?o } }";
            var query3  = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <http://example.org/3> { ?s ?p ?o } }";
            var update1 = "INSERT DATA { GRAPH <http://example.org/3> { <ex:subj> <ex:pred> <ex:obj> } }";

            var q1 = _parser.ParseFromString(query1);
            var q2 = _parser.ParseFromString(query2);
            var q3 = _parser.ParseFromString(query3);

            Assert.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");
            Assert.False(q3.UsesDefaultDataset, "Query 3 should not be thread safe");

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

            var dataset = new InMemoryDataset();
            var g       = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            g.BaseUri = new Uri("http://example.org/1");
            var h = new Graph();

            h.LoadFromEmbeddedResource("VDS.RDF.Query.Expressions.LeviathanFunctionLibrary.ttl");
            h.BaseUri = new Uri("http://example.org/2");
            var i = new Graph {
                BaseUri = new Uri("http://example.org/3")
            };

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

            var d  = new QueryWithGraphDelegate(QueryWithGraph);
            var d2 = new RunUpdateDelegate(RunUpdate);
            var t1 = Task.Factory.StartNew(() => QueryWithGraph(q1, processor));
            var t2 = Task.Factory.StartNew(() => QueryWithGraph(q2, processor));
            var t3 = Task.Factory.StartNew(() => QueryWithGraph(q3, processor));
            var t4 = Task.Factory.StartNew(() => RunUpdate(cmds, upProcessor));

            Task.WaitAll(t1, t2, t3, t4);
            var gQuery = t1.Result;
            var hQuery = t2.Result;
            var iQuery = t3.Result;

            Assert.Equal(g, gQuery);
            Assert.Equal(h, hQuery);
            if (iQuery.IsEmpty)
            {
                _output.WriteLine("Query 3 executed before the INSERT DATA command - running again to get the resulting graph");
                iQuery = QueryWithGraph(q3, processor);
            }
            else
            {
                _output.WriteLine("Query 3 executed after the INSERT DATA command");
            }
            //Test iQuery against an empty Graph
            Assert.False(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple");
            Assert.NotEqual(new Graph(), iQuery);

            Assert.NotEqual(g, h);
        }
Ejemplo n.º 17
0
        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.False(q1.UsesDefaultDataset, "Query 1 should not be thread safe");
            Assert.False(q2.UsesDefaultDataset, "Query 2 should not be thread safe");
            Assert.False(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.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.Equal(g, gQuery);

            IGraph hQuery = d.EndInvoke(r2);

            Assert.Equal(h, hQuery);

            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.False(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple");
            Assert.NotEqual(new Graph(), iQuery);

            Assert.NotEqual(g, h);
        }