我有一个对象限制,定义如下
hasYear some integer[minLength 2, maxLength 4, >=1995, <=2012]
如何使用Jena读取限制中定义的各个值。
答案 0 :(得分:4)
您可以使用不同的方法。首先,您可以通过以下代码遍历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(感谢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
}