From 5362ea44265478c20814f07d6b55ee011ad22973 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 8 Feb 2025 07:17:26 +0000 Subject: [PATCH 1/5] Add check missing items --- pyiron_ontology/parser.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index 56f6268..5e22fa6 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -136,6 +136,7 @@ def get_triples( data: dict, workflow_namespace: str | None = None, ontology=PNS, + check_missing_items: bool = True, ) -> Graph: """ Generate triples from a dictionary containing input output information. @@ -199,7 +200,10 @@ def get_triples( ) if data["function"].get("uri", None) is not None: graph.add((URIRef(node_label), RDF.type, URIRef(data["function"]["uri"]))) - for t in _get_triples_from_restrictions(data["function"]): + for t in _get_triples_from_restrictions( + data["function"], + check_missing_items=check_missing_items, + ): graph.add(_parse_triple(t, ns=node_label, label=URIRef(node_label))) for io_ in ["inputs", "outputs"]: for key, d in data[io_].items(): @@ -225,12 +229,16 @@ def get_triples( units=d.get("units", None), ontology=ontology, ) - for t in _get_triples_from_restrictions(d): + for t in _get_triples_from_restrictions( + d, check_missing_items=check_missing_items + ): graph.add(_parse_triple(t, ns=node_label, label=channel_label)) return graph -def _get_triples_from_restrictions(data: dict) -> list: +def _get_triples_from_restrictions( + data: dict, check_missing_items: bool = True +) -> list: triples = [] if data.get("restrictions", None) is not None: triples = restriction_to_triple(data["restrictions"]) From e70ed44bccd6865aeb1b113cf1de23e99185b5f5 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 8 Feb 2025 07:48:46 +0000 Subject: [PATCH 2/5] Move it to workflow parser and add tests --- pyiron_ontology/parser.py | 29 ++++++++++++++++++----------- tests/unit/test_parser.py | 8 +------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index 5e22fa6..591615f 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -136,7 +136,6 @@ def get_triples( data: dict, workflow_namespace: str | None = None, ontology=PNS, - check_missing_items: bool = True, ) -> Graph: """ Generate triples from a dictionary containing input output information. @@ -200,10 +199,7 @@ def get_triples( ) if data["function"].get("uri", None) is not None: graph.add((URIRef(node_label), RDF.type, URIRef(data["function"]["uri"]))) - for t in _get_triples_from_restrictions( - data["function"], - check_missing_items=check_missing_items, - ): + for t in _get_triples_from_restrictions(data["function"]): graph.add(_parse_triple(t, ns=node_label, label=URIRef(node_label))) for io_ in ["inputs", "outputs"]: for key, d in data[io_].items(): @@ -229,16 +225,12 @@ def get_triples( units=d.get("units", None), ontology=ontology, ) - for t in _get_triples_from_restrictions( - d, check_missing_items=check_missing_items - ): + for t in _get_triples_from_restrictions(d): graph.add(_parse_triple(t, ns=node_label, label=channel_label)) return graph -def _get_triples_from_restrictions( - data: dict, check_missing_items: bool = True -) -> list: +def _get_triples_from_restrictions(data: dict) -> list: triples = [] if data.get("restrictions", None) is not None: triples = restriction_to_triple(data["restrictions"]) @@ -381,6 +373,7 @@ def parse_workflow( graph: Graph | None = None, inherit_properties: bool = True, ontology=PNS, + append_missing_items: bool = True, ) -> Graph: """ Generate RDF graph from a pyiron workflow object @@ -413,4 +406,18 @@ def parse_workflow( ) if inherit_properties: _inherit_properties(graph, ontology=ontology) + if append_missing_items: + graph = _append_missing_items(graph) + return graph + + +def _append_missing_items(graph: Graph) -> Graph: + for p, o in zip( + [OWL.onProperty, OWL.someValuesFrom, OWL.allValuesFrom], + [RDF.Property, OWL.Class, OWL.Class] + ): + for obj in graph.objects(None, p): + triple = (obj, RDF.type, o) + if not triple in graph: + graph.add(triple) return graph diff --git a/tests/unit/test_parser.py b/tests/unit/test_parser.py index 5df83f8..a81d9cd 100644 --- a/tests/unit/test_parser.py +++ b/tests/unit/test_parser.py @@ -136,13 +136,7 @@ def test_triples(self): def test_correct_analysis(self): def get_graph(wf): - graph = Graph() - graph.add((EX.HasOperation, RDF.type, RDF.Property)) - graph.add((EX.Addition, RDF.type, OWL.Class)) - graph.add((EX.Multiplication, RDF.type, OWL.Class)) - for value in wf.children.values(): - data = get_inputs_and_outputs(value) - graph += get_triples(data=data) + graph = parse_workflow(wf) _inherit_properties(graph) DeductiveClosure(OWLRL_Semantics).expand(graph) return graph From 013bd73f6250fbbaee83eafdda0ace5fc3195143 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 8 Feb 2025 07:49:58 +0000 Subject: [PATCH 3/5] Add comments --- pyiron_ontology/parser.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index 591615f..d844d5e 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -412,6 +412,10 @@ def parse_workflow( def _append_missing_items(graph: Graph) -> Graph: + """ + This function makes sure that all properties defined in the descriptions + become valid. + """ for p, o in zip( [OWL.onProperty, OWL.someValuesFrom, OWL.allValuesFrom], [RDF.Property, OWL.Class, OWL.Class] From 153823708086bd8094b743af54bac9a444933245 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 8 Feb 2025 07:50:47 +0000 Subject: [PATCH 4/5] Run black --- pyiron_ontology/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index d844d5e..15db3c2 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -418,7 +418,7 @@ def _append_missing_items(graph: Graph) -> Graph: """ for p, o in zip( [OWL.onProperty, OWL.someValuesFrom, OWL.allValuesFrom], - [RDF.Property, OWL.Class, OWL.Class] + [RDF.Property, OWL.Class, OWL.Class], ): for obj in graph.objects(None, p): triple = (obj, RDF.type, o) From 355d08e96d3e10dc32fa57f9f2acd2b4ba05b3a8 Mon Sep 17 00:00:00 2001 From: samwaseda Date: Sat, 8 Feb 2025 07:54:34 +0000 Subject: [PATCH 5/5] Another black --- tests/unit/test_parser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_parser.py b/tests/unit/test_parser.py index a81d9cd..08c9461 100644 --- a/tests/unit/test_parser.py +++ b/tests/unit/test_parser.py @@ -167,12 +167,12 @@ def test_parse_workflow(self): graph = parse_workflow(wf) tag = "correct_analysis.addition.inputs.a" self.assertEqual( - len(list(graph.triples((URIRef(tag), RDFS.label, Literal(tag))))), 1, + len(list(graph.triples((URIRef(tag), RDFS.label, Literal(tag))))), + 1, ) self.assertTrue( - EX.Addition in list( - graph.objects(URIRef("correct_analysis.addition"), RDF.type) - ) + EX.Addition + in list(graph.objects(URIRef("correct_analysis.addition"), RDF.type)) ) def test_macro(self):