Skip to content

Commit

Permalink
Merge pull request #122 from pyiron/property
Browse files Browse the repository at this point in the history
Append properties not defined in the graph
  • Loading branch information
samwaseda authored Feb 8, 2025
2 parents 19af8df + 355d08e commit b89e516
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
19 changes: 19 additions & 0 deletions pyiron_ontology/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,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
Expand Down Expand Up @@ -405,4 +406,22 @@ 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:
"""
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],
):
for obj in graph.objects(None, p):
triple = (obj, RDF.type, o)
if not triple in graph:
graph.add(triple)
return graph
16 changes: 5 additions & 11 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -173,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):
Expand Down

0 comments on commit b89e516

Please sign in to comment.