use of org.apache.jena.query.QuerySolutionMap in project webofneeds by researchstudio-sat.
the class SparqlSelectFunction method addInitialBindings.
public SparqlSelectFunction<T> addInitialBindings(QuerySolution moreInitialBindings) {
if (this.initialBinding == null) {
this.initialBinding = new QuerySolutionMap();
}
for (Iterator<String> it = moreInitialBindings.varNames(); it.hasNext(); ) {
String varName = it.next();
this.initialBinding.add(varName, moreInitialBindings.get(varName));
}
return this;
}
use of org.apache.jena.query.QuerySolutionMap in project webofneeds by researchstudio-sat.
the class SparqlSelectFunction method apply.
@Override
public List<T> apply(Dataset dataset) {
boolean existingTransaction = dataset.isInTransaction();
if (!existingTransaction) {
dataset.begin(ReadWrite.READ);
}
Dataset result = DatasetFactory.createGeneral();
result.begin(ReadWrite.WRITE);
try {
Query theQuery = this.getQuery();
if (this.limit != null) {
theQuery.setLimit(this.limit);
}
if (this.offset != null) {
theQuery.setOffset(this.offset);
}
if (this.orderBy != null) {
for (SortCondition sortCondition : this.orderBy) {
theQuery.addOrderBy(sortCondition);
}
}
if (this.havingCondiditions != null) {
for (Expr havingCondition : this.havingCondiditions) {
theQuery.addHavingCondition(havingCondition);
}
}
QuerySolution binding = this.initialBinding;
if (binding == null) {
binding = new QuerySolutionMap();
}
List<T> ret = new ArrayList<>();
try (QueryExecution queryExecution = QueryExecutionFactory.create(theQuery, dataset, binding)) {
ResultSet resultSet = queryExecution.execSelect();
while (resultSet.hasNext()) {
ret.add(this.resultGenerator.apply(resultSet.next()));
}
}
return ret;
} finally {
if (!existingTransaction) {
dataset.end();
}
result.commit();
}
}
use of org.apache.jena.query.QuerySolutionMap in project webofneeds by researchstudio-sat.
the class WonMessageQueryBuilder method senderConnection.
public WonMessageQueryBuilder senderConnection(URI senderConnection) {
QuerySolutionMap initialBinding = new QuerySolutionMap();
initialBinding.add("senderConnection", new ResourceImpl(senderConnection.toString()));
delegate.addInitialBindings(initialBinding);
return this;
}
use of org.apache.jena.query.QuerySolutionMap in project rdf2neo by Rothamsted.
the class RdfDataManager method addCypherProps.
/**
* Take an existing {@link CypherEntity} and adds the properties that can be mapped from the underlining TDB by means
* of a property query, like {@link CyNodeLoadingHandler#getNodePropsSparql()}, or
* {@link CyRelationLoadingHandler#getRelationPropsSparql()}.
*
* It doesn't do anything if the query is null.
*/
protected void addCypherProps(CypherEntity cyEnt, String propsSparql) {
ensureOpen();
Model model = this.dataSet.getDefaultModel();
QuerySolutionMap params = new QuerySolutionMap();
params.add("iri", model.getResource(cyEnt.getIri()));
// It may be omitted, if you don't have any property except the IRI.
if (propsSparql == null)
return;
Query qry = this.queryCache.getUnchecked(propsSparql);
Function<String, String> propIdConverter = this.getCyPropertyIdConverter();
boolean wasInTnx = dataSet.isInTransaction();
if (!wasInTnx)
dataSet.begin(ReadWrite.READ);
try {
QueryExecution qx = QueryExecutionFactory.create(qry, model, params);
qx.execSelect().forEachRemaining(row -> {
String propName = this.getCypherId(row.get("name"), propIdConverter);
if (propName == null)
throw new IllegalArgumentException("Null property name for " + cyEnt.getIri());
String propValue = JENAUTILS.literal2Value(row.getLiteral("value")).get();
cyEnt.addPropValue(propName, propValue);
});
} finally {
if (!wasInTnx && dataSet.isInTransaction())
dataSet.end();
}
}
use of org.apache.jena.query.QuerySolutionMap in project jena by apache.
the class EvalSparql method evalSparqlComponent.
public static Collection<Node> evalSparqlComponent(Graph data, Node node, SparqlComponent sparqlComponent) {
checkForRequiredParams(data, node, sparqlComponent);
Query query = sparqlComponent.getQuery();
if (!query.isSelectType())
throw new ShaclException("Not a SELECT query");
// Parameters
if (USE_QueryTransformOps) {
// Done with QueryTransformOps.transform
DatasetGraph dsg = DatasetGraphFactory.wrap(data);
Map<Var, Node> substitutions = parametersToSyntaxSubstitutions(data, node, sparqlComponent.getParams());
Query query2 = QueryTransformOps.transform(query, substitutions);
try (QueryExecution qExec = QueryExecutionFactory.create(query2, dsg)) {
return evalSparqlOneVar(qExec);
}
} else {
// Done with pre-binding.
Model model = ModelFactory.createModelForGraph(data);
QuerySolutionMap qsm = parametersToPreBinding(model, node, sparqlComponent.getParams());
try (QueryExecution qExec = QueryExecution.create().query(query).model(model).initialBinding(qsm).build()) {
return evalSparqlOneVar(qExec);
}
}
}
Aggregations