Skip to content

Commit

Permalink
Merge pull request #109 from pyiron/bugfix
Browse files Browse the repository at this point in the history
Move the location of units and change node names
  • Loading branch information
samwaseda authored Jan 27, 2025
2 parents d6e38aa + 99c4a56 commit 06f0e94
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
55 changes: 29 additions & 26 deletions pyiron_ontology/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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(
(
Expand Down Expand Up @@ -283,6 +285,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)",
"}",
)
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: <http://example.org/>",
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
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)
Expand Down

0 comments on commit 06f0e94

Please sign in to comment.