From 4bb391f32aaec7c7cab35c576b060b479bb0c631 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Mon, 27 Jan 2025 12:23:48 +0000 Subject: [PATCH 1/4] Small bugfix --- pyiron_ontology/parser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index 4e289c9..c8c0ae2 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -283,6 +283,7 @@ def _inherit_properties(graph: Graph, n: int | None = None): " FILTER(?p != ns:inheritsPropertiesFrom)", " FILTER(?p != rdfs:label)", " FILTER(?p != rdf:value)", + " FILTER(?p != ns:hasValue)", " FILTER(?p != rdf:type)", "}", ) From 47d01c01ed4c9290778491b3f6f5335759bbdbf3 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Mon, 27 Jan 2025 15:59:09 +0000 Subject: [PATCH 2/4] Correct tests --- pyiron_ontology/parser.py | 54 ++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index c8c0ae2..8e9c396 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -62,9 +62,19 @@ def get_inputs_and_outputs(node: Node) -> dict: } -def _translate_has_value(graph: Graph, label: URIRef, tag: str, value: Any): +def _translate_has_value( + graph: Graph, + label: URIRef, + tag: str, + value: Any = None, + units: URIRef | None = None +) -> Graph: graph.add((label, PNS.hasValue, URIRef(tag))) - graph.add((URIRef(tag), RDF.value, Literal(value))) + if value is not None: + graph.add((URIRef(tag), RDF.value, Literal(value))) + if units is not None: + graph.add((URIRef(tag), PNS.hasUnits, URIRef(units))) + graph.add((URIRef(units), RDF.value, Literal(units))) return graph @@ -142,30 +152,22 @@ def get_triples( graph.add((label, PNS.inputOf, URIRef(full_label))) elif io_ == "outputs": graph.add((label, PNS.outputOf, URIRef(full_label))) - if d.get("units", None) is not None: - graph.add((label, PNS.hasUnits, URIRef(d["units"]))) - if "value" in d: - if io_ == "inputs" and d.get("connection", None) is not None: - graph = _translate_has_value( - graph, - label, - workflow_namespace + d["connection"] + ".value", - d["value"], - ) - elif io_ == "inputs": - graph = _translate_has_value( - graph, - label, - str(d["value"]), - d["value"], - ) - else: - graph = _translate_has_value( - graph, - label, - label + ".value", - d["value"], - ) + if io_ == "inputs" and d.get("connection", None) is not None: + graph = _translate_has_value( + graph, + label, + workflow_namespace + d["connection"] + ".value", + d.get("value", None), + units=d.get("units", None), + ) + else: + graph = _translate_has_value( + graph, + label, + label + ".value", + d.get("value", None), + units=d.get("units", None), + ) if d.get("connection", None) is not None and io_ == "inputs": graph.add( ( From ec3b75e55fdb8622e35e376b62642ceb22531e58 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Mon, 27 Jan 2025 17:39:57 +0000 Subject: [PATCH 3/4] Add a sparql query test --- tests/unit/test_parser.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/unit/test_parser.py b/tests/unit/test_parser.py index d6bb36d..5acd0ce 100644 --- a/tests/unit/test_parser.py +++ b/tests/unit/test_parser.py @@ -88,6 +88,32 @@ def test_parser(self): for label in ["inputs", "outputs", "function", "label"]: self.assertIn(label, output_dict) + def test_units_with_sparql(self): + wf = Workflow("speed") + wf.speed = calculate_speed() + wf.run() + graph = parse_workflow(wf) + query_txt = [ + "PREFIX ex: ", + "PREFIX rdf: ", + f"PREFIX pns: <{PNS.BASE}>", + "SELECT DISTINCT ?speed ?units", + "WHERE {", + " ?output pns:hasValue ?output_tag .", + " ?output_tag rdf:value ?speed .", + " ?output_tag pns:hasUnits ?units_arg .", + " ?units_arg rdf:value ?units .", + "}", + ] + query = "\n".join(query_txt) + results = graph.query(query) + self.assertEqual(len(results), 3) + result_list = [[value.value for value in row] for row in graph.query(query)] + self.assertEqual( + sorted(result_list), + [[2.0, 'second'], [5.0, 'meter/second'], [10.0, 'meter']] + ) + def test_triples(self): speed = calculate_speed() data = get_inputs_and_outputs(speed) From 99c4a5644f72af2108c22a92e0170b5514a7c85e Mon Sep 17 00:00:00 2001 From: samwaseda Date: Mon, 27 Jan 2025 17:42:10 +0000 Subject: [PATCH 4/4] run black --- pyiron_ontology/parser.py | 2 +- tests/unit/test_parser.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index 8e9c396..53b89a3 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -67,7 +67,7 @@ def _translate_has_value( label: URIRef, tag: str, value: Any = None, - units: URIRef | None = None + units: URIRef | None = None, ) -> Graph: graph.add((label, PNS.hasValue, URIRef(tag))) if value is not None: diff --git a/tests/unit/test_parser.py b/tests/unit/test_parser.py index 5acd0ce..30fc408 100644 --- a/tests/unit/test_parser.py +++ b/tests/unit/test_parser.py @@ -111,7 +111,7 @@ def test_units_with_sparql(self): result_list = [[value.value for value in row] for row in graph.query(query)] self.assertEqual( sorted(result_list), - [[2.0, 'second'], [5.0, 'meter/second'], [10.0, 'meter']] + [[2.0, "second"], [5.0, "meter/second"], [10.0, "meter"]], ) def test_triples(self):