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);
}
}
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);
}
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);
}
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;
}
Aggregations