public void SparqlGroupByAssignmentSimple2() { String query = "SELECT ?x (COUNT(?p) AS ?predicates) WHERE { ?s ?p ?o } GROUP BY ?s AS ?x"; SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromString(query); SparqlFormatter formatter = new SparqlFormatter(); Console.WriteLine(formatter.Format(q)); Console.WriteLine(); QueryableGraph g = new QueryableGraph(); FileLoader.Load(g, "InferenceTest.ttl"); Object results = g.ExecuteQuery(q); if (results is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)results; TestTools.ShowResults(rset); Assert.IsTrue(rset.All(r => r.HasValue("x") && !r.HasValue("s") && r.HasValue("predicates")), "All Results should have a ?x and ?predicates variables and no ?s variable"); } else { Assert.Fail("Didn't get a Result Set as expected"); } }
public void SparqlGroupByWithValues2() { String query = @"SELECT ?a ?b WHERE { VALUES ( ?a ?b ) { ( 1 2 ) ( 1 UNDEF ) ( UNDEF 2 ) } } GROUP BY ?a ?b"; QueryableGraph g = new QueryableGraph(); SparqlResultSet results = g.ExecuteQuery(query) as SparqlResultSet; Assert.NotNull(results); Assert.False(results.IsEmpty); Assert.Equal(3, results.Count); }
public void ClassQuery() { var graphSource = new GraphSource("http://localhost:7200/repositories/Pets"); var graphProvider = new GraphProvider <SparqlBgpEvaluator>(graphSource); var classGraph = (new LabelledTreeNode <object, Term>(new Variable())).AddChild(new Rdfs("subClassOf"), new Variable()); var query = new QueryableGraph(graphProvider, new GraphExpression(classGraph)); int count = 0; foreach (var item in query) { count++; } Assert.Equal(291, count); }
public void LeftJoinTest() { var graphSource = new GraphSource("http://localhost:7200/repositories/Pets"); var graphProvider = new GraphProvider <SparqlBgpEvaluator>(graphSource); var propertyGraph = (new LabelledTreeNode <object, Term>(new Variable())) .AddChild(new Rdf("type"), new Rdf("Property")); var propertyQuery = new QueryableGraph(graphProvider, new GraphExpression(propertyGraph)); var classGraph = (new LabelledTreeNode <object, Term>(new Variable())) .AddChild(new Rdfs("subClassOf"), new Variable()); var classQuery = new QueryableGraph(graphProvider, new GraphExpression(classGraph)); var query = propertyQuery.Expand(new Rdfs("range"), classQuery); int count = 0; foreach (var item in query) { count++; Debug.WriteLine(item); } Assert.Equal(79, count); }
public void SparqlSimplePropertyPathDuplicates() { QueryableGraph g = new QueryableGraph(); FileLoader.Load(g, "property-path-duplicates.ttl"); String foafPrefix = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"; String twoStepQuery = foafPrefix + "SELECT ?x ?y WHERE { ?x foaf:knows ?z . ?z foaf:knows ?y }"; String pathQuery = foafPrefix + "SELECT ?x ?y WHERE {?x foaf:knows{2} ?y}"; Object resultsA = g.ExecuteQuery(twoStepQuery); Object resultsB = g.ExecuteQuery(pathQuery); Console.WriteLine("Two Step Query"); Console.WriteLine(); Console.WriteLine(twoStepQuery); TestTools.ShowResults(resultsA); Console.WriteLine(); Console.WriteLine("Equivalent Path Query"); Console.WriteLine(); Console.WriteLine(pathQuery); TestTools.ShowResults(resultsB); Console.WriteLine(); Assert.AreEqual(resultsA, resultsB, "Result Sets should have been equivalent"); }
public void SparqlGroupByAssignmentExpression3() { String query = "SELECT ?lang (SAMPLE(?o) AS ?example) WHERE { ?s ?p ?o . FILTER(ISLITERAL(?o)) } GROUP BY (LANG(?o) AS ?lang) HAVING LANGMATCHES(?lang, \"*\")"; SparqlQueryParser parser = new SparqlQueryParser(); SparqlQuery q = parser.ParseFromString(query); SparqlFormatter formatter = new SparqlFormatter(); Console.WriteLine(formatter.Format(q)); Console.WriteLine(); QueryableGraph g = new QueryableGraph(); UriLoader.Load(g, new Uri("http://dbpedia.org/resource/Southampton")); Object results = g.ExecuteQuery(q); if (results is SparqlResultSet) { SparqlResultSet rset = (SparqlResultSet)results; TestTools.ShowResults(rset); Assert.IsTrue(rset.All(r => r.HasValue("lang") && r.HasValue("example")), "All Results should have a ?lang and a ?example variable"); } else { Assert.Fail("Didn't get a Result Set as expected"); } }