From 9988505c1ae7a82a4c31fdd637401b3a23403fb5 Mon Sep 17 00:00:00 2001 From: Sujay Patil Date: Tue, 31 Oct 2023 16:21:37 -0700 Subject: [PATCH 1/3] optimize get_classes_by_slot() schemaview method --- linkml_runtime/utils/schemaview.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/linkml_runtime/utils/schemaview.py b/linkml_runtime/utils/schemaview.py index 78f94240..954908f2 100644 --- a/linkml_runtime/utils/schemaview.py +++ b/linkml_runtime/utils/schemaview.py @@ -1435,7 +1435,7 @@ def slot_range_as_union(self, slot: SlotDefinition) -> List[ElementName]: if x.range: range_union_of.append(x.range) return range_union_of - + def get_classes_by_slot(self, slot: SlotDefinition, include_induced: bool = False) -> List[ClassDefinitionName]: """Get all classes that use a given slot, either as a direct or induced slot. @@ -1443,21 +1443,21 @@ def get_classes_by_slot(self, slot: SlotDefinition, include_induced: bool = Fals :param include_induced: supplement all direct slots with induced slots, defaults to False :return: list of slots, either direct, or both direct and induced """ - slots_list = [] # list of all direct or induced slots + direct_slots_list = [] # list of classes with direct slots + induced_slots_list = [] # list of classes with induced slots for c_name, c in self.all_classes().items(): - # check if slot is direct specification on class if slot.name in c.slots: - slots_list.append(c_name) - - # include induced classes also if requested - if include_induced: - for c_name, c in self.all_classes().items(): + direct_slots_list.append(c_name) + elif include_induced: for ind_slot in self.class_induced_slots(c_name): if ind_slot.name == slot.name: - slots_list.append(c_name) + induced_slots_list.append(c_name) - return list(dict.fromkeys(slots_list)) + if include_induced: + return list(set(direct_slots_list + induced_slots_list)) + else: + return list(set(direct_slots_list)) @lru_cache() def get_slots_by_enum(self, enum_name: ENUM_NAME = None) -> List[SlotDefinition]: From 20169e673d01591d49c0939ff7d03e10ff94443f Mon Sep 17 00:00:00 2001 From: Sujay Patil Date: Tue, 31 Oct 2023 16:25:01 -0700 Subject: [PATCH 2/3] test corresponding to get_classes_by_slot() method --- tests/test_utils/test_schemaview.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_utils/test_schemaview.py b/tests/test_utils/test_schemaview.py index aca871ff..aa9f5c66 100644 --- a/tests/test_utils/test_schemaview.py +++ b/tests/test_utils/test_schemaview.py @@ -735,7 +735,7 @@ def test_get_classes_by_slot(self): actual_result = sv.get_classes_by_slot(slot, include_induced=True) expected_result = ["Person", "Adult"] - self.assertListEqual(actual_result, expected_result) + self.assertListEqual(sorted(actual_result), sorted(expected_result)) def test_materialize_patterns(self): sv = SchemaView(SCHEMA_WITH_STRUCTURED_PATTERNS) From 954c2070d06441c31a30f5492aa9d8448e81c7eb Mon Sep 17 00:00:00 2001 From: Sujay Patil Date: Thu, 2 Nov 2023 15:11:46 -0700 Subject: [PATCH 3/3] clarify variable names in schemaview get_classes_by_slot() --- linkml_runtime/utils/schemaview.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/linkml_runtime/utils/schemaview.py b/linkml_runtime/utils/schemaview.py index 954908f2..49343632 100644 --- a/linkml_runtime/utils/schemaview.py +++ b/linkml_runtime/utils/schemaview.py @@ -1443,21 +1443,21 @@ def get_classes_by_slot(self, slot: SlotDefinition, include_induced: bool = Fals :param include_induced: supplement all direct slots with induced slots, defaults to False :return: list of slots, either direct, or both direct and induced """ - direct_slots_list = [] # list of classes with direct slots - induced_slots_list = [] # list of classes with induced slots + direct_classes_list = [] # list of classes associated with slot directly + induced_classes_list = [] # list of classes associated with slot indirectly for c_name, c in self.all_classes().items(): if slot.name in c.slots: - direct_slots_list.append(c_name) + direct_classes_list.append(c_name) elif include_induced: for ind_slot in self.class_induced_slots(c_name): if ind_slot.name == slot.name: - induced_slots_list.append(c_name) + induced_classes_list.append(c_name) if include_induced: - return list(set(direct_slots_list + induced_slots_list)) + return list(set(direct_classes_list + induced_classes_list)) else: - return list(set(direct_slots_list)) + return list(set(direct_classes_list)) @lru_cache() def get_slots_by_enum(self, enum_name: ENUM_NAME = None) -> List[SlotDefinition]: