use of org.apache.jena.query.QueryExecution in project jena by apache.
the class TestDatasetWithLuceneStoredLiterals method doTestSearchWithLiteralsMultiple.
protected List<Node> doTestSearchWithLiteralsMultiple(String turtle, String queryString, String expectedEntityURI) {
List<Node> literals = new LinkedList<>();
Model model = dataset.getDefaultModel();
Reader reader = new StringReader(turtle);
dataset.begin(ReadWrite.WRITE);
model.read(reader, "", "TURTLE");
dataset.commit();
Query query = QueryFactory.create(queryString);
dataset.begin(ReadWrite.READ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
int count;
for (count = 0; results.hasNext(); count++) {
QuerySolution soln = results.nextSolution();
String entityURI = soln.getResource("s").getURI();
assertEquals(expectedEntityURI, entityURI);
Literal literal = soln.getLiteral("literal");
assertNotNull(literal);
literals.add(literal.asNode());
}
} finally {
dataset.end();
}
return literals;
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class SelectBuilderTest method testVarReplacementInSubQuery.
@Test
public void testVarReplacementInSubQuery() throws ParseException {
Model m = ModelFactory.createDefaultModel();
Resource r = m.createResource("urn:one");
m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(1));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
r = m.createResource("urn:two");
m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(2));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(4));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(6));
SelectBuilder sb = new SelectBuilder().addVar("?x").addVar("?p").addWhere("?x", "?p", "?o").addFilter("?o < ?limit");
builder.addPrefix("xsd", XSD.getURI()).addVar("?x").addVar("count(?p)", "?c").addWhere("?x", "?p", "?o2").addSubQuery(sb).addGroupBy("?x");
builder.setVar("?limit", 4);
QueryExecution qexec = QueryExecutionFactory.create(builder.build(), m);
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
assertTrue(soln.contains("x"));
assertTrue(soln.contains("c"));
if ("urn:one".equals(soln.get("?x").asResource().getURI())) {
assertEquals(2, soln.get("?c").asLiteral().getInt());
} else {
assertEquals(1, soln.get("?c").asLiteral().getInt());
}
}
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class SelectBuilderTest method testAggregatorsInSubQuery.
@Test
public void testAggregatorsInSubQuery() throws ParseException {
Model m = ModelFactory.createDefaultModel();
Resource r = m.createResource("urn:one");
m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(1));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
r = m.createResource("urn:two");
m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(2));
m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(4));
m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(6));
SelectBuilder sb = new SelectBuilder().addVar("?x").addVar("max(?o)", "?max").addWhere("?x", "?p", "?o").addGroupBy("?x");
builder.addPrefix("xsd", XSD.getURI()).addVar("?x").addVar("min(?o2)", "?min").addWhere("?x", "?p2", "?o2").addSubQuery(sb).addFilter("?max = '6'^^xsd:int").addGroupBy("?x");
QueryExecution qexec = QueryExecutionFactory.create(builder.build(), m);
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
assertTrue(soln.contains("x"));
assertTrue(soln.contains("min"));
assertEquals("urn:two", soln.get("?x").asResource().getURI());
assertEquals(2, soln.get("?min").asLiteral().getInt());
}
}
use of org.apache.jena.query.QueryExecution in project jena by apache.
the class JenaOSGITest method runQuery.
private void runQuery(Dataset dataset) {
Query query = QueryFactory.create("" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "SELECT ?bob WHERE { " + " GRAPH <http://example.com/graph> { " + " ?alice foaf:knows ?bob . " + " }" + "}");
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
QuerySolution r = results.next();
assertEquals(bob, r.get("bob").asResource());
}
}
use of org.apache.jena.query.QueryExecution in project opentheso by miledrousset.
the class AlignmentQuery method queryAgrovoc.
/**
* Cette fonction permet de récupérer les alignements présents sur Agrovoc
* pour un concept passé en paramètre
*
* @param idC
* @param idTheso
* @param lexicalValue
* @param lang
* @return
*/
private ArrayList<NodeAlignment> queryAgrovoc(String idC, String idTheso, String lexicalValue, String lang, String source) {
listeAlign = new ArrayList<>();
lexicalValue = String.valueOf(lexicalValue.charAt(0)).toUpperCase() + lexicalValue.substring(1);
String sparqlQueryString1 = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " + "SELECT * WHERE {" + " ?uri skos:prefLabel ?pl ." + " FILTER(regex(?pl,\"" + lexicalValue + "*\"))" + " FILTER ( (lang(?pl)=\"" + lang + "\") )" + " OPTIONAL { " + " ?uri skos:scopeNote ?def ." + " FILTER ( (lang(?def)=\"" + lang + "\") )" + " }" + " }";
// System.out.println(sparqlQueryString1);
Query query = QueryFactory.create(sparqlQueryString1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("URL A DEFINIR", query);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.next();
NodeAlignment na = new NodeAlignment();
na.setInternal_id_concept(idC);
na.setInternal_id_thesaurus(idTheso);
na.setConcept_target(qs.get("pl").toString());
na.setDef_target(qs.get("def").toString());
na.setThesaurus_target(source);
na.setUri_target(qs.get("uri").toString());
listeAlign.add(na);
}
qexec.close();
return listeAlign;
}
Aggregations