Search in sources :

Example 26 with BindingMap

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

the class JSONInput method parse.

private void parse(InputStream in) {
    JsonObject obj = JSON.parse(in);
    if (obj.hasKey(kBoolean)) {
        checkContains(obj, true, true, kHead, kBoolean);
        booleanResult = obj.get(kBoolean).getAsBoolean().value();
        rows = null;
        return;
    }
    rows = new ArrayList<>(1000);
    checkContains(obj, true, true, kHead, kResults);
    // process head
    if (!obj.get(kHead).isObject())
        throw new ResultSetException("Key 'head' must have a JSON object as value: found: " + obj.get(kHead));
    JsonObject head = obj.get(kHead).getAsObject();
    // -- Link - array.
    if (head.hasKey(kLink)) {
        List<String> links = new ArrayList<>();
        if (head.get(kLink).isString()) {
            Log.warn(this, "Link field is a string, should be an array of strings");
            links.add(head.get(kLink).getAsString().value());
        } else {
            if (!head.get(kLink).isArray())
                throw new ResultSetException("Key 'link' must have be an array: found: " + obj.get(kLink));
            for (JsonValue v : head.get(kLink).getAsArray()) {
                if (!v.isString())
                    throw new ResultSetException("Key 'link' must have be an array of strings: found: " + v);
                links.add(v.getAsString().value());
            }
        }
    }
    // -- Vars
    vars = parseVars(head);
    // ---- Results
    JsonObject results = obj.get(kResults).getAsObject();
    if (!results.get(kBindings).isArray())
        throw new ResultSetException("'bindings' must be an array");
    JsonArray array = results.get(kBindings).getAsArray();
    Iterator<JsonValue> iter = array.iterator();
    for (; iter.hasNext(); ) {
        BindingMap b = BindingFactory.create();
        JsonValue v = iter.next();
        if (!v.isObject())
            throw new ResultSetException("Entry in 'bindings' array must be an object {}");
        JsonObject x = v.getAsObject();
        Set<String> varNames = x.keys();
        for (String vn : varNames) {
            // if ( ! vars.contains(vn) ) {}
            JsonValue vt = x.get(vn);
            if (!vt.isObject())
                throw new ResultSetException("Binding for variable '" + vn + "' is not a JSON object: " + vt);
            Node n = parseOneTerm(vt.getAsObject());
            b.add(Var.alloc(vn), n);
        }
        rows.add(b);
    }
}
Also used : JsonArray(org.apache.jena.atlas.json.JsonArray) LabelToNode(org.apache.jena.riot.lang.LabelToNode) Node(org.apache.jena.graph.Node) JsonValue(org.apache.jena.atlas.json.JsonValue) JsonObject(org.apache.jena.atlas.json.JsonObject) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 27 with BindingMap

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

the class version method execEvaluated.

@Override
public QueryIterator execEvaluated(Binding binding, PropFuncArg subject, Node predicate, PropFuncArg object, ExecutionContext execCxt) {
    List<Binding> results = new ArrayList<>();
    Node subj = subject.getArg();
    Node obj = object.getArg();
    Iterator<SystemInfo> iter = SystemARQ.registeredSubsystems();
    for (; iter.hasNext(); ) {
        SystemInfo info = iter.next();
        if (!isSameOrVar(subj, info.getIRI()))
            continue;
        Node version = NodeFactory.createLiteral(info.getVersion());
        if (!isSameOrVar(obj, version))
            continue;
        BindingMap b = BindingFactory.create(binding);
        if (subj.isVariable())
            b.add(Var.alloc(subj), info.getIRI());
        if (subj.isVariable())
            b.add(Var.alloc(obj), version);
        results.add(b);
    }
    return new QueryIterPlainWrapper(results.iterator(), execCxt);
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) SystemInfo(org.apache.jena.sparql.mgt.SystemInfo) Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 28 with BindingMap

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

the class versionARQ method execEvaluated.

@Override
public QueryIterator execEvaluated(Binding binding, PropFuncArg subject, Node predicate, PropFuncArg object, ExecutionContext execCxt) {
    BindingMap b = BindingFactory.create(binding);
    Node subj = subject.getArg();
    if (!isSameOrVar(subj, arq))
        IterLib.noResults(execCxt);
    if (subj.isVariable())
        b.add(Var.alloc(subj), arq);
    Node obj = object.getArg();
    if (!isSameOrVar(obj, version))
        IterLib.noResults(execCxt);
    if (obj.isVariable())
        b.add(Var.alloc(obj), version);
    return IterLib.result(b, execCxt);
}
Also used : Node(org.apache.jena.graph.Node) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Example 29 with BindingMap

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

the class TextQueryPF method resultsToQueryIterator.

private QueryIterator resultsToQueryIterator(Binding binding, Node s, Node score, Node literal, Collection<TextHit> results, ExecutionContext execCxt) {
    Var sVar = Var.isVar(s) ? Var.alloc(s) : null;
    Var scoreVar = (score == null) ? null : Var.alloc(score);
    Var literalVar = (literal == null) ? null : Var.alloc(literal);
    Function<TextHit, Binding> converter = (TextHit hit) -> {
        if (score == null && literal == null)
            return sVar != null ? BindingFactory.binding(binding, sVar, hit.getNode()) : BindingFactory.binding(binding);
        BindingMap bmap = BindingFactory.create(binding);
        if (sVar != null)
            bmap.add(sVar, hit.getNode());
        if (scoreVar != null)
            bmap.add(scoreVar, NodeFactoryExtra.floatToNode(hit.getScore()));
        if (literalVar != null)
            bmap.add(literalVar, hit.getLiteral());
        return bmap;
    };
    Iterator<Binding> bIter = Iter.map(results.iterator(), converter);
    QueryIterator qIter = new QueryIterPlainWrapper(bIter, execCxt);
    return qIter;
}
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) BindingMap(org.apache.jena.sparql.engine.binding.BindingMap)

Aggregations

BindingMap (org.apache.jena.sparql.engine.binding.BindingMap)29 Node (org.apache.jena.graph.Node)18 Var (org.apache.jena.sparql.core.Var)12 Binding (org.apache.jena.sparql.engine.binding.Binding)10 ArrayList (java.util.ArrayList)8 QueryIterPlainWrapper (org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper)5 ResultSetStream (org.apache.jena.sparql.engine.ResultSetStream)4 ResultSet (org.apache.jena.query.ResultSet)3 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)3 SQLException (java.sql.SQLException)2 SqlNode (org.apache.jena.sdb.core.sqlnode.SqlNode)2 BuilderResultSet (org.apache.jena.sparql.sse.builders.BuilderResultSet)2 ResultSet (java.sql.ResultSet)1 List (java.util.List)1 Function (java.util.function.Function)1 CSVParser (org.apache.jena.atlas.csv.CSVParser)1 IteratorDelayedInitialization (org.apache.jena.atlas.iterator.IteratorDelayedInitialization)1 JsonArray (org.apache.jena.atlas.json.JsonArray)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1 JsonValue (org.apache.jena.atlas.json.JsonValue)1