Skip to content
Snippets Groups Projects
Commit 61a8c244 authored by Sabine Schröder's avatar Sabine Schröder
Browse files

enabled saving onthologies in different formats

parent 6a595f00
No related branches found
No related tags found
No related merge requests found
Pipeline #58450 passed
......@@ -33,7 +33,7 @@ class TOAR_Ontology:
get_class(title):
find the onthology class for the given title
get_class_by_id(self, id):
get_class_by_id(id):
find the onthology class for the given id
show_class_definition(ont_class):
......@@ -42,7 +42,7 @@ class TOAR_Ontology:
show_entries(ont_class):
show information on given ontology class
add_entry(self, ont_class, idcount, label, definition):
add_entry(ont_class, idcount, label, definition):
add entry to given ontology class
check_entry(entry, ont_class):
......@@ -51,14 +51,24 @@ class TOAR_Ontology:
print(format=""):
Prints the onthology in the given format
save(self, filename):
save the onthology in json-ld format to the given file
save(filename, format):
save the onthology in given format to the given file
"""
def __init__(self, filename):
"""
initialize the onthology
Parameters
----------
filename: str
filename containing the ontology in json-ld format
Returns
-------
TOAR_Ontology
"""
self.filename = filename
g = rdflib.Graph()
self.graph = g.parse(self.filename,format='json-ld')
......@@ -66,6 +76,15 @@ class TOAR_Ontology:
def get_class(self, title):
"""
find the onthology class for the given title
Parameters
----------
title: str
title of class
Returns
-------
OWL.Class
"""
for s, p, o in self.graph.triples( (None, RDF.type, OWL.Class) ):
value = self.graph.value(subject=s,predicate=RDFS.label)
......@@ -76,6 +95,15 @@ class TOAR_Ontology:
def get_class_by_id(self, id):
"""
find the onthology class for the given id
Parameters
----------
id: str
id of class
Returns
-------
OWL.Class
"""
for s, p, o in self.graph.triples( (None, RDF.type, OWL.Class) ):
if s.strip() == id.strip():
......@@ -85,6 +113,15 @@ class TOAR_Ontology:
def show_class_definition(self, ont_class):
"""
show information on given ontology class
Parameters
----------
ont_class: OWL.class
onthology class
Returns
-------
None
"""
for s, p, o in self.graph.triples( (ont_class, SKOS.definition, None) ):
print(f"definition of {ont_class}: {o}")
......@@ -92,6 +129,15 @@ class TOAR_Ontology:
def show_entries(self, ont_class):
"""
show information on given ontology class
Parameters
----------
ont_class: OWL.class
onthology class
Returns
-------
None
"""
for s, p, o in self.graph.triples( (None, RDF.type, ont_class ) ):
label=self.graph.value(subject=s,predicate=RDFS.label)
......@@ -102,6 +148,21 @@ class TOAR_Ontology:
def add_entry(self, ont_class, idcount, label, definition):
"""
add entry to given ontology class
Parameters
----------
ont_class: OWL.class
onthology class
id_count: int
integer for constructing ID "OWLNamedIndividual_nnnnnn"
label: str
title of entry to be inserted
definition: str
definition of entry to be inserted
Returns
-------
None
"""
# still to do:
# how to get rid of "http://www.fz-juelich.de/ontologies/2020/ToarOntotology"?!
......@@ -134,6 +195,17 @@ class TOAR_Ontology:
def check_entry(self, entry, ont_class):
"""
check whether entry is a valid value of the ontology
Parameters
----------
entry: str
title of entry to be checked
ont_class: OWL.class
onthology class
Returns
-------
None
"""
found=False
for s, p, o in self.graph.triples( (None, RDF.type, ont_class ) ):
......@@ -144,15 +216,35 @@ class TOAR_Ontology:
def print(self, format='json-ld'):
"""
print the onthology in the given format
Parameters
----------
format: str, optional
output format (default: json-ld)
Returns
-------
None
"""
print(self.graph.serialize(format=format).decode("utf-8"))
def save(self, filename):
def save(self, filename, format='json-ld'):
"""
save the onthology in json-ld format to the given file
Parameters
----------
filename: str
output file name
format: str, optional
output format (default: json-ld)
Returns
-------
None
"""
outfile = open(filename,'w')
outfile.write(self.graph.serialize(format='json-ld').decode("utf-8"))
outfile.write(self.graph.serialize(format=format).decode("utf-8"))
# #check_entry would have been nicer, if something like this worked:
# subj2 = rdflib.URIRef("http://www.fz-juelich.de/ontologies/2020/ToarOntotology#OWLNamedIndividual_000141")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment