Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace EX by NS because I guess EX stands for example #97

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions pyiron_ontology/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,46 @@ def get_inputs_and_outputs(node):

def get_triples(
data,
EX,
NS,
hasSourceFunction=None,
hasUnits=None,
inheritsPropertiesFrom=None,
update_query=True,
):
if hasSourceFunction is None:
hasSourceFunction = EX.hasSourceFunction
hasSourceFunction = NS.hasSourceFunction
if hasUnits is None:
hasUnits = EX.hasUnits
hasUnits = NS.hasUnits
if inheritsPropertiesFrom is None:
inheritsPropertiesFrom = EX.inheritsPropertiesFrom
inheritsPropertiesFrom = NS.inheritsPropertiesFrom
graph = Graph()
# Triple already exists
label_def_triple = (EX[data["label"]], hasSourceFunction, EX[data["function"]])
label_def_triple = (NS[data["label"]], hasSourceFunction, NS[data["function"]])
if len(list(graph.triples(label_def_triple))) > 0:
return graph
graph.add(label_def_triple)
for io_ in ["inputs", "outputs"]:
for key, d in data[io_].items():
full_key = data["label"] + f".{io_}." + key
label = EX[full_key]
label = NS[full_key]
graph.add((label, RDFS.label, Literal(full_key)))
if d.get("uri", None) is not None:
graph.add((label, RDF.type, d["uri"]))
if d.get("value", NOT_DATA) is not NOT_DATA:
graph.add((label, RDF.value, Literal(d["value"])))
graph.add((label, EX[io_[:-1] + "Of"], EX[data["label"]]))
graph.add((label, NS[io_[:-1] + "Of"], NS[data["label"]]))
if d.get("units", None) is not None:
graph.add((label, hasUnits, EX[d["units"]]))
graph.add((label, hasUnits, NS[d["units"]]))
if d.get("connection", None) is not None:
graph.add((label, inheritsPropertiesFrom, EX[d["connection"]]))
for t in _get_triples_from_restrictions(d, EX):
graph.add(_parse_triple(t, EX, label=label, data=data))
graph.add((label, inheritsPropertiesFrom, NS[d["connection"]]))
for t in _get_triples_from_restrictions(d, NS):
graph.add(_parse_triple(t, NS, label=label, data=data))
if update_query:
inherit_properties(graph, EX)
inherit_properties(graph, NS)
return graph


def _get_triples_from_restrictions(data, EX):
def _get_triples_from_restrictions(data, NS):
triples = []
if data.get("restrictions", None) is not None:
triples = restriction_to_triple(data["restrictions"])
Expand All @@ -114,7 +114,7 @@ def restriction_to_triple(restrictions):
return triples


def _parse_triple(triples, EX, label=None, data=None):
def _parse_triple(triples, NS, label=None, data=None):
if len(triples) == 2:
subj, pred, obj = label, triples[0], triples[1]
elif len(triples) == 3:
Expand All @@ -124,7 +124,7 @@ def _parse_triple(triples, EX, label=None, data=None):
if obj.startswith("inputs.") or obj.startswith("outputs."):
obj = data["label"] + "." + obj
if not isinstance(obj, URIRef):
obj = EX[obj]
obj = NS[obj]
return subj, pred, obj


Expand Down
36 changes: 18 additions & 18 deletions tests/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rdflib import Namespace


EX = Namespace("http://example.org/")
NS = Namespace("http://example.org/")


@Workflow.wrap.as_function_node("speed")
Expand All @@ -22,22 +22,22 @@ def calculate_speed(
) -> u(
float,
units="meter/second",
triples=((EX.isOutputOf, "inputs.time"), (EX.subject, EX.predicate, EX.object)),
triples=((NS.isOutputOf, "inputs.time"), (NS.subject, NS.predicate, NS.object)),
):
return distance / time


@Workflow.wrap.as_function_node("result")
def add(a: float, b: float) -> u(float, triples=(EX.HasOperation, EX.Addition)):
def add(a: float, b: float) -> u(float, triples=(NS.HasOperation, NS.Addition)):
return a + b


@Workflow.wrap.as_function_node("result")
def multiply(a: float, b: float) -> u(
float,
triples=(
(EX.HasOperation, EX.Multiplication),
(EX.inheritsPropertiesFrom, "inputs.a"),
(NS.HasOperation, NS.Multiplication),
(NS.inheritsPropertiesFrom, "inputs.a"),
),
):
return a * b
Expand All @@ -48,8 +48,8 @@ def correct_analysis(
a: u(
float,
restrictions=(
(OWL.onProperty, EX.HasOperation),
(OWL.someValuesFrom, EX.Addition),
(OWL.onProperty, NS.HasOperation),
(OWL.someValuesFrom, NS.Addition),
),
)
) -> float:
Expand All @@ -61,8 +61,8 @@ def wrong_analysis(
a: u(
float,
restrictions=(
(OWL.onProperty, EX.HasOperation),
(OWL.someValuesFrom, EX.Division),
(OWL.onProperty, NS.HasOperation),
(OWL.someValuesFrom, NS.Division),
),
)
) -> float:
Expand All @@ -84,34 +84,34 @@ def test_parser(self):
def test_triples(self):
speed = calculate_speed()
data = get_inputs_and_outputs(speed)
graph = get_triples(data, EX)
graph = get_triples(data, NS)
self.assertGreater(
len(list(graph.triples((None, EX.hasUnits, EX["meter/second"])))), 0
len(list(graph.triples((None, NS.hasUnits, NS["meter/second"])))), 0
)
self.assertEqual(
len(
list(
graph.triples(
(None, EX.isOutputOf, EX["calculate_speed.inputs.time"])
(None, NS.isOutputOf, NS["calculate_speed.inputs.time"])
)
)
),
1,
)
self.assertEqual(
len(list(graph.triples((EX.subject, EX.predicate, EX.object)))), 1
len(list(graph.triples((NS.subject, NS.predicate, NS.object)))), 1
)

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))
graph.add((NS.HasOperation, RDF.type, RDF.Property))
graph.add((NS.Addition, RDF.type, OWL.Class))
graph.add((NS.Multiplication, RDF.type, OWL.Class))
for value in wf.children.values():
data = get_inputs_and_outputs(value)
graph += get_triples(data, EX)
inherit_properties(graph, EX)
graph += get_triples(data, NS)
inherit_properties(graph, NS)
DeductiveClosure(OWLRL_Semantics).expand(graph)
return graph

Expand Down
Loading