use of org.apache.jena.query.QuerySolutionMap in project jena by apache.
the class TestAPI method testInitialBindings5.
/**
* Initial binding substitution happens before optimization so initial bindings can make a semantically always false query into one that can return true
*/
@Test
public void testInitialBindings5() {
// From JENA-500
Query query = QueryFactory.create("ASK\n" + "WHERE {\n" + " FILTER (?a = <http://constant>) .\n" + "}");
// System.out.println(Algebra.optimize(Algebra.compile(query)).toString());
Model model = ModelFactory.createDefaultModel();
model.add(OWL.Thing, RDF.type, OWL.Class);
QuerySolutionMap initialBinding = new QuerySolutionMap();
initialBinding.add("a", ResourceFactory.createResource("http://constant"));
try (QueryExecution qExec = QueryExecution.model(model).query(query).initialBinding(initialBinding).build()) {
boolean result = qExec.execAsk();
assertTrue(result);
}
}
use of org.apache.jena.query.QuerySolutionMap in project rdf2neo by Rothamsted.
the class RdfDataManager method getCyNode.
/**
* Uses the underlining TDB and mapping queries to create a new {@link CyNode} instance.
*
* @param nodeRes the RDF/Jena resource correspnding to the Cypher node. This provides the ?iri paramter in the queries below.
* @param labelsSparql the node labels query, which is usually taken from {@link CyNodeLoadingHandler#getLabelsSparql()}.
* @param propsSparql the node properties query, which is usually taken from {@link CyNodeLoadingHandler#getNodePropsSparql()}.
*/
public CyNode getCyNode(Resource nodeRes, String labelsSparql, String propsSparql) {
ensureOpen();
Model model = this.dataSet.getDefaultModel();
QuerySolutionMap params = new QuerySolutionMap();
params.add("iri", nodeRes);
CyNode cyNode = new CyNode(nodeRes.getURI());
// The node's labels
if (labelsSparql != null) {
// If it's omitted, it will get the default label.
Query qry = this.queryCache.getUnchecked(labelsSparql);
Function<String, String> labelIdConverter = this.getCyNodeLabelIdConverter();
boolean wasInTnx = dataSet.isInTransaction();
if (!wasInTnx)
dataSet.begin(ReadWrite.READ);
try {
QueryExecution qx = QueryExecutionFactory.create(qry, model, params);
qx.execSelect().forEachRemaining(row -> cyNode.addLabel(this.getCypherId(row.get("label"), labelIdConverter)));
} finally {
if (!wasInTnx && dataSet.isInTransaction())
dataSet.end();
}
}
// and the properties
this.addCypherProps(cyNode, propsSparql);
return cyNode;
}
use of org.apache.jena.query.QuerySolutionMap in project jena by apache.
the class TestAPI method testInitialBindingsConstruct2.
@Test
public void testInitialBindingsConstruct2() {
QuerySolutionMap init = new QuerySolutionMap();
init.add("o", m.createLiteral("x1"));
String qs = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
Model r = qExec.execConstruct();
assertTrue("Empty model", r.size() > 0);
Property p1 = m.createProperty(ns + "p1");
assertTrue("Empty model", r.contains(null, p1, init.get("x1")));
}
}
use of org.apache.jena.query.QuerySolutionMap in project jena by apache.
the class TestAPI method testInitialBindings1.
@Test
public void testInitialBindings1() {
QuerySolutionMap init = new QuerySolutionMap();
init.add("o", m.createLiteral("y1"));
String qs = "SELECT * {?s ?p ?o}";
try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
int count = queryAndCount(qExec);
assertEquals("Initial binding didn't restrict query properly", 1, count);
}
}
use of org.apache.jena.query.QuerySolutionMap in project jena by apache.
the class TestAPI method testInitialBindings2.
@Test
public void testInitialBindings2() {
QuerySolutionMap init = new QuerySolutionMap();
init.add("z", m.createLiteral("zzz"));
String qs = "SELECT * {?s ?p ?o}";
try (QueryExecution qExec = QueryExecution.model(m).query(qs).initialBinding(init).build()) {
int count = queryAndCount(qExec);
assertEquals("Initial binding restricted query improperly", 3, count);
}
}
Aggregations