Skip to content

Commit

Permalink
Use BFS in find function and variants (#833)
Browse files Browse the repository at this point in the history
A workaround for issue #820 (caching mechanism incompatible with DFS).
  • Loading branch information
kysrpex authored Dec 6, 2022
1 parent 1afbefa commit 05fd0e1
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions simphony_osp/tools/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def find(
annotation = set()
annotation = frozenset(annotation)

result = _iter(criterion, root, rel, annotation, max_depth)
result = iter(())
if criterion(root):
result = chain(result, (root,))
result = chain(result, _iter(criterion, root, rel, annotation, max_depth))
if not find_all:
result = next(result, None)

Expand Down Expand Up @@ -110,24 +113,28 @@ def _iter(
"""
visited = visited or set()
visited.add(root.uid)
if criterion(root):
yield root

if current_depth < max_depth:
for sub in chain(
# Originally, this function was using DFS (no `list`), but it is
# incompatible with the caching mechanism. See issue #820.
# TODO: Fix
# [issue #820](https://github.com/simphony/simphony-osp/issues/820).
children = chain(
*(root.iter(rel=r) for r in rel),
*(root.annotations_iter(rel=r) for r in annotation)
):
if sub.uid not in visited:
yield from _iter(
criterion=criterion,
root=sub,
rel=rel,
annotation=annotation,
max_depth=max_depth,
current_depth=current_depth + 1,
visited=visited,
)
)
children = set(child for child in children if child.uid not in visited)
yield from (child for child in children if criterion(child))
for sub in children:
yield from _iter(
criterion=criterion,
root=sub,
rel=rel,
annotation=annotation,
max_depth=max_depth,
current_depth=current_depth + 1,
visited=visited,
)


def find_by_identifier(
Expand Down

0 comments on commit 05fd0e1

Please sign in to comment.