-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
floogen: Use unique IDs for endpoints (#78)
* floogen(routing)!: Rename `id_offset` to `xy_id_offset` An ID offset is only needed for XY Routing * floogen(routing): Remove support for ID offset for non-XY-Routing * floogen(routing): Introduce unique `uid` for enum generation * floogen(graph): Make it possible to get node ID with object * floogen(graph): Make it possible to return either only object or name of a node * floogen(routing): More reasonable ordering of endpoint name * docs: Update CHANGELOG
- Loading branch information
Showing
11 changed files
with
99 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
# | ||
# Author: Tim Fischer <[email protected]> | ||
|
||
import re | ||
from typing import List, Tuple | ||
|
||
import networkx as nx | ||
|
@@ -87,66 +88,70 @@ def is_link_edge(self, edge): | |
"""Return whether the edge is a link edge.""" | ||
return self.edges[edge]["type"] == "link" | ||
|
||
def get_nodes(self, filters=None, with_name=False): | ||
def get_nodes(self, filters=None, with_obj=True, with_name=False): | ||
"""Filter the nodes from the graph.""" | ||
nodes = self.nodes | ||
if filters is not None: | ||
for flt in filters: | ||
nodes = list(filter(flt, nodes)) | ||
if with_name: | ||
if with_obj and with_name: | ||
return [(node, self.get_node_obj(node)) for node in nodes] | ||
return [self.get_node_obj(node) for node in nodes] | ||
if with_obj: | ||
return [self.get_node_obj(node) for node in nodes] | ||
return nodes | ||
|
||
def get_edges(self, filters=None, with_name=False): | ||
def get_edges(self, filters=None, with_obj=True, with_name=False): | ||
"""Filter the edges from the graph.""" | ||
edges = self.edges | ||
if filters is not None: | ||
for flt in filters: | ||
edges = list(filter(flt, edges)) | ||
if with_name: | ||
if with_obj and with_name: | ||
return [(edge, self.get_edge_obj(edge)) for edge in edges] | ||
return [self.get_edge_obj(edge) for edge in edges] | ||
if with_obj: | ||
return [self.get_edge_obj(edge) for edge in edges] | ||
return edges | ||
|
||
def get_edges_from(self, node, filters=None, with_name=False): | ||
def get_edges_from(self, node, filters=None, with_obj=True, with_name=False): | ||
"""Return the outgoing edges from the node.""" | ||
if filters is None: | ||
filters = [] | ||
filters = [lambda e: e[0] == node] + filters | ||
return self.get_edges(filters=filters, with_name=with_name) | ||
return self.get_edges(filters=filters, with_obj=with_obj, with_name=with_name) | ||
|
||
def get_edges_to(self, node, filters=None, with_name=False): | ||
def get_edges_to(self, node, filters=None, with_obj=True, with_name=False): | ||
"""Return the incoming edges to the node.""" | ||
if filters is None: | ||
filters = [] | ||
filters = [lambda e: e[1] == node] + filters | ||
return self.get_edges(filters=filters, with_name=with_name) | ||
return self.get_edges(filters=filters, with_obj=with_obj, with_name=with_name) | ||
|
||
def get_edges_of(self, node, filters=None, with_name=False): | ||
def get_edges_of(self, node, filters=None, with_obj=True, with_name=False): | ||
"""Return the edges of the node.""" | ||
if filters is None: | ||
filters = [] | ||
filters = [lambda e: node in e] + filters | ||
return self.get_edges(filters=filters, with_name=with_name) | ||
return self.get_edges(filters=filters, with_obj=with_obj, with_name=with_name) | ||
|
||
def get_ni_nodes(self, with_name=False): | ||
def get_ni_nodes(self, with_obj=True, with_name=False): | ||
"""Return the ni nodes.""" | ||
return self.get_nodes(filters=[self.is_ni_node], with_name=with_name) | ||
return self.get_nodes(filters=[self.is_ni_node], with_obj=with_obj, with_name=with_name) | ||
|
||
def get_rt_nodes(self, with_name=False): | ||
def get_rt_nodes(self, with_obj=True, with_name=False): | ||
"""Return the router nodes.""" | ||
return self.get_nodes(filters=[self.is_rt_node], with_name=with_name) | ||
return self.get_nodes(filters=[self.is_rt_node], with_obj=with_obj, with_name=with_name) | ||
|
||
def get_ep_nodes(self, with_name=False): | ||
def get_ep_nodes(self, with_obj=True, with_name=False): | ||
"""Return the endpoint nodes.""" | ||
return self.get_nodes(filters=[self.is_ep_node], with_name=with_name) | ||
return self.get_nodes(filters=[self.is_ep_node], with_obj=with_obj, with_name=with_name) | ||
|
||
def get_prot_edges(self, with_name=False): | ||
def get_prot_edges(self, with_obj=True, with_name=False): | ||
"""Return the protocol edges.""" | ||
return self.get_edges(filters=[self.is_prot_edge], with_name=with_name) | ||
return self.get_edges(filters=[self.is_prot_edge], with_obj=with_obj, with_name=with_name) | ||
|
||
def get_link_edges(self, with_name=False): | ||
def get_link_edges(self, with_obj=True, with_name=False): | ||
"""Return the link edges.""" | ||
return self.get_edges(filters=[self.is_link_edge], with_name=with_name) | ||
return self.get_edges(filters=[self.is_link_edge], with_obj=with_obj, with_name=with_name) | ||
|
||
def get_nodes_from_range(self, node: str, rng: List[Tuple[int]]): | ||
"""Return the nodes from the range.""" | ||
|
@@ -282,8 +287,28 @@ def add_nodes_as_array( | |
def create_unique_ep_id(self, node) -> int: | ||
"""Return the endpoint id.""" | ||
ep_nodes = [name for name, _ in self.get_ep_nodes(with_name=True)] | ||
return sorted(ep_nodes).index(node) | ||
|
||
def get_node_id(self, node): | ||
# Custom sorting function: extract numeric part after letters | ||
def extract_number(name): | ||
match = re.search(r"(\d+)$", name) | ||
return int(match.group(1)) if match else -1 | ||
|
||
return sorted( | ||
ep_nodes, key=lambda name: (re.sub(r"\d+$", "", name), extract_number(name)) | ||
).index(node) | ||
|
||
def get_node_id(self, node_name=None, node_obj=None): | ||
"""Return the node id.""" | ||
return self.nodes[node]["id"] | ||
if node_name is not None: | ||
return self.nodes[node_name]["id"] | ||
if node_obj is not None: | ||
return self.nodes[node_obj.name]["id"] | ||
raise ValueError("Node name or object not provided") | ||
|
||
def get_node_uid(self, node_name=None, node_obj=None): | ||
"""Return the unique node id.""" | ||
if node_name is not None: | ||
return self.nodes[node_name]["uid"] | ||
if node_obj is not None: | ||
return self.nodes[node_obj.name]["uid"] | ||
raise ValueError("Node name or object not provided") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters