Newer
Older
B-ASIC Signal Flow Graph Module.
TODO: More info.
"""
from typing import List, Dict, Union, Optional

Jacob Wahlman
committed
from b_asic.operation import Operation
from b_asic.basic_operation import BasicOperation
from b_asic.signal import Signal
from b_asic.simulation import SimulationState, OperationState
from typing import List

Jacob Wahlman
committed
from b_asic.graph_id import GraphIDGenerator, GraphID

Jacob Wahlman
committed
_graph_objects_by_id: Dict[GraphID, Union[Operation, Signal]]
_graph_id_generator: GraphIDGenerator
def __init__(self, input_destinations: List[Signal], output_sources: List[Signal]):

Jacob Wahlman
committed
super().__init__()
# TODO: Allocate input/output ports with appropriate IDs.

Jacob Wahlman
committed
self._graph_objects_by_id = dict # Map Operation ID to Operation objects
self._graph_id_generator = GraphIDGenerator()

Jacob Wahlman
committed
# TODO: Traverse the graph between the inputs/outputs and add to self._operations.
# TODO: Connect ports with signals with appropriate IDs.
def evaluate(self, inputs: list) -> list:
return [] # TODO: Implement

Jacob Wahlman
committed
def add_operation(self, operation: Operation) -> GraphID:
"""Adds the entered operation to the SFG's dictionary of graph objects and
returns a generated GraphID for it.

Jacob Wahlman
committed
Keyword arguments:
operation: Operation to add to the graph.
"""
return self._add_graph_obj(operation, operation.type_name())

Jacob Wahlman
committed
def add_signal(self, signal: Signal) -> GraphID:
"""Adds the entered signal to the SFG's dictionary of graph objects and returns
a generated GraphID for it.

Jacob Wahlman
committed
Keyword argumentst:
signal: Signal to add to the graph.
"""
return self._add_graph_obj(signal, 'sig')
def find_by_id(self, graph_id: GraphID) -> Optional[Operation]:
"""Finds a graph object based on the entered Graph ID and returns it. If no graph
object with the entered ID was found then returns None.
Keyword arguments:
graph_id: Graph ID of the wanted object.
"""
if graph_id in self._graph_objects_by_id:
return self._graph_objects_by_id[graph_id]

Jacob Wahlman
committed
def _add_graph_obj(self, obj: Union[Operation, Signal], operation_id_type: str):
graph_id = self._graph_id_generator.get_next_id(operation_id_type)
self._graph_objects_by_id[graph_id] = obj