【问题标题】:Reification and SPARQL* in Jena: CompatibilityJena 中的 Reification 和 SPARQL*:兼容性
【发布时间】:2021-04-19 07:48:37
【问题描述】:

我可以使用 SPARQL* 查询来查询使用具体化的经典 RDF 模型吗? 对我来说,Jena documentation 在这里有点模糊。

下面的代码创建了一个具体化的语句:

<< <http://www.mysubject.com> <http://www.mypredicate.com> <http://www.myobject.com> >> <http://www.sayed.de#sayed> <http://www.sayer.de> .

该代码还包含两个查询:(i) 一个经典的 SPARQL 查询,(ii) 一个 SPARQL* 查询。 两者都查询 &lt;http://www.sayer.de&gt; 作为结果。虽然 (i) 提出了解决方案,但 (ii) 没有这样做。

我在做什么/理解错了?

import org.apache.jena.ontology.DatatypeProperty;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;

import java.io.StringWriter;

public class RdfStar {

    public static void main(String[] args) {

        // let's create an ontModel and fill it with data:
        OntModel model =  ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
        ReifiedStatement s = model.createReifiedStatement(
                model.createStatement(
                        ResourceFactory.createResource("http://www.mysubject.com"),
                        ResourceFactory.createProperty("http://www.mypredicate.com"),
                        ResourceFactory.createResource("http://www.myobject.com"))
        );
        ObjectProperty sayedProperty = model.createObjectProperty("http://www.sayed.de#sayed");
        s.addProperty(sayedProperty, model.createResource("http://www.sayer.de"));

        // write to console
        StringWriter myWriter = new StringWriter();
        model.write(myWriter, "NT");
        String result = myWriter.toString();
        System.out.println(result);

        // now let's create a regular query
        String queryString = "SELECT ?who WHERE {" +
                "?statement <http://www.sayed.de#sayed> ?who ." +
                "}";
        Query query = QueryFactory.create(queryString) ;
        try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            ResultSet results = qexec.execSelect();
            while (results.hasNext()) {
                QuerySolution soln = results.nextSolution();
                RDFNode x = soln.get("who");
                System.out.println(x);
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        // NOW SPARQL STAR

        queryString = "SELECT ?who WHERE {" +
                "<< ?a ?b ?c >> ?d ?who ." +
                "}";
        query = QueryFactory.create(queryString, Syntax.syntaxARQ) ;
        try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            ResultSet results = qexec.execSelect();
            while (results.hasNext()) {
                QuerySolution soln = results.nextSolution();
                RDFNode x = soln.get("who");
                System.out.println("Star result: " + x);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 首先:让我们明确一下术语&lt;&lt;:s :p :o&gt;&gt; :q :z . 不是具体的陈述。 RDF Reification 使用属性rdf;subjectrdf;predicaterdf:object:参见w3.org/wiki/RdfReification。有可能向物化转变。
  • 是的,对于不准确之处深表歉意!我只是用这个符号很短。如果您执行 Java 文件,您可以看到实际的三元组。

标签: sparql jena reification rdf-star


【解决方案1】:

目前在 Turtle(或 N-Triples)中写入数据比通过 Model API 更容易。

model.createReifiedStatement 是用于具体化的旧功能,它不是 RDF*。

在耶拿的 RDF*/SPARQL* 的实施将跟踪 RDF-star 社区组中的工作,并且需要进行更改。

当前的 SPARQL* 植根于原始 RDF* 论文。它使用&lt;&lt;&gt;&gt; 的三重索引,因此使用索引进行匹配(它类似于“PG 模式”): > :q :z .

试试下面的数据(D.ttl)(Jena的开发版也有新的注解语法:

PREFIX ex: <http://example/>

ex:s ex:p ex:o .
<<ex:s ex:p ex:o>> ex:q ex:z .

和查询(Q.rq):

PREFIX ex: <http://example/>

SELECT * {
  << ?s ?p ?o >> ?q ?z .
}

其中,使用命令行工具

sparql --data D.ttl --query Q.rq 

给予

------------------------------------
| s    | p    | o    | q    | z    |
====================================
| ex:s | ex:p | ex:o | ex:q | ex:z |
------------------------------------

【讨论】:

  • 感谢您的详细解答。我可以在命令行中重现记录的行为。我有两个小的后续问题:(1)目前根本没有通过模型 API 的“便捷方式”吗? (2) SPARQL* 最终是否也适用于具体化语句?
  • (1) 不在模型 API 中 - 在 Graph/Triple API 中有。模型 API 可以处理 RDF* &lt;&lt;&gt;&gt; 术语但不能创建它们。最终方向和语义尚未完全确定,Model API 应谨慎稳定。
  • (2) 具体化 - 如rdf:subjectrdf:predicaterdf:object?它已经做到了但这与&lt;&lt;:s :p :o&gt;&gt;无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2023-04-07
相关资源
最近更新 更多