From abbdb8e760974ea30a4025c618bd8079596c9bdf Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Tue, 2 Aug 2022 23:51:37 +0200 Subject: [PATCH] Update demo (#437) Updated emmo2meta.py to match changes in EMMO. Support for value restrictions must be added... --- demo/horizontal/emmo2meta.py | 29 +++++++++++++++------- demo/horizontal/step1_generate_metadata.py | 5 +++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/demo/horizontal/emmo2meta.py b/demo/horizontal/emmo2meta.py index 7950f4a7e..9ca36036d 100644 --- a/demo/horizontal/emmo2meta.py +++ b/demo/horizontal/emmo2meta.py @@ -20,7 +20,7 @@ from dlite import Instance, Dimension, Property from ontopy import get_ontology -from ontopy.utils import asstring +from ontopy.utils import asstring, get_label import owlready2 # pylint: disable=wrong-import-order @@ -96,7 +96,7 @@ def get_uuid(uri=None): def get_label(entity): """Returns a label for entity.""" if hasattr(entity, "label"): - return entity.label.first() + return get_label(entity) name = repr(entity) label, _ = re.subn(r"emmo(-[a-z]+)?\.", "", name) return label @@ -116,6 +116,9 @@ def find_label(self, inst): def add(self, entity): """Adds owl entity to collection and returns a reference to the new metadata.""" + # if isinstance(entity, str): + # entity = self.onto[entity] + if entity == owlready2.Thing: raise ValueError(f"invalid entity: {entity}") @@ -125,26 +128,28 @@ def add(self, entity): if isinstance(entity, owlready2.ClassConstruct): return self.add_class_construct(entity) - raise ValueError(f"invalid entity: {entity}") + raise ValueError( + f'invalid entity "{entity}" of class {entity.__class__}' + ) def add_class(self, cls): """Adds owl class `cls` to collection and returns a reference to the new metadata.""" if isinstance(cls, str): cls = self.onto[cls] - label = cls.label.first() + label = get_label(cls) if not self.coll.has(label): uri = self.get_uri(label) dims, props = self.get_properties(cls) - entity = Instance(uri, dims, props, self.get_description(cls)) + entity = Instance.create_metadata( + uri, dims, props, self.get_description(cls) + ) self.coll.add(label, entity) for relation in cls.is_a: if relation is owlready2.Thing: pass elif isinstance(relation, owlready2.ThingClass): - self.coll.add_relation( - label, "is_a", relation.label.first() - ) + self.coll.add_relation(label, "is_a", get_label(relation)) self.add_class(relation) elif isinstance(relation, owlready2.Restriction): # Correct this test if EMMO reintroduce isPropertyOf @@ -285,6 +290,12 @@ def add_restriction(self, restriction): vlabel = self.get_label(restriction.value) self.coll.add(label, inst) self.coll.add_relation(label, asstring(restriction.property), vlabel) + print() + print(f"*** {restriction=}") + print(f"*** {restriction.type=}") + print(f"*** {restriction.value=}") + print(f"*** {label=}") + print(f"*** {vlabel=}") if not self.coll.has(vlabel): self.add(restriction.value) return inst @@ -312,7 +323,7 @@ def add_restriction_entity(self): ), ), ] - entity = Instance( + entity = Instance.create_metadata( uri, [], props, diff --git a/demo/horizontal/step1_generate_metadata.py b/demo/horizontal/step1_generate_metadata.py index 609eaa2bf..b200fd916 100755 --- a/demo/horizontal/step1_generate_metadata.py +++ b/demo/horizontal/step1_generate_metadata.py @@ -26,7 +26,10 @@ # Load our ontology from the vertical case ontopath = os.path.abspath( os.path.join( - os.path.dirname(__file__), "..", "vertical", "usercase_ontology.owl" + # os.path.dirname(__file__), "..", "vertical", "usercase_ontology.owl" + os.path.dirname(__file__), + "..", + "demo.owl", ) ) onto = get_ontology(ontopath)