Search in sources :

Example 21 with Binding

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));
        }
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Var(org.apache.jena.sparql.core.Var) ServerTest(org.apache.jena.fuseki.ServerTest) Test(org.junit.Test)

Example 22 with Binding

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();
        }
    }));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Function(java.util.function.Function) Quad(org.apache.jena.sparql.core.Quad)

Example 23 with Binding

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();
        }
    }));
}
Also used : Triple(org.apache.jena.graph.Triple) Binding(org.apache.jena.sparql.engine.binding.Binding) Function(java.util.function.Function)

Example 24 with Binding

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();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Element(org.apache.jena.sparql.syntax.Element)

Example 25 with Binding

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();
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Query(org.apache.jena.query.Query) Node(org.apache.jena.graph.Node) Element(org.apache.jena.sparql.syntax.Element)

Aggregations

Binding (org.apache.jena.sparql.engine.binding.Binding)132 Node (org.apache.jena.graph.Node)39 ArrayList (java.util.ArrayList)36 Var (org.apache.jena.sparql.core.Var)33 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)27 Test (org.junit.Test)21 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)16 BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)10 BindingComparator (org.apache.jena.sparql.engine.binding.BindingComparator)8 BuilderBinding (org.apache.jena.sparql.sse.builders.BuilderBinding)8 NoSuchElementException (java.util.NoSuchElementException)7 Triple (org.apache.jena.graph.Triple)7 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)7 ARQInternalErrorException (org.apache.jena.sparql.ARQInternalErrorException)5 Op (org.apache.jena.sparql.algebra.Op)5 BasicPattern (org.apache.jena.sparql.core.BasicPattern)5 ResultSet (org.apache.jena.query.ResultSet)4 SortCondition (org.apache.jena.query.SortCondition)4 TableN (org.apache.jena.sparql.algebra.table.TableN)4 File (java.io.File)3