AntecedentVisitor extends AbstractQueryModelVisitor<RuntimeException> { public Set<StatementPattern> getAntecedents() { return antecedentStatementPatterns; }  Set<StatementPattern> getAntecedents(); @Override void meet(StatementPattern sp);  }
@Test public void testSelectQuery() throws Exception { String text = "PREFIX foaf: <" + FOAF.NAMESPACE + ">\n" + "SELECT * WHERE {\n" + " ?x a foaf:Person .\n" + " ?y a foaf:Person .\n" + " ?x foaf:knows ?y .\n" + "}"; ParsedQuery query = new SPARQLParser().parseQuery(text, null); AntecedentVisitor visitor = new AntecedentVisitor(); query.getTupleExpr().visit(visitor); Set<StatementPattern> expected = Sets.newHashSet( new StatementPattern(new Var("x"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(new Var("y"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(new Var("x"), c(FOAF.KNOWS), new Var("y"))); Assert.assertEquals(expected, visitor.getAntecedents()); }
@Test public void testConstructQuery() throws Exception { String text = "PREFIX foaf: <" + FOAF.NAMESPACE + ">\n" + "CONSTRUCT {\n" + " ?y foaf:knows ?x .\n" + " ?y <urn:knows> ?x .\n" + " ?x <urn:knows> ?y .\n" + "} WHERE {\n" + " ?x a foaf:Person .\n" + " ?y a foaf:Person .\n" + " ?x foaf:knows ?y .\n" + "}"; ParsedQuery query = new SPARQLParser().parseQuery(text, null); AntecedentVisitor visitor = new AntecedentVisitor(); query.getTupleExpr().visit(visitor); Set<StatementPattern> expected = Sets.newHashSet( new StatementPattern(new Var("x"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(new Var("y"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(new Var("x"), c(FOAF.KNOWS), new Var("y"))); Assert.assertEquals(expected, visitor.getAntecedents()); }
@Test public void testComplexQuery() throws Exception { String text = "PREFIX foaf: <" + FOAF.NAMESPACE + ">\n" + "PREFIX ex: <" + EX + ">\n" + "SELECT * WHERE {\n" + " { ?x a foaf:Person } UNION {\n" + " GRAPH ex:Graph1 { ?y a foaf:Person }\n" + " } .\n" + " GRAPH ex:Graph2 {\n" + " ?x foaf:knows ?y .\n" + " }\n ." + " OPTIONAL { ?x foaf:mbox ?m } .\n" + " FILTER (?x != ?y) .\n" + "}"; ParsedQuery query = new SPARQLParser().parseQuery(text, null); AntecedentVisitor visitor = new AntecedentVisitor(); query.getTupleExpr().visit(visitor); Set<StatementPattern> expected = Sets.newHashSet( new StatementPattern(Scope.NAMED_CONTEXTS, new Var("y"), c(RDF.TYPE), c(FOAF.PERSON), c(G1)), new StatementPattern(new Var("x"), c(RDF.TYPE), c(FOAF.PERSON)), new StatementPattern(Scope.NAMED_CONTEXTS, new Var("x"), c(FOAF.KNOWS), new Var("y"), c(G2)), new StatementPattern(new Var("x"), c(FOAF.MBOX), new Var("m"))); Assert.assertEquals(expected, visitor.getAntecedents()); }
@Test public void testBNodeQuery() throws Exception { String text = "PREFIX foaf: <" + FOAF.NAMESPACE + ">\n" + "SELECT * WHERE {\n" + " ?x a [ rdfs:subClassOf foaf:Person ] .\n" + " ?x foaf:knows ?y .\n" + "}"; ParsedQuery query = new SPARQLParser().parseQuery(text, null); AntecedentVisitor visitor = new AntecedentVisitor(); query.getTupleExpr().visit(visitor); Set<StatementPattern> actual = visitor.getAntecedents(); Assert.assertEquals(3, actual.size()); StatementPattern knows = new StatementPattern(new Var("x"), c(FOAF.KNOWS), new Var("y")); Assert.assertTrue(actual.remove(knows)); Assert.assertTrue(actual.removeIf(sp -> { return sp.getSubjectVar().equals(new Var("x")) && RDF.TYPE.equals(sp.getPredicateVar().getValue()) && sp.getObjectVar().getValue() == null; })); Assert.assertTrue(actual.removeIf(sp -> { return sp.getSubjectVar().getValue() == null && RDFS.SUBCLASSOF.equals(sp.getPredicateVar().getValue()) && FOAF.PERSON.equals(sp.getObjectVar().getValue()); })); }
@Test public void testNoSP() throws Exception { String text = "CONSTRUCT {\n" + " owl:Thing a owl:Class ." + " owl:Nothing a owl:Class ." + " owl:Nothing rdfs:subClassOf owl:Thing ." + "} WHERE { }"; ParsedQuery query = new SPARQLParser().parseQuery(text, null); AntecedentVisitor visitor = new AntecedentVisitor(); query.getTupleExpr().visit(visitor); Set<StatementPattern> expected = Sets.newHashSet(); Assert.assertEquals(expected, visitor.getAntecedents()); }