Search code examples
owlrdflib

import namespace from ".owl" file and use terms in RDFLIB graph


I am creating a graph using rdflib. I want to use some terms from a ".owl" file I have. How can I import this owl file as MyImportedTerminology with rdflib, and access its terms, so that I can do something like this in the graph?

g.add((Thing, OWL.sameAs, MyImportedTerminology.OtherThing))

I tried out owlready2, specifically:

MyImportedTerminology = get_ontology("file:///path/to/owl/file.owl").load()

But I can't seem to use it directly. I get the error: Object MyImportedTerminology.OtherThing must be an rdflib term

Any help with this would be appreciated.


Solution

  • So owlready2 is not the same things as RDFlib, which is what you've tagged this question with.

    RDFlib is lower-level than owlready2 and lets you build RDF triples using URIs, namespaces and literals directly. You don't need to import the OWL file to use terms from it in RDFlib, you just need to quote from your OWL file which you can do like this:

    from rdflib import URIref
    
    g.add((Thing, OWL.sameAs, URIRef("http://namespace-from-owl-file.org#OtherThing")))
    

    You can alternatively create a Namespace object for the namespace in your OWL file and do this:

    from rdflib import Namespace
    
    MYNS = Namespace("http://namespace-from-owl-file.org#")
    
    g.add((Thing, OWL.sameAs, MYNS.OtherThing))