如何在耶拿获得具有限制的 ObjectProperty 的确切范围?
How to get in Jena the exact range of an ObjectProperty with restrictions?
我有一个 owl/rdf 模式,其对象属性具有范围限制,我无法使用 Jena API 获得有效范围 class.
我想获取 class 中定义的 ontology 为 属性 的范围。例如,使用以下架构:
<rdf:RDF xmlns="http://localhost/Test"
xml:base="http://localhost/TEST"
xmlns:sf="http://www.opengis.net/ont/sf#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owlgred="http://lumii.lv/2011/1.0/owlgred#">
<owl:Ontology rdf:about="http://localhost/TEST"/>
<owl:Class rdf:about="http://localhost/TEST#Aircraft"/>
<owl:Class rdf:about="http://localhost/TEST#Waypoint"/>
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
<owl:ObjectProperty rdf:about="http://localhost/TEST#hasWaypoint">
<rdfs:domain rdf:resource="http://localhost/TEST#Aircraft"/>
<rdfs:range>
<owl:Restriction>
<owl:onProperty rdf:resource="http://localhost/TEST#hasWaypoint"/>
<owl:someValuesFrom rdf:resource="http://localhost/TEST#Waypoint"/>
</owl:Restriction>
</rdfs:range>
</owl:ObjectProperty>
</rdf:RDF>
我正在做:
model.read(...);
OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
ExtendedIterator properties = property.listDomain();
OntClass thisClass = (OntClass) properties.next();
String dom_localname = thisClass.getLocalName();
if (thisClass.getLocalName() == null) {
Restriction restriction = thisClass.asRestriction();
OntResource resource = restriction.getOnProperty().getDomain();
dom_localname = resource.getLocalName();
}
properties = property.listRange();
thisClass = (OntClass) properties.next();
String range_localname = thisClass.getLocalName();
if (thisClass.getLocalName() == null) {
Restriction restriction = thisClass.asRestriction();
OntResource resource = restriction.getOnProperty().getRange();
range_localname = resource.getLocalName();
}
System.out.println("Domain localName: " + dom_localname);
System.out.println("Range localName: " + range_localname);
我期望得到这个结果:
Domain localName: Aircraft
Range localName: Waypoint
但我得到:
Domain localName: Aircraft
Range localName: null
如果我不使用限制,同样的代码可以毫无问题地工作,例如,使用以下模式:
<rdf:RDF xmlns="http://localhost/Test"
xml:base="http://localhost/TEST"
xmlns:sf="http://www.opengis.net/ont/sf#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owlgred="http://lumii.lv/2011/1.0/owlgred#">
<owl:Ontology rdf:about="http://localhost/TEST"/>
<owl:Class rdf:about="http://localhost/TEST#Aircraft"/>
<owl:Class rdf:about="http://localhost/TEST#Waypoint"/>
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
<owl:ObjectProperty rdf:about="http://localhost/TEST#hasWaypoint">
<rdfs:domain rdf:resource="http://localhost/TEST#Aircraft"/>
<rdfs:range rdf:resource="http://localhost/TEST#Waypoint"/>
</owl:ObjectProperty>
</rdf:RDF>
我得到了预期的结果:
Domain localName: Aircraft
Range localName: Waypoint
看来我没有正确处理 属性 使用限制的情况。我做错了什么?
根据上面的有用评论,它现在可以使用了。我在做:
model.read(...);
OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
ExtendedIterator properties = property.listRange();
OntClass thisClass = (OntClass) properties.next();
String range_localname = thisClass.getLocalName();
if (range_localname == null) {
Resource resource = null;
Restriction restriction = thisClass.asRestriction();
if (restriction.isAllValuesFromRestriction()) {
AllValuesFromRestriction restriction1 = restriction.asAllValuesFromRestriction();
resource = restriction1.getAllValuesFrom();
} else if (restriction.isHasValueRestriction()) {
HasValueRestriction restriction1 = restriction.asHasValueRestriction();
resource = restriction1.getHasValue().asResource();
} else if (restriction.isSomeValuesFromRestriction()) {
SomeValuesFromRestriction restriction1 = restriction.asSomeValuesFromRestriction();
resource = restriction1.getSomeValuesFrom();
} else if (restriction.isMaxCardinalityRestriction()) {
MaxCardinalityRestriction restriction1 = restriction.asMaxCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isMinCardinalityRestriction()) {
MinCardinalityRestriction restriction1 = restriction.asMinCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isCardinalityRestriction()) {
CardinalityRestriction restriction1 = restriction.asCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
}
if (resource != null) {
range_localname = resource.getLocalName();
}
}
System.out.println("Range localName: " + range_localname);
我有正确的结果:
Range localName: Waypoint
补全前面的答案,如果限制是合格的,Jena 没有直接获取范围的方法。但你可以这样做:
model.read(...);
OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
ExtendedIterator properties = property.listRange();
OntClass thisClass = (OntClass) properties.next();
String range_localname = thisClass.getLocalName();
if (range_localname == null) {
Resource resource = null;
Restriction restriction = thisClass.asRestriction();
if (restriction.isAllValuesFromRestriction()) {
AllValuesFromRestriction restriction1 = restriction.asAllValuesFromRestriction();
resource = restriction1.getAllValuesFrom();
} else if (restriction.isHasValueRestriction()) {
HasValueRestriction restriction1 = restriction.asHasValueRestriction();
resource = restriction1.getHasValue().asResource();
} else if (restriction.isSomeValuesFromRestriction()) {
SomeValuesFromRestriction restriction1 = restriction.asSomeValuesFromRestriction();
resource = restriction1.getSomeValuesFrom();
} else if (restriction.isMaxCardinalityRestriction()) {
MaxCardinalityRestriction restriction1 = restriction.asMaxCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isMinCardinalityRestriction()) {
MinCardinalityRestriction restriction1 = restriction.asMinCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isCardinalityRestriction()) {
CardinalityRestriction restriction1 = restriction.asCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else {
// qualified restrictions can be handled like that
RDFNode node = restriction.getPropertyValue(OWL2.onClass);
resource = node.asResource();
}
if (resource != null) {
range_localname = resource.getLocalName();
}
}
System.out.println("Range localName: " + range_localname);
我有一个 owl/rdf 模式,其对象属性具有范围限制,我无法使用 Jena API 获得有效范围 class.
我想获取 class 中定义的 ontology 为 属性 的范围。例如,使用以下架构:
<rdf:RDF xmlns="http://localhost/Test"
xml:base="http://localhost/TEST"
xmlns:sf="http://www.opengis.net/ont/sf#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owlgred="http://lumii.lv/2011/1.0/owlgred#">
<owl:Ontology rdf:about="http://localhost/TEST"/>
<owl:Class rdf:about="http://localhost/TEST#Aircraft"/>
<owl:Class rdf:about="http://localhost/TEST#Waypoint"/>
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
<owl:ObjectProperty rdf:about="http://localhost/TEST#hasWaypoint">
<rdfs:domain rdf:resource="http://localhost/TEST#Aircraft"/>
<rdfs:range>
<owl:Restriction>
<owl:onProperty rdf:resource="http://localhost/TEST#hasWaypoint"/>
<owl:someValuesFrom rdf:resource="http://localhost/TEST#Waypoint"/>
</owl:Restriction>
</rdfs:range>
</owl:ObjectProperty>
</rdf:RDF>
我正在做:
model.read(...);
OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
ExtendedIterator properties = property.listDomain();
OntClass thisClass = (OntClass) properties.next();
String dom_localname = thisClass.getLocalName();
if (thisClass.getLocalName() == null) {
Restriction restriction = thisClass.asRestriction();
OntResource resource = restriction.getOnProperty().getDomain();
dom_localname = resource.getLocalName();
}
properties = property.listRange();
thisClass = (OntClass) properties.next();
String range_localname = thisClass.getLocalName();
if (thisClass.getLocalName() == null) {
Restriction restriction = thisClass.asRestriction();
OntResource resource = restriction.getOnProperty().getRange();
range_localname = resource.getLocalName();
}
System.out.println("Domain localName: " + dom_localname);
System.out.println("Range localName: " + range_localname);
我期望得到这个结果:
Domain localName: Aircraft
Range localName: Waypoint
但我得到:
Domain localName: Aircraft
Range localName: null
如果我不使用限制,同样的代码可以毫无问题地工作,例如,使用以下模式:
<rdf:RDF xmlns="http://localhost/Test"
xml:base="http://localhost/TEST"
xmlns:sf="http://www.opengis.net/ont/sf#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owlgred="http://lumii.lv/2011/1.0/owlgred#">
<owl:Ontology rdf:about="http://localhost/TEST"/>
<owl:Class rdf:about="http://localhost/TEST#Aircraft"/>
<owl:Class rdf:about="http://localhost/TEST#Waypoint"/>
<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing"/>
<owl:ObjectProperty rdf:about="http://localhost/TEST#hasWaypoint">
<rdfs:domain rdf:resource="http://localhost/TEST#Aircraft"/>
<rdfs:range rdf:resource="http://localhost/TEST#Waypoint"/>
</owl:ObjectProperty>
</rdf:RDF>
我得到了预期的结果:
Domain localName: Aircraft
Range localName: Waypoint
看来我没有正确处理 属性 使用限制的情况。我做错了什么?
根据上面的有用评论,它现在可以使用了。我在做:
model.read(...);
OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
ExtendedIterator properties = property.listRange();
OntClass thisClass = (OntClass) properties.next();
String range_localname = thisClass.getLocalName();
if (range_localname == null) {
Resource resource = null;
Restriction restriction = thisClass.asRestriction();
if (restriction.isAllValuesFromRestriction()) {
AllValuesFromRestriction restriction1 = restriction.asAllValuesFromRestriction();
resource = restriction1.getAllValuesFrom();
} else if (restriction.isHasValueRestriction()) {
HasValueRestriction restriction1 = restriction.asHasValueRestriction();
resource = restriction1.getHasValue().asResource();
} else if (restriction.isSomeValuesFromRestriction()) {
SomeValuesFromRestriction restriction1 = restriction.asSomeValuesFromRestriction();
resource = restriction1.getSomeValuesFrom();
} else if (restriction.isMaxCardinalityRestriction()) {
MaxCardinalityRestriction restriction1 = restriction.asMaxCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isMinCardinalityRestriction()) {
MinCardinalityRestriction restriction1 = restriction.asMinCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isCardinalityRestriction()) {
CardinalityRestriction restriction1 = restriction.asCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
}
if (resource != null) {
range_localname = resource.getLocalName();
}
}
System.out.println("Range localName: " + range_localname);
我有正确的结果:
Range localName: Waypoint
补全前面的答案,如果限制是合格的,Jena 没有直接获取范围的方法。但你可以这样做:
model.read(...);
OntProperty property = model.getOntProperty("http://localhost/TEST#hasWaypoint");
ExtendedIterator properties = property.listRange();
OntClass thisClass = (OntClass) properties.next();
String range_localname = thisClass.getLocalName();
if (range_localname == null) {
Resource resource = null;
Restriction restriction = thisClass.asRestriction();
if (restriction.isAllValuesFromRestriction()) {
AllValuesFromRestriction restriction1 = restriction.asAllValuesFromRestriction();
resource = restriction1.getAllValuesFrom();
} else if (restriction.isHasValueRestriction()) {
HasValueRestriction restriction1 = restriction.asHasValueRestriction();
resource = restriction1.getHasValue().asResource();
} else if (restriction.isSomeValuesFromRestriction()) {
SomeValuesFromRestriction restriction1 = restriction.asSomeValuesFromRestriction();
resource = restriction1.getSomeValuesFrom();
} else if (restriction.isMaxCardinalityRestriction()) {
MaxCardinalityRestriction restriction1 = restriction.asMaxCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isMinCardinalityRestriction()) {
MinCardinalityRestriction restriction1 = restriction.asMinCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else if (restriction.isCardinalityRestriction()) {
CardinalityRestriction restriction1 = restriction.asCardinalityRestriction();
resource = restriction1.getIsDefinedBy();
} else {
// qualified restrictions can be handled like that
RDFNode node = restriction.getPropertyValue(OWL2.onClass);
resource = node.asResource();
}
if (resource != null) {
range_localname = resource.getLocalName();
}
}
System.out.println("Range localName: " + range_localname);