public void ParsingMalformedSparqlXml() { SparqlResultSet results = new SparqlResultSet(); SparqlXmlParser parser = new SparqlXmlParser(); Assert.Throws <RdfParseException>(() => parser.Load(results, "resources\\bad_srx.srx")); }
public void ParsingMalformedSparqlXml() { SparqlResultSet results = new SparqlResultSet(); SparqlXmlParser parser = new SparqlXmlParser(); parser.Load(results, "bad_srx.srx"); }
public void SparqlXmlWriter() { try { Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); Object results = g.ExecuteQuery("SELECT * WHERE {?s ?p ?o}"); if (results is SparqlResultSet) { TestTools.ShowResults(results); } StringBuilder output = new StringBuilder(); System.IO.StringWriter writer = new System.IO.StringWriter(output); SparqlXmlWriter sparqlWriter = new SparqlXmlWriter(); sparqlWriter.Save((SparqlResultSet)results, writer); Console.WriteLine(); Console.WriteLine(output.ToString()); Console.WriteLine(); SparqlXmlParser parser = new SparqlXmlParser(); SparqlResultSet results2 = new SparqlResultSet(); StringParser.ParseResultSet(results2, output.ToString()); Assert.AreEqual(((SparqlResultSet)results).Count, results2.Count, "Result Sets should have contained same number of Results"); Assert.IsTrue(((SparqlResultSet)results).Equals(results2), "Result Sets should have been equal"); } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } }
public void ParsingResultSetHandlerImplicitSparqlXml() { this.EnsureTestData("test.srx"); SparqlXmlParser parser = new SparqlXmlParser(); SparqlResultSet results = new SparqlResultSet(); parser.Load(results, "test.srx"); NTriplesFormatter formatter = new NTriplesFormatter(); foreach (SparqlResult r in results) { Console.WriteLine(r.ToString(formatter)); } Assert.IsFalse(results.IsEmpty, "Result Set should not be empty"); Assert.AreEqual(SparqlResultsType.VariableBindings, results.ResultsType, "Results Type should be VariableBindings"); }
public void WritingSparqlXmlWithNulls() { TripleStore store = new TripleStore(); store.Add(new Graph()); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/graph"); store.Add(g); Object results = store.ExecuteQuery("SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o } }"); if (results is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)results; SparqlXmlWriter writer = new SparqlXmlWriter(); writer.Save(rset, "temp.srx"); SparqlXmlParser parser = new SparqlXmlParser(); SparqlResultSet rset2 = new SparqlResultSet(); parser.Load(rset2, "temp.srx"); rset.Trim(); Console.WriteLine("Original Results"); TestTools.ShowResults(rset); Console.WriteLine(); rset2.Trim(); Console.WriteLine("Serializes and Parsed Results"); TestTools.ShowResults(rset2); Console.WriteLine(); Assert.AreEqual(rset, rset2, "Result Sets should be equal"); } else { Assert.Fail("Query did not return a Result Set as expected"); } }
private void CompareResultGraphs(string results, string expectedResultsPath, bool reduced) { var rdfParser = new SparqlRdfParser(); var xmlParser = new SparqlXmlParser(); var actualResultSet = new SparqlResultSet(); var expectedResultSet = new SparqlResultSet(); using(var tr = new StringReader(results)) { xmlParser.Load(actualResultSet, tr); } rdfParser.Load(expectedResultSet, expectedResultsPath); CompareSparqlResults(actualResultSet, expectedResultSet, reduced); }
public static void Main(string[] args) { StreamWriter output = new StreamWriter("RemoteSPARQLTestSuite.txt",false,Encoding.UTF8); String[] skipTests = { }; Console.SetOut(output); output.WriteLine("### Running Remote SPARQL Endpoint Tests"); output.WriteLine(); output.WriteLine("Accept Header for SPARQL SELECT and ASK: " + MimeTypesHelper.HttpSparqlAcceptHeader); output.WriteLine("Accept Header for SPARQL DESCRIBE and CONSTRUCT: " + MimeTypesHelper.HttpAcceptHeader); output.WriteLine(); //Options.HttpDebugging = true; try { SparqlRemoteEndpoint dbpedia = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlResultSet results = new SparqlResultSet(); String[] queries = new String[3]; queries[0] = "select distinct ?Concept where {[] a ?Concept} limit 10"; queries[1] = "select distinct ?Concept where {[] a ?Concept} limit 10 offset 5"; queries[2] = "prefix skos: <http://www.w3.org/2004/02/skos/core#> select distinct ?City where {?City skos:subject <http://dbpedia.org/resource/Category:Cities_in_England>}"; foreach (String query in queries) { output.WriteLine("## Making a SPARQL SELECT Query"); output.WriteLine("# Query"); output.WriteLine(); output.WriteLine(query); output.WriteLine(); output.WriteLine("# Results"); results = dbpedia.QueryWithResultSet(query); foreach (SparqlResult result in results) { output.WriteLine(result.ToString()); } output.WriteLine(); } //Options.HttpFullDebugging = true; String gquery = "DESCRIBE <http://dbpedia.org/resource/Southampton>"; output.WriteLine("## Making a SPARQL DESCRIBE Query"); output.WriteLine("# Query"); output.WriteLine(); output.WriteLine(gquery); output.WriteLine(); output.WriteLine("# Results"); IGraph g = dbpedia.QueryWithResultGraph(gquery); foreach (Triple t in g.Triples) { output.WriteLine(t.ToString()); } } catch (XmlException xmlEx) { reportError(output, "XML Exception", xmlEx); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } //Options.HttpDebugging = false; output.WriteLine(); output.WriteLine("### Running Federated SPARQL Test"); try { SparqlRemoteEndpoint dbpedia = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlRemoteEndpoint bbcProgs = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://www.bbc.co.uk/programmes"); SparqlRemoteEndpoint books = new SparqlRemoteEndpoint(new Uri("http://sparql.org/books")); String fedQuery = "SELECT * WHERE {?s a ?type} LIMIT 10"; FederatedSparqlRemoteEndpoint fedEndpoint = new FederatedSparqlRemoteEndpoint(new SparqlRemoteEndpoint[] { dbpedia, bbcProgs/*, books*/ }); fedEndpoint.MaxSimultaneousRequests = 1; SparqlResultSet results = fedEndpoint.QueryWithResultSet(fedQuery); foreach (SparqlResult result in results) { output.WriteLine(result.ToString()); } output.WriteLine(); } catch (XmlException xmlEx) { reportError(output, "XML Exception", xmlEx); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } output.WriteLine(); output.WriteLine("### Running Result Set Parser Tests"); try { int testsPassed = 0; int testsFailed = 0; String[] files = Directory.GetFiles("sparql_tests"); bool passed, passDesired; SparqlResultSet results = new SparqlResultSet(); SparqlXmlParser parser = new SparqlXmlParser(); foreach (String file in files) { if (skipTests.Contains(Path.GetFileName(file))) { output.WriteLine("## Skipping Test of File " + Path.GetFileName(file)); output.WriteLine(); continue; } if (Path.GetExtension(file) != ".srx") { continue; } Debug.WriteLine("Testing File " + Path.GetFileName(file)); output.WriteLine("## Testing File " + Path.GetFileName(file)); output.WriteLine("# Test Started at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat)); passed = false; passDesired = true; try { if (Path.GetFileNameWithoutExtension(file).StartsWith("bad")) { passDesired = false; output.WriteLine("# Desired Result = Parsing Failed"); } else { output.WriteLine("# Desired Result = Parses OK"); } results = new SparqlResultSet(); parser.Load(results, file); passed = true; output.WriteLine("Parsed OK"); } catch (XmlException xmlEx) { reportError(output, "XML Exception", xmlEx); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } finally { if (passed && passDesired) { //Passed and we wanted to Pass testsPassed++; output.WriteLine("# Result = Test Passed"); } else if (!passed && passDesired) { //Failed when we should have Passed testsFailed++; output.WriteLine("# Result = Test Failed"); } else if (passed && !passDesired) { //Passed when we should have Failed testsFailed++; output.WriteLine("# Result = Test Failed"); } else { //Failed and we wanted to Fail testsPassed++; output.WriteLine("# Result = Test Passed"); } output.WriteLine("# Results Generated = " + results.Count); output.WriteLine("# Query Result was " + results.Result); output.WriteLine("# Test Ended at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat)); } output.WriteLine(); } output.WriteLine(testsPassed + " Tests Passed"); output.WriteLine(testsFailed + " Tests Failed"); } catch (Exception ex) { reportError(output, "Other Exception", ex); } Console.SetOut(Console.Out); Console.WriteLine("Done"); Debug.WriteLine("Finished"); output.Close(); }
private void CompareResultGraphs(string results, string expectedResultsPath, bool reduced) { var expectedResultGraph = new Graph(); FileLoader.Load(expectedResultGraph, expectedResultsPath); var resultSet = expectedResultGraph.GetUriNode(new Uri("http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet")); if (resultSet != null) { var rdfParser = new SparqlRdfParser(); var xmlParser = new SparqlXmlParser(); var actualResultSet = new SparqlResultSet(); var expectedResultSet = new SparqlResultSet(); using (var tr = new StringReader(results)) { xmlParser.Load(actualResultSet, tr); } rdfParser.Load(expectedResultSet, expectedResultsPath); var bnodeMap = new Dictionary<string, string>(); CompareSparqlResults(actualResultSet, expectedResultSet, reduced, bnodeMap); } else { // This is a constructed graph var actualGraph = new Graph(); actualGraph.LoadFromString(results); CompareTripleCollections(actualGraph.Triples, expectedResultGraph.Triples, reduced); } }
private void CompareSparqlResults(string results, string expectedResultsPath, bool reduced, ISparqlResultsReader resultsReader = null) { if (resultsReader == null) { resultsReader = new SparqlXmlParser(); } var actualResultSet = new SparqlResultSet(); using (var tr = new StringReader(results)) { resultsReader.Load(actualResultSet, tr); } var expectedResultSet = new SparqlResultSet(); resultsReader.Load(expectedResultSet, expectedResultsPath); var bnodeMap = new Dictionary<string, string>(); CompareSparqlResults(actualResultSet, expectedResultSet, reduced, bnodeMap); }
private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List<String> dataFiles, String resultFile) { Console.WriteLine("# Processing Evaluation Test " + Path.GetFileName(queryFile)); if (commentDef != null) { Console.WriteLine(commentDef.Object.ToString()); Console.WriteLine(); } if (dataFiles.Contains(dataFile)) dataFiles.Remove(dataFile); if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8); if (dataFile != null && dataFile.StartsWith("file:///")) dataFile = dataFile.Substring(8); if (resultFile.StartsWith("file:///")) resultFile = resultFile.Substring(8); Console.WriteLine("Query File is " + queryFile); if (evaluationTestOverride.Any(x => queryFile.EndsWith(x))) { Console.WriteLine(); Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)"); testsPassed++; testsEvaluationPassed++; return 1; } if (dataFile != null) Console.WriteLine("Default Graph File is " + dataFile); foreach (String file in dataFiles) { Console.WriteLine("Uses Named Graph File " + file); } Console.WriteLine("Expected Result File is " + resultFile); Console.WriteLine(); SparqlQuery query; try { query = parser.ParseFromFile(queryFile); Console.WriteLine(query.ToString()); Console.WriteLine(); Console.WriteLine("Formatted with SparqlFormatter"); SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap); Console.WriteLine(formatter.Format(query)); Console.WriteLine(); try { Console.WriteLine(query.ToAlgebra().ToString()); Console.WriteLine(); } catch { //Do Nothing } } catch (RdfParseException parseEx) { this.ReportError("Query Parser Error", parseEx); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result = Unable to parse query (Test Failed)"); return -1; } IInMemoryQueryableStore store; if (dataFile != null) { store = new TripleStore(); } else { store = new WebDemandTripleStore(); } //Load Default Graph Graph defaultGraph = new Graph(); try { if (dataFile != null) { FileLoader.Load(defaultGraph, dataFile); } store.Add(defaultGraph); } catch (RdfParseException parseEx) { this.ReportError("Parser Error", parseEx); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result = Unable to parse Default Graph (Test Failed)"); return -1; } //Load Named Graphs try { foreach (String graphFile in dataFiles) { Graph namedGraph = new Graph(); if (graphFile.StartsWith("file:///")) { FileLoader.Load(namedGraph, graphFile.Substring(8)); } else { FileLoader.Load(namedGraph, graphFile); } store.Add(namedGraph); } } catch (RdfParseException parseEx) { this.ReportError("Parser Error", parseEx); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Unable to parse Named Graph (Test Failed)"); return -1; } //Create a Dataset and then Set Graphs InMemoryDataset dataset = new InMemoryDataset(store); if (!query.DefaultGraphs.Any()) { query.AddDefaultGraph(defaultGraph.BaseUri); //dataset.SetActiveGraph(defaultGraph.BaseUri); } if (!query.NamedGraphs.Any()) { foreach (String namedGraphUri in dataFiles) { query.AddNamedGraph(new Uri(namedGraphUri)); } } //Try and get the result Object results = null; try { results = query.Evaluate(dataset); } catch (RdfQueryException queryEx) { this.ReportError("Query Error", queryEx); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Query execution failed (Test Failed)"); return -1; } catch (Exception ex) { this.ReportError("Other Error", ex); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Query failed (Test Failed)"); return -1; } if (results == null) { testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - No result was returned from the Query (Test Failed)"); return -1; } //Load in the expected results if (results is SparqlResultSet) { //Save our Results so we can manually compare as needed SparqlResultSet ourResults = (SparqlResultSet)results; SparqlXmlWriter writer = new SparqlXmlWriter(); writer.Save(ourResults, resultFile + ".out"); SparqlResultSet expectedResults = new SparqlResultSet(); if (resultFile.EndsWith(".srx")) { try { SparqlXmlParser resultSetParser = new SparqlXmlParser(); resultSetParser.Load(expectedResults, resultFile); } catch (RdfParseException parseEx) { this.ReportError("Result Set Parser Error", parseEx); testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)"); return 0; } } else if (resultFile.EndsWith(".ttl") || resultFile.EndsWith(".rdf")) { try { SparqlRdfParser resultSetParser = new SparqlRdfParser(); resultSetParser.Load(expectedResults, resultFile); } catch (RdfParseException parseEx) { this.ReportError("Result Set Parser Error", parseEx); testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)"); return 0; } } else { testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Unable to load the expected Result Set (Test Indeterminate)"); return 0; } try { ourResults.Trim(); expectedResults.Trim(); if (ourResults.Equals(expectedResults)) { testsPassed++; testsEvaluationPassed++; Console.WriteLine("# Test Result - Result Set as expected (Test Passed)"); return 1; } else { Console.WriteLine("Final Query"); Console.WriteLine(query.ToString()); Console.WriteLine(); this.ShowTestData(store); this.ShowResultSets(ourResults, expectedResults); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Result Set not as expected (Test Failed)"); return -1; } } catch (NotImplementedException) { this.ShowResultSets(ourResults, expectedResults); testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Unable to establish if Result Set was as expected (Test Indeterminate)"); return 0; } } else if (results is Graph) { if (resultFile.EndsWith(".ttl")) { //Save our Results so we can manually compare as needed Graph ourResults = (Graph)results; CompressingTurtleWriter writer = new CompressingTurtleWriter(); writer.Save(ourResults, resultFile + ".out"); try { Graph expectedResults = new Graph(); TurtleParser ttlparser = new TurtleParser(); ttlparser.Load(expectedResults, resultFile); try { if (ourResults.Equals(expectedResults)) { testsPassed++; testsEvaluationPassed++; Console.WriteLine("# Test Result - Graph as expected (Test Passed)"); return 1; } else { this.ShowTestData(store); this.ShowGraphs(ourResults, expectedResults); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Graph not as expected (Test Failed)"); return -1; } } catch (NotImplementedException) { this.ShowGraphs(ourResults, expectedResults); testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Unable to establish if Graph was as expected (Test Indeterminate)"); return 0; } } catch (RdfParseException parseEx) { this.ReportError("Graph Parser Error", parseEx); testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Error loading expected Graph (Test Indeterminate)"); return 0; } } else { testsIndeterminate++; testsEvaluationIndeterminate++; Console.WriteLine("# Test Result - Unable to load expected Graph (Test Indeterminate)"); return 0; } } else { testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Didn't produce a Graph as expected (Test Failed)"); return -1; } }
private void CompareResultGraphs(string results, string expectedResultsPath, bool reduced) { var expectedResultGraph = new Graph(); FileLoader.Load(expectedResultGraph, expectedResultsPath); var resultSet = expectedResultGraph.GetUriNode(new Uri("http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet")); if (resultSet != null) { var rdfParser = new SparqlRdfParser(); var xmlParser = new SparqlXmlParser(); var actualResultSet = new SparqlResultSet(); var expectedResultSet = new SparqlResultSet(); using (var tr = new StringReader(results)) { xmlParser.Load(actualResultSet, tr); } rdfParser.Load(expectedResultSet, expectedResultsPath); var bnodeMap = new Dictionary<string, string>(); CompareSparqlResults(actualResultSet, expectedResultSet, reduced, bnodeMap); } else { // This is a constructed graph var actualGraph = new Graph(); actualGraph.LoadFromString(results); var toReplace = actualGraph.Triples.Where( t => IsGenid(t.Subject) || IsGenid(t.Predicate) || IsGenid(t.Object)).ToList(); foreach (var t in toReplace) { var mapped = MapGenidToBlank(t); actualGraph.Retract(t); actualGraph.Assert(mapped); } CompareTripleCollections(actualGraph.Triples, expectedResultGraph.Triples, reduced); } }
public void SparqlBNodeIDsInResults() { try { SparqlXmlParser xmlparser = new SparqlXmlParser(); SparqlResultSet results = new SparqlResultSet(); xmlparser.Load(results, "bnodes.srx"); TestTools.ShowResults(results); Assert.AreEqual(results.Results.Distinct().Count(), 1, "All Results should be the same as they should all generate same BNode"); SparqlJsonParser jsonparser = new SparqlJsonParser(); results = new SparqlResultSet(); jsonparser.Load(results, "bnodes.json"); TestTools.ShowResults(results); Assert.AreEqual(results.Results.Distinct().Count(), 1, "All Results should be the same as they should all generate same BNode"); } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } }
public void SparqlJsonResultSet() { Console.WriteLine("Tests that JSON Parser parses language specifiers correctly"); String query = "PREFIX rdfs: <" + NamespaceMapper.RDFS + ">\nSELECT DISTINCT ?comment WHERE {?s rdfs:comment ?comment}"; TripleStore store = new TripleStore(); Graph g = new Graph(); FileLoader.Load(g, "json.owl"); store.Add(g); Object results = store.ExecuteQuery(query); if (results is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)results; //Serialize to both XML and JSON Results format SparqlXmlWriter xmlwriter = new SparqlXmlWriter(); xmlwriter.Save(rset, "results.xml"); SparqlJsonWriter jsonwriter = new SparqlJsonWriter(); jsonwriter.Save(rset, "results.json"); //Read both back in SparqlXmlParser xmlparser = new SparqlXmlParser(); SparqlResultSet r1 = new SparqlResultSet(); xmlparser.Load(r1, "results.xml"); Console.WriteLine("Result Set after XML serialization and reparsing contains:"); foreach (SparqlResult r in r1) { Console.WriteLine(r.ToString()); } Console.WriteLine(); SparqlJsonParser jsonparser = new SparqlJsonParser(); SparqlResultSet r2 = new SparqlResultSet(); jsonparser.Load(r2, "results.json"); Console.WriteLine("Result Set after JSON serialization and reparsing contains:"); foreach (SparqlResult r in r2) { Console.WriteLine(r.ToString()); } Console.WriteLine(); Assert.AreEqual(r1, r2, "Results Sets should be equal"); Console.WriteLine("Result Sets were equal as expected"); } else { Assert.Fail("Query did not return a Result Set"); } }
public void SparqlResultSetEquality() { SparqlXmlParser parser = new SparqlXmlParser(); SparqlRdfParser rdfparser = new SparqlRdfParser(); SparqlResultSet a = new SparqlResultSet(); SparqlResultSet b = new SparqlResultSet(); parser.Load(a, "list-3.srx"); parser.Load(b, "list-3.srx.out"); a.Trim(); b.Trim(); Assert.IsTrue(a.Equals(b)); a = new SparqlResultSet(); b = new SparqlResultSet(); parser.Load(a, "no-distinct-opt.srx"); parser.Load(b, "no-distinct-opt.srx.out"); a.Trim(); b.Trim(); Assert.IsTrue(a.Equals(b)); a = new SparqlResultSet(); b = new SparqlResultSet(); rdfparser.Load(a, "result-opt-3.ttl"); parser.Load(b, "result-opt-3.ttl.out"); a.Trim(); b.Trim(); Assert.IsTrue(a.Equals(b)); }