Search in sources :

Example 11 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class container method execEvaluatedCalc.

// Ask by finding all the rdf:_N + rdf:type  
private QueryIterator execEvaluatedCalc(Binding binding, Node containerNode, Node predicate, Node member, ExecutionContext execCxt) {
    Graph graph = execCxt.getActiveGraph();
    if (!containerNode.isVariable()) {
        // Container a ground term.
        if (!GraphContainerUtils.isContainer(execCxt.getActiveGraph(), containerNode, typeNode))
            return IterLib.noResults(execCxt);
        return oneContainer(binding, containerNode, member, execCxt);
    }
    // Container a variable. 
    Collection<Node> c = null;
    if (member.isVariable())
        c = findContainers(graph, typeNode);
    else
        c = findContainingContainers(graph, typeNode, member);
    QueryIterConcat cIter = new QueryIterConcat(execCxt);
    Var cVar = Var.alloc(containerNode);
    for (Node cn : c) {
        //Binding the container node. 
        Binding b = BindingFactory.binding(binding, cVar, cn);
        Node m = member;
        // Special case of ?x rdfs:member ?x
        if (Var.isVar(member) && member.equals(cVar)) {
            m = cn;
        }
        cIter.add(oneContainer(b, cn, m, execCxt));
    }
    return cIter;
//throw new QueryFatalException(Utils.className(this)+": Arg 1 is too hard : "+containerNode) ;
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Graph(org.apache.jena.graph.Graph) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node)

Example 12 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class splitIRI method subjectIsIRI.

private QueryIterator subjectIsIRI(Node subject, PropFuncArg argObject, Binding binding, ExecutionContext execCxt) {
    String namespace = subject.getNameSpace();
    String localname = subject.getLocalName();
    Node namespaceNode = argObject.getArg(0);
    Node localnameNode = argObject.getArg(1);
    // New binding to return.
    BindingMap b = null;
    if (Var.isVar(namespaceNode) || Var.isVar(localnameNode))
        b = BindingFactory.create(binding);
    if (// .isVariable() )
    Var.isVar(namespaceNode)) {
        b.add(Var.alloc(namespaceNode), NodeFactory.createURI(namespace));
        // but it's possible for strange URI schemes.
        if (localnameNode.isVariable() && namespaceNode.getName() == localnameNode.getName())
            localnameNode = NodeFactory.createURI(namespace);
    } else {
        String ns = null;
        // Allow both IRIs and plain literals in the namespace position.
        if (namespaceNode.isURI())
            ns = namespaceNode.getURI();
        if (namespaceNode.isLiteral())
            ns = NodeUtils.stringLiteral(namespaceNode);
        if (ns == null || !ns.equals(namespace))
            return IterLib.noResults(execCxt);
    // Fall through and proceed to localname 
    }
    if (Var.isVar(localnameNode))
        b.add(Var.alloc(localnameNode), NodeFactory.createLiteral(localname));
    else {
        // Only string literals (plain strings or datatype xsd:string) 
        String lc = NodeUtils.stringLiteral(localnameNode);
        if (lc == null || !lc.equals(localname))
            return IterLib.noResults(execCxt);
    }
    Binding b2 = (b == null) ? binding : b;
    return IterLib.result(b, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 13 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class strSplit method execEvaluated.

@Override
public QueryIterator execEvaluated(final Binding binding, final Node subject, final Node predicate, final PropFuncArg object, final ExecutionContext execCxt) {
    if (!Var.isVar(subject))
        throw new ExprEvalException("Subject is not a variable (" + subject + ")");
    if (object.getArgListSize() != 2)
        throw new ExprEvalException("Object list must contain exactly two arguments, the string to split and a regular expression");
    String s = object.getArg(0).getLiteralLexicalForm();
    String regex = object.getArg(1).getLiteralLexicalForm();
    final Var subjectVar = Var.alloc(subject);
    // StrUtils will also trim whitespace
    String[] tokens = StrUtils.split(s, regex);
    Iterator<Binding> it = Iter.map(Arrays.asList(tokens).iterator(), item -> BindingFactory.binding(binding, subjectVar, NodeFactory.createLiteral(item)));
    return new QueryIterPlainWrapper(it, execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) Var(org.apache.jena.sparql.core.Var) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 14 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class listIndex method findIndexMember.

private static QueryIterator findIndexMember(Graph graph, Binding binding, Node listNode, Var varIndex, Var varMember, ExecutionContext execCxt) {
    // Iterate over list
    List<Node> members = GraphList.members(new GNode(graph, listNode));
    List<Binding> bindings = new ArrayList<>();
    for (int i = 0; i < members.size(); i++) {
        Node idx = NodeFactoryExtra.intToNode(i);
        Node member = members.get(i);
        BindingMap b = BindingFactory.create(binding);
        b.add(varIndex, idx);
        b.add(varMember, member);
        bindings.add(b);
    }
    return new QueryIterPlainWrapper(bindings.iterator(), execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) GNode(org.apache.jena.sparql.util.graph.GNode) Node(org.apache.jena.graph.Node) GNode(org.apache.jena.sparql.util.graph.GNode) ArrayList(java.util.ArrayList) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 15 with Binding

use of org.apache.jena.sparql.engine.binding.Binding in project jena by apache.

the class QueryTest method unique.

private static ResultSetRewindable unique(ResultSetRewindable results) {
    // VERY crude.  Utilises the fact that bindings have value equality.
    List<Binding> x = new ArrayList<>();
    Set<Binding> seen = new HashSet<>();
    for (; results.hasNext(); ) {
        Binding b = results.nextBinding();
        if (seen.contains(b))
            continue;
        seen.add(b);
        x.add(b);
    }
    QueryIterator qIter = new QueryIterPlainWrapper(x.iterator());
    ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter);
    return ResultSetFactory.makeRewindable(rs);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) ResultSetStream(org.apache.jena.sparql.engine.ResultSetStream) HashSet(java.util.HashSet)

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