def get_ontology_imports(self, ontology_file_path='./imports/'):
"""
Detects all the import files in a loaded OWL ontology graph and adds them to the graph.
Currently assumes imports are sitting in a folder called "imports" in parent folder of this script.
"""
query = rdflib.plugins.sparql.prepareQuery("""
SELECT distinct ?import_file
WHERE {?s owl:imports ?import_file . }
ORDER BY (?import_file)
""", initNs = self.namespace)
imports = self.graph.query(query, initNs = self.namespace)
print("It has %s import files ..." % len(imports))
for result_row in imports: # a rdflib.query.ResultRow
file = result_row.import_file.rsplit('/',1)[1]
file_path = ontology_file_path + '/' + file
try:
if os.path.isfile( file_path):
self.graph.parse(file_path)
else:
print ('WARNING:' + file_path + " could not be loaded! Does its ontology include purl have a corresponding local file? \n")
except rdflib.exceptions.ParserError as e:
print (file_path + " needs to be in RDF OWL format!")
评论列表
文章目录