public void ParsingResultSetHandlerImplicitSparqlJson() { this.EnsureTestData("test.srj"); SparqlJsonParser parser = new SparqlJsonParser(); SparqlResultSet results = new SparqlResultSet(); parser.Load(results, "test.srj"); 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 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); } }
private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List<String> dataFiles, String resultFile) { Console.WriteLine("# Processing Query 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); 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; } catch (Exception ex) { this.ReportError("Unexpected Parsing Error", ex); 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()) { if (defaultGraph.BaseUri != null) 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(".srj")) { try { SparqlJsonParser resultSetParser = new SparqlJsonParser(); 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; } 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"); SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap); Console.WriteLine(formatter.Format(query)); Console.WriteLine(); Console.WriteLine("# Graphs in Test Data"); foreach (Uri u in store.Graphs.GraphUris) { Console.WriteLine(this.ToSafeString(u)); } Console.WriteLine(); this.ShowTestData(store); this.ShowResultSets(ourResults, expectedResults); testsFailed++; testsEvaluationFailed++; Console.WriteLine("# Test Result - Result Set not as expected (Test Failed)"); return -1; } } 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; } }
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"); } }