Skip to content
Snippets Groups Projects
Commit 361b6a37 authored by Jacob Wahlman's avatar Jacob Wahlman :ok_hand:
Browse files

Refactored traverse tree into basic_operation instead

parent 72292e38
No related branches found
No related tags found
1 merge request!7Resolve "Operation Traversing"
Pipeline #10165 passed
......@@ -12,3 +12,4 @@ from b_asic.schema import *
from b_asic.signal_flow_graph import *
from b_asic.signal import *
from b_asic.simulation import *
from b_asic.utilities import *
......@@ -11,6 +11,7 @@ from b_asic.port import InputPort, OutputPort
from b_asic.signal import SignalSource, SignalDestination
from b_asic.operation import Operation
from b_asic.simulation import SimulationState, OperationState
from b_asic.utilities import breadth_first_search
class BasicOperation(Operation):
......@@ -102,4 +103,14 @@ class BasicOperation(Operation):
return neighbours
def traverse(self) -> List[Operation]:
"""Traverse the the operation tree and return operation where type matches.
If the type is None then return the entire tree.
Keyword arguments:
type_ -- the operation type to search for (default None)
"""
return breadth_first_search(self)
# TODO: More stuff.
"""@package docstring
B-ASIC Operation Tree Traversing Module.
"""
from typing import List, Optional
from typing import List
from collections import deque
from b_asic.operation import Operation
class Traverse:
"""Traverse operation tree."""
def __init__(self, operation: Operation):
"""Construct a TraverseTree."""
self._initial_operation = operation
def _breadth_first_search(self, start: Operation) -> List[Operation]:
def breadth_first_search(start: Operation) -> List[Operation]:
"""Use breadth first search to traverse the operation tree."""
visited: List[Operation] = [start]
queue = deque([start])
while queue:
operation = queue.popleft()
yield operation
for n_operation in operation.neighbours:
if n_operation not in visited:
visited.append(n_operation)
queue.append(n_operation)
return visited
def traverse(self, type_: Optional[Operation] = None) -> List[Operation]:
"""Traverse the the operation tree and return operation where type matches.
If the type is None then return the entire tree.
Keyword arguments:
type_ -- the operation type to search for (default None)
"""
operations: List[Operation] = self._breadth_first_search(self._initial_operation)
if type_ is not None:
operations = [oper for oper in operations if isinstance(oper, type_)]
return operations
from b_asic.core_operations import Constant, Addition
from b_asic.traverse_tree import Traverse
import pytest
def test_traverse_single_tree(operation):
"""Traverse a tree consisting of one operation."""
traverse = Traverse(operation)
assert traverse.traverse() == [operation]
constant = Constant(None)
assert list(constant.traverse()) == [constant]
def test_traverse_tree(addition_tree):
"""Traverse a basic addition tree with two constants."""
traverse = Traverse(addition_tree)
assert len(traverse.traverse()) == 3
assert len(list(addition_tree.traverse())) == 3
def test_traverse_large_tree(large_operation_tree):
"""Traverse a larger tree."""
traverse = Traverse(large_operation_tree)
assert len(traverse.traverse()) == 7
assert len(list(large_operation_tree.traverse())) == 7
def test_traverse_type(large_operation_tree):
"""Traverse the operation tree and return operations of specific type."""
traverse = Traverse(large_operation_tree)
assert len(traverse.traverse(Addition)) == 3
assert len(traverse.traverse(Constant)) == 4
operations = list(large_operation_tree.traverse())
assert len(list(filter(lambda type_: isinstance(type_, Addition), operations))) == 3
assert len(list(filter(lambda type_: isinstance(type_, Constant), operations))) == 4
def test_traverse_loop(looping_addition_tree):
"""Traverse two operations coupled to eachother in a loop."""
traverse = Traverse(looping_addition_tree)
assert len(traverse.traverse()) == 2
\ No newline at end of file
assert len(list(looping_addition_tree.traverse())) == 2
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment