diff --git a/pyiron_ontology/parser.py b/pyiron_ontology/parser.py index c040565..2dcdc60 100644 --- a/pyiron_ontology/parser.py +++ b/pyiron_ontology/parser.py @@ -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"]) @@ -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: @@ -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 diff --git a/tests/unit/test_parser.py b/tests/unit/test_parser.py index 5acff34..f0a9dc3 100644 --- a/tests/unit/test_parser.py +++ b/tests/unit/test_parser.py @@ -12,7 +12,7 @@ from rdflib import Namespace -EX = Namespace("http://example.org/") +NS = Namespace("http://example.org/") @Workflow.wrap.as_function_node("speed") @@ -22,13 +22,13 @@ 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 @@ -36,8 +36,8 @@ def add(a: float, b: float) -> u(float, triples=(EX.HasOperation, EX.Addition)): 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 @@ -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: @@ -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: @@ -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