diff --git a/toardb/utils/TOARII_ontology.py b/toardb/utils/TOARII_ontology.py index 7ba629bdef5923e865a8511baf76b173ee6f20e4..8ab38cb14efe43afcee1bb019fc5f2ae57a42c8c 100644 --- a/toardb/utils/TOARII_ontology.py +++ b/toardb/utils/TOARII_ontology.py @@ -5,45 +5,120 @@ class TOAR_Ontology """ import rdflib -from rdflib.namespace import RDF, RDFS - -g = rdflib.Graph() -result = g.parse("TOAR-II-Ontology_2020-11-24.owl",format='json-ld') -#print("graph has %s statements." % len(g)) +from rdflib import URIRef, Literal +from rdflib.namespace import RDF, RDFS, SKOS class TOAR_Ontology: """ - some information + A class to represent the TOAR onthology. + + ... + + Attributes + ---------- + filename : str + filename of file containing onthology (in json-ld-Format) + + graph : Graph + onthology graph + + Methods + ------- + get_class(title): + find the onthology class for the given title + + def show_class_definition(ont_class): + show information on given ontology class + + show_entries(ont_class): + show information on given ontology class + + def add_entry(self, ont_class, idcount, label, definition): + add entry to given ontology class + + def check_entry(entry, ont_class): + check whether entry is a valid value of the ontology + + print(format=""): + Prints the onthology in the given format + + def save(self, filename): + save the onthology in json-ld format to the given file """ - def get_class(title): + def __init__(self, filename): + """ + initialize the onthology + """ + self.filename = filename + g = rdflib.Graph() + self.graph = g.parse("TOAR-II-Ontology_2020-11-24.owl",format='json-ld') + + def get_class(self, title): """ - find the right class for the given title + find the onthology class for the given title """ - for s, p, o in g.triples( (None, RDF.type, rdflib.URIRef("http://www.w3.org/2002/07/owl#Class") ) ): - value = g.value(subject=s,predicate=RDFS.label) + for s, p, o in self.graph.triples( (None, RDF.type, rdflib.URIRef("http://www.w3.org/2002/07/owl#Class") ) ): + value = self.graph.value(subject=s,predicate=RDFS.label) + print(f"value: {value}, title: {title}") if value and value.strip() == title.strip(): ont_class = s return ont_class - def show_entries(ont_class): + def show_class_definition(self, ont_class): """ show information on given ontology class """ - print(f"show information on '{ont_class}'") - for s, p, o in g.triples( (None, RDF.type, ont_class ) ): - print("%s is %s"%(s, g.value(subject=s,predicate=RDFS.label))) + for s, p, o in self.graph.triples( (ont_class, SKOS.definition, None) ): + print(f"definition of {ont_class}: {o}") - def check_entry(entry, ont_class): + def show_entries(self, ont_class): + """ + show information on given ontology class + """ + for s, p, o in self.graph.triples( (None, RDF.type, ont_class ) ): + label=self.graph.value(subject=s,predicate=RDFS.label) + print(f"{s} is {label}") + for s, p, o in self.graph.triples( (s, SKOS.definition, None) ): + print(f"definition of {label}: {o}") + + def add_entry(self, ont_class, idcount, label, definition): + """ + add entry to given ontology class + """ + print(f"add entry to '{ont_class}'") + s=URIRef(f"http://www.fz-juelich.de/ontologies/2020/ToarOntotology#OWLNamedIndividual_{idcount:06d}") + p=URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") + o=URIRef(ont_class) + self.graph.add((s,p,o)) + o=URIRef("http://www.w3.org/2002/07/owl#NamedIndividual") + self.graph.add((s,p,o)) + self.graph.add((s,RDFS.label,Literal(label,lang="en"))) + self.graph.add((s,SKOS.definition,Literal(definition))) + + def check_entry(self, entry, ont_class): """ check whether entry is a valid value of the ontology """ found=False - for s, p, o in g.triples( (None, RDF.type, ont_class ) ): - if g.value(subject=s,predicate=RDFS.label).strip() == entry: + for s, p, o in self.graph.triples( (None, RDF.type, ont_class ) ): + if self.graph.value(subject=s,predicate=RDFS.label).strip() == entry: found=True return found + def print(self, format='json-ld'): + """ + print the onthology in the given format + """ + print(self.graph.serialize(format=format).decode("utf-8")) + + def save(self, filename): + """ + save the onthology in json-ld format to the given file + """ + outfile = open(filename,'w') + outfile.write(self.graph.serialize(format='json-ld').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") # pred2 = RDFS.label diff --git a/toardb/utils/onto_example.py b/toardb/utils/onto_example.py new file mode 100644 index 0000000000000000000000000000000000000000..16247184e11b8dec8de3342453a909a3821d1bc2 --- /dev/null +++ b/toardb/utils/onto_example.py @@ -0,0 +1,30 @@ +import TOARII_ontology +help(TOARII_ontology) +onto=TOARII_ontology.TOAR_Ontology('TOAR-II-Ontology_2020-11-24.owl') +help(onto) +onto.print() +onto.print(format="turtle") +onto.print(format='application/rdf+xml') +dlclass=onto.get_class('Station Dominant Landcover Type') +onto.show_class_definition(dlclass) +onto.show_entries(dlclass) +onto.check_entry('Croplands',dlclass) +onto.check_entry('Craplands',dlclass) +czclass=onto.get_class('Climatic Zone') +onto.show_class_definition(czclass) +onto.show_entries(czclass) +onto.check_entry('PolarMoist',czclass) +onto.check_entry('PolarWet',czclass) +onto=TOARII_ontology.TOAR_Ontology('TOAR-Ontology-TimeZone-CountryCode.owl') +dlclass=onto.get_class('Station Dominant Landcover Type') +onto.show_class_definition(dlclass) +onto.show_entries(dlclass) +ccclass=onto.get_class('Country Code') +ccclass=onto.get_class('Country Code') +onto.show_class_definition(ccclass) +onto.show_entries(ccclass) +#highest index of NamedInvidual is 194 +#==> start counting from here +onto.add_entry(ccclass,195,"GB","United Kingdom of Great Britain and Northern Ireland") +onto.show_entries(ccclass) +onto.save('TOAR-II-Ontology_2021-01-26.owl')