繁体   English   中英

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

[英]Reification and SPARQL* in Jena: Compatibility

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

下面的代码创建了一个具体化的声明:

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

目前在 Turtle(或 N-Triples)中写入数据比通过 Model 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 |
------------------------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM