public void SparqlAggEmptyGroupMax1()
        {
            String query = @"PREFIX ex: <http://example.com/>
SELECT (MAX(?value) AS ?max)
WHERE {
	?x ex:p ?value
} GROUP BY ?x";

            SparqlQuery             q         = new SparqlQueryParser().ParseFromString(query);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(new TripleStore());

            SparqlResultSet results = processor.ProcessQuery(q) as SparqlResultSet;

            Assert.NotNull(results);

            Assert.True(results.IsEmpty, "Results should be empty");
        }
        public void SparqlAggEmptyGroupCount1()
        {
            // counting no results with grouping returns no results
            var         query     = @"PREFIX : <http://example/>

SELECT (count(*) AS ?C)
WHERE {
   ?s :p ?x
}
GROUP BY ?s";
            SparqlQuery q         = new SparqlQueryParser().ParseFromString(query);
            var         processor = new LeviathanQueryProcessor(new TripleStore());
            var         results   = processor.ProcessQuery(q) as SparqlResultSet;

            Assert.NotNull(results);
            Assert.True(results.IsEmpty);
        }
示例#3
0
        private void TestQuery(IInMemoryQueryableStore store, String query, String queryName, int differences)
        {
            Console.WriteLine(queryName);
            SparqlQuery q = this._sparqlParser.ParseFromString(query);

            Console.WriteLine(q.ToAlgebra().ToString());

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(store);

            using (SparqlResultSet resultSet = processor.ProcessQuery(q) as SparqlResultSet)
            {
                Assert.IsNotNull(resultSet);
                TestTools.ShowResults(resultSet);
                Assert.AreEqual(differences, resultSet.Count);
            }
            Console.WriteLine();
        }
示例#4
0
        private void TestBindings(ISparqlDataset data, String queryWithBindings)
        {
            LeviathanQueryProcessor processor     = new LeviathanQueryProcessor(data);
            SparqlQuery             bindingsQuery = this._parser.ParseFromString(queryWithBindings);

            SparqlResultSet bindingsResults = processor.ProcessQuery(bindingsQuery) as SparqlResultSet;

            if (bindingsResults == null)
            {
                Assert.Fail("Did not get a SPARQL Result Set for the Bindings Query");
            }

            Console.WriteLine("Bindings Results");
            TestTools.ShowResults(bindingsResults);
            Console.WriteLine();

            Assert.IsTrue(bindingsResults.IsEmpty, "Result Set should be empty");
        }
示例#5
0
        private void TestNegation(ISparqlDataset data, String queryWithNegation)
        {
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(data);
            SparqlQuery             negQuery  = this._parser.ParseFromString(queryWithNegation);

            SparqlResultSet negResults = processor.ProcessQuery(negQuery) as SparqlResultSet;

            if (negResults == null)
            {
                Assert.Fail("Did not get a SPARQL Result Set for the Negation Query");
            }

            Console.WriteLine("Negation Results");
            TestTools.ShowResults(negResults);
            Console.WriteLine();

            Assert.IsTrue(negResults.IsEmpty, "Result Set should be empty");
        }
示例#6
0
        public void SparqlServiceUsingDBPediaAndBindings()
        {
            String            query  = "SELECT * WHERE { SERVICE <http://dbpedia.org/sparql> { ?s a ?type } } VALUES ?s { <http://dbpedia.org/resource/Southampton> <http://dbpedia.org/resource/Ilkeston> }";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(query);

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

            if (results is SparqlResultSet)
            {
                TestTools.ShowResults(results);
            }
            else
            {
                Assert.Fail("Should have returned a SPARQL Result Set");
            }
        }
示例#7
0
        public void SparqlConstructEmptyWhereCore407()
        {
            const String queryStr = @"CONSTRUCT
{
<http://s> <http://p> <http://o> .
}
WHERE
{}";

            SparqlQuery             q         = new SparqlQueryParser().ParseFromString(queryStr);
            InMemoryDataset         dataset   = new InMemoryDataset();
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            IGraph g = processor.ProcessQuery(q) as IGraph;

            Assert.NotNull(g);
            Assert.False(g.IsEmpty, "Graph should not be empty");
            Assert.Equal(1, g.Triples.Count);
        }
示例#8
0
        public void SparqlQueryTimeoutDuringProductLazy()
        {
            String      query = "SELECT * WHERE { ?s ?p ?o . ?x ?y ?z . ?a ?b ?c }";
            SparqlQuery q     = this._parser.ParseFromString(query);

            q.Timeout = 1;

            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            store.Add(g);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(AsDataset(store));

            Assert.Throws <RdfQueryTimeoutException>(() =>
            {
                processor.ProcessQuery(q);
            });
        }
示例#9
0
        public void SparqlServiceWithNonExistentService()
        {
            String            query  = "SELECT * WHERE { SERVICE <http://www.dotnetrdf.org/noSuchService> { ?s a ?type } } LIMIT 10";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(query);

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(new TripleStore());

            try
            {
                Object results = processor.ProcessQuery(q);
                Assert.Fail("Should have errored");
            }
            catch (RdfQueryException queryEx)
            {
                Console.WriteLine("Errored as expected");
                TestTools.ReportError("Query Error", queryEx);
            }
        }
示例#10
0
        public void SparqlPropertyPathEvaluationCore395ExactQuery()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources/pp.rdf");

            InMemoryDataset         dataset   = new InMemoryDataset(g);
            SparqlQuery             query     = new SparqlQueryParser().ParseFromFile(@"resources/pp.rq");
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
            Object results = processor.ProcessQuery(query);

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsType <SparqlResultSet>(results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.Equal(3, rset.Count);
        }
        public void SparqlServiceUsingDBPediaAndBindings()
        {
            Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run");

            String            query  = "SELECT * WHERE { SERVICE <http://dbpedia.org/sparql> { ?s a ?type } } VALUES ?s { <http://dbpedia.org/resource/Southampton> <http://dbpedia.org/resource/Ilkeston> }";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(query);

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

            if (results is SparqlResultSet)
            {
                TestTools.ShowResults(results);
            }
            else
            {
                Assert.True(false, "Should have returned a SPARQL Result Set");
            }
        }
示例#12
0
        public void SparqlPropertyPathEvaluationCore441NoPath()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\core-441\data.ttl");

            InMemoryDataset dataset = new InMemoryDataset(g);
            SparqlQuery     query   = new SparqlQueryParser().ParseFromFile(@"resources\core-441\no-path.rq");

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

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsType <SparqlResultSet>(results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.Equal(1, rset.Count);
            Assert.Equal(g.CreateUriNode("Frame:Sheep"), rset[0]["prey"]);
        }
示例#13
0
        public void SparqlEvaluationMultipleOptionals2()
        {
            TripleStore store = new TripleStore();

            store.LoadFromFile("resources\\multiple-options.trig");

            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       query  = parser.ParseFromFile("resources\\multiple-optionals-alternate.rq");

            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(store);
            Object results = processor.ProcessQuery(query);

            if (results is SparqlResultSet)
            {
                TestTools.ShowResults(results);
            }
            else
            {
                Assert.Fail("Expected a SPARQL Result Set");
            }
        }
示例#14
0
        public void SparqlConstructFromSubqueryWithLimitCore420()
        {
            const string queryStr = @"CONSTRUCT
{
  ?s ?p ?o .
} WHERE {
  ?s ?p ?o .
  {
    SELECT ?s WHERE {
      ?s a <http://xmlns.com/foaf/0.1/Person> .
    } LIMIT 3
  }
}";
            SparqlQuery  q        = new SparqlQueryParser().ParseFromString(queryStr);
            IGraph       g        = new Graph();

            g.NamespaceMap.AddNamespace("rdf", new Uri("http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
            g.NamespaceMap.AddNamespace("foaf", new Uri("http://xmlns.com/foaf/0.1/"));
            var rdfType = g.CreateUriNode("rdf:type");
            var person  = g.CreateUriNode("foaf:Person");
            var name    = g.CreateUriNode("foaf:name");
            var age     = g.CreateUriNode("foaf:age");

            for (int i = 0; i < 3; i++)
            {
                // Create 3 statements for each instance of foaf:Person (including rdf:type statement)
                var s = g.CreateUriNode(new Uri("http://example.com/people/" + i));
                g.Assert(new Triple(s, rdfType, person));
                g.Assert(new Triple(s, name, g.CreateLiteralNode("Person " + i)));
                g.Assert(new Triple(s, age, g.CreateLiteralNode((20 + i).ToString(CultureInfo.InvariantCulture))));
            }

            //ISparqlQueryProcessor processor = new ExplainQueryProcessor(new InMemoryDataset(g), ExplanationLevel.ShowAll | ExplanationLevel.AnalyseAll | ExplanationLevel.OutputToConsoleStdOut);
            var processor   = new LeviathanQueryProcessor(new InMemoryDataset(g));
            var resultGraph = processor.ProcessQuery(q) as IGraph;

            Assert.NotNull(resultGraph);
            Assert.Equal(9, resultGraph.Triples.Count); // Returns 3 rather than 9
        }
        public void SparqlGroupByAggregateEmptyGroup2()
        {
            String query = @"PREFIX ex: <http://example.com/>
SELECT (MAX(?value) AS ?max)
WHERE {
	?x ex:p ?value
} GROUP BY ?x";

            SparqlQuery             q         = new SparqlQueryParser().ParseFromString(query);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(new TripleStore());

            Console.WriteLine(q.ToAlgebra().ToString());

            SparqlResultSet results = processor.ProcessQuery(q) as SparqlResultSet;

            if (results == null)
            {
                Assert.True(false, "Null results");
            }

            Assert.False(results.IsEmpty, "Results should not be empty");
        }
示例#16
0
        public void SparqlPropertyPathEvaluationCore441OneOrMorePath()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\core-441\data.ttl");

            InMemoryDataset dataset = new InMemoryDataset(g);
            SparqlQuery     query   = new SparqlQueryParser().ParseFromFile(@"resources\core-441\plus-path.rq");

            Console.WriteLine(query.ToAlgebra().ToString());

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

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsType <SparqlResultSet>(results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.Equal(0, rset.Count);
        }
示例#17
0
        public void SparqlEvaluationGraphNonExistentUri()
        {
            String      query     = "SELECT * WHERE { GRAPH <http://example.org/noSuchGraph> { ?s ?p ?o } }";
            TripleStore store     = new TripleStore();
            var         processor = new LeviathanQueryProcessor(store);
            var         parser    = new SparqlQueryParser();
            var         q         = parser.ParseFromString(query);
            Object      results   = processor.ProcessQuery(q);

            if (results is SparqlResultSet)
            {
                TestTools.ShowResults(results);

                SparqlResultSet rset = (SparqlResultSet)results;
                Assert.True(rset.IsEmpty, "Result Set should be empty");
                Assert.Equal(3, rset.Variables.Count());
            }
            else
            {
                Assert.True(false, "Query should have returned a SPARQL Result Set");
            }
        }
示例#18
0
        public void SparqlPropertyPathEvaluationCore441ZeroOrMorePath()
        {
            IGraph g = new Graph();

            g.LoadFromFile(@"resources\core-441\data.ttl");

            InMemoryDataset dataset = new InMemoryDataset(g);
            SparqlQuery     query   = new SparqlQueryParser().ParseFromFile(@"resources\core-441\star-path.rq");

            Console.WriteLine(query.ToAlgebra().ToString());

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

            Assert.NotNull(results);
            TestTools.ShowResults(results);

            Assert.IsInstanceOf(typeof(SparqlResultSet), results);
            SparqlResultSet rset = (SparqlResultSet)results;

            Assert.AreEqual(1, rset.Count);
            Assert.AreEqual(g.CreateUriNode("Frame:Sheep"), rset[0]["prey"]);
        }
示例#19
0
        public void SparqlServiceUsingDBPedia()
        {
            if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing))
            {
                throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run");
            }

            String            query  = "SELECT * WHERE { SERVICE <http://dbpedia.org/sparql> { ?s a ?type } } LIMIT 10";
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(query);

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

            if (results is SparqlResultSet)
            {
                TestTools.ShowResults(results);
            }
            else
            {
                Assert.True(false, "Should have returned a SPARQL Result Set");
            }
        }
示例#20
0
        public void SparqlQueryTimeout()
        {
            String      query = "SELECT * WHERE { ?s ?p ?o . ?s ?p2 ?o2 . ?a ?b ?c }";
            SparqlQuery q     = this._parser.ParseFromString(query);

            q.Timeout = 1;

            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            store.Add(g);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(AsDataset(store));

            Assert.Throws <RdfQueryTimeoutException>(() =>
            {
                //Try multiple times because sometimes machine load may mean we don't timeout
                for (int i = 0; i < 10; i++)
                {
                    processor.ProcessQuery(q);
                }
            });
        }
        public void SparqlParallelEvaluationOptional1()
        {
            String data = @"<http://a> <http://p> <http://x> .
<http://b> <http://p> <http://y> .
<http://c> <http://p> <http://z> .
<http://x> <http://value> ""X"" .
<http://z> <http://value> ""Z"" .";

            String      query = "SELECT * WHERE { ?s <http://p> ?o . OPTIONAL { ?o <http://value> ?value } }";
            SparqlQuery q     = this._parser.ParseFromString(query);

            TripleStore store = new TripleStore();

            StringParser.ParseDataset(store, data, new NQuadsParser());
            InMemoryDataset         dataset   = new InMemoryDataset(store);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            Stopwatch timer = new Stopwatch();

            timer.Start();
            int i;

            for (i = 1; i <= 100000; i++)
            {
                SparqlResultSet results = processor.ProcessQuery(q) as SparqlResultSet;
                Assert.NotNull(results);
                if (results.Count != 3)
                {
                    TestTools.ShowResults(results);
                }
                Assert.Equal(3, results.Count);
            }
            timer.Stop();

            Console.WriteLine("Completed " + i + " Iterations OK");
            Console.WriteLine("Took " + timer.Elapsed);
        }
示例#22
0
        public void SparqlQueryTimeoutMinimal()
        {
            Graph g = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");

            String query = "SELECT * WHERE { ?s ?p ?o . ?x ?y ?z }";

            long currTimeout = Options.QueryExecutionTimeout;

            try
            {
                Options.QueryExecutionTimeout = 1;

                SparqlQuery q = this._parser.ParseFromString(query);
                q.PartialResultsOnTimeout = true;
                TripleStore store = new TripleStore();
                store.Add(g);

                LeviathanQueryProcessor processor = new LeviathanQueryProcessor(AsDataset(store));
                Object results = processor.ProcessQuery(q);
                if (results is SparqlResultSet)
                {
                    SparqlResultSet rset = (SparqlResultSet)results;
                    Console.WriteLine("Results: " + rset.Count + " - Query Time: " + q.QueryExecutionTime);
                    Assert.IsTrue(rset.Count < (g.Triples.Count * g.Triples.Count));
                }
                else
                {
                    Assert.Fail("Did not get a Result Set as expected");
                }
            }
            finally
            {
                Options.QueryExecutionTimeout = currTimeout;
            }
        }
示例#23
0
        public void SparqlQueryTimeoutDuringProductLazy2()
        {
            String      query = "ASK WHERE { ?s ?p ?o . ?x ?y ?z }";
            SparqlQuery q     = this._parser.ParseFromString(query);

            q.Timeout = 1;
            Console.WriteLine(q.ToAlgebra().ToString());

            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl");
            store.Add(g);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(AsDataset(store));

            Assert.Throws <RdfQueryTimeoutException>(() =>
            {
                //Try multiple times because sometimes machine load may mean we don't timeout
                for (int i = 0; i < 100; i++)
                {
                    processor.ProcessQuery(q);
                }
            });
        }
示例#24
0
        public void SparqlGroupByAggregateEmptyGroup1()
        {
            String query = @"PREFIX ex: <http://example.com/>
SELECT (MAX(?value) AS ?max)
WHERE {
	?x ex:p ?value
}";

            SparqlQuery             q         = new SparqlQueryParser().ParseFromString(query);
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(new TripleStore());

            Console.WriteLine(q.ToAlgebra().ToString());

            SparqlResultSet results = processor.ProcessQuery(q) as SparqlResultSet;

            if (results == null)
            {
                Assert.Fail("Null results");
            }

            Assert.IsFalse(results.IsEmpty, "Results should not be empty");
            Assert.AreEqual(1, results.Count, "Should be a single result");
            Assert.IsTrue(results.First().All(kvp => kvp.Value == null), "Should be no bound values");
        }
示例#25
0
        public void SparqlPropertyPathParser()
        {
            //Load our test data
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            FileLoader.Load(g, "resources\\InferenceTest.ttl");
            store.Add(g);

            List <String> testQueries = new List <string>();
            String        rdfsPrefix  = "PREFIX rdfs: <" + NamespaceMapper.RDFS + ">\n";

            //Cardinality Paths
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf* <http://example.org/vehicles/Vehicle>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf+ <http://example.org/vehicles/Vehicle>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf? <http://example.org/vehicles/Vehicle>}");
            //testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf{2,4} <http://example.org/vehicles/Vehicle>}");
            //testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf{2,} <http://example.org/vehicles/Vehicle>}");
            //testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf{,4} <http://example.org/vehicles/Vehicle>}");
            //testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf{2} <http://example.org/vehicles/Vehicle>}");

            //Simple Inverse Paths
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?type ^a ?entity}");

            //Sequence Paths
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf / rdfs:subClassOf <http://example.org/vehicles/Vehicle>}");
            //testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf{2} / rdfs:subClassOf <http://example.org/vehicles/Vehicle>}");
            //testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf / rdfs:subClassOf{2} <http://example.org/vehicles/Vehicle>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass a / rdfs:subClassOf <http://example.org/vehicles/Plane>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?vehicle a ^ rdfs:subClassOf <http://example.org/vehicles/Plane>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass a / ^ rdfs:subClassOf <http://example.org/vehicles/Vehicle>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?entity a ^ a ?type}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?entity a ^ a / rdfs:subClassOf <http://example.org/vehicles/GroundVehicle>}");


            //Alternative Paths
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf | a <http://example.org/vehicles/Vehicle>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass (rdfs:subClassOf | a) | rdfs:someProperty <http://example.org/vehicles/Vehicle>}");
            testQueries.Add(rdfsPrefix + "SELECT * WHERE {?subclass rdfs:subClassOf | a | rdfs:someProperty <http://example.org/vehicles/Vehicle>}");

            SparqlQueryParser parser = new SparqlQueryParser();

            foreach (String query in testQueries)
            {
                //Parse the Query and output to console
                SparqlQuery q = parser.ParseFromString(query);
                Console.WriteLine(q.ToString());

                //Now we'll try and evaluate it (if this is possible)
                try
                {
                    var    processor = new LeviathanQueryProcessor(store);
                    Object results   = processor.ProcessQuery(q);

                    Console.WriteLine("Evaluated OK");
                    TestTools.ShowResults(results);
                    Console.WriteLine();
                }
                catch (RdfQueryException queryEx)
                {
                    Console.WriteLine("Unable to evaluate:");
                    Console.WriteLine(queryEx.Message);
                    Console.WriteLine(queryEx.StackTrace);
                }
            }
        }
示例#26
0
        public void SparqlBgpEvaluation()
        {
            //Prepare the Store
            TripleStore store = new TripleStore();
            Graph       g     = new Graph();

            FileLoader.Load(g, "resources\\Turtle.ttl");
            store.Add(g);

            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery       q      = parser.ParseFromString(@"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {?s ?p ?o . ?s rdfs:label ?label}");
            var    processor         = new LeviathanQueryProcessor(store);
            Object testResult        = processor.ProcessQuery(q);

            if (testResult is SparqlResultSet)
            {
                SparqlResultSet rset = (SparqlResultSet)testResult;
                Console.WriteLine(rset.Count + " Results");
                foreach (SparqlResult r in rset)
                {
                    Console.WriteLine(r.ToString());
                }
                Console.WriteLine();
            }

            //Create some Triple Patterns
            TriplePattern t1 = new TriplePattern(new VariablePattern("?s"), new VariablePattern("?p"), new VariablePattern("?o"));
            TriplePattern t2 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(g.CreateUriNode("rdfs:label")), new VariablePattern("?label"));
            TriplePattern t3 = new TriplePattern(new VariablePattern("?x"), new VariablePattern("?y"), new VariablePattern("?z"));
            TriplePattern t4 = new TriplePattern(new VariablePattern("?s"), new NodeMatchPattern(g.CreateUriNode(":name")), new VariablePattern("?name"));

            //Build some BGPs
            Bgp selectNothing  = new Bgp();
            Bgp selectAll      = new Bgp(t1);
            Bgp selectLabelled = new Bgp(new List <ITriplePattern>()
            {
                t1, t2
            });
            Bgp selectAllDisjoint = new Bgp(new List <ITriplePattern>()
            {
                t1, t3
            });
            Bgp selectLabels = new Bgp(t2);
            Bgp selectNames  = new Bgp(t4);
            //LeftJoin selectOptionalNamed = new LeftJoin(selectAll, new Optional(selectNames));
            LeftJoin selectOptionalNamed = new LeftJoin(selectAll, selectNames);
            Union    selectAllUnion      = new Union(selectAll, selectAll);
            Union    selectAllUnion2     = new Union(selectAllUnion, selectAll);
            Filter   selectAllUriObjects = new Filter(selectAll, new UnaryExpressionFilter(new IsUriFunction(new VariableTerm("o"))));

            //Test out the BGPs
            //Console.WriteLine("{}");
            //this.ShowMultiset(selectNothing.Evaluate(new SparqlEvaluationContext(null, store)));

            //Console.WriteLine("{?s ?p ?o}");
            //this.ShowMultiset(selectAll.Evaluate(new SparqlEvaluationContext(null, store)));

            //Console.WriteLine("{?s ?p ?o . ?s rdfs:label ?label}");
            //SparqlEvaluationContext context = new SparqlEvaluationContext(null, store);
            //this.ShowMultiset(selectLabelled.Evaluate(context));
            //SparqlResultSet lvnResult = new SparqlResultSet(context);

            //Console.WriteLine("{?s ?p ?o . ?x ?y ?z}");
            //this.ShowMultiset(selectAllDisjoint.Evaluate(new SparqlEvaluationContext(null, store)));

            //Console.WriteLine("{?s ?p ?o . OPTIONAL {?s :name ?name}}");
            //this.ShowMultiset(selectOptionalNamed.Evaluate(new SparqlEvaluationContext(null, store)));

            Console.WriteLine("{{?s ?p ?o} UNION {?s ?p ?o}}");
            this.ShowMultiset(selectAllUnion.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store))));

            Console.WriteLine("{{?s ?p ?o} UNION {?s ?p ?o} UNION {?s ?p ?o}}");
            this.ShowMultiset(selectAllUnion2.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store))));

            Console.WriteLine("{?s ?p ?o FILTER (ISURI(?o))}");
            this.ShowMultiset(selectAllUriObjects.Evaluate(new SparqlEvaluationContext(null, new InMemoryDataset(store))));
        }
示例#27
0
        private void TestProductTimeout(IGraph data, String query, bool useGlobal, int expectedResults)
        {
            Console.WriteLine("Maximum Expected Results: " + expectedResults);
            Console.WriteLine("Initial Global Timeout: " + Options.QueryExecutionTimeout);
            Console.WriteLine();

            long globalOrig = Options.QueryExecutionTimeout;

            try
            {
                if (useGlobal)
                {
                    Console.WriteLine("Global Timeout setting in use");
                }
                else
                {
                    Console.WriteLine("Per Query Timeout setting in use");
                }
                Console.WriteLine();

                TripleStore store = new TripleStore();
                store.Add(data);

                SparqlQuery             q         = this._parser.ParseFromString(query);
                LeviathanQueryProcessor processor = new LeviathanQueryProcessor(AsDataset(store));

                SparqlFormatter formatter = new SparqlFormatter();
                Console.WriteLine("Query:");
                Console.WriteLine(formatter.Format(q));

                //Evaluate for each Timeout
                foreach (long t in this._timeouts)
                {
                    //Set the Timeout and ask for Partial Results
                    if (useGlobal)
                    {
                        Options.QueryExecutionTimeout = t;
                    }
                    else
                    {
                        q.Timeout = t;
                    }
                    q.PartialResultsOnTimeout = true;

                    //Check that the reported Timeout matches the expected
                    SparqlEvaluationContext context = new SparqlEvaluationContext(q, null);
                    long expected;
                    if (useGlobal)
                    {
                        expected = t;
                    }
                    else
                    {
                        if (Options.QueryExecutionTimeout > 0 && t <= Options.QueryExecutionTimeout)
                        {
                            expected = t;
                        }
                        else if (Options.QueryExecutionTimeout == 0)
                        {
                            expected = t;
                        }
                        else
                        {
                            expected = Options.QueryExecutionTimeout;
                        }
                    }
                    Assert.AreEqual(expected, context.QueryTimeout, "Reported Timeout not as expected");

                    //Run the Query
                    Object results = processor.ProcessQuery(q);
                    if (results is SparqlResultSet)
                    {
                        SparqlResultSet rset = (SparqlResultSet)results;

                        Console.WriteLine("Requested Timeout: " + t + " - Actual Timeout: " + expected + "ms - Results: " + rset.Count + " - Query Time: " + q.QueryExecutionTime);
                        Assert.IsTrue(rset.Count <= expectedResults, "Results should be <= expected");
                    }
                    else
                    {
                        Assert.Fail("Did not get a Result Set as expected");
                    }
                }
            }
            finally
            {
                Options.QueryExecutionTimeout = globalOrig;
            }
        }
        private void TestQuery(string query, bool checkGraphEquality)
        {
            EnsureTestData();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            var q = _parser.ParseFromString(query);

            _output.WriteLine("Query:");
            _output.WriteLine(_formatter.Format(q));
            _output.WriteLine(string.Empty);

            _output.WriteLine("Normal Algebra:");
            _output.WriteLine(q.ToAlgebra().ToString());
            _output.WriteLine(string.Empty);

            var timer = new Stopwatch();

            //Evaluate normally
            timer.Start();
            var normResults = _processor.ProcessQuery(q);

            timer.Stop();
            _output.WriteLine("Normal Evaluation took " + timer.Elapsed);
            timer.Reset();

            if (normResults is SparqlResultSet)
            {
                SparqlResultSet rsetNorm = (SparqlResultSet)normResults;
                _output.WriteLine("Normal Evaluation returned " + rsetNorm.Count + " Result(s)");
                _output.WriteLine(string.Empty);

                //Evaluate parallelised
                q.AlgebraOptimisers = new IAlgebraOptimiser[] { new ParallelEvaluationOptimiser() };
                _output.WriteLine("Parallel Algebra:");
                _output.WriteLine(q.ToAlgebra().ToString());
                _output.WriteLine(string.Empty);

                timer.Start();
                var parResults = _processor.ProcessQuery(q);
                timer.Stop();
                _output.WriteLine("Parallel Evaluation took " + timer.Elapsed);

                if (parResults is SparqlResultSet rsetPar)
                {
                    _output.WriteLine("Parallel Evaluation returned " + rsetPar.Count + " Result(s)");
                    Assert.Equal(rsetNorm.Count, rsetPar.Count);
                    if (checkGraphEquality)
                    {
                        Assert.StrictEqual(rsetNorm, rsetPar);
                    }
                }
                else
                {
                    Assert.True(false, "Query did not return a SPARQL Result Set as expected");
                }
            }
            else
            {
                Assert.True(false, "Query did not return a SPARQL Result Set for normal evaluation as expected");
            }
        }
示例#29
0
        public Object Evaluate(ISparqlDataset dataset)
        {
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            return(processor.ProcessQuery(this));
        }
示例#30
0
        public void Evaluate(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, ISparqlDataset dataset)
        {
            LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);

            processor.ProcessQuery(rdfHandler, resultsHandler, this);
        }