Skip to content

Commit

Permalink
Optimisations in ontology queries (#313)
Browse files Browse the repository at this point in the history
* optimisations in ontology queries

* fix failing and test parprod

* fix allowed
  • Loading branch information
dsavchenko authored Jan 14, 2025
1 parent 7f2eac3 commit 5cf0eb0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 5 additions & 9 deletions oda_api/ontology_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5cf0eb0

Please sign in to comment.