diff --git a/b_asic/graph_id.py b/b_asic/graph_id.py index 191a798e3cfcea588d5dff9133d3d306d088f5d6..e7a027d3a8f2d2bfc41dc815fbe13445bfab1644 100644 --- a/b_asic/graph_id.py +++ b/b_asic/graph_id.py @@ -10,15 +10,18 @@ class GraphIDGenerator: """ A class that generates Graph IDs for objects. """ - + _next_id_number: DefaultDict(str, int) def __init__(self): - _next_id_number = defaultdict(lambda: 1) # Initalises every key element to 1 + self._next_id_number = defaultdict(lambda: 1) # Initalises every key element to 1 def get_next_id(self, graph_id_type: str): - graph_id = GraphID(graph_id_type, _next_id_number[graph_id_type]) - _next_id_number[graph_id_type] += 1 + """ + Retrns the next graph id for a certain graph id type. + """ + graph_id = GraphID(graph_id_type, self._next_id_number[graph_id_type]) + self._next_id_number[graph_id_type] += 1 # Increment the current number return graph_id @@ -58,7 +61,3 @@ class GraphID: Returns a new GraphID of the same type with an incremented id number. """ return GraphID(self.graph_id_type, self.graph_id_number + 1) - - - -def is_id_in_use() \ No newline at end of file diff --git a/b_asic/signal_flow_graph.py b/b_asic/signal_flow_graph.py index 3f804f0e9b81dfa2797ba40016eaba96414b2509..d4c7b689f0dc9de79bfa816db85ecdfb20427b1c 100644 --- a/b_asic/signal_flow_graph.py +++ b/b_asic/signal_flow_graph.py @@ -7,12 +7,8 @@ from b_asic.operation import Operation from b_asic.basic_operation import BasicOperation from b_asic.signal import Signal, SignalSource, SignalDestination from b_asic.simulation import SimulationState, OperationState -from typing import List, Dict - -# Types -OperationId = NewType("OperationId", int) -SignalId = NewType("SignalId", int) - +from typing import List, Dict, Union +from graph_id import GraphIDGenerator, GraphID class SFG(BasicOperation): @@ -21,18 +17,18 @@ class SFG(BasicOperation): TODO: More info. """ - _operations: Dict[OperationID, Operation] - _signals: Dict[SignalID, Signal] + _graph_objects: Dict(GraphID, Union(Operation, Signal)) + _graph_id_generator: GraphIDGenerator def __init__(self, input_destinations: List[SignalDestination], output_sources: List[SignalSource]): """ Construct a SFG. """ - super().__init__(identifier) + super().__init__() # TODO: Allocate input/output ports with appropriate IDs. - self._operations = dict # Map Operation ID to Operation objects - self._signals = dict # Map Signal ID to Signal objects + self._graph_objects = dict # Map Operation ID to Operation objects + self._graph_id_generator = GraphIDGenerator() # TODO: Traverse the graph between the inputs/outputs and add to self._operations. # TODO: Connect ports with signals with appropriate IDs. @@ -40,6 +36,15 @@ class SFG(BasicOperation): def evaluate(self, inputs: list) -> list: return [] # TODO: Implement - def split(self) -> List[Operation]: - return self + def add_operation(self, operation: Operation) -> GraphID: + """ + Adds the entered operation to the SFG's dictionary of graph objects and + generates and returns a GraphID for it. + """ + graph_id = self._graph_id_generator.get_next_id("op") + self._graph_objects[graph_id] = operation + return graph_id + + def _generate_id(self) +