java - 使用 Jena 读取限制值

标签 java jena owl protege

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

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.

Good example of such approach (感谢伊恩·迪金森)

另一种方法是使用具有相同含义的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
}

关于java - 使用 Jena 读取限制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10337455/

相关文章:

java - 为什么在传递空持久对象而不初始化具有原始数据类型时,smallint 会自动插入 0

java - Reg~x 代表 N~number

java - 如何从 SurfaceView 正确更新 Android UI

uml - OWL/RDF到UML转换器

java - 如何将 Json 转换为 POJO

java - RDF数据集: write a DataSource into a file

rdf - 从文件加载本体时如何覆盖默认前缀?

java - 使用 Jena 读取限制约束

java - 如何获取指定个体的对象属性值?

java - 耶拿 NoReaderForLangException : owl