StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("PREFIX foaf: <" + FOAF_NS + "> ");
queryBuilder.append("ASK ");
queryBuilder.append("{ ?p foaf:name ?name }");
BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
query.setBinding("name", nameBob);
assertTrue(query.evaluate());
DatasetImpl dataset = new DatasetImpl();
// default graph: {context1}
dataset.addDefaultGraph(context1);
query.setDataset(dataset);
assertTrue(query.evaluate());
// default graph: {context1, context2}
dataset.addDefaultGraph(context2);
query.setDataset(dataset);
assertTrue(query.evaluate());
// default graph: {context2}
dataset.removeDefaultGraph(context1);
query.setDataset(dataset);
assertFalse(query.evaluate());
queryBuilder.setLength(0);
queryBuilder.append("PREFIX foaf: <" + FOAF_NS + "> ");
queryBuilder.append("ASK ");
queryBuilder.append("{ GRAPH ?g { ?p foaf:name ?name } }");
query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
query.setBinding("name", nameBob);
// default graph: {context2}; named graph: {}
query.setDataset(dataset);
assertFalse(query.evaluate());
// default graph: {context1, context2}; named graph: {context2}
dataset.addDefaultGraph(context1);
dataset.addNamedGraph(context2);
query.setDataset(dataset);
assertFalse(query.evaluate());
// default graph: {context1, context2}; named graph: {context1, context2}
dataset.addNamedGraph(context1);
query.setDataset(dataset);
assertTrue(query.evaluate());
}