URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
sc.addStatement(uriA, uriB, uriC);
sc.commit();
sc.begin();
SPARQLParser parser = new SPARQLParser();
BindingSet bindings = new EmptyBindingSet();
String baseURI = "http://example.org/bogus/";
String queryStr;
ParsedQuery query;
CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
int count;
// s ?p ?o SELECT
queryStr = "SELECT ?y ?z WHERE { <http://example.org/uriA> ?y ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI y = (URI) set.getValue("y");
Value z = (Value) set.getValue("z");
assertNotNull(y);
assertNotNull(z);
// System.out.println("y = " + y + ", z = " + z);
}
results.close();
assertTrue(count > 0);
// s p ?o SELECT using a namespace prefix
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
languages = new HashSet<String>();
while (results.hasNext()) {
count++;
BindingSet set = results.next();
Literal z = (Literal) set.getValue("z");
assertNotNull(z);
languages.add(z.getLanguage());
}
results.close();
assertTrue(count > 0);
assertEquals(2, languages.size());
assertTrue(languages.contains("en"));
assertTrue(languages.contains("is"));
// ?s p o SELECT using a plain literal value with no language tag
queryStr = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT ?s WHERE { ?s rdfs:comment \"he really knows where his towel is\" }";
URI fordUri = sail.getValueFactory().createURI(prefix + "ford");
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, fordUri);
}
results.close();
assertTrue(count > 0);
// ?s p o SELECT using a language-specific literal value
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\"@en }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, thorUri);
}
results.close();
assertTrue(count > 0);
// The language tag is necessary
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\" }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// ?s p o SELECT using a typed literal value
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\"^^xsd:string }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI s = (URI) set.getValue("s");
assertNotNull(s);
assertEquals(s, thorUri);
}
results.close();
assertTrue(count > 0);
// The data type is necessary
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\" }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();
}
results.close();
assertEquals(0, count);
// s ?p o SELECT
// TODO: commented out languages for now
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?p WHERE { <" + prefix + "thor> ?p \"Thor\"@en }";
query = parser.parseQuery(queryStr, baseURI);
URI foafNameUri = sail.getValueFactory().createURI("http://xmlns.com/foaf/0.1/name");
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
BindingSet set = results.next();
URI p = (URI) set.getValue("p");
assertNotNull(p);
assertEquals(p, foafNameUri);
}
results.close();
assertTrue(count > 0);
// context-specific SELECT
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <" + prefix + "ctx1>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
languages = new HashSet<String>();
while (results.hasNext()) {
count++;
BindingSet set = results.next();
Literal z = (Literal) set.getValue("z");
assertNotNull(z);
languages.add(z.getLanguage());
}
results.close();
assertTrue(count > 0);
assertEquals(2, languages.size());
assertTrue(languages.contains("en"));
assertTrue(languages.contains("is"));
queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <http://example.org/emptycontext>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
query = parser.parseQuery(queryStr, baseURI);
results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
count = 0;
while (results.hasNext()) {
count++;
results.next();