public void SparqlQueryTimeoutNone() { 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 = 0; SparqlQuery q = this._parser.ParseFromString(query); 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.AreEqual(g.Triples.Count * g.Triples.Count, rset.Count); } else { Assert.Fail("Did not get a Result Set as expected"); } } finally { Options.QueryExecutionTimeout = currTimeout; } }
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.True(false, "Null results"); } Assert.False(results.IsEmpty, "Results should not be empty"); Assert.Equal(1, results.Count); Assert.True(results.First().All(kvp => kvp.Value == null), "Should be no bound values"); }
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); } }); }
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); }
public void Evaluate(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, ISparqlDataset dataset) { LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset); processor.ProcessQuery(rdfHandler, resultsHandler, this); }
public Object Evaluate(ISparqlDataset dataset) { LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset); return(processor.ProcessQuery(this)); }
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); }
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; } }
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)))); }
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); } } }