use of org.apache.jena.query.QuerySolution in project jena by apache.
the class ExTDB5 method main.
public static void main(String... argv) {
// Direct way: Make a TDB-back Jena model in the named directory.
String directory = "MyDatabases/DB1";
Dataset dataset = TDBFactory.createDataset(directory);
// Potentially expensive query.
String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
// See http://incubator.apache.org/jena/documentation/query/app_api.html
Query query = QueryFactory.create(sparqlQueryString);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext(); ) {
QuerySolution soln = results.nextSolution();
int count = soln.getLiteral("count").getInt();
System.out.println("count = " + count);
}
} finally {
qexec.close();
}
// Close the dataset.
dataset.close();
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class FusekiConfig method servicesAndDatasets.
private static List<DataAccessPoint> servicesAndDatasets(Model model) {
// Old style configuration file : server to services.
DatasetDescriptionRegistry dsDescMap = FusekiServer.registryForBuild();
// ---- Services
ResultSet rs = FusekiLib.query("SELECT * { ?s fu:services [ list:member ?service ] }", model);
List<DataAccessPoint> accessPoints = new ArrayList<>();
if (!rs.hasNext())
// No "fu:services ( .... )" so try looking for services directly.
// This means Fuseki2, service configuration files (no server section) work for --conf.
rs = FusekiLib.query("SELECT ?service { ?service a fu:Service }", model);
for (; rs.hasNext(); ) {
QuerySolution soln = rs.next();
Resource svc = soln.getResource("service");
DataAccessPoint acc = FusekiBuilder.buildDataAccessPoint(svc, dsDescMap);
accessPoints.add(acc);
}
return accessPoints;
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class FusekiBuilder method addServiceEP.
private static void addServiceEP(DataService dataService, OperationName opName, Resource svc, Property property) {
String p = "<" + property.getURI() + ">";
ResultSet rs = query("SELECT * { ?svc " + p + " ?ep}", svc.getModel(), "svc", svc);
for (; rs.hasNext(); ) {
QuerySolution soln = rs.next();
String epName = soln.getLiteral("ep").getLexicalForm();
Endpoint operation = new Endpoint(opName, epName);
addServiceEP(dataService, opName, epName);
//log.info(" " + opName.name + " = " + dataAccessPoint.getName() + "/" + epName) ;
}
}
use of org.apache.jena.query.QuerySolution in project jena by apache.
the class TextOutput method colWidths.
private int[] colWidths(ResultSetRewindable rs) {
int numCols = rs.getResultVars().size();
int numRows = 0;
int[] colWidths = new int[numCols];
// Widths at least that of the variable name. Assumes we will print col headings.
for (int i = 0; i < numCols; i++) colWidths[i] = (rs.getResultVars().get(i)).length();
// Preparation pass : find the maximum width for each column
for (; rs.hasNext(); ) {
numRows++;
QuerySolution rBind = rs.nextSolution();
int col = -1;
for (String s1 : rs.getResultVars()) {
col++;
String rVar = s1;
String s = getVarValueAsString(rBind, rVar);
if (colWidths[col] < s.length()) {
colWidths[col] = s.length();
}
}
}
rs.reset();
return colWidths;
}
use of org.apache.jena.query.QuerySolution 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());
}
}
Aggregations