2

我有一个 Protege 4.2.0 生成的本体文件。它包括如下定义的 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é 屏幕截图

现在我正在使用 Jena 来解析 Ontology 文件。我能够获得与“范围”标签对应的 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。

4

2 回答 2

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é 中的重要属性及其领域:

Protégé 的 hasFlavor 属性

现在,您需要做的就是获取rdf:List枚举资源。 hasFlavor有一个rdfs:rangewhich 是一个owl:Datatype,并且owl: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 );
    }
}

这将产生输出:

[chocolate, strawberry, vanilla]
于 2013-09-25T20:01:19.420 回答
1

Remove the

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

this blocks compact notation in 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> .
于 2013-02-22T12:25:16.483 回答