Skip to content
Snippets Groups Projects

Resolve "Replacing Operations in SFG"

Merged Jacob Wahlman requested to merge 15-replacing-operations-in-sfg into develop
All threads resolved!
Files
2
@@ -5,7 +5,7 @@ TODO: More info.
from typing import NewType, List, Iterable, Sequence, Dict, Optional, DefaultDict, Set
from numbers import Number
from collections import defaultdict, deque
from collections import defaultdict, deque, Iterable as CIterable
from b_asic.port import SignalSourceProvider, OutputPort
from b_asic.operation import Operation, AbstractOperation
@@ -350,6 +350,53 @@ class SFG(AbstractOperation):
input_values.append(self._evaluate_source(input_src))
return src.operation.evaluate_output(src.index, input_values)
def replace_component(self, _component: List[Operation], _id: List[GraphID] = None, _type: List[Operation] = None):
"""Find and replace all components matching either on GraphID, Type or both.
Then return a new deepcopy of the sfg with the replaced component.
Arguments:
_component: The list of new component(s), e.g Multiplication
Keyword arguments:
_id: The list of GraphID(s) to match the component to replace.
_type: The list of Type(s) to match the component to replace.
"""
_id = [_id] if not isinstance(_id, CIterable) or isinstance(_id, str) else _id
_type = [_type] if not isinstance(_type, CIterable) else _type
_component = [_component] if not isinstance(_component, CIterable) else _component
components = set()
for comp_id in _id:
if comp_id is None:
continue
components |= {value for key, value in self._components_by_id.items() if key == comp_id}
for comp_type in _type:
if comp_type is None:
continue
components |= {comp for comp in self.components if isinstance(comp, comp_type)}
assert sum([comp.output_count for comp in _component]) == sum([comp.output_count for comp in components])
assert sum([comp.input_count for comp in _component]) == sum([comp.input_count for comp in components])
for index, comp in enumerate(components):
component = _component if not isinstance(_component, CIterable) else _component[index]
for index_in, _inp in enumerate(comp.inputs):
for _signal in _inp.signals:
_signal.remove_destination()
_signal.set_destination(component.input(index_in))
for index_out, _out in enumerate(comp.outputs):
for _signal in _out.signals:
_signal.remove_source()
_signal.set_source(component.output(index_out))
# The old SFG will be deleted by Python GC
return self.deep_copy()
def __str__(self):
"""Prints operations, inputs and outputs in a SFG
Loading