private void TestQuery(String query) { this.EnsureTestData(); SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine("Query:"); Console.WriteLine(this._formatter.Format(q)); Console.WriteLine(); Console.WriteLine("Normal Algebra:"); Console.WriteLine(q.ToAlgebra().ToString()); Console.WriteLine(); Stopwatch timer = new Stopwatch(); //Evaluate normally timer.Start(); Object normResults = this._processor.ProcessQuery(q); timer.Stop(); Console.WriteLine("Normal Evaluation took " + timer.Elapsed); timer.Reset(); if (normResults is SparqlResultSet) { SparqlResultSet rsetNorm = (SparqlResultSet)normResults; Console.WriteLine("Normal Evaluation returned " + rsetNorm.Count + " Result(s)"); Console.WriteLine(); //Evaluate parallelised q.AlgebraOptimisers = new IAlgebraOptimiser[] { new ParallelEvaluationOptimiser() }; Console.WriteLine("Parallel Algebra:"); Console.WriteLine(q.ToAlgebra().ToString()); Console.WriteLine(); timer.Start(); Object parResults = this._processor.ProcessQuery(q); timer.Stop(); Console.WriteLine("Parallel Evaluation took " + timer.Elapsed); if (parResults is SparqlResultSet) { SparqlResultSet rsetPar = (SparqlResultSet)parResults; Console.WriteLine("Parallel Evaluation returned " + rsetPar.Count + " Result(s)"); Assert.Equal(rsetNorm.Count, rsetPar.Count); Assert.Equal(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"); } }
public fclsInspect(SparqlQuery query, long parseTime, String origQuery) { InitializeComponent(); this.lblParseTime.Text = "Took " + parseTime + "ms to parse"; this.txtQuery.Text = this._formatter.Format(query); this.txtAlgebra.Text = query.ToAlgebra().ToString(); //Compute the actual syntax compatability SparqlQueryParser parser = new SparqlQueryParser(); parser.SyntaxMode = SparqlQuerySyntax.Sparql_1_0; try { SparqlQuery q = parser.ParseFromString(origQuery); this.lblSyntaxCompatability.Text += " SPARQL 1.0 (Standard)"; } catch { try { parser.SyntaxMode = SparqlQuerySyntax.Sparql_1_1; SparqlQuery q = parser.ParseFromString(origQuery); this.lblSyntaxCompatability.Text += " SPARQL 1.1 (Current Working Draft Standard)"; } catch { parser.SyntaxMode = SparqlQuerySyntax.Extended; SparqlQuery q = parser.ParseFromString(origQuery); this.lblSyntaxCompatability.Text += " SPARQL 1.1 (Implementation specific extensions)"; } } }
public void SparqlOptimiserQueryFilterPlacement4() { String query = @"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { ?s rdfs:label ?label . ?s a ?type . OPTIONAL { FILTER (LANGMATCHES(LANG(?label), 'en')) } } "; SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine(this._formatter.Format(q)); Assert.False(q.RootGraphPattern.TriplePatterns[0].PatternType == TriplePatternType.Filter, "First Triple Pattern should not be a FilterPattern"); Assert.False(q.RootGraphPattern.TriplePatterns[1].PatternType == TriplePatternType.Filter, "Second Triple Pattern should not be a FilterPattern"); Assert.Equal(0, q.RootGraphPattern.ChildGraphPatterns[0].TriplePatterns.Count); Assert.True(q.RootGraphPattern.ChildGraphPatterns[0].IsFiltered, "Child Graph Pattern should be filtered"); String algebra = q.ToAlgebra().ToString(); Console.WriteLine(algebra); Assert.True(algebra.Contains("LeftJoin("), "Algebra should have a LeftJoin() operator in it"); }
public void SparqlOptimiserQueryFilterPlacement5() { // given var query = new SparqlQuery { QueryType = SparqlQueryType.Select }; query.AddVariable(new SparqlVariable("s", true)); query.RootGraphPattern = new GraphPattern(); var subj = new VariablePattern("s"); var rdfType = new NodeMatchPattern(new UriNode(null, new Uri(RdfSpecsHelper.RdfType))); var type = new VariablePattern("type"); var triplePattern = new TriplePattern(subj, rdfType, type); query.RootGraphPattern.AddTriplePattern(triplePattern); query.RootGraphPattern.AddFilter(new UnaryExpressionFilter(new InFunction(new VariableTerm("type"), new[] { new ConstantTerm(new UriNode(null, new Uri("http://example.com/Type1"))), new ConstantTerm(new UriNode(null, new Uri("http://example.com/Type2"))), new ConstantTerm(new UriNode(null, new Uri("http://example.com/Type3"))) }))); // when var algebra = query.ToAlgebra(); // then Assert.IsType <Select>(algebra); Assert.IsType <Filter>(((Select)algebra).InnerAlgebra); }
public void SparqlQueryTimeoutDuringProductLazy2() { // This is ignored because in practise it is suprisingly easy to compute this in under a millisecond given a reasonable machine // since it only needs to compute one value 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)); try { //Try multiple times because sometimes machine load may mean we don't timeout for (int i = 0; i < 100; i++) { processor.ProcessQuery(q); } Assert.Fail("Did not throw a RdfQueryTimeoutException as expected"); } catch (RdfQueryTimeoutException timeoutEx) { TestTools.ReportError("Timeout", timeoutEx); Console.WriteLine(); Console.WriteLine("Execution Time: " + q.QueryExecutionTime.Value.ToString()); } }
private void TestQuery(String query) { SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine(q.ToString()); Console.WriteLine(); Console.WriteLine(q.ToAlgebra().ToString()); }
public void SparqlParsingNestedGraphPatternFirstItem2() { SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromFile("resources\\childgraphpattern2.rq"); Console.WriteLine(q.ToString()); Console.WriteLine(); Console.WriteLine(q.ToAlgebra().ToString()); }
public void SparqlOptimiserAlgebraImplicitJoinComplex1() { String query = "SELECT * WHERE { ?x a ?type . { SELECT ?y WHERE { ?y a ?type } }. FILTER(?x = ?y) }"; SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine(this._formatter.Format(q)); String algebra = q.ToAlgebra().ToString(); Console.WriteLine(algebra); Assert.False(algebra.Contains("Extend("), "Algebra should not be optimised to use Extend"); }
public void SparqlOptimiserAlgebraImplicitJoinSimple4() { String query = "SELECT * WHERE { ?x a ?a . ?y a ?b . FILTER(SAMETERM(?a, ?b)) }"; SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine(this._formatter.Format(q)); String algebra = q.ToAlgebra().ToString(); Console.WriteLine(algebra); Assert.True(algebra.Contains("Extend("), "Algebra should be optimised to use Extend"); Assert.False(algebra.Contains("Filter("), "Algebra should not be optimised to not use Filter"); }
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(); }
public void SparqlOptimiserAlgebraSelectSimple() { String query = @"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE { ?s ?p ?o . ?s rdfs:label ?label . } LIMIT 10"; SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine(this._formatter.Format(q)); String algebra = q.ToAlgebra().ToString(); Console.WriteLine(algebra); Assert.True(algebra.Contains("LazyBgp("), "Algebra should be optimised to use LazyBgp()'s"); }
public void SparqlOptimiserAlgebraAskUnion() { String query = @"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> ASK WHERE { { ?s a ?type } UNION { ?s rdfs:label ?label } }"; SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine(this._formatter.Format(q)); String algebra = q.ToAlgebra().ToString(); Console.WriteLine(algebra); Assert.True(algebra.Contains("AskBgp("), "Algebra should be optimised to use AskBgp()'s"); Assert.True(algebra.Contains("AskUnion("), "Algebra should be optimised to use AskUnion()'s"); }
private string TranslateSparqlToSQL(SparqlQuery query) { //Do Handler null checks before evaluating the query if (query == null) { throw new ArgumentNullException("query", "Cannot evaluate a null query"); } //Reset Query Timers query.QueryExecutionTime = null; //Convert to Algebra and execute the Query //SparqlEvaluationContext context = this.GetContext(query); ISparqlAlgebra algebra = query.ToAlgebra(); Console.WriteLine(algebra.ToString()); //result = context.Evaluate(algebra); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! return(ProcessAlgebra(algebra)); }
private void TestStrictOptimiser(String query, String[] expectedOperators) { SparqlQuery q = this._parser.ParseFromString(query); Console.WriteLine("Query:"); Console.WriteLine(this._formatter.Format(q)); Console.WriteLine(); q.AlgebraOptimisers = new IAlgebraOptimiser[] { this._optimiser }; ISparqlAlgebra algebra = q.ToAlgebra(); String output = algebra.ToString(); Console.WriteLine("Algebra:"); Console.WriteLine(output); Console.WriteLine(); foreach (String op in expectedOperators) { Assert.True(output.Contains(op), "Should have contained " + op + " Operator"); } }
private void TestSubstitution(SparqlQuery q, String findVar, INode replaceTerm, IEnumerable <String> expected, IEnumerable <String> notExpected) { Console.WriteLine("Input Query:"); Console.WriteLine(this._formatter.Format(q)); Console.WriteLine(); ISparqlAlgebra algebra = q.ToAlgebra(); VariableSubstitutionTransformer transformer = new VariableSubstitutionTransformer(findVar, replaceTerm); try { ISparqlAlgebra resAlgebra = transformer.Optimise(algebra); algebra = resAlgebra; } catch (Exception ex) { //Ignore errors Console.WriteLine("Error Transforming - " + ex.Message); Console.WriteLine(ex.StackTrace); Console.WriteLine(); } SparqlQuery resQuery = algebra.ToQuery(); String resText = this._formatter.Format(resQuery); Console.WriteLine("Resulting Query:"); Console.WriteLine(resText); Console.WriteLine(); foreach (String x in expected) { Assert.IsTrue(resText.Contains(x), "Expected Transformed Query to contain string '" + x + "'"); } foreach (String x in notExpected) { Assert.IsFalse(resText.Contains(x), "Transformed Query contained string '" + x + "' which was not expected"); } }
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); } }); }
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}"); Object testResult = store.ExecuteQuery(q); ISparqlAlgebra testAlgebra = q.ToAlgebra(); 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)))); }
/// <summary> /// Processes a SPARQL Query sending the results to a RDF/SPARQL Results handler as appropriate /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="query">SPARQL Query</param> public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query) { //Do Handler null checks before evaluating the query if (query == null) { throw new ArgumentNullException("query", "Cannot evaluate a null query"); } if (rdfHandler == null && (query.QueryType == SparqlQueryType.Construct || query.QueryType == SparqlQueryType.Describe || query.QueryType == SparqlQueryType.DescribeAll)) { throw new ArgumentNullException("rdfHandler", "Cannot use a null RDF Handler when the Query is a CONSTRUCT/DESCRIBE"); } if (resultsHandler == null && (query.QueryType == SparqlQueryType.Ask || SparqlSpecsHelper.IsSelectQuery(query.QueryType))) { throw new ArgumentNullException("resultsHandler", "Cannot use a null resultsHandler when the Query is an ASK/SELECT"); } //Handle the Thread Safety of the Query Evaluation #if !NO_RWLOCK ReaderWriterLockSlim currLock = (this._dataset is IThreadSafeDataset) ? ((IThreadSafeDataset)this._dataset).Lock : this._lock; try { currLock.EnterReadLock(); #endif //Reset Query Timers query.QueryExecutionTime = null; bool datasetOk = false, defGraphOk = false; try { //Set up the Default and Active Graphs if (query.DefaultGraphs.Any()) { //Call HasGraph() on each Default Graph but ignore the results, we just do this //in case a dataset has any kind of load on demand behaviour foreach (Uri defGraphUri in query.DefaultGraphs) { this._dataset.HasGraph(defGraphUri); } this._dataset.SetDefaultGraph(query.DefaultGraphs); defGraphOk = true; } else if (query.NamedGraphs.Any()) { //No FROM Clauses but one/more FROM NAMED means the Default Graph is the empty graph this._dataset.SetDefaultGraph(Enumerable.Empty <Uri>()); } this._dataset.SetActiveGraph(this._dataset.DefaultGraphUris); datasetOk = true; //Convert to Algebra and execute the Query SparqlEvaluationContext context = this.GetContext(query); BaseMultiset result; try { context.StartExecution(); ISparqlAlgebra algebra = query.ToAlgebra(); result = context.Evaluate(algebra); context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); } catch (RdfQueryException) { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } catch { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } //Return the Results switch (query.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: //For SELECT and ASK can populate a Result Set directly from the Evaluation Context //return new SparqlResultSet(context); resultsHandler.Apply(context); break; case SparqlQueryType.Construct: //Create a new Empty Graph for the Results try { rdfHandler.StartRdf(); foreach (String prefix in query.NamespaceMap.Prefixes) { if (!rdfHandler.HandleNamespace(prefix, query.NamespaceMap.GetNamespaceUri(prefix))) { ParserHelper.Stop(); } } //Construct the Triples for each Solution foreach (ISet s in context.OutputMultiset.Sets) { //List<Triple> constructedTriples = new List<Triple>(); try { ConstructContext constructContext = new ConstructContext(rdfHandler, s, false); foreach (IConstructTriplePattern p in query.ConstructTemplate.TriplePatterns.OfType <IConstructTriplePattern>()) { try { if (!rdfHandler.HandleTriple(p.Construct(constructContext))) { ParserHelper.Stop(); } //constructedTriples.Add(((IConstructTriplePattern)p).Construct(constructContext)); } catch (RdfQueryException) { //If we get an error here then we could not construct a specific triple //so we continue anyway } } } catch (RdfQueryException) { //If we get an error here this means we couldn't construct for this solution so the //entire solution is discarded continue; } //h.Assert(constructedTriples); } rdfHandler.EndRdf(true); } catch (RdfParsingTerminatedException) { rdfHandler.EndRdf(true); } catch { rdfHandler.EndRdf(false); throw; } break; case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: //For DESCRIBE we retrieve the Describe algorithm and apply it ISparqlDescribe describer = query.Describer; describer.Describe(rdfHandler, context); break; default: throw new RdfQueryException("Unknown query types cannot be processed by Leviathan"); } } finally { if (defGraphOk) { this._dataset.ResetDefaultGraph(); } if (datasetOk) { this._dataset.ResetActiveGraph(); } } #if !NO_RWLOCK } finally { currLock.ExitReadLock(); } #endif }