1

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

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

<< <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)没有这样做。

我在这里做什么/理解错误?

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

4

1 回答 1

2

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

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

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

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

于 2021-01-14T12:56:49.523 回答