繁体   English   中英

使用Jena读取限制值

[英]Read restriction values using Jena

我有一个对象限制,定义如下

hasYear some integer[minLength 2, maxLength 4, >=1995, <=2012]

如何使用Jena读取限制中定义的各个值。

您可以使用不同的方法。 首先,您可以通过以下代码遍历Jena Model

model.read(...);
StmtIterator si = model.listStatements(
        model.getResource("required property uri"), RDFS.range, (RDFNode) null);
while (si.hasNext()) {
    Statement stmt = si.next();
    Resource range = stmt.getObject().asResource();
    // get restrictions collection
    Resource nextNode = range.getPropertyResourceValue(OWL2.withRestrictions);
    for (;;) {
        Resource restr = nextNode.getPropertyResourceValue(RDF.first);
        if (restr == null)
            break;

        StmtIterator pi = restr.listProperties();
        while (pi.hasNext()) {
            Statement restrStmt = pi.next();
            Property restrType = restrStmt.getPredicate();
            Literal value = restrStmt.getObject().asLiteral();
            // print type and value for each restriction
            System.out.println(restrType + " = " + value);
        }
        // go to the next element of collection
        nextNode = nextNode.getPropertyResourceValue(RDF.rest);
    }
}

如果使用OntModel表示RDF图形代码可以通过使用简化

model.listRestrictions()
ontClass.asRestriction()
etc.

这种方法的好例子 (感谢Ian Dickinson)

另一种方法是使用具有相同含义的SPARQL 1.1查询

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?datatype ?restr_type ?restr_value {
    ?prop rdfs:range ?range.
    ?range owl:onDatatype ?datatype;
        owl:withRestrictions ?restr_list.
    ?restr_list rdf:rest*/rdf:first ?restr.
    ?restr ?restr_type ?restr_value
}

暂无
暂无

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

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