是否可以读取类的数据类型属性的允许值?

时间:2013-05-04 01:03:47

标签: java rdf jena ontology rdfs

我正在使用一个本体,我在其中创建了一个名为“USER”的类。此类包含名为“gender”的数据类型属性。然后我为这个数据类型插入了两个允许的值,即“male”和“female”。我想提取这些数据类型属性值,以便将它们插入到网页中。但我没有找到合适的方法来做到这一点。我甚至不知道是否可以读取这些值。
这是我的本体论的一部分。我会非常感谢任何帮助或建议。

<owl:DatatypeProperty rdf:ID="Gender">
    <rdfs:domain rdf:resource="#USER"/>
    <rdfs:range>
      <owl:DataRange>
        <owl:oneOf rdf:parseType="Resource">
          <rdf:first rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
          >male</rdf:first>
          <rdf:rest rdf:parseType="Resource">
            <rdf:first rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
            >female</rdf:first>
            <rdf:rest rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"/>
          </rdf:rest>
        </owl:oneOf>
      </owl:DataRange>
    </rdfs:range>
  </owl:DatatypeProperty>

ps:我不是在谈论实例,它只是关于类的数据类型属性的允许值:)我正在使用Jena API。


非常感谢你的帮助,这真的很棒,但我仍然无法得到我的价值观:(我也修复了我的classe的名字和我的财产的名字。但我总是收到以下错误:

Exception in thread "main" com.hp.hpl.jena.shared.NoReaderForLangException: 20 Turtle
    at com.hp.hpl.jena.rdf.model.impl.RDFReaderFImpl.getReader(RDFReaderFImpl.java:98)
    at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:215)
    at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:2098)
    at test1.SafwenExample.run(SafwenExample.java:36)
    at test1.SafwenExample.main(SafwenExample.java:31)
Java Result: 1

我在最近的代码中发现了2个投射错误,因此我尝试修复它。这是我的代码:

package test1;

import java.io.StringReader;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class SafwenExample
{
    public static String NS = "http://www.owl-ontologies.com/Ontology_profile.owl#";

    public static String SOURCE = "@prefix : <http://www.owl-ontologies.com/Ontologyprofile.owl#> .\n" +
            "@prefix owl: <http://www.w3.org/2002/07/owl#>.\n" +
            "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n" +
            "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n" +
            ":User a owl:Class.\n" +
            ":gender a owl:DatatypeProperty ;\n" +
            "  rdfs:domain :User ;\n" +
            "  rdfs:range [\n" +
            "    a owl:DataRange ;\n" +
            "    owl:oneOf ( \"male\"^^xsd:string \"female\"^^xsd:string )\n" +
            "].";

    public static void main( String[] args ) {
        new SafwenExample().run();
    }

    public void run() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
        m.read( new StringReader( SOURCE ), null, "Turtle" );

        OntProperty gender = m.getOntProperty( NS + "gender" );

        DataRange genderRange = (DataRange) gender.getRange().as( DataRange.class );
        for (ExtendedIterator i = genderRange.listOneOf(); i.hasNext(); ) {
            Literal l = (Literal) i.next();
            System.out.println( "DataRange has value " + l.getLexicalForm() +
                                " with datatype: " + l.getDatatype() );
        }
    }
}

Ps:非常感谢您的回答:)

1 个答案:

答案 0 :(得分:3)

是的,你可以使用Jena的Ontology API

来做到这一点
package example;

import java.io.StringReader;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class SafwenExample
{
    public static String NS = "http://example.com/example#";

    public static String SOURCE = "@prefix : <http://example.com/example#> .\n" +
            "@prefix owl: <http://www.w3.org/2002/07/owl#>.\n" +
            "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n" +
            "@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.\n" +
            ":User a owl:Class.\n" +
            ":gender a owl:DatatypeProperty ;\n" +
            "  rdfs:domain :User ;\n" +
            "  rdfs:range [\n" +
            "    a owl:DataRange ;\n" +
            "    owl:oneOf ( \"male\"^^xsd:string \"female\"^^xsd:string )\n" +
            "].";

    public static void main( String[] args ) {
        new SafwenExample().run();
    }

    public void run() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
        m.read( new StringReader( SOURCE ), null, "Turtle" );

        OntProperty gender = m.getOntProperty( NS + "gender" );

        DataRange genderRange = gender.getRange().as( DataRange.class );
        for (ExtendedIterator<Literal> i = genderRange.listOneOf(); i.hasNext(); ) {
            Literal l = i.next();
            System.out.println( "DataRange has value " + l.getLexicalForm() +
                                " with datatype: " + l.getDatatype() );
        }
    }
}

顺便说一下,我修改了你的名字:class User(领先大写,然后是camel case),属性gender(领先小写)。