Search code examples
javapythonrdfowlontology

While using Jena (java) or RDFLib (python), how should I find out whether to open the ontology as 'turtle' or 'xml'?


Ontology files usually have extensions such as .owl or .rdf.

I want to know when I should open ontologies with 'turtle' and when with 'xml' or other formats? Because it seems that each of them is useful for a type of format and unfortunately, it seems that people sometimes save files with the wrong extension.

Here is a sample code in Python (but java is not very different either):

g.parse('ontology.owl', format='turtle')

So, how do I know here the turtle is the correct format?

Thanks in advance, RF


Solution

  • You are opening RDF files, not ontologies.

    RDF is an abstract data model. It has several serialization formats:

    1. RDF/XML:
    <?xml version="1.0"?>
    <rdf:RDF xmlns="http://example.com/ontology#"
         xml:base="http://example.com/ontology"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:owl="http://www.w3.org/2002/07/owl#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
        <owl:Ontology rdf:about="http://example.com/ontology"/>
    
        <owl:Class rdf:about="http://example.com/ontology#Person"/>
        <owl:Class rdf:about="http://example.com/ontology#Woman">
            <rdfs:subClassOf rdf:resource="http://example.com/ontology#Person"/>
        </owl:Class>
    </rdf:RDF>
    
    1. Turtle:
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    
    <http://example.com/ontology> a owl:Ontology .
    <http://example.com/ontology#Person> a owl:Class .
    <http://example.com/ontology#Woman>
        a owl:Class ;
        rdfs:subClassOf <http://example.com/ontology#Person> .
    
    1. JSON-LD:
    [ {
      "@id" : "http://example.com/ontology",
      "@type" : [ "http://www.w3.org/2002/07/owl#Ontology" ]
    }, {
      "@id" : "http://example.com/ontology#Person",
      "@type" : [ "http://www.w3.org/2002/07/owl#Class" ]
    }, {
      "@id" : "http://example.com/ontology#Woman",
      "@type" : [ "http://www.w3.org/2002/07/owl#Class" ],
      "http://www.w3.org/2000/01/rdf-schema#subClassOf" : [ {
        "@id" : "http://example.com/ontology#Person"
      } ]
    } ]
    

    Open your file with a text editor, view what your file content looks similar to, and select approptiate option. You can use this online service to convert RDF files from one serialization format to another.

    RDF (abstract) syntax is not the only syntax for OWL ontologies. There are few others:

    1. OWL/XML:
    <?xml version="1.0"?>
    <Ontology xmlns="http://www.w3.org/2002/07/owl#"
         xml:base="http://example.com/ontology"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:xml="http://www.w3.org/XML/1998/namespace"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
         ontologyIRI="http://example.com/ontology">
        <Prefix name="" IRI="http://example.com/ontology#"/>
        <Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
        <Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
        <Prefix name="xml" IRI="http://www.w3.org/XML/1998/namespace"/>
        <Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
        <Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
        <Declaration>
            <Class IRI="#Person"/>
        </Declaration>
        <Declaration>
            <Class IRI="#Woman"/>
        </Declaration>
        <SubClassOf>
            <Class IRI="#Woman"/>
            <Class IRI="#Person"/>
        </SubClassOf>
    </Ontology>
    
    1. Functional syntax:
    Prefix(:=<http://example.com/ontology#>)
    Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
    Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
    Prefix(xml:=<http://www.w3.org/XML/1998/namespace>)
    Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
    Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
    
    Ontology(<http://example.com/ontology>
    
    Declaration(Class(:Person))
    Declaration(Class(:Woman))
    
    SubClassOf(:Woman :Person)
    )
    
    1. Manchester syntax:
    Prefix: : <http://example.com/ontology#>
    Prefix: owl: <http://www.w3.org/2002/07/owl#>
    Prefix: rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    Prefix: rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    Prefix: xml: <http://www.w3.org/XML/1998/namespace>
    Prefix: xsd: <http://www.w3.org/2001/XMLSchema#>
    
    Ontology: <http://example.com/ontology>
    
    Class: Person
    
    Class: Woman
    
    SubClassOf: 
        Person
    

    AFAIK, one can not parse files in these formats using RDFlib. You can use this online service to convert OWL files between these formats (and RDF formats).