pydistsim.algorithm.node_algorithm.NodeAlgorithm

class NodeAlgorithm(*args, **kwargs)[source]

Bases: BaseAlgorithm

Base class for distributed algorithms.

In subclass, the following functions and attributes should be defined:

  • Class attribute Status - subclass of StatusValues that defines all the possible statuses of the nodes STATUS must be written at the bottom after all functions are defined

  • Initializer: (optionally) Pass INI message to nodes that should start the algorithm and set some attributes needed by the specific algorithm

Methods

__init__

add_observers

alarm

apply_restrictions

Apply all applicable restrictions.

block_inbox

Block the inbox of a node, so that only messages that satisfy the filter can be processed.

check_algorithm_initialization

Check if the algorithm's current state matches the expected initial state.

check_algorithm_termination

Check if the algorithm's current state matches the expected termination state.

check_restrictions

Check if the restrictions are satisfied.

clear_observers

close

Close the inbox of the node for a specific neighbor.

default

disable_alarm

Disable an alarm.

disable_all_node_alarms

Disable all alarms set for the node.

initializer

Method used to initialize the algorithm.

is_halted

Check if the distributed algorithm has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.

is_initialized

notify_observers

open

Open the inbox of the node for a specific neighbor.

receiving

reset

Reset the algorithm to its initial state.

send

Send content to destination, with header.

send_msg

Send a message to nodes listed in message's destination field.

set_alarm

Set an alarm for the node.

spontaneously

step

unblock_inbox

Unblock the inbox of a node, so that all messages can be processed.

update_alarm_time

Update the time of an alarm by adding a time difference.

Attributes

INI

S_init

Tuple of statuses that nodes should have at the beginning of the algorithm.

S_term

Tuple of statuses that nodes should have at the end of the algorithm.

algorithm_restrictions

Tuple of restrictions that must be satisfied for the algorithm to run.

default_params

network

required_params

nwm

Node wrapper manager.

NODE_WRAPPER_MANAGER_TYPE

Type of the node wrapper manager. Default is WrapperManager.

alias of WrapperManager

S_init = ()

Tuple of statuses that nodes should have at the beginning of the algorithm.

S_term = ()

Tuple of statuses that nodes should have at the end of the algorithm.

class Status(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StatusValues

Example of StatusValues subclass that defines the possible statuses of the nodes.

__new__(value)
_generate_next_value_(start, count, last_values)

Return the lower-cased version of the member name.

_alarms: list[Alarm]

List of alarms set for the nodes.

_node_step(node: Node)[source]

Executes one step of the algorithm for given node.

algorithm_restrictions = (<class 'pydistsim.restrictions.axioms.FiniteCommunicationDelays'>, <class 'pydistsim.restrictions.axioms.LocalOrientation'>)

Tuple of restrictions that must be satisfied for the algorithm to run.

block_inbox(node: NodeAccess, filter: Callable[[Message[_NodeWrapper]], bool]) object[source]

Block the inbox of a node, so that only messages that satisfy the filter can be processed.

Parameters:
  • node (NodeAccess) – The node for which the inbox is blocked.

  • filter (Callable[[Message], bool]) – The filter that the messages must satisfy.

check_algorithm_initialization()[source]

Check if the algorithm’s current state matches the expected initial state.

This method depends on the correct configuration of the S_init class attribute of the algorithm.

check_algorithm_termination()[source]

Check if the algorithm’s current state matches the expected termination state.

This method depends on the correct configuration of the S_term class attribute of the algorithm.

close(node: NodeAccess, neighbor: NeighborLabel)[source]

Close the inbox of the node for a specific neighbor.

Implementation detail

The inbox is closed by blocking all messages that have the neighbor as the source. This is done by adding a filter to the inbox that blocks all messages that have the neighbor as the source. To be able to unblock the inbox later, the filter is stored in the node’s memory, under the key __closed_edges_filters__.

param node:

The node for which the inbox is closed.

type node:

NodeAccess

param neighbor:

The neighbor for which the inbox is closed.

type neighbor:

NeighborLabel

disable_alarm(alarm: Alarm)[source]

Disable an alarm.

Parameters:

alarm (Alarm) – The alarm to be disabled.

disable_all_node_alarms(node_p: NodeAccess)[source]

Disable all alarms set for the node.

Parameters:

node (NodeProxy) – The node for which the alarms are disabled.

initializer()[source]

Method used to initialize the algorithm. Is always called first. NodeAlgorithm subclasses may want to reimplement it.

Base implementation sends an INI message to the node with the lowest id and applies all restrictions.

is_halted()[source]

Check if the distributed algorithm has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.

A not-started algorithm is considered halted.

Returns:

True if the algorithm is halted, False otherwise.

Return type:

bool

nwm

Node wrapper manager.

open(node: NodeAccess, neighbor: NeighborLabel)[source]

Open the inbox of the node for a specific neighbor.

Parameters:
  • node (NodeAccess) – The node for which the inbox is opened.

  • neighbor (NeighborLabel) – The neighbor for which the inbox is opened.

reset()[source]

Reset the algorithm to its initial state.

send(node_source: NodeAccess, data: object, destination: NeighborLabel | list[NeighborLabel], header: str = None)[source]

Send content to destination, with header.

Parameters:
  • node_w – Sender node

  • data – Content to be received by destination nodes.

  • destination – Destination node or list of destinations.

  • header – Optional, header for the message.

send_msg(node_source: NodeAccess, message: Message[_NodeWrapper])[source]

Send a message to nodes listed in message’s destination field.

Note: Destination should be a list of nodes or one node.

Update message’s source field and inserts in node’s outbox one copy of it for each destination.

Parameters:
  • node_source (NodeAccess) – The node that sends the message.

  • message (Message) – The message to be sent.

set_alarm(node_p: NodeAccess, time: int, message: Message[_NodeWrapper] | None = None)[source]

Set an alarm for the node. One unit of time is one step of the algorithm. After the time has passed, the alarm will trigger and the message will be sent to the node. When will that message be processed is not guaranteed to be immediate.

Returns the alarm that was set, useful for disabling it later.

Parameters:
  • node_p – The node for which the alarm is set.

  • time (int) – The time after which the alarm will trigger.

  • message (Message) – The message to be sent when the alarm triggers.

Returns:

The alarm that was set.

Return type:

Alarm

unblock_inbox(node: NodeAccess, filter: object)[source]

Unblock the inbox of a node, so that all messages can be processed.

Parameters:
  • node (NodeAccess) – The node for which the inbox is unblocked.

  • filter (object) – The filter that was used to block the inbox.

update_alarm_time(alarm: Alarm, time_diff: int)[source]

Update the time of an alarm by adding a time difference. The time difference can be negative.

Parameters:
  • alarm (Alarm) – The alarm for which the time is updated.

  • time_diff (int) – The time difference to be added to the alarm’s time.