【问题标题】:Read restriction values using Jena使用 Jena 读取限制值
【发布时间】:2012-04-26 16:24:25
【问题描述】:

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

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

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

【问题讨论】:

  • 我可能会建议,如果您尝试提取这些值的原因是因为您想强制执行它们,那么您可以考虑使用推理器为您执行此操作。
  • 好的。如果这是可能的,那么如何实现它。即我从文档中读取一个整数值,然后我需要确认该值符合限制,因此可以被视为“年”。我是 OWL 和 Jena 的新手。
  • 您实际上不必实现它,这就是重点。只要确保你的本体是正确的,推理器会告诉你是否有不一致的地方。我建议您阅读 OWL 入门 (w3.org/TR/owl-primer),然后查看推理器。 Jena 内置了一些可以帮助您入门的工具,但您可以使用 Pellet 或 Hermit 等其他选项。

标签: java jena owl protege


【解决方案1】:

您可以使用不同的方法。首先你可以通过以下代码遍历 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
}

【讨论】:

  • “但是有一个限制,你不能检索空白节点指定的限制(就像 Protege 经常做的那样)。”这是不正确的 - Jena 会很好地处理 bNode 限制。
  • 我发现没有办法让 *Restriction 方法在 bnodes 上工作。如果你能提供这样的例子,那就太好了。
  • 感谢您的澄清。如果您不介意,我会将此信息添加到答案中。
  • 我绝对不介意 - StackOverflow 的主要目标是协作策划对问题的良好答案。干得好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 2018-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多