Search in sources :

Example 1 with DescribeHandler

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;
}
Also used : DescribeHandler(org.apache.jena.sparql.core.describe.DescribeHandler) Var(org.apache.jena.sparql.core.Var) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node) Resource(org.apache.jena.rdf.model.Resource) QueryExecException(org.apache.jena.query.QueryExecException) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Model(org.apache.jena.rdf.model.Model) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 2 with DescribeHandler

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;
}
Also used : DescribeHandler(org.apache.jena.sparql.core.describe.DescribeHandler) Node(org.apache.jena.graph.Node) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) HashSet(java.util.HashSet)

Aggregations

Node (org.apache.jena.graph.Node)2 DescribeHandler (org.apache.jena.sparql.core.describe.DescribeHandler)2 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)2 HashSet (java.util.HashSet)1 QueryExecException (org.apache.jena.query.QueryExecException)1 Model (org.apache.jena.rdf.model.Model)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 Resource (org.apache.jena.rdf.model.Resource)1 Var (org.apache.jena.sparql.core.Var)1