首先,我找到了相关的问题和答案:
如果我在SPARQL查询中添加PREFIX,它可以正常工作。但是,我不想复制所有SPARQL查询中的所有前缀,而只是定义它们一次。我尝试以编程方式为rdfs:
:
model.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
query.setPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
并且它有效,但是如果我尝试用我自己的本体来做它它不起作用:
model.setNsPrefix("myOnt", "http://example.com/ontologies/myOnt#");
query.setPrefix("myOnt", "http://example.com/ontologies/myOnt#");
答案 0 :(得分:1)
您可以设置Prologue以包含前缀映射,然后使用您的特定SPARQL查询将其传递到QueryFactory。
以下内容使用 Apache Jena 3.0.0 :
为我工作Prologue queryPrologue = new Prologue();
queryPrologue.setPrefix("skos", "http://www.w3.org/2004/02/skos/core#");
String sparql = "SELECT (COUNT ?s) WHERE { ?s a skos:Concept . }"
Query query = QueryFactory.parse(new Query(queryPrologue), sparql, null, null);
try(QueryExecution queryExec = QueryExecutionFactory.create(query, dataset)) {
// ...
}