使用Jena解析Protege生成的枚举数据类型

时间:2013-02-22 07:12:36

标签: rdf jena ontology protege

我有一个由Protege 4.2.0生成的Ontology文件。它包括如下定义的DatatypeProperty。

<owl:DatatypeProperty rdf:about="http://example.com/NLPSchema.owl#race">
    <rdf:type rdf:resource="&owl;FunctionalProperty"/>
    <rdfs:domain rdf:resource="http://example.com/NLPSchema.owl#Person"/>
    <rdfs:subPropertyOf rdf:resource="http://example.com/NLPSchema.owl#semanticProperty"/>
    <rdfs:range>
        <rdfs:Datatype>
            <owl:oneOf>
                <rdf:Description>
                    <rdf:type rdf:resource="&rdf;List"/>
                    <rdf:first>african_american</rdf:first>
                    <rdf:rest>
                        <rdf:Description>
                            <rdf:type rdf:resource="&rdf;List"/>
                            <rdf:first>asian</rdf:first>
                            <rdf:rest>
                                <rdf:Description>
                                    <rdf:type rdf:resource="&rdf;List"/>
                                    <rdf:first>caucasian</rdf:first>
                                    <rdf:rest>
                                        <rdf:Description>
                                            <rdf:type rdf:resource="&rdf;List"/>
                                            <rdf:first>hispanic</rdf:first>
                                            <rdf:rest>
                                                <rdf:Description>
                                                    <rdf:type rdf:resource="&rdf;List"/>
                                                    <rdf:first>other</rdf:first>
                                                    <rdf:rest rdf:resource="&rdf;nil"/>
                                                </rdf:Description>
                                            </rdf:rest>
                                        </rdf:Description>
                                    </rdf:rest>
                                </rdf:Description>
                            </rdf:rest>
                        </rdf:Description>
                    </rdf:rest>
                </rdf:Description>
            </owl:oneOf>
        </rdfs:Datatype>
    </rdfs:range>
</owl:DatatypeProperty>

在Protege中,它看起来像这样:

Protégé screen capture

现在我正在使用Jena来解析Ontology文件。我能够获得对应于“range”标签的OntClass对象:

DatatypeProperty p = ontModel.getDatatypeProperty("http://example.com/NLPSchema.owl#race");
OntClass range = p.getRange().asClass();    

然后我怎样才能获得像Protege那样的精彩枚举数组{“african_american”,“asian”,“Caucasian”,“hispanic”,“other”}?

我知道DataRange有一个名为“listOneOf”的方法,但是我不知道如何创建一个DataRange对象,至少“p.isDataRange()”返回false。

2 个答案:

答案 0 :(得分:2)

考虑这个简单的本体,只是声明一个DatatypeProperty,hasFlavor(首先,在RDF / XML中,因为这是你使用的):

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://example.org/icecream#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Ontology rdf:about="http://example.org/icecream"/>
  <owl:DatatypeProperty rdf:about="http://example.org/icecream#hasFlavor">
    <rdfs:range>
      <rdfs:Datatype>
        <owl:oneOf>
          <rdf:List>
            <rdf:first>chocolate</rdf:first>
            <rdf:rest>
              <rdf:List>
                <rdf:first>strawberry</rdf:first>
                <rdf:rest>
                  <rdf:List>
                    <rdf:first>vanilla</rdf:first>
                    <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
                  </rdf:List>
                </rdf:rest>
              </rdf:List>
            </rdf:rest>
          </rdf:List>
        </owl:oneOf>
      </rdfs:Datatype>
    </rdfs:range>
  </owl:DatatypeProperty>
</rdf:RDF>

又一次,但这次是在Turtle,因为它更容易阅读:

@prefix :      <http://example.org/icecream#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:hasFlavor  a       owl:DatatypeProperty ;
        rdfs:range  [ a          rdfs:Datatype ;
                      owl:oneOf  [ a          rdf:List ;
                                   rdf:first  "chocolate" ;
                                   rdf:rest   [ a          rdf:List ;
                                                rdf:first  "strawberry" ;
                                                rdf:rest   [ a          rdf:List ;
                                                             rdf:first  "vanilla" ;
                                                             rdf:rest   ()

                                                           ]
                                              ]
                                 ]
                    ] .

<http://example.org/icecream>
        a       owl:Ontology .

Protégé中的重要财产及其领域:

hasFlavor property in Protégé

现在,您需要做的就是访问枚举的rdf:List资源。 hasFlavor的{​​{1}}为rdfs:rangeowl:Datatypeowl:Datatype的枚举相关。

owl:oneOf

这会产生输出:

import java.util.List;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFList;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDFS;

public class ExtractEnumeratedDatatypeElements {
    public static void main(String[] args) {
        // Load the ontology.
        final OntModel iceCream = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        iceCream.read( "file:///home/taylorj/tmp/ontologies/icecream/icecream.owl" );

        // Get to the property and the datatype that is its range.
        final DatatypeProperty hasFlavor = iceCream.createDatatypeProperty( "http://example.org/icecream#hasFlavor" );
        final Resource datatype = hasFlavor.getPropertyResourceValue( RDFS.range );

        // The datatype is related to a list of values by owl:oneOf.
        final RDFList enumeration = datatype.getPropertyResourceValue( OWL.oneOf ).as( RDFList.class );

        // The RDFList can be converted to a Java List.
        final List<RDFNode> list = enumeration.asJavaList();
        System.out.println( list );
    }
}

答案 1 :(得分:1)

删除

<rdf:type rdf:resource="&rdf;List"/>

这可以阻止Turtle中的紧凑符号。

@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://example.com/NLPSchema.owl#race>
      a       owl:FunctionalProperty , owl:DatatypeProperty ;
      rdfs:domain <http://example.com/NLPSchema.owl#Person> ;
      rdfs:range
              [ a       rdfs:Datatype ;
                owl:oneOf ("african_american" "asian" "caucasian" "hispanic" "other")
              ] ;
      rdfs:subPropertyOf <http://example.com/NLPSchema.owl#semanticProperty> .