如何获取Jena查询的所有主题? [英] How to get all of the subjects of a Jena Query?

查看:181
本文介绍了如何获取Jena查询的所有主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些jena查询对象:

Suppose I have some jena query object :

String query = "SELECT * WHERE{ ?s <some_uri> ?o ...etc. }";
Query q = QueryFactory.create(query, Syntax.syntaxARQ);

在查询中获取三元组的所有主题的最佳方法是什么?优选地,不必手动进行任何字符串解析/操作。

What would be the best way to get all of the subjects of the triples in the query? Preferably without having to do any string parsing/manipulation manually.

例如,给定一个查询

SELECT * WHERE {
    ?s ?p ?o;
       ?p2 ?o2.
    ?s2 ?p3 ?o3.
    ?s3 ?p4 ?o4.
    <http://example.com> ?p5 ?o5.
}

我希望能够返回一些看起来像

I would hope to have returned some list which looks like

[?s, ?s2, ?s3, <http://example.com>]

换句话说,我想要查询中所有主题的列表。即使只有那些变量或文字/ uris的主题也会有用,但我想在查询中找到所有主题的列表。

In other words, I want the list of all subjects in a query. Even having only those subjects which were variables or those which were literals/uris would be useful, but I'd like to find a list of all of the subjects in the query.

我知道有方法可以返回结果变量( Query.getResultVars )和其他一些信息(参见http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/Query。 html ),但我似乎找不到任何具体的查询主题(所有结果变量的列表也会返回谓词和对象)。

I know there are methods to return the result variables (Query.getResultVars) and some other information (see http://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/Query.html), but I can't seem to find anything which will get specifically the subjects of the query (a list of all result variables would return the predicates and objects as well).

任何帮助表示赞赏。

推荐答案

有趣的问题。你需要做的是完成查询,并为每个三元组迭代并查看第一部分。

Interesting question. What you need to do is go through the query, and for each block of triples iterate through and look at the first part.

最有效的方法是通过元素walker将遍历查询的每个部分。它可能看起来在你的情况下超过顶部,但查询可以包含各种各样的东西,包括 FILTERs OPTIONALs ,和嵌套的 SELECTs 。使用walker意味着你可以忽略这些东西,只关注你想要的东西:

The most robust way to do this is via an element walker which will go through each part of the query. It might seem over the top in your case, but queries can contain all sorts of things, including FILTERs, OPTIONALs, and nested SELECTs. Using the walker means that you can ignore that stuff and focus on only what you want:

Query q = QueryFactory.create(query); // SPARQL 1.1

// Remember distinct subjects in this
final Set<Node> subjects = new HashSet<Node>();

// This will walk through all parts of the query
ElementWalker.walk(q.getQueryPattern(),
    // For each element...
    new ElementVisitorBase() {
        // ...when it's a block of triples...
        public void visit(ElementPathBlock el) {
            // ...go through all the triples...
            Iterator<TriplePath> triples = el.patternElts();
            while (triples.hasNext()) {
                // ...and grab the subject
                subjects.add(triples.next().getSubject());
            }
        }
    }
);

这篇关于如何获取Jena查询的所有主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆