From 7a86d6bcd6484a1a6e817690ae2054dc0dbe94b3 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Wed, 26 May 2021 09:47:07 +1000 Subject: [PATCH] Avoid repeatedly checking if rule adds a new triple, if we already know the rule adds one triple. Fix linter error in cli.py --- pyshacl/cli.py | 1 + pyshacl/extras/js/rules.py | 4 ++-- pyshacl/rules/sparql/__init__.py | 2 +- pyshacl/rules/triple/__init__.py | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pyshacl/cli.py b/pyshacl/cli.py index a5e3a78..b8ac6c1 100644 --- a/pyshacl/cli.py +++ b/pyshacl/cli.py @@ -135,6 +135,7 @@ def __call__(self, parser, namespace, values, option_string=None): ) # parser.add_argument('-h', '--help', action="help", help='Show this help text.') + def main(): basename = os.path.basename(sys.argv[0]) if basename == "__main__.py": diff --git a/pyshacl/extras/js/rules.py b/pyshacl/extras/js/rules.py index 292155d..8498219 100644 --- a/pyshacl/extras/js/rules.py +++ b/pyshacl/extras/js/rules.py @@ -45,9 +45,9 @@ def apply(self, data_graph: 'GraphLike') -> int: set_to_add = set() for t in triples: s, p, o = tr = t[:3] - if tr not in data_graph: + if not this_added and tr not in data_graph: this_added = True - set_to_add.add(tr) + set_to_add.add(tr) sets_to_add.append(set_to_add) if this_added: added += 1 diff --git a/pyshacl/rules/sparql/__init__.py b/pyshacl/rules/sparql/__init__.py index b94f175..bac6d79 100644 --- a/pyshacl/rules/sparql/__init__.py +++ b/pyshacl/rules/sparql/__init__.py @@ -74,7 +74,7 @@ def apply(self, data_graph: 'GraphLike') -> int: raise ReportableRuntimeError("Query executed by a SHACL SPARQLRule must be CONSTRUCT query.") this_added = False for i in results.graph: - if i not in data_graph: + if not this_added and i not in data_graph: this_added = True # We only need to know at least one triple was added, then break! break diff --git a/pyshacl/rules/triple/__init__.py b/pyshacl/rules/triple/__init__.py index a206cc0..76fcb67 100644 --- a/pyshacl/rules/triple/__init__.py +++ b/pyshacl/rules/triple/__init__.py @@ -190,7 +190,7 @@ def apply(self, data_graph: 'GraphLike') -> int: iterate_limit = 100 while True: if iterate_limit < 1: - raise ReportableRuntimeError("Local rule iteration exceeded iteration limit of 100.") + raise ReportableRuntimeError("sh:rule iteration exceeded iteration limit of 100.") iterate_limit -= 1 added = 0 to_add = [] @@ -201,9 +201,9 @@ def apply(self, data_graph: 'GraphLike') -> int: new_triples = itertools.product(s_set, p_set, o_set) this_added = False for i in iter(new_triples): - if i not in data_graph: - to_add.append(i) + if not this_added and i not in data_graph: this_added = True + to_add.append(i) if this_added: added += 1 if added > 0: