6

I have an ontology in an 'owl' file (nif.owl). I am familiar with Java, but it kept crashing; therefore, I tried using Python. However, since I have not used Python before, I am not sure if I am loading the ontology correctly!

Here is the part that I believe is related to loading the ontology:

g = rdflib.Graph()
g.parse ('nif.owl', format='xml')
nif = rdflib.Namespace('http://purl.org/nif/ontology/nif.owl')
g.bind('nif', nif)

I believe the problem is where the g.parse sets the format to 'xml'. I think maybe the 'xml' is wrong.

I have also attached the header of the ontology file as an image.

enter image description here

The reason I think there is a mistake with the code is the result I get which I showed in the image below: enter image description here

Thanks!

PS: Below is the full code in case there is something wrong with it:

import logging
import rdflib
import time

logging.basicConfig()
logger = logging.getLogger('logger')
logger.warning('The system may break down')

start_time = time.time()

g = rdflib.Graph()
g.parse ('nif.owl', format='xml')
nif = rdflib.Namespace('http://purl.org/nif/ontology/nif.owl')
g.bind('nif', nif)
query = """
select distinct ?p 
where { ?s ?p ?o}
        LIMIT 5
        """
result = g.query(query)
print(result.serialize(format='csv'))

print("--- %s seconds ---" % (time.time() - start_time))
16
  • 1
    AFAICT the code does exactly what it should be expected to do. It’s just looking weird because the print statement formats the single-column CSV output into a single line. See the question I linked above for the solution to that.
    – cygri
    Commented Jun 17, 2019 at 13:29
  • 1
    nothing has "crashing" here - it's just your broken output of the query result. Commented Jun 17, 2019 at 13:58
  • 1
    that said, are you sure rdflib is the appropriate API here? I might be wrong, but your ontology basically contains just a set of owl:import statements - this, in fact is a feature of OWL 2 - I doubt that rdflib will handle those imports is intended given that rdflib is designed for RDF. I'm pretty sure it will not load all imports into the graph. Honestly, if you don't need SPARQL, a dedicate OWL lib like owlready2 would be the better way to work on OWL ontologies. Commented Jun 17, 2019 at 14:00
  • 1
    Not sure where you were looking, but Protege supports OWL and SPARQL. And the latest version is 5.5.0 - whether the ontology fits into memory, I don't know. Commented Jun 18, 2019 at 12:33
  • 1
    if you still want to use rdflib, the only workaround is to load all the ontologies into the same graph. Either you provide the list of ontology URLs and use a loop that adds the data, or you make use the owl:import statements, i.e. use the RDF/SPARQL capabilities of rdflib and get the import URLs from the ontology document. (this indeed has to be done transitively if the imported ontologies do import other ontologies) Commented Jun 18, 2019 at 12:36

1 Answer 1

4

There is nothing wrong with your code except that the format should be format='application/rdf+xml'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.