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

onthologies: also added new entries to generic ('oneOf') classes

parent d08ec2b5
No related branches found
No related tags found
No related merge requests found
Pipeline #58442 passed
......@@ -4,9 +4,15 @@ class TOAR_Ontology
===================
"""
__author__ = "Sabine Schroeder"
__version__ = "0.9.1"
__maintainer__ = "Sabine Schroeder"
__email__ = "s.schroeder@fz-juelich.de"
import rdflib
from rdflib import URIRef, Literal
from rdflib.namespace import RDF, RDFS, SKOS
from rdflib import URIRef, Literal, BNode
from rdflib.namespace import RDF, RDFS, SKOS, OWL
from rdflib.collection import Collection
class TOAR_Ontology:
"""
......@@ -27,22 +33,25 @@ class TOAR_Ontology:
get_class(title):
find the onthology class for the given title
def show_class_definition(ont_class):
get_class_by_id(self, id):
find the onthology class for the given id
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(self, ont_class, idcount, label, definition):
add entry to given ontology class
def check_entry(entry, ont_class):
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(self, filename):
save the onthology in json-ld format to the given file
"""
......@@ -58,12 +67,21 @@ class TOAR_Ontology:
"""
find the onthology class for the given title
"""
for s, p, o in self.graph.triples( (None, RDF.type, rdflib.URIRef("http://www.w3.org/2002/07/owl#Class") ) ):
for s, p, o in self.graph.triples( (None, RDF.type, OWL.Class) ):
value = self.graph.value(subject=s,predicate=RDFS.label)
if value and value.strip() == title.strip():
ont_class = s
return ont_class
def get_class_by_id(self, id):
"""
find the onthology class for the given id
"""
for s, p, o in self.graph.triples( (None, RDF.type, OWL.Class) ):
if s.strip() == id.strip():
ont_class = s
return ont_class
def show_class_definition(self, ont_class):
"""
show information on given ontology class
......@@ -85,15 +103,33 @@ class TOAR_Ontology:
"""
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")
# still to do:
# how to get rid of "http://www.fz-juelich.de/ontologies/2020/ToarOntotology"?!
subject=URIRef(f"http://www.fz-juelich.de/ontologies/2020/ToarOntotology#OWLNamedIndividual_{idcount:06d}")
p=RDF.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)))
self.graph.add((subject,p,o))
o=OWL.NamedIndividual
self.graph.add((subject,p,o))
self.graph.add((subject,RDFS.label,Literal(label,lang="en")))
self.graph.add((subject,SKOS.definition,Literal(definition)))
# if there is an equivalent class: add the entry also there!
for s, p, o in self.graph.triples( (ont_class, OWL.equivalentClass, None) ):
equivalentClass = self.get_class_by_id(o)
# get oneOf-List of equivalentClass
for s, p, o in self.graph.triples( (equivalentClass, OWL.oneOf, None) ):
c = Collection(self.graph, BNode(o))
c.append(subject)
# The following is just an example how to iterate through a linked list
# # The obtained list is a linked list,
# # consisting of the first element and the rest (which is a linked list again)
# newSubject = o
# while newSubject != RDF.nil:
# for s, p, o in self.graph.triples( (newSubject, RDF.first, None) ):
# print(f"first {newSubject}: s={s}, p={p}, o={o}")
# for s, p, o in self.graph.triples( (newSubject, RDF.rest, None) ):
# print(f"rest {newSubject}: s={s}, p={p}, o={o}")
# newSubject=o
def check_entry(self, entry, ont_class):
"""
......
......@@ -28,5 +28,4 @@ onto.add_entry(ccclass,196,'SX', 'Sint Maarten (Dutch part)')
onto.add_entry(ccclass,197,'BE', 'Belgium')
onto.check_entry('BE',ccclass)
onto.show_entries(ccclass)
#nun hier entsprechendes für Timezone
onto.save('TOAR-Ontology-TimeZone-CountryCode_20210128.owl')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment