use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class TestQuery method query_recursive_01.
@Test
public void query_recursive_01() {
String query = "SELECT * WHERE { SERVICE <" + serviceQuery() + "> { ?s ?p ?o . BIND(?o AS ?x) } }";
try (QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
ResultSet rs = qExec.execSelect();
Var x = Var.alloc("x");
while (rs.hasNext()) {
Binding b = rs.nextBinding();
Assert.assertNotNull(b.get(x));
}
}
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class TemplateLib method calcQuads.
/** Substitute into quad patterns */
public static Iterator<Quad> calcQuads(final List<Quad> quads, Iterator<Binding> bindings) {
return Iterators.concat(Iter.map(bindings, new Function<Binding, Iterator<Quad>>() {
Map<Node, Node> bNodeMap = new HashMap<>();
@Override
public Iterator<Quad> apply(final Binding b) {
// Iteration is a new mapping of bnodes.
bNodeMap.clear();
List<Quad> quadList = new ArrayList<>(quads.size());
for (Quad quad : quads) {
Quad q = subst(quad, b, bNodeMap);
if (!q.isConcrete()) {
// "+FmtUtils.stringForQuad(quad)) ;
continue;
}
quadList.add(q);
}
return quadList.iterator();
}
}));
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class TemplateLib method calcTriples.
/** Substitute into triple patterns */
public static Iterator<Triple> calcTriples(final List<Triple> triples, Iterator<Binding> bindings) {
return Iterators.concat(Iter.map(bindings, new Function<Binding, Iterator<Triple>>() {
Map<Node, Node> bNodeMap = new HashMap<>();
@Override
public Iterator<Triple> apply(final Binding b) {
// Iteration is a new mapping of bnodes.
bNodeMap.clear();
List<Triple> tripleList = new ArrayList<>(triples.size());
for (Triple triple : triples) {
Triple q = subst(triple, b, bNodeMap);
if (!q.isConcrete() || !ModelUtils.isValidAsStatement(q.getSubject(), q.getPredicate(), q.getObject())) {
// "+FmtUtils.stringForQuad(quad)) ;
continue;
}
tripleList.add(q);
}
return tripleList.iterator();
}
}));
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class UpdateEngineWorker method visit.
@Override
public void visit(UpdateDeleteWhere update) {
List<Quad> quads = update.getQuads();
// Removed from SPARQL : Convert bNodes to named variables first.
//quads = convertBNodesToVariables(quads) ;
// Convert quads to a pattern.
Element el = elementFromQuads(quads);
// Decided to serialize the bindings, but could also have decided to
// serialize the quads after applying the template instead.
ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(datasetGraph.getContext());
DataBag<Binding> db = BagFactory.newDefaultBag(policy, SerializationFactoryFinder.bindingSerializationFactory());
try {
Iterator<Binding> bindings = evalBindings(el);
db.addAll(bindings);
Iter.close(bindings);
Iterator<Binding> it = db.iterator();
execDelete(datasetGraph, quads, null, it);
Iter.close(it);
} finally {
db.close();
}
}
use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.
the class UpdateEngineWorker method visit.
@Override
public void visit(UpdateModify update) {
Node withGraph = update.getWithIRI();
Element elt = update.getWherePattern();
// null or a dataset for USING clause.
// USING/USING NAMED
DatasetGraph dsg = processUsing(update);
// USING overrides WITH
if (dsg == null && withGraph != null) {
// Subtle difference : WITH <uri>... WHERE {}
// and an empty/unknown graph <uri>
// rewrite with GRAPH -> no match.
// redo as dataset with different default graph -> match
// SPARQL is unclear about what happens when the graph does not exist.
// but the rewrite with ElementNamedGraph is closer to SPARQL.
// Better, treat as
// WHERE { GRAPH <with> { ... } }
// This is the SPARQL wording (which is a bit loose).
elt = new ElementNamedGraph(withGraph, elt);
}
if (dsg == null)
dsg = datasetGraph;
Query query = elementToQuery(elt);
ThresholdPolicy<Binding> policy = ThresholdPolicyFactory.policyFromContext(datasetGraph.getContext());
DataBag<Binding> db = BagFactory.newDefaultBag(policy, SerializationFactoryFinder.bindingSerializationFactory());
try {
Iterator<Binding> bindings = evalBindings(query, dsg, inputBinding, context);
if (false) {
List<Binding> x = Iter.toList(bindings);
System.out.printf("====>> Bindings (%d)\n", x.size());
Iter.print(System.out, x.iterator());
System.out.println("====<<");
bindings = Iter.iter(x);
}
db.addAll(bindings);
Iter.close(bindings);
Iterator<Binding> it = db.iterator();
execDelete(datasetGraph, update.getDeleteQuads(), withGraph, it);
Iter.close(it);
Iterator<Binding> it2 = db.iterator();
execInsert(datasetGraph, update.getInsertQuads(), withGraph, it2);
Iter.close(it2);
} finally {
db.close();
}
}
Aggregations