1

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

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


非常感谢您的帮助,这真是太棒了,但我仍然无法获得我的价值观:(我还修复了我的班级名称和我的财产名称。但我总是遇到以下错误:

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:非常感谢您的回答:)

4

1 回答 1

3

Yes, you can do this using Jena's 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() );
        }
    }
}

By the way, I fixed your names: class User (leading upper case, then camel case), property gender (leading lowercase).

于 2013-05-04T09:44:01.493 回答