Jena 中的具体化和 SPARQL*:兼容性

Reification and SPARQL* in Jena: Compatibility

我可以使用 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* 查询。 两者都查询 <http://www.sayer.de> 作为结果。虽然 (i) 返回解决方案,但 (ii) 没有这样做。

我哪里错了doing/understanding?

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();
        }
    }
}

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

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

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

当前的 SPARQL* 植根于原始的 RDF* 论文。它使用 <<>> 的三重索引,因此使用索引进行匹配(类似于“PG 模式”): <<:s:p:o>>: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 |
------------------------------------