From 676a6e96aa83d0bfa5f87c3817be4385d131d1ef Mon Sep 17 00:00:00 2001 From: Kevin <Kevin> Date: Tue, 28 Apr 2020 14:49:12 +0200 Subject: [PATCH] Added some defintions, need to implement operation to SFG conversion in another issue --- b_asic/operation.py | 4 ++++ b_asic/signal_flow_graph.py | 15 ++++++++++++++- test/test_sfg.py | 6 +++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/b_asic/operation.py b/b_asic/operation.py index e5f91fe7..36bad20e 100644 --- a/b_asic/operation.py +++ b/b_asic/operation.py @@ -180,6 +180,10 @@ class Operation(GraphComponent, SignalSourceProvider): """ raise NotImplementedError + @abstractmethod + def to_sfg(self) -> self: + """Convert a operation to its correspnding + """ class AbstractOperation(Operation, AbstractGraphComponent): """Generic abstract operation class which most implementations will derive from. diff --git a/b_asic/signal_flow_graph.py b/b_asic/signal_flow_graph.py index 7cd3972a..be6862c4 100644 --- a/b_asic/signal_flow_graph.py +++ b/b_asic/signal_flow_graph.py @@ -405,7 +405,7 @@ class SFG(AbstractOperation): # The old SFG will be deleted by Python GC return self() - def replace_operations(self, inputs: Sequence[Input], outputs: Sequence[Output], operation: Operation): + def replace_operations(self, operation_ids: Sequence[GraphID], operation: Operation): """Replace multiple operations in the sfg with a operation of equivalent functionallity with the same number of inputs and outputs. Then return a new deepcopy of the sfg with the replaced operations. @@ -414,6 +414,19 @@ class SFG(AbstractOperation): outputs: The outputs of the operations we are replacing. operation: The operation used for replacement. """ + + operations = [self.find_by_id(_id) for _id in operation_ids] + + for _index, _inp in enumerate(inputs): + for _signal in _inp.output_signals: + _signal.remove_destination() + _signal.set_destination(operation.input(_index)) + + for _index, _out in enumerate(outputs): + for _signal in _out.input_signals: + _signal.remove_destination() + _signal.set_source(operation.output(_index)) + return self() def _evaluate_source(self, src: OutputPort, results: MutableResultMap, registers: MutableRegisterMap, prefix: str) -> Number: diff --git a/test/test_sfg.py b/test/test_sfg.py index e8879ee6..de9b65ac 100644 --- a/test/test_sfg.py +++ b/test/test_sfg.py @@ -266,10 +266,10 @@ class TestReplaceOperations: sfg = SFG(inputs=[in1, in2, in3], outputs=[out1]) mad1 = MAD() - sfg.replace_operations([in1, in2, in3], [out1], mad1) + _sfg = sfg.replace_operations(['add1', 'mul1'], mad1) - assert mad1 in sfg.operations - assert {add1, mul1} not in sfg.operations + assert mad1 in _sfg.operations + assert {add1, mul1} not in _sfg.operations def test_replace_neg_add_with_sub(self): in1 = Input() -- GitLab