/// <summary> /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries. /// </summary> /// <param name="query">SPARQL Query String.</param> /// <param name="callback">Callback to invoke when the query completes.</param> /// <param name="state">State to pass to the callback.</param> public void QueryWithResultSet(String query, SparqlResultsCallback callback, Object state) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri); request.Method = "POST"; request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; request.Accept = ResultsAcceptHeader; ApplyRequestOptions(request); Tools.HttpDebugRequest(request); try { request.BeginGetRequestStream(result => { try { Stream stream = request.EndGetRequestStream(result); using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write("query="); writer.Write(HttpUtility.UrlEncode(query)); foreach (String u in DefaultGraphs) { writer.Write("&default-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } foreach (String u in NamedGraphs) { writer.Write("&named-graph-uri="); writer.Write(HttpUtility.UrlEncode(u)); } writer.Close(); } request.BeginGetResponse(innerResult => { try { using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult)) { Tools.HttpDebugResponse(response); ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false); SparqlResultSet rset = new SparqlResultSet(); parser.Load(rset, new StreamReader(response.GetResponseStream())); response.Close(); callback(rset, state); } } catch (SecurityException secEx) { callback(null, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state)); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }, null); } catch (SecurityException secEx) { callback(null, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state)); } catch (WebException webEx) { if (webEx.Response != null) { Tools.HttpDebugResponse((HttpWebResponse)webEx.Response); } callback(null, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state)); } catch (Exception ex) { callback(null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }, null); } catch (Exception ex) { callback(null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state)); } }
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> /// Determines whether two Result Sets are equal /// </summary> /// <param name="obj"></param> /// <returns></returns> /// <remarks> /// Experimental and not yet complete /// </remarks> public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj is SparqlResultSet) { SparqlResultSet results = (SparqlResultSet)obj; //Must contain same number of Results to be equal if (this.Count != results.Count) { return(false); } //Must have same Boolean result to be equal if (this.Result != results.Result) { return(false); } //Must contain the same set of variables if (this.Variables.Count() != results.Variables.Count()) { return(false); } if (!this.Variables.All(v => results.Variables.Contains(v))) { return(false); } if (results.Variables.Any(v => !this._variables.Contains(v))) { return(false); } //If both have no results then they are equal if (this.Count == 0 && results.Count == 0) { return(true); } //All Ground Results from the Result Set must appear in the Other Result Set List <SparqlResult> otherResults = results.OrderByDescending(r => r.Variables.Count()).ToList(); List <SparqlResult> localResults = new List <SparqlResult>(); int grCount = 0; foreach (SparqlResult result in this.Results.OrderByDescending(r => r.Variables.Count())) { if (result.IsGroundResult) { //If a Ground Result in this Result Set is not in the other Result Set we're not equal if (!otherResults.Remove(result)) { return(false); } grCount++; } else { localResults.Add(result); } } //If all the Results were ground results and we've emptied all the Results from the other Result Set //then we were equal if (this.Count == grCount && otherResults.Count == 0) { return(true); } //If the Other Results still contains Ground Results we're not equal if (otherResults.Any(r => r.IsGroundResult)) { return(false); } //Create Graphs of the two sets of non-Ground Results SparqlResultSet local = new SparqlResultSet(); SparqlResultSet other = new SparqlResultSet(); foreach (String var in this._variables) { local.AddVariable(var); other.AddVariable(var); } foreach (SparqlResult r in localResults) { local.AddResult(r); } foreach (SparqlResult r in otherResults) { other.AddResult(r); } //Compare the two Graphs for equality SparqlRdfWriter writer = new SparqlRdfWriter(); IGraph g = writer.GenerateOutput(local); IGraph h = writer.GenerateOutput(other); return(g.Equals(h)); } else { return(false); } }
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"); } }
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 SparqlRemoteEndpointSyncVsAsyncTimeLocalVirtuoso() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseVirtuoso)) { Assert.Inconclusive("Test Config marks Virtuoso as unavailable, test cannot be run"); } String query; using (StreamReader reader = new StreamReader("dbpedia-query-time.rq")) { query = reader.ReadToEnd(); reader.Close(); } SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(TestConfigManager.GetSetting(TestConfigManager.VirtuosoEndpoint))); endpoint.Timeout = AsyncTimeout; Stopwatch timer = new Stopwatch(); timer.Start(); SparqlResultSet syncGetResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (GET): " + timer.Elapsed); TestTools.ShowResults(syncGetResults); Console.WriteLine(); timer.Reset(); timer.Start(); endpoint.HttpMode = "POST"; SparqlResultSet syncPostResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (POST): " + timer.Elapsed); TestTools.ShowResults(syncPostResults); Console.WriteLine(); timer.Reset(); ManualResetEvent signal = new ManualResetEvent(false); SparqlResultSet asyncResults = null; //DateTime start = DateTime.Now; //DateTime end = start; timer.Start(); endpoint.QueryWithResultSet(query, (r, s) => { //end = DateTime.Now; timer.Stop(); asyncResults = r; signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.IsTrue(signal.SafeWaitHandle.IsClosed, "Wait Handle should be closed"); Console.WriteLine("Async Query: " + timer.Elapsed);//(end - start)); TestTools.ShowResults(asyncResults); Assert.AreEqual(syncGetResults, asyncResults, "Result Sets should be equal"); }