private static QuerySolutionMap querySolution(String varName, RDFNode value) { QuerySolutionMap qsm = new QuerySolutionMap() ; querySolution(qsm, varName, value) ; return qsm ; }
public SparqlSelectFunction<T> addInitialBinding(String varName, RDFNode value) { if (this.initialBinding == null) { this.initialBinding = new QuerySolutionMap(); } this.initialBinding.add(varName, value); return this; }
/** * Calls a given SPARQL function with no arguments. * * @param function the URI resource of the function to call * @param dataset the Dataset to operate on or null for default * @return the result of the function call */ public static Node invokeFunction0(Resource function, Dataset dataset) { final String expression = "<" + function + ">()"; QuerySolutionMap initialBinding = new QuerySolutionMap(); return invokeExpression(expression, initialBinding, dataset); }
String q = "select * where {?s ?p \"foo\"}"; QuerySolutionMap qsm = new QuerySolutionMap(); qsm.bind( "p", DC.creator ); Query query = QueryFactory.create( q ); QueryExecution exec = QueryExecutionFactory.create( query, model, qsm ); ResultSet rs = exec.execSelect();
public WonMessageQueryBuilder senderConnection(URI senderConnection) { QuerySolutionMap initialBinding = new QuerySolutionMap(); initialBinding.add("senderConnection", new ResourceImpl(senderConnection.toString())); delegate.addInitialBindings(initialBinding); return this; }
public static List<Resource> findRootsByType(Model model, Resource atype) { String s = String.join("\n", "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>", "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>", "SELECT DISTINCT ?root { { ?root rdf:type ?ATYPE } UNION { ?root rdf:type ?t . ?t rdfs:subClassOf ?ATYPE } }") ; Query q = QueryFactory.create(s) ; QuerySolutionMap qsm = new QuerySolutionMap() ; qsm.add("ATYPE", atype) ; try(QueryExecution qExec = QueryExecutionFactory.create(q, model, qsm)) { return ListUtils.toList( QueryExecUtils.getAll(qExec, "root").stream().map(r->(Resource)r)); } }
public static Resource findRootByType(Model model, Resource atype) { String s = String.join("\n", "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>", "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>", "SELECT DISTINCT ?root { { ?root rdf:type ?ATYPE } UNION { ?root rdf:type ?t . ?t rdfs:subClassOf ?ATYPE } }") ; Query q = QueryFactory.create(s) ; QuerySolutionMap qsm = new QuerySolutionMap() ; qsm.add("ATYPE", atype) ; try(QueryExecution qExec = QueryExecutionFactory.create(q, model, qsm)) { return (Resource)QueryExecUtils.getAtMostOne(qExec, "root") ; } }
private static QuerySolutionMap binding(final String var, final RDFNode value) { final QuerySolutionMap b = new QuerySolutionMap(); b.add(var, value); return b; }
public static Node invokeFunction3(Resource function, RDFNode argument1, RDFNode argument2, RDFNode argument3, Dataset dataset) { final String expression = "<" + function + ">(?arg1, ?arg2, ?arg3)"; QuerySolutionMap initialBinding = new QuerySolutionMap(); initialBinding.add("arg1", argument1); if(argument2 != null) { initialBinding.add("arg2", argument2); } if(argument3 != null) { initialBinding.add("arg3", argument3); } return invokeExpression(expression, initialBinding, dataset); }
public static Node invokeFunction3(Resource function, RDFNode argument1, RDFNode argument2, RDFNode argument3, Dataset dataset) { final String expression = "<" + function + ">(?arg1, ?arg2, ?arg3)"; QuerySolutionMap initialBinding = new QuerySolutionMap(); initialBinding.add("arg1", argument1); if(argument2 != null) { initialBinding.add("arg2", argument2); } if(argument3 != null) { initialBinding.add("arg3", argument3); } return invokeExpression(expression, initialBinding, dataset); }
@Test public void testInitialBindings1() { QueryExecution qExec = makeQExec("SELECT * {?s ?p ?o}") ; QuerySolutionMap init = new QuerySolutionMap() ; init.add("o", m.createLiteral("y1")); qExec.setInitialBinding(init) ; int count = queryAndCount(qExec) ; assertEquals("Initial binding didn't restrict query properly", 1, count) ; }
@Test public void testInitialBindings2() { QueryExecution qExec = makeQExec("SELECT * {?s ?p ?o}") ; QuerySolutionMap init = new QuerySolutionMap() ; init.add("z", m.createLiteral("zzz")); qExec.setInitialBinding(init) ; int count = queryAndCount(qExec) ; assertEquals("Initial binding restricted query improperly", 3, count) ; }
@Test public void test_param_string_constructor_4() { // Test constructor with predefined parameters QuerySolutionMap map = new QuerySolutionMap(); Resource r = ResourceFactory.createResource("http://example.org"); map.add("s", r); ParameterizedSparqlString query = new ParameterizedSparqlString("", map); Assert.assertEquals(r.asNode(), query.getParam("s")); }
@Test public void test_param_string_constructor_5() { // Test constructor with predefined parameters - variant of constructor // that does not require command text QuerySolutionMap map = new QuerySolutionMap(); Resource r = ResourceFactory.createResource("http://example.org"); map.add("s", r); ParameterizedSparqlString query = new ParameterizedSparqlString(map); Assert.assertEquals(r.asNode(), query.getParam("s")); }
@Test public void testInitialBindings3() { try(QueryExecution qExec = makeQExec("SELECT * {?s ?p 'x1'}")) { QuerySolutionMap init = new QuerySolutionMap() ; init.add("z", m.createLiteral("zzz")); qExec.setInitialBinding(init) ; ResultSet rs = qExec.execSelect() ; QuerySolution qs = rs.nextSolution() ; assertTrue("Initial setting not set correctly now", qs.getLiteral("z").getLexicalForm().equals("zzz")) ; } }
@Test public void testInitialBindings7() { // JENA-1354 Query query = QueryFactory.create("SELECT DISTINCT ?x WHERE {}"); Dataset ds = DatasetFactory.create(); QuerySolutionMap initialBinding = new QuerySolutionMap(); initialBinding.add("a", ResourceFactory.createTypedLiteral(Boolean.TRUE)); try ( QueryExecution qexec = QueryExecutionFactory.create(query, ds, initialBinding) ) { assertFalse(qexec.execSelect().next().contains("a")); } }
@Test public void testInitialBindingsConstruct1() { try(QueryExecution qExec = makeQExec("CONSTRUCT {?s ?p ?z} {?s ?p 'x1'}")) { QuerySolutionMap init = new QuerySolutionMap() ; init.add("z", m.createLiteral("zzz")); qExec.setInitialBinding(init) ; 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("z"))) ; } }
@Test public void test_param_string_constructor_6() { // Test constructor with predefined parameters QuerySolutionMap map = new QuerySolutionMap(); Resource r = ResourceFactory.createResource("http://example.org"); map.add("s", r); Literal l = ResourceFactory.createPlainLiteral("example"); map.add("o", l); ParameterizedSparqlString query = new ParameterizedSparqlString("", map); Assert.assertEquals(r.asNode(), query.getParam("s")); Assert.assertEquals(l.asNode(), query.getParam("o")); }
@Test public void test_param_string_constructor_7() { // Test constructor with predefined parameters - variant of constructor // that does not require command text QuerySolutionMap map = new QuerySolutionMap(); Resource r = ResourceFactory.createResource("http://example.org"); map.add("s", r); Literal l = ResourceFactory.createPlainLiteral("example"); map.add("o", l); ParameterizedSparqlString query = new ParameterizedSparqlString(map); Assert.assertEquals(r.asNode(), query.getParam("s")); Assert.assertEquals(l.asNode(), query.getParam("o")); }
@Test public void testInitialBindingsConstruct2() { try(QueryExecution qExec = makeQExec("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }")) { QuerySolutionMap init = new QuerySolutionMap() ; init.add("o", m.createLiteral("x1")); qExec.setInitialBinding(init) ; 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"))) ; } }