private ISparqlDataset GetTestData() { InMemoryDataset dataset = new InMemoryDataset(); Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); dataset.AddGraph(g); return dataset; }
protected virtual ISparqlDataset GetNonEmptyDataset() { InMemoryDataset dataset = new InMemoryDataset(); Graph g = new Graph(); g.BaseUri = TestGraphUri; dataset.AddGraph(g); return dataset; }
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.IsFalse(q1.UsesDefaultDataset, "Query 1 should not be thread safe"); Assert.IsFalse(q2.UsesDefaultDataset, "Query 2 should not be thread safe"); Assert.IsFalse(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.Functions.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.AreEqual(g, gQuery, "Query 1 Result not as expected"); IGraph hQuery = d.EndInvoke(r2); Assert.AreEqual(h, hQuery, "Query 2 Result not as expected"); 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.IsFalse(iQuery.IsEmpty, "Graph should not be empty as INSERT DATA should have inserted a Triple"); Assert.AreNotEqual(new Graph(), iQuery, "Graphs should not be equal"); Assert.AreNotEqual(g, h, "Graphs should not be different"); }
private void SparqlQueryThreadSafeEvaluationActual() { 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 } }"; SparqlQuery q1 = this._parser.ParseFromString(query1); SparqlQuery q2 = this._parser.ParseFromString(query2); Assert.IsFalse(q1.UsesDefaultDataset, "Query 1 should not be thread safe"); Assert.IsFalse(q2.UsesDefaultDataset, "Query 2 should not be thread safe"); 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.Functions.LeviathanFunctionLibrary.ttl"); h.BaseUri = new Uri("http://example.org/2"); dataset.AddGraph(g); dataset.AddGraph(h); LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset); QueryWithGraphDelegate d = new QueryWithGraphDelegate(this.QueryWithGraph); IAsyncResult r1 = d.BeginInvoke(q1, processor, null, null); IAsyncResult r2 = d.BeginInvoke(q2, processor, null, null); WaitHandle.WaitAll(new WaitHandle[] { r1.AsyncWaitHandle, r2.AsyncWaitHandle }); IGraph gQuery = d.EndInvoke(r1); Assert.AreEqual(g, gQuery, "Query 1 Result not as expected"); IGraph hQuery = d.EndInvoke(r2); Assert.AreEqual(h, hQuery, "Query 2 Result not as expected"); Assert.AreNotEqual(g, h, "Graphs should not be different"); }
public void SparqlGraphClause4() { String query = "SELECT * WHERE { GRAPH ?g { ?s ?p ?o } }"; SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromString(query); InMemoryDataset dataset = new InMemoryDataset(false); IGraph ex = new Graph(); FileLoader.Load(ex, "InferenceTest.ttl"); ex.BaseUri = new Uri("http://example.org/graph"); dataset.AddGraph(ex); IGraph def = new Graph(); dataset.AddGraph(def); LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset); Object results = processor.ProcessQuery(q); if (results is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)results; TestTools.ShowResults(rset); Assert.AreEqual(ex.Triples.Count, rset.Count, "Number of Results should have been equal to number of Triples"); } else { Assert.Fail("Did not get a SPARQL Result Set as expected"); } }
public void SparqlUpdateDeleteDataCombination() { SparqlParameterizedString command = new SparqlParameterizedString(); command.Namespaces.AddNamespace("ex", new Uri("http://example.org/")); command.CommandText = "DELETE DATA { ex:a ex:b ex:c GRAPH <http://example.org/graph> { ex:a ex:b ex:c } }"; SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds = parser.ParseFromString(command); Assert.IsFalse(cmds.Commands.All(cmd => cmd.AffectsSingleGraph), "Commands should report that they do not affect a single Graph"); Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(null)), "Commands should report that they affect the Default Graph"); Assert.IsTrue(cmds.Commands.All(cmd => cmd.AffectsGraph(new Uri("http://example.org/graph"))), "Commands should report that they affect the named Graph"); InMemoryDataset dataset = new InMemoryDataset(); IGraph def = new Graph(); def.NamespaceMap.Import(command.Namespaces); def.Assert(new Triple(def.CreateUriNode("ex:a"), def.CreateUriNode("ex:b"), def.CreateUriNode("ex:c"))); dataset.AddGraph(def); IGraph ex = new Graph(); ex.BaseUri = new Uri("http://example.org/graph"); ex.NamespaceMap.Import(command.Namespaces); ex.Assert(new Triple(ex.CreateUriNode("ex:a"), ex.CreateUriNode("ex:b"), ex.CreateUriNode("ex:c"))); dataset.AddGraph(ex); LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); processor.ProcessCommandSet(cmds); Graph g = new Graph(); g.NamespaceMap.Import(command.Namespaces); Triple t = new Triple(g.CreateUriNode("ex:a"), g.CreateUriNode("ex:b"), g.CreateUriNode("ex:c")); def = dataset[null]; Assert.AreEqual(0, def.Triples.Count, "Should be 0 Triples in the Default Graph"); Assert.IsFalse(def.ContainsTriple(t), "Should not have the deleted Triple in the Default Graph"); ex = dataset[new Uri("http://example.org/graph")]; Assert.AreEqual(0, ex.Triples.Count, "Should be 0 Triples in the Named Graph"); Assert.IsFalse(ex.ContainsTriple(t), "Should not have the deleted Triple in the Named Graph"); }
private int ProcessUpdateEvaluationTest(IGraph manifest, INode testNode) { try { IUriNode utData = manifest.CreateUriNode("ut:data"); IUriNode utGraph = manifest.CreateUriNode("ut:graph"); IUriNode utGraphData = manifest.CreateUriNode("ut:graphData"); IUriNode rdfsLabel = manifest.CreateUriNode("rdfs:label"); //Get the test name and comment String name = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:name")).Select(t => t.Object).First().ToString(); String comment = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("rdfs:comment")).Select(t => t.Object).First().ToString(); //Get the test action and file INode actionNode = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:action")).Select(t => t.Object).First(); String updateFile = manifest.GetTriplesWithSubjectPredicate(actionNode, manifest.CreateUriNode("ut:request")).Select(t => t.Object).First().ToString(); Console.WriteLine("# Processing Update Evaluation Test " + updateFile); Console.WriteLine(name); Console.WriteLine(comment); Console.WriteLine(); if (evaluationTestOverride.Any(x => updateFile.EndsWith(x))) { Console.WriteLine(); Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)"); testsPassed++; testsEvaluationPassed++; return 1; } //Parse the Update SparqlUpdateParser parser = new SparqlUpdateParser(); SparqlUpdateCommandSet cmds; try { if (updateFile.StartsWith("file:///")) { updateFile = updateFile.Substring(8); } cmds = parser.ParseFromFile(updateFile); Console.WriteLine("Update Commands:"); Console.WriteLine(cmds.ToString()); } catch (Exception ex) { this.ReportError("Error Parsing Update Commands", ex); Console.WriteLine("# Test Result - Update Command failed to pass (Test Failed)"); testsEvaluationFailed++; testsFailed++; return -1; } //Build the Initial Dataset InMemoryDataset dataset = new InMemoryDataset(new TripleStore()); try { foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(actionNode, utData)) { Console.WriteLine("Uses Default Graph File " + t.Object.ToString()); Graph g = new Graph(); UriLoader.Load(g, ((IUriNode)t.Object).Uri); g.BaseUri = null; dataset.AddGraph(g); } foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(actionNode, utGraphData)) { Graph g = new Graph(); INode dataNode = manifest.GetTriplesWithSubjectPredicate(t.Object, utData).Concat(manifest.GetTriplesWithSubjectPredicate(t.Object, utGraph)).Select(x => x.Object).FirstOrDefault(); UriLoader.Load(g, ((IUriNode)dataNode).Uri); INode nameNode = manifest.GetTriplesWithSubjectPredicate(t.Object, rdfsLabel).Select(x => x.Object).FirstOrDefault(); g.BaseUri = new Uri(nameNode.ToString()); Console.WriteLine("Uses Named Graph File " + dataNode.ToString() + " named as " + nameNode.ToString()); dataset.AddGraph(g); } } catch (Exception ex) { this.ReportError("Error Building Initial Dataset", ex); Console.WriteLine("# Test Result - Unable to build Initial Dataset (Test Indeterminate)"); testsEvaluationIndeterminate++; testsIndeterminate++; return 0; } Console.WriteLine(); //Try running the Update try { LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(dataset); //Since all Tests assume that the WHERE is limited to the unnamed default graph unless specified //then must set this to be the Active Graph for the dataset dataset.SetDefaultGraph(dataset[null]); //Try the Update processor.ProcessCommandSet(cmds); dataset.ResetDefaultGraph(); } catch (SparqlUpdateException updateEx) { //TODO: Some Update tests might be to test cases where a failure should occur this.ReportError("Unexpected Error while performing Update", updateEx); Console.WriteLine("# Test Result - Update Failed (Test Failed)"); testsEvaluationFailed++; testsFailed++; return -1; } catch (Exception ex) { this.ReportError("Unexpected Error while performing Update", ex); Console.WriteLine("# Test Result - Update Failed (Test Failed)"); testsEvaluationFailed++; testsFailed++; return -1; } //Build the Result Dataset INode resultNode = manifest.GetTriplesWithSubjectPredicate(testNode, manifest.CreateUriNode("mf:result")).Select(t => t.Object).First(); InMemoryDataset resultDataset = new InMemoryDataset(new TripleStore()); try { foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(resultNode, utData)) { Console.WriteLine("Uses Result Default Graph File " + t.Object.ToString()); Graph g = new Graph(); UriLoader.Load(g, ((IUriNode)t.Object).Uri); g.BaseUri = null; resultDataset.AddGraph(g); } foreach (Triple t in manifest.GetTriplesWithSubjectPredicate(resultNode, utGraphData)) { Graph g = new Graph(); INode dataNode = manifest.GetTriplesWithSubjectPredicate(t.Object, utData).Concat(manifest.GetTriplesWithSubjectPredicate(t.Object, utGraph)).Select(x => x.Object).FirstOrDefault(); UriLoader.Load(g, ((IUriNode)dataNode).Uri); INode nameNode = manifest.GetTriplesWithSubjectPredicate(t.Object, rdfsLabel).Select(x => x.Object).FirstOrDefault(); g.BaseUri = new Uri(nameNode.ToString()); Console.WriteLine("Uses Result Named Graph File " + dataNode.ToString() + " named as " + nameNode.ToString()); resultDataset.AddGraph(g); } //Do this just to ensure that if the Result Dataset doesn't have a default unnamed graph it will //have one LeviathanUpdateProcessor processor = new LeviathanUpdateProcessor(resultDataset); } catch (Exception ex) { this.ReportError("Error Building Result Dataset", ex); Console.WriteLine("# Test Result - Unable to build Result Dataset (Test Indeterminate)"); testsEvaluationIndeterminate++; testsIndeterminate++; return 0; } Console.WriteLine(); //Now compare the two datasets to see if the tests passes foreach (Uri u in resultDataset.GraphUris) { if (dataset.HasGraph(u)) { if (!resultDataset[u].Equals(dataset[u])) { this.ShowGraphs(dataset[u], resultDataset[u]); Console.WriteLine("# Test Result - Expected Result Dataset Graph '" + this.ToSafeString(u) + "' is different from the Graph with that name in the Updated Dataset (Test Failed)"); testsEvaluationFailed++; testsFailed++; return -1; } } else { Console.WriteLine("# Test Result - Expected Result Dataset has Graph '" + this.ToSafeString(u) + "' which is not present in the Updated Dataset (Test Failed)"); testsEvaluationFailed++; testsFailed++; return -1; } } foreach (Uri u in dataset.GraphUris) { if (!resultDataset.HasGraph(u)) { Console.WriteLine("# Test Result - Updated Dataset has additional Graph '" + this.ToSafeString(u) + "' which is not present in the Expected Result Dataset (Test Failed)"); } } Console.WriteLine("# Test Result - Updated Dataset matches Expected Result Dataset (Test Passed)"); testsEvaluationPassed++; testsPassed++; return 1; } catch (Exception ex) { this.ReportError("Unexpected Error", ex); Console.WriteLine("# Test Result - Unexpected Error (Test Failed)"); testsEvaluationFailed++; testsFailed++; return -1; } }