Created
May 24, 2024 09:50
-
-
Save fkleedorfer/71f805ddcfc9f2dc5676fe0775ed11d3 to your computer and use it in GitHub Desktop.
Reproducing the optional position bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.rdf4j.bugs; | |
import org.eclipse.rdf4j.model.vocabulary.RDF; | |
import org.eclipse.rdf4j.query.TupleQuery; | |
import org.eclipse.rdf4j.query.TupleQueryResult; | |
import org.eclipse.rdf4j.repository.RepositoryConnection; | |
import org.eclipse.rdf4j.repository.sail.SailRepository; | |
import org.eclipse.rdf4j.sail.memory.MemoryStore; | |
import static org.eclipse.rdf4j.model.util.Values.iri; | |
public class OptionalPositionBug { | |
/** | |
* Problem: position of optional clause influences result. A leading OPTIONAL causes the solution for which there | |
* is no binding for ?o to be omitted. | |
* | |
* Output: | |
* | |
* PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |
* PREFIX ex: <http://example.com/> | |
* SELECT ?s ?o | |
* WHERE { | |
* ?s rdf:type ex:Car . | |
* OPTIONAL { ?s ex:hasColor ?o . } | |
* } | |
* Result: | |
* [s=http://example.com/car1;o=http://example.com/blue] | |
* [s=http://example.com/car2] | |
* | |
* PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | |
* PREFIX ex: <http://example.com/> | |
* SELECT ?s ?o | |
* WHERE { | |
* OPTIONAL { ?s ex:hasColor ?o . } | |
* ?s rdf:type ex:Car . | |
* } | |
* Result: | |
* [s=http://example.com/car1;o=http://example.com/blue] | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
SailRepository repo = new SailRepository(new MemoryStore()); | |
RepositoryConnection con = repo.getConnection(); | |
con.add(iri("http://example.com/car1"), RDF.TYPE, iri("http://example.com/Car")); | |
con.add(iri("http://example.com/car2"), RDF.TYPE, iri("http://example.com/Car")); | |
con.add(iri("http://example.com/car1"), iri("http://example.com/hasColor"), iri("http://example.com/blue")); | |
con.commit(); | |
String queryWithTrailingOptionalStr = | |
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" | |
+ "PREFIX ex: <http://example.com/>\n" | |
+ "SELECT ?s ?o \n" | |
+ "WHERE { \n" | |
+ " ?s rdf:type ex:Car .\n" | |
+ " OPTIONAL { ?s ex:hasColor ?o . }\n" | |
+ "}"; | |
String queryWithLeadingOptionalStr = | |
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" | |
+ "PREFIX ex: <http://example.com/>\n" | |
+ "SELECT ?s ?o \n" | |
+ "WHERE { \n" | |
+ " OPTIONAL { ?s ex:hasColor ?o . }\n" | |
+ " ?s rdf:type ex:Car .\n" | |
+ "}"; | |
printResult(con, queryWithLeadingOptionalStr); | |
printResult(con, queryWithTrailingOptionalStr); | |
} | |
private static void printResult(RepositoryConnection con, String queryString) { | |
TupleQuery query = con.prepareTupleQuery(queryString); | |
System.out.println(queryString); | |
System.out.println("Result:"); | |
try(TupleQueryResult result = query.evaluate()){ | |
while(result.hasNext()) { | |
System.out.println(result.next()); | |
} | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment