Module RelayModel.GraphGeneration
Expand source code
import random
from RelayModel import KeyGeneration
from RelayModel.RelayId import RelayId
class InBlueprint:
"""
Holds information to establish an incoming connection with relays.
Attributes:
key (str): Holds the key that authorizes the incoming connection
rid (RelayLayerId): Holds the RelayLayerId of the RelayLayer that is allowed to send messages over this incoming connection.
"""
def __init__(self, key, rid):
"""Creates an InBlueprint object with the given key and rid.
Args:
key (str): Defines the key that authenticates the incoming blueprint.
rid (RelayLayerId): Defines the RelayLayerId of the RelayLayer that has a connection to this blueprint.
"""
self.key = key
self.rid = rid
class OutBlueprint:
"""
Holds information to establish an outgoing connection with relays.
Attributes:
key (str): Holds the key that authorizes the outgoing connection
sink_rid (RelayLayerId): Holds the RelayLayerId of the RelayLayer that manages the relay of the sink relay of this connection
out_id (RelayId): Holds the RelayId of the relay the outgoing connection is pointing to
direction (int): Holds information about a possible sorted list configuration. 0 is nothing is set. If 1 is set the connection
is set to the left attribute of the sorted list. If 2 is set the right is set to this relay
"""
def __init__(self, key, sink_rid, direction):
"""Creates an OutBlueprint object with the given informations
Args:
key (str): Defines the key that authenticates the outgoing connection.
sink_rid (RelayLayerId): Defines the RelayLayerId of the Relay holding the sink relay for this connection.
direction (int): Defines the possible direction set in the SortedListNode.
"""
self.key = key
self.sink_rid = sink_rid
self.out_id = RelayId(sink_rid, 0)
# 0 nothing 1 left 2 right
self.direction = direction
class Blueprint:
"""
Defines a class that is needed to make a initial graph of nodes and relays. It holds outgoing and incoming
blueprint to establish a connection to other nodes.
Attributes:
in_blueprints (list): Holds all incoming blueprints to establish incoming connections of a node
out_blueprints (list): Holds all outgoing blueprints to establish outgoing connections to other nodes over relays
"""
def __init__(self):
"""Creates a Blueprint object.
The in_blueprints and out_blueprints are set to an empty list. They need to be filled up after creation.
"""
self.in_blueprints = []
self.out_blueprints = []
def make_weakly_connected_sorted_list(node_list):
"""Makes a list of Blueprints which information builds a weakly connected directed graph.
It takes a list of RelayLayerIds for a specification which nodes should be present in the graph.
Args:
node_list (list): Holds a list of RelayLayerIds defining Nodes that should be present in the resulting graph
Returns:
dict: A dictionary where the key is the RelayLayerId of a node and the value is the Blueprint class for this node
"""
max_nodes = len(node_list)
if max_nodes == 1:
print("Too few nodes got provided")
return
node_blueprints = {}
connected_groups = []
for relay_layer_id in node_list:
node_blueprints[relay_layer_id] = Blueprint()
connected_groups += [[relay_layer_id]]
while len(connected_groups) > 1:
group_index_1 = random.randint(0, len(connected_groups) - 1)
group_index_2 = random.randint(0, len(connected_groups) - 1)
while group_index_1 == group_index_2:
group_index_2 = random.randint(0, len(connected_groups) - 1)
connected_group_1 = connected_groups[group_index_1]
connected_group_2 = connected_groups[group_index_2]
connected_index_1 = random.randint(0, len(connected_group_1) - 1)
connected_index_2 = random.randint(0, len(connected_group_2) - 1)
node_1 = connected_group_1[connected_index_1]
node_2 = connected_group_2[connected_index_2]
connection_direction = random.randint(0, 1)
if connection_direction == 0:
send_node = node_1
sink_node = node_2
else:
send_node = node_2
sink_node = node_1
key = KeyGeneration.generate_key(sink_node)
direction = random.randint(0, 2)
out_bp = OutBlueprint(key, sink_node, direction)
in_bp = InBlueprint(key, send_node)
node_blueprints[sink_node].in_blueprints.append(in_bp)
node_blueprints[send_node].out_blueprints.append(out_bp)
connected_group_1.extend(connected_group_2)
del connected_groups[group_index_2]
return node_blueprints
Functions
def make_weakly_connected_sorted_list(node_list)-
Makes a list of Blueprints which information builds a weakly connected directed graph.
It takes a list of RelayLayerIds for a specification which nodes should be present in the graph.
Args
node_list:list- Holds a list of RelayLayerIds defining Nodes that should be present in the resulting graph
Returns
dict- A dictionary where the key is the RelayLayerId of a node and the value is the Blueprint class for this node
Expand source code
def make_weakly_connected_sorted_list(node_list): """Makes a list of Blueprints which information builds a weakly connected directed graph. It takes a list of RelayLayerIds for a specification which nodes should be present in the graph. Args: node_list (list): Holds a list of RelayLayerIds defining Nodes that should be present in the resulting graph Returns: dict: A dictionary where the key is the RelayLayerId of a node and the value is the Blueprint class for this node """ max_nodes = len(node_list) if max_nodes == 1: print("Too few nodes got provided") return node_blueprints = {} connected_groups = [] for relay_layer_id in node_list: node_blueprints[relay_layer_id] = Blueprint() connected_groups += [[relay_layer_id]] while len(connected_groups) > 1: group_index_1 = random.randint(0, len(connected_groups) - 1) group_index_2 = random.randint(0, len(connected_groups) - 1) while group_index_1 == group_index_2: group_index_2 = random.randint(0, len(connected_groups) - 1) connected_group_1 = connected_groups[group_index_1] connected_group_2 = connected_groups[group_index_2] connected_index_1 = random.randint(0, len(connected_group_1) - 1) connected_index_2 = random.randint(0, len(connected_group_2) - 1) node_1 = connected_group_1[connected_index_1] node_2 = connected_group_2[connected_index_2] connection_direction = random.randint(0, 1) if connection_direction == 0: send_node = node_1 sink_node = node_2 else: send_node = node_2 sink_node = node_1 key = KeyGeneration.generate_key(sink_node) direction = random.randint(0, 2) out_bp = OutBlueprint(key, sink_node, direction) in_bp = InBlueprint(key, send_node) node_blueprints[sink_node].in_blueprints.append(in_bp) node_blueprints[send_node].out_blueprints.append(out_bp) connected_group_1.extend(connected_group_2) del connected_groups[group_index_2] return node_blueprints
Classes
class Blueprint-
Defines a class that is needed to make a initial graph of nodes and relays. It holds outgoing and incoming blueprint to establish a connection to other nodes.
Attributes
in_blueprints:list- Holds all incoming blueprints to establish incoming connections of a node
out_blueprints:list- Holds all outgoing blueprints to establish outgoing connections to other nodes over relays
Creates a Blueprint object.
The in_blueprints and out_blueprints are set to an empty list. They need to be filled up after creation.
Expand source code
class Blueprint: """ Defines a class that is needed to make a initial graph of nodes and relays. It holds outgoing and incoming blueprint to establish a connection to other nodes. Attributes: in_blueprints (list): Holds all incoming blueprints to establish incoming connections of a node out_blueprints (list): Holds all outgoing blueprints to establish outgoing connections to other nodes over relays """ def __init__(self): """Creates a Blueprint object. The in_blueprints and out_blueprints are set to an empty list. They need to be filled up after creation. """ self.in_blueprints = [] self.out_blueprints = [] class InBlueprint (key, rid)-
Holds information to establish an incoming connection with relays.
Attributes
key:str- Holds the key that authorizes the incoming connection
rid:RelayLayerId- Holds the RelayLayerId of the RelayLayer that is allowed to send messages over this incoming connection.
Creates an InBlueprint object with the given key and rid.
Args
key:str- Defines the key that authenticates the incoming blueprint.
rid:RelayLayerId- Defines the RelayLayerId of the RelayLayer that has a connection to this blueprint.
Expand source code
class InBlueprint: """ Holds information to establish an incoming connection with relays. Attributes: key (str): Holds the key that authorizes the incoming connection rid (RelayLayerId): Holds the RelayLayerId of the RelayLayer that is allowed to send messages over this incoming connection. """ def __init__(self, key, rid): """Creates an InBlueprint object with the given key and rid. Args: key (str): Defines the key that authenticates the incoming blueprint. rid (RelayLayerId): Defines the RelayLayerId of the RelayLayer that has a connection to this blueprint. """ self.key = key self.rid = rid class OutBlueprint (key, sink_rid, direction)-
Holds information to establish an outgoing connection with relays.
Attributes
key:str- Holds the key that authorizes the outgoing connection
sink_rid:RelayLayerId- Holds the RelayLayerId of the RelayLayer that manages the relay of the sink relay of this connection
out_id:RelayId- Holds the RelayId of the relay the outgoing connection is pointing to
direction:int- Holds information about a possible sorted list configuration. 0 is nothing is set. If 1 is set the connection is set to the left attribute of the sorted list. If 2 is set the right is set to this relay
Creates an OutBlueprint object with the given informations
Args
key:str- Defines the key that authenticates the outgoing connection.
sink_rid:RelayLayerId- Defines the RelayLayerId of the Relay holding the sink relay for this connection.
direction:int- Defines the possible direction set in the SortedListNode.
Expand source code
class OutBlueprint: """ Holds information to establish an outgoing connection with relays. Attributes: key (str): Holds the key that authorizes the outgoing connection sink_rid (RelayLayerId): Holds the RelayLayerId of the RelayLayer that manages the relay of the sink relay of this connection out_id (RelayId): Holds the RelayId of the relay the outgoing connection is pointing to direction (int): Holds information about a possible sorted list configuration. 0 is nothing is set. If 1 is set the connection is set to the left attribute of the sorted list. If 2 is set the right is set to this relay """ def __init__(self, key, sink_rid, direction): """Creates an OutBlueprint object with the given informations Args: key (str): Defines the key that authenticates the outgoing connection. sink_rid (RelayLayerId): Defines the RelayLayerId of the Relay holding the sink relay for this connection. direction (int): Defines the possible direction set in the SortedListNode. """ self.key = key self.sink_rid = sink_rid self.out_id = RelayId(sink_rid, 0) # 0 nothing 1 left 2 right self.direction = direction