use of org.apache.jena.query.Query in project jena by apache.
the class WhereProcessor method parseExpr.
private Expr parseExpr(String expression) {
Query query = new Query();
query.setPrefixMapping(prefixHandler.getPrefixes());
return ExprUtils.parse(query, expression, true);
}
use of org.apache.jena.query.Query in project jena by apache.
the class ExTDB4 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);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(results);
qexec.close();
dataset.close();
}
use of org.apache.jena.query.Query in project jena by apache.
the class ExTDB_Txn1 method execQuery.
public static void execQuery(String sparqlQueryString, Dataset dataset) {
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();
}
}
use of org.apache.jena.query.Query in project webofneeds by researchstudio-sat.
the class DatasetToUriListBySparqlFunction method apply.
@Override
public List<URI> apply(Dataset dataset) {
dataset.begin(ReadWrite.READ);
Dataset result = DatasetFactory.createGeneral();
result.begin(ReadWrite.WRITE);
Query query = QueryFactory.create(sparql);
List<URI> ret = new ArrayList<URI>();
try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset)) {
ResultSet resultSet = queryExecution.execSelect();
if (!resultSet.getResultVars().contains("uri")) {
throw new IllegalStateException("Query has no variable named 'uri' (read from: " + sparqlFile + ")");
}
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
RDFNode uriNode = solution.get("uri");
if (uriNode == null) {
throw new IllegalStateException("Query has no variable named 'uri' (read from: " + sparqlFile + ")");
}
if (!uriNode.isURIResource()) {
throw new IllegalStateException("Value of result variable 'uri' is not a resource (read from: " + sparqlFile + ")");
}
ret.add(URI.create(uriNode.asResource().getURI().toString()));
}
}
return ret;
}
use of org.apache.jena.query.Query in project webofneeds by researchstudio-sat.
the class DynamicDatasetToDatasetBySparqlGSPOSelectFunction method apply.
@Override
public Dataset apply(Dataset dataset) {
dataset.begin(ReadWrite.READ);
Dataset result = DatasetFactory.createGeneral();
result.begin(ReadWrite.WRITE);
Query query = QueryFactory.create(sparql);
try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset, initialBinding)) {
ResultSet resultSet = queryExecution.execSelect();
RDFNode currentProposal = null;
Model currentProposalContent = ModelFactory.createDefaultModel();
while (resultSet.hasNext()) {
QuerySolution solution = resultSet.next();
RDFNode proposalNode = solution.get("g");
if (currentProposal == null) {
// first solution: remember uri of first proposal
currentProposal = proposalNode;
} else {
// the currentProposal URI and prepare a new empty model for the next proposal
if (!currentProposal.equals(proposalNode)) {
// we have seen all triples of currentProposal
result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
currentProposalContent = ModelFactory.createDefaultModel();
}
currentProposal = proposalNode;
}
// add current triple into currentAgreementModel
RDFNode s = solution.get("s");
RDFNode p = solution.get("p");
RDFNode o = solution.get("o");
Statement newStatement = new StatementImpl(s.asResource(), new PropertyImpl(p.asResource().getURI()), o);
currentProposalContent.add(newStatement);
}
// add the last model
if (currentProposal != null) {
result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
}
return result;
} finally {
dataset.commit();
result.commit();
}
}
Aggregations