Вопрос

I'm using an ontology in which I created a class named "USER". This class contains a datatype property named "gender". Then I inserted two allowed values for this datatype which are "male" and "female". I wanted to extract these datatype property values in order to insert them into a web page. But I didn't find the right method to do it. I don't even know if it's possible to read these values either.
Here is a part of my ontology. I would be really thankful for any help or suggestions.

<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: I'm not talking about instances, it's just about allowed values for a datatype property of a class :) and I'm using the Jena API.


Many thanks for your help, that was really awesome but i still can't get my values :( i also fixed my classe's name and my property's name as well . but i'm always gatting the following error:

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

i found 2 casting errors on the recent code so i tried to fix it. here is my code :

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: many thanks for your answer :)

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top