use of org.apache.jena.sparql.core.describe.DescribeHandler 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.sparql.core.describe.DescribeHandler in project jena by apache.
the class QueryExecutionBase method execDescribe.
@Override
public Model execDescribe(Model model) {
checkNotClosed();
if (!query.isDescribeType())
throw new QueryExecException("Attempt to get a DESCRIBE result from a " + labelForQuery(query) + " query");
//Was: query.setQueryResultStar(true) ; but why?
query.setResultVars();
// If there was no WhereClause, use an empty pattern (one solution, no columns).
if (query.getQueryPattern() == null)
query.setQueryPattern(new ElementGroup());
Set<RDFNode> set = new HashSet<>();
//May return null (no query pattern)
ResultSet qRes = execResultSet();
// Prefixes for result (after initialization)
insertPrefixesInto(model);
if (qRes != null) {
for (; qRes.hasNext(); ) {
QuerySolution rb = qRes.nextSolution();
for (String varName : query.getResultVars()) {
RDFNode n = rb.get(varName);
set.add(n);
}
}
}
if (query.getResultURIs() != null) {
// Any URIs in the DESCRIBE
for (Node n : query.getResultURIs()) {
// Need to make dataset available to describe handlers.
RDFNode rNode = ModelUtils.convertGraphNodeToRDFNode(n, dataset.getDefaultModel());
set.add(rNode);
}
}
// 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 (RDFNode n : set) {
if (n instanceof Resource) {
for (DescribeHandler dh : dhList) {
dh.describe((Resource) n);
}
} else {
// Can't describe literals
continue;
}
}
for (DescribeHandler dh : dhList) dh.finish();
this.close();
return model;
}
Aggregations