3

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.

Ignazio
  • 10,504
  • 1
  • 14
  • 25
abra
  • 153
  • 2
  • 10

1 Answers1

1

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

Nicholas Car
  • 1,164
  • 4
  • 7
  • Thanks for helping out! I know owlready2 and rdflib are different things, but since I was trying to use a command from one with the other, I wanted to check with both user-bases to see if someone had experience getting them to work together. I tried your second solution and it worked perfectly, thanks! – abra May 12 '20 at 09:43