2121from spinn_machine import Machine , RoutingEntry
2222from pacman .data import PacmanDataView
2323from pacman .exceptions import PacmanRoutingException
24+ from pacman .model .graphs import AbstractVertex
2425from pacman .model .routing_table_by_partition import (
2526 MulticastRoutingTableByPartition )
2627from pacman .utilities .algorithm_utilities .routing_algorithm_utilities import (
3132from pacman .model .graphs .machine import MachineVertex , MulticastEdgePartition
3233from pacman .model .graphs import AbstractEdgePartition
3334
34- _AnyVertex : TypeAlias = Union [ApplicationVertex , MachineVertex ]
3535_Node : TypeAlias = Tuple [int , XY ]
3636_OptInt : TypeAlias = Optional [int ]
37- _MappedSrc : TypeAlias = Tuple [_AnyVertex , _OptInt , _OptInt ]
37+ _MappedSrc : TypeAlias = Tuple [AbstractVertex , _OptInt , _OptInt ]
3838
3939
4040class _Targets (object ):
@@ -45,10 +45,10 @@ class _Targets(object):
4545
4646 def __init__ (self ) -> None :
4747 self .__targets_by_source : Dict [
48- _AnyVertex , Tuple [List [int ], List [int ]]] = defaultdict (
48+ AbstractVertex , Tuple [List [int ], List [int ]]] = defaultdict (
4949 lambda : (list (), list ()))
5050
51- def ensure_source (self , source_vertex : _AnyVertex ) -> None :
51+ def ensure_source (self , source_vertex : AbstractVertex ) -> None :
5252 """
5353 Ensure that a source exists, even if it targets nothing.
5454
@@ -60,7 +60,8 @@ def ensure_source(self, source_vertex: _AnyVertex) -> None:
6060
6161 def add_sources_for_target (
6262 self , core : _OptInt , link : _OptInt ,
63- source_vertices : Iterable [_AnyVertex ], partition_id : str ) -> None :
63+ source_vertices : Iterable [AbstractVertex ],
64+ partition_id : str ) -> None :
6465 """
6566 Add a set of vertices that target a given core or link.
6667
@@ -78,14 +79,17 @@ def add_sources_for_target(
7879 self .__add_m_vertices (vertex , partition_id , core , link )
7980 else :
8081 self .__add_source (vertex , core , link )
81- else :
82+ elif isinstance ( vertex , MachineVertex ) :
8283 if vertex .app_vertex in self .__targets_by_source :
8384 self .__replace_app_vertex (vertex .app_vertex , partition_id )
8485 self .__add_source (vertex , core , link )
86+ else :
87+ raise TypeError (f"Unexpected { vertex = } " )
8588
8689 def add_machine_sources_for_target (
8790 self , core : _OptInt , link : _OptInt ,
88- source_vertices : Iterable [_AnyVertex ], partition_id : str ) -> None :
91+ source_vertices : Iterable [AbstractVertex ],
92+ partition_id : str ) -> None :
8993 """
9094 Add a set of machine vertices that target a given core or link.
9195
@@ -102,10 +106,12 @@ def add_machine_sources_for_target(
102106 if vertex in self .__targets_by_source :
103107 self .__replace_app_vertex (vertex , partition_id )
104108 self .__add_m_vertices (vertex , partition_id , core , link )
105- else :
109+ elif isinstance ( vertex , MachineVertex ) :
106110 if vertex .app_vertex in self .__targets_by_source :
107111 self .__replace_app_vertex (vertex .app_vertex , partition_id )
108112 self .__add_source (vertex , core , link )
113+ else :
114+ raise TypeError (f"Unexpected { vertex = } " )
109115
110116 def __is_m_vertex (
111117 self , vertex : ApplicationVertex , partition_id : str ) -> bool :
@@ -145,7 +151,7 @@ def __add_m_vertices(
145151 self .__add_source (vtx , core , link )
146152
147153 def __add_source (
148- self , source : _AnyVertex , core : _OptInt , link : _OptInt ) -> None :
154+ self , source : AbstractVertex , core : _OptInt , link : _OptInt ) -> None :
149155 """
150156 :param source:
151157 :param core:
@@ -159,7 +165,7 @@ def __add_source(
159165
160166 @property
161167 def targets_by_source (self ) -> Iterable [
162- Tuple [_AnyVertex , Tuple [List [int ], List [int ]]]]:
168+ Tuple [AbstractVertex , Tuple [List [int ], List [int ]]]]:
163169 """
164170 List of (source, (list of cores, list of links)) to target.
165171
@@ -168,8 +174,8 @@ def targets_by_source(self) -> Iterable[
168174 """
169175 return self .__targets_by_source .items ()
170176
171- def get_targets_for_source (self , vertex : _AnyVertex ) -> Tuple [
172- _AnyVertex , Tuple [List [int ], List [int ]]]:
177+ def get_targets_for_source (self , vertex : AbstractVertex ) -> Tuple [
178+ AbstractVertex , Tuple [List [int ], List [int ]]]:
173179 """
174180 Get the cores and links for a specific source.
175181
@@ -916,7 +922,7 @@ def _find_path(
916922
917923def _convert_a_route (
918924 routing_tables : MulticastRoutingTableByPartition ,
919- source_vertex : _AnyVertex , partition_id : str ,
925+ source_vertex : AbstractVertex , partition_id : str ,
920926 first_incoming_processor : _OptInt , first_incoming_link : _OptInt ,
921927 first_route : RoutingTree , targets : Dict [XY , _Targets ],
922928 use_source_for_targets : bool = False ,
@@ -964,7 +970,7 @@ def _convert_a_route(
964970 if (x , y ) in targets :
965971 chip_targets = targets [x , y ]
966972 targets_by_source : Iterable [
967- Tuple [_AnyVertex , Tuple [List [int ], List [int ]]]]
973+ Tuple [AbstractVertex , Tuple [List [int ], List [int ]]]]
968974 if use_source_for_targets :
969975 targets_by_source = [
970976 chip_targets .get_targets_for_source (source_vertex )]
@@ -978,8 +984,11 @@ def _convert_a_route(
978984 for (source , (add_cores , add_links )) in targets_by_source :
979985 if isinstance (source , ApplicationVertex ):
980986 app_vertex_source = True
981- else :
987+ elif isinstance ( source , MachineVertex ) :
982988 machine_vertex_sources .add (source )
989+ else :
990+ raise TypeError (f"Unexpected vertex { source } " )
991+
983992 entry = RoutingEntry (
984993 link_ids = link_ids + add_links ,
985994 processor_ids = processor_ids + add_cores ,
@@ -1016,7 +1025,7 @@ def _add_routing_entry(
10161025 first_route : RoutingTree ,
10171026 routing_tables : MulticastRoutingTableByPartition ,
10181027 entry : RoutingEntry ,
1019- x : int , y : int , source : _AnyVertex , partition_id : str ) -> None :
1028+ x : int , y : int , source : AbstractVertex , partition_id : str ) -> None :
10201029 try :
10211030 routing_tables .add_path_entry (entry , x , y , source , partition_id )
10221031 except Exception as e :
0 commit comments