简体   繁体   English

如何在耶拿中获得有限制的 ObjectProperty 的确切范围?

[英]How to get in Jena the exact range of an ObjectProperty with restrictions?

I have an owl/rdf schema with object properties with range restrictions, and I'm unable to get with the Jena API the effective range class.我有一个具有范围限制的 object 属性的 owl/rdf 模式,并且我无法使用 Jena API 获得有效范围 class。

I would like to get the classes which are defined in the ontology as the range of a property.我想将本体中定义的类作为属性的范围。 For example, with the following schema:例如,使用以下架构:

<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>

And I'm doing:我正在做:

  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);

I expected to get this result:我希望得到这个结果:

Domain localName: Aircraft
Range localName: Waypoint

But I get:但我得到:

Domain localName: Aircraft
Range localName: null

This same code works without any problem if I don't use restrictions, for example, with the following schema:如果我不使用限制,则相同的代码可以正常工作,例如,使用以下模式:

<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>

I get the expected result:我得到了预期的结果:

Domain localName: Aircraft
Range localName: Waypoint

It looks that I am not handling correctly cases where a property use a restriction.看起来我没有正确处理属性使用限制的情况。 What Am I doing wrong?我究竟做错了什么?

Per the useful comments above, it now works.根据上面有用的评论,它现在可以工作了。 I'm doing:我正在做:

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);

I have the right result:我有正确的结果:

Range localName: Waypoint

To complete the previous answer, if the restriction is qualified, Jena does not have a direct way to get the range.完成上一个答案,如果限制条件合格,Jena 没有直接的方法来获取范围。 But you can do:但你可以这样做:

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);

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

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