use of org.apache.jena.query.QuerySolution in project jena by apache.
the class TestTextMultilingualEnhancements method doTestSearchWithLiterals.
protected Map<String, Literal> doTestSearchWithLiterals(String queryString, Set<String> expectedEntityURIs) {
Map<String, Literal> literals = new HashMap<>();
Query query = QueryFactory.create(queryString);
dataset.begin(ReadWrite.READ);
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
assertEquals(expectedEntityURIs.size() > 0, results.hasNext());
int count;
for (count = 0; results.hasNext(); count++) {
QuerySolution soln = results.nextSolution();
String entityUri = soln.getResource("s").getURI();
assertTrue(expectedEntityURIs.contains(entityUri));
Literal literal = soln.getLiteral("lit");
assertNotNull(literal);
literals.put(entityUri, literal);
}
assertEquals(expectedEntityURIs.size(), count);
} finally {
dataset.end();
}
return literals;
}
use of org.apache.jena.query.QuerySolution 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.QuerySolution 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.QuerySolution in project jena by apache.
the class FusekiConfig method readSystemDatabase.
// ---- System database
/**
* Read the system database
*/
public static List<DataAccessPoint> readSystemDatabase(Dataset ds) {
// Webapp only.
DatasetDescriptionMap dsDescMap = new DatasetDescriptionMap();
String qs = StrUtils.strjoinNL(FusekiPrefixes.PREFIXES, "SELECT * {", " GRAPH ?g {", " ?s fu:name ?name;", " fu:status ?status .", " }", "}");
List<DataAccessPoint> refs = new ArrayList<>();
ds.begin(ReadWrite.WRITE);
try {
ResultSet rs = BuildLib.query(qs, ds);
for (; rs.hasNext(); ) {
QuerySolution row = rs.next();
Resource s = row.getResource("s");
Resource g = row.getResource("g");
Resource rStatus = row.getResource("status");
// String name = row.getLiteral("name").getLexicalForm();
DataServiceStatus status = DataServiceStatus.status(rStatus);
Model m = ds.getNamedModel(g.getURI());
// Rebase the resource of the service description to the containing graph.
Resource svc = m.wrapAsResource(s.asNode());
DataAccessPoint ref = buildDataAccessPoint(svc, dsDescMap);
if (ref != null)
refs.add(ref);
}
ds.commit();
return refs;
} catch (Throwable th) {
th.printStackTrace();
return refs;
} finally {
ds.end();
}
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class FusekiConfig method accEndpointOldStyle.
// Old style.
// fuseki:serviceQuery "sparql";
// or
// fuseki:serviceQuery [ fuseki:name "sparql" ; fuseki:allowedUsers (..) ];
private static void accEndpointOldStyle(Collection<Endpoint> endpoints, Operation operation, Resource svc, Property property) {
String p = "<" + property.getURI() + ">";
ResultSet rs = BuildLib.query("SELECT * { ?svc " + p + " ?ep}", svc.getModel(), "svc", svc);
for (; rs.hasNext(); ) {
QuerySolution soln = rs.next();
// No policy yet - set below if one is found.
AuthPolicy authPolicy = null;
RDFNode ep = soln.get("ep");
String endpointName = null;
if (ep.isLiteral())
// fuseki:serviceQuery "sparql"
endpointName = soln.getLiteral("ep").getLexicalForm();
else if (ep.isResource()) {
Resource r = (Resource) ep;
try {
// [ fuseki:name ""; fuseki:allowedUsers ( "" "" ) ]
Statement stmt = r.getProperty(FusekiVocab.pEndpointName);
if (stmt == null)
throw new FusekiConfigException("Expected property <" + FusekiVocab.pEndpointName + "> with <" + property.getURI() + "> for <" + svc + ">");
endpointName = stmt.getString();
List<RDFNode> x = GraphUtils.multiValue(r, FusekiVocab.pAllowedUsers);
if (x.size() > 1)
throw new FusekiConfigException("Multiple fuseki:" + FusekiVocab.pAllowedUsers.getLocalName() + " for " + r);
if (!x.isEmpty())
authPolicy = allowedUsers(r);
} catch (JenaException | ClassCastException ex) {
throw new FusekiConfigException("Failed to parse endpoint: " + r);
}
} else {
throw new FusekiConfigException("Unrecognized: " + ep);
}
if (StringUtils.isEmpty(endpointName))
endpointName = null;
Endpoint endpoint = Endpoint.create(operation, endpointName, authPolicy);
endpoints.add(endpoint);
}
}
Aggregations