use of org.apache.jena.query.QueryExecException in project jena by apache.
the class QueryExecDataset method execJson.
@Override
public JsonArray execJson() {
checkNotClosed();
if (!query.isJsonType())
throw new QueryExecException("Attempt to get a JSON result from a " + labelForQuery(query) + " query");
startQueryIterator();
JsonArray jsonArray = new JsonArray();
List<String> resultVars = query.getResultVars();
while (queryIterator.hasNext()) {
Binding binding = queryIterator.next();
JsonObject jsonObject = new JsonObject();
for (String resultVar : resultVars) {
Node n = binding.get(Var.alloc(resultVar));
JsonValue value = RDFTerm2Json.fromNode(n);
jsonObject.put(resultVar, value);
}
jsonArray.add(jsonObject);
}
return jsonArray;
}
use of org.apache.jena.query.QueryExecException in project jena by apache.
the class QueryExecDataset method describe.
// -- Describe
@Override
public Graph describe(Graph graph) {
checkNotClosed();
Model model = ModelFactory.createModelForGraph(graph);
if (!query.isDescribeType())
throw new QueryExecException("Attempt to get a DESCRIBE result from a " + labelForQuery(query) + " query");
query.setResultVars();
// columns).
if (query.getQueryPattern() == null)
query.setQueryPattern(new ElementGroup());
Set<Node> set = new HashSet<>();
RowSet rows = execute();
// Prefixes for result (after initialization)
insertPrefixesInto(graph);
// Variables in DESCRIBE
if (rows != null) {
// Single pass over rows.
rows.forEachRemaining(row -> {
for (Var var : rows.getResultVars()) {
Node n = row.get(var);
if (n != null)
set.add(n);
}
});
}
// Any URIs in the DESCRIBE
if (query.getResultURIs() != null) {
query.getResultURIs().forEach(set::add);
}
// DescribeHandlers work on models.
// Create new handlers for this process.
List<DescribeHandler> dhList = DescribeHandlerRegistry.get().newHandlerList();
getContext().put(ARQConstants.sysCurrentDataset, getDataset());
// Notify start of describe phase
for (DescribeHandler dh : dhList) dh.start(model, getContext());
// Do describe for each resource found.
for (Node n : set) {
RDFNode rdfNode = ModelUtils.convertGraphNodeToRDFNode(n, model);
if (rdfNode instanceof Resource) {
for (DescribeHandler dh : dhList) {
dh.describe((Resource) rdfNode);
}
} else {
// Can't describe literals
continue;
}
}
for (DescribeHandler dh : dhList) dh.finish();
this.close();
return graph;
}
use of org.apache.jena.query.QueryExecException in project jena by apache.
the class QueryExecDataset method execJsonItems.
@Override
public Iterator<JsonObject> execJsonItems() {
checkNotClosed();
if (!query.isJsonType())
throw new QueryExecException("Attempt to get a JSON result from a " + labelForQuery(query) + " query");
startQueryIterator();
return new JsonIterator(queryIterator, query.getResultVars());
}
use of org.apache.jena.query.QueryExecException in project jena by apache.
the class TextQueryPF method exec.
@Override
public QueryIterator exec(Binding binding, PropFuncArg argSubject, Node predicate, PropFuncArg argObject, ExecutionContext execCxt) {
if (log.isTraceEnabled()) {
IndentedLineBuffer subjBuff = new IndentedLineBuffer();
argSubject.output(subjBuff, null);
IndentedLineBuffer objBuff = new IndentedLineBuffer();
argObject.output(objBuff, null);
log.trace("exec: {} text:query {}", subjBuff, objBuff);
}
if (textIndex == null) {
if (!warningIssued) {
Log.warn(getClass(), "No text index - no text search performed");
warningIssued = true;
}
// Not a text dataset - no-op
return IterLib.result(binding, execCxt);
}
argSubject = Substitute.substitute(argSubject, binding);
argObject = Substitute.substitute(argObject, binding);
Node s = null;
Node score = null;
Node literal = null;
Node graph = null;
Node prop = null;
if (argSubject.isList()) {
// Length checked in build()
s = argSubject.getArg(0);
if (argSubject.getArgListSize() > 1) {
score = argSubject.getArg(1);
if (!score.isVariable())
throw new QueryExecException("Hit score is not a variable: " + argSubject);
}
if (argSubject.getArgListSize() > 2) {
literal = argSubject.getArg(2);
if (!literal.isVariable())
throw new QueryExecException("Hit literal is not a variable: " + argSubject);
}
if (argSubject.getArgListSize() > 3) {
graph = argSubject.getArg(3);
if (!graph.isVariable())
throw new QueryExecException("Hit graph is not a variable: " + argSubject);
}
if (argSubject.getArgListSize() > 4) {
prop = argSubject.getArg(4);
if (!prop.isVariable())
throw new QueryExecException("Hit prop is not a variable: " + argSubject);
}
} else {
s = argSubject.getArg();
}
if (s.isLiteral())
// Does not match
return IterLib.noResults(execCxt);
StrMatch match = objectToStruct(argObject, true);
if (match == null) {
// can't match
return IterLib.noResults(execCxt);
}
QueryIterator qIter = prepareQuery(binding, s, score, literal, graph, prop, match, execCxt);
if (match.getLimit() >= 0)
qIter = new QueryIterSlice(qIter, 0, match.getLimit(), execCxt);
return qIter;
}
use of org.apache.jena.query.QueryExecException in project jena by apache.
the class Service method exec.
/**
* Executes a service operator
*
* @param op
* Service
* @param context
* Context
* @return Query iterator of service results
*/
public static QueryIterator exec(OpService op, Context context) {
if (context != null && context.isFalse(serviceAllowed))
throw new QueryExecException("SERVICE execution disabled");
if (!op.getService().isURI())
throw new QueryExecException("Service URI not bound: " + op.getService());
// This relies on the observation that the query was originally correct,
// so reversing the scope renaming is safe (it merely restores the
// algebra expression).
// Any variables that reappear should be internal ones that were hidden
// by renaming in the first place.
// Any substitution is also safe because it replaced variables by
// values.
Op opRemote = Rename.reverseVarRename(op.getSubOp(), true);
// JENA-494 There is a bug here that the renaming means that if this is
// deeply nested and joined to other things at the same level of you end
// up with the variables being disjoint and the same results
// The naive fix for this is to map the variables visible in the inner
// operator to those visible in the rewritten operator
// There may be some cases where the re-mapping is incorrect due to
// deeply nested SERVICE clauses
Map<Var, Var> varMapping = new HashMap<>();
Set<Var> originalVars = OpVars.visibleVars(op);
Set<Var> remoteVars = OpVars.visibleVars(opRemote);
boolean requiresRemapping = false;
for (Var v : originalVars) {
if (v.getName().contains("/")) {
// A variable which was scope renamed so has a different name
String origName = v.getName().substring(v.getName().lastIndexOf('/') + 1);
Var remoteVar = Var.alloc(origName);
if (remoteVars.contains(remoteVar)) {
varMapping.put(remoteVar, v);
requiresRemapping = true;
}
} else {
// A variable which does not have a different name
if (remoteVars.contains(v))
varMapping.put(v, v);
}
}
// Explain.explain("HTTP", opRemote, context) ;
Query query;
//@formatter:off
// Comment (for the future?)
// if ( false )
// {
// // ***** Interacts with substitution.
// Element el = op.getServiceElement().getElement() ;
// if ( el instanceof ElementSubQuery )
// query = ((ElementSubQuery)el).getQuery() ;
// else
// {
// query = QueryFactory.create() ;
// query.setQueryPattern(el) ;
// query.setResultVars() ;
// }
// }
// else
//@formatter:on
query = OpAsQuery.asQuery(opRemote);
Explain.explain("HTTP", query, context);
String uri = op.getService().getURI();
HttpQuery httpQuery = configureQuery(uri, context, query);
InputStream in = httpQuery.exec();
// Read the whole of the results now.
// Avoids the problems with calling back into the same system e.g.
// Fuseki+SERVICE <http://localhost:3030/...>
ResultSet rs = ResultSetFactory.fromXML(in);
QueryIterator qIter = QueryIter.materialize(new QueryIteratorResultSet(rs));
// And close connection now, not when qIter is closed.
IO.close(in);
// nested SERVICE clauses
if (requiresRemapping) {
qIter = QueryIter.map(qIter, varMapping);
}
return qIter;
}
Aggregations