From 5cf0eb0c2da5f39f178ca91b117162e48c04718e Mon Sep 17 00:00:00 2001 From: Denys Savchenko <56398430+dsavchenko@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:17:53 +0100 Subject: [PATCH] Optimisations in ontology queries (#313) * optimisations in ontology queries * fix failing and test parprod * fix allowed --- oda_api/ontology_helper.py | 14 +++++--------- tests/test_ontology.py | 8 ++++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/oda_api/ontology_helper.py b/oda_api/ontology_helper.py index 8b9a22a2..14f7b0d6 100644 --- a/oda_api/ontology_helper.py +++ b/oda_api/ontology_helper.py @@ -246,14 +246,14 @@ def get_uri_hierarchy(self, uri, base_uri): uri_m = self._normalize_uri(uri) base_uri_m = self._normalize_uri(base_uri) query = """ - select ?mid ( count(?mid2) as ?midcount ) where { + SELECT ?mid ( count(?mid2) as ?midcount ) WHERE { %s (rdfs:subClassOf|a)* ?mid . ?mid rdfs:subClassOf* ?mid2 . ?mid2 rdfs:subClassOf* %s . } - group by ?mid - order by desc(?midcount) + GROUP BY ?mid + ORDER BY DESC(?midcount) """ % ( uri_m, base_uri_m ) qres = self.g.query(query) @@ -381,14 +381,10 @@ def get_allowed_values(self, param_uri): def get_parprod_terms(self): query = """ - SELECT ?s WHERE { + SELECT DISTINCT ?s WHERE { ?s (rdfs:subClassOf|a)* ?mid0. - ?mid0 rdfs:subClassOf* oda:DataProduct. - - ?s (rdfs:subClassOf|a)* ?mid1. - ?mid1 rdfs:subClassOf* oda:WorkflowParameter . + ?mid0 rdfs:subClassOf* oda:ParameterProduct . } - GROUP BY ?s """ qres = self.g.query(query) return [str(row[0]) for row in qres] diff --git a/tests/test_ontology.py b/tests/test_ontology.py index cb64a4a7..f4907155 100644 --- a/tests/test_ontology.py +++ b/tests/test_ontology.py @@ -387,3 +387,11 @@ def test_get_requested_resources(onto): assert data[0] == dict(resource="MyS3", required=True, env_vars=set(["My_S3_CREDENTIALS"])) data = list(onto.get_requested_resources(g)) assert len(data) == 2 + +def test_parprod_terms(onto): + parprod = onto.get_parprod_terms() + assert parprod[0] == 'http://odahub.io/ontology#ParameterProduct' + assert 'http://odahub.io/ontology#Float' in parprod + assert 'http://odahub.io/ontology#String' in parprod + assert 'http://odahub.io/ontology#StartTimeISOT' in parprod + \ No newline at end of file