From e6321c3dca0480e9413252bc1d1656becd51f28e Mon Sep 17 00:00:00 2001
From: PrimozGodec
Date: Mon, 6 Feb 2023 14:26:09 +0100
Subject: [PATCH] Ontology - show labels instead of names for imported owls
---
orangecontrib/text/widgets/owontology.py | 3 +-
.../text/widgets/tests/test_owontology.py | 29 +++++++++++++++++++
2 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/orangecontrib/text/widgets/owontology.py b/orangecontrib/text/widgets/owontology.py
index 0cf012eab..5ef8d3b90 100644
--- a/orangecontrib/text/widgets/owontology.py
+++ b/orangecontrib/text/widgets/owontology.py
@@ -104,7 +104,8 @@ def _tree_to_html(tree: Dict) -> str:
def _onto_to_tree(thing: Thing, world: World) -> Dict:
tree = {}
for cl in list(thing.subclasses(world=world)):
- tree[cl.name] = _onto_to_tree(cl, world)
+ label = (cl.label.first() or cl.name).replace("\n", " ").strip()
+ tree[label] = _onto_to_tree(cl, world)
return tree
diff --git a/orangecontrib/text/widgets/tests/test_owontology.py b/orangecontrib/text/widgets/tests/test_owontology.py
index 20cc389af..124f1a7b0 100644
--- a/orangecontrib/text/widgets/tests/test_owontology.py
+++ b/orangecontrib/text/widgets/tests/test_owontology.py
@@ -10,6 +10,7 @@
QItemSelectionRange
from AnyQt.QtWidgets import QFileDialog
from AnyQt.QtTest import QTest
+from owlready2 import get_ontology, Thing, types
from Orange.data import Table
from Orange.widgets.tests.base import WidgetTest
@@ -279,6 +280,34 @@ def test_library_import(self, _):
os.path.basename(f.name))
self.assertEqual(get_ontology_data(), ontology)
+ def test_library_import_owl(self):
+ # create ontology
+ onto = get_ontology("http://test.org/onto.owl")
+ with onto:
+ # foo3 will be missing the label attribute - widget display name
+ foo3 = types.new_class("foo3", (Thing,))
+ # other two classes will have label attribute - widget display label
+ for c, label in (("bar3", "Bar 3"), ("baz3", "Baz 3")):
+ thing = types.new_class(c, (foo3,))
+ thing.label = label
+
+ get_ontology_data = self.widget._OWOntology__ontology_view.get_data
+ with tempfile.TemporaryDirectory() as tmp_dir_name:
+ # safe to file
+ path = os.path.join(tmp_dir_name, "onto.owl")
+ onto.save(file=path, format="rdfxml")
+
+ # open with widget
+ with patch.object(
+ QFileDialog, "getOpenFileName", Mock(return_value=(path, None))
+ ):
+ self.widget._OWOntology__on_import_file()
+ self.assertEqual(self.widget._OWOntology__get_selected_row(), 2)
+ name = self.widget._OWOntology__model[2].name
+ self.assertEqual(name, os.path.basename(path))
+ expected = {"foo3": {"Bar 3": {}, "Baz 3": {}}}
+ self.assertEqual(get_ontology_data(), expected)
+
def test_library_save(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".pkl",
delete=False) as f: