node.node module

Module node contains an abstract BaseNode class which is the base class for all communication node entities in the simulation.

Note

  • Created by: CH Foh (2021)

class node.node.BaseNode(simworld, id=None, node_type=None)

Bases: abc.ABC, node.draw.Drawing

This is an abstract base class for all communication node entities to extend.

The subclass can access to several convenient functions via the base class. Each node instance should have a unique id to be identified easily. Currently, the recommended data type for the id is string.

Attributes
idnode.node.BaseNode.ID

The id of this node. Ideally, this should be unique for each node so that a lookup can always find the appropriate node instance based on the id. However, the code will not check for uniqueness.

typenode.node.BaseNode.Type

The type of this node. The type is used in the simulation to draw the node on the screen. Different types will be drawn differently.

Methods

ID(id_str)

The data structure for the node id.

Type(node_type)

The definition of a node type.

clear_drawing()

Clear the drawing for this node by removing all drawables associated with this node.

del_circle(radius)

(Deprecated) Remove an earlier added circle for this node.

del_drawable(drawable)

Remove a specific drawable from the drawing list.

del_line(other_node)

(Deprecated) Remove an earlier added line for this node.

del_sector(radius, pointing_angle, width_angle)

(Deprecated) Remove an earlier added sector drawing.

del_text(dx, dy, text)

(Deprecated) Remove an earlier added text message.

draw_circle(radius[, pen, brush])

Put a circle around this node where the node is the center of the circle.

draw_line(other_node[, pen, brush])

Draw a line from this node to other_node.

draw_line_segments(points[, pen, brush])

Draw line segments around this node where (0,0) is the center of the node.

draw_sector(radius, pointing_angle, width_angle)

Draw a sector where its center is at this node, its radius is radius its pointing angle is pointing_angle, its width (in degree) is specified in width_angle.

draw_text(dx, dy, text, font[, foreground, …])

Draw a text at around this node.

get(query_str)

Query the node to retrieve corresponding information.

get_color()

Get the color that is used to draw this node.

get_node_list()

Provide the list of all nodes.

is_disabled()

Use this method to check if a node is disabled.

is_visible()

Use this method to check if a node is visible.

lookup(id)

This is a static method for performing a lookup for the node with id.

remove_from_simulation()

This method is used to remove this node from the simulation.

reset()

Reset the global node list.

reset_color()

Reset the color for the node to the default color of wx.BLACK.

set_color(color)

Set the color to draw for this node.

set_mobility(mobility[, direction])

Set the mobility for this instance.

set_transceiver(transceiver)

Use this method to set a transceiver for the node.

set_visible([flag])

Use this method to set the visibility of a node.

class ID(id_str: str)

Bases: object

The data structure for the node id. The current implementation uses string for the id.

__init__(id_str: str)

Initialize self. See help(type(self)) for accurate signature.

class Type(node_type)

Bases: object

The definition of a node type. The animation process will use the type to decide how to draw the node on the screen. Besides, this can be used for user simulation to check the belonging of a node and also conveniently retrieve all nodes of the same type.

Note

This is now an obsolete feature and will be removed soon. Please use any node.type.BaseType subclass for node type instead.

BS = 0

Use it to describe a base station. A small circle will be drawn to to represent a base station.

Vehicle = 1

Use it to describe a vehicle. A triangle will be drawn to represent a vehicle.

Drone = 2

Use it to describe a drone. It is not implemented at the moment.

UserDefined = 1000

Set a node to this type for user defined type. If the simulation needs more than one user defined types, use UserDefined+1, UserDefined+2, and so on to extend user defined types. Any number that is larger or equal to UserDefined is considered a user defined type. The animation will not draw user defined types on the screen, the user simulation must manually draw a shape or show a bitmap image to indicate the presence of the node.

__init__(node_type)

Initialize self. See help(type(self)) for accurate signature.

abstract __init__(simworld, id=None, node_type=None)

This is the constructor. It must be reimplemented and super() must be called.

Parameters
simworldsim.simulation.World

To provide the container of the node to be initiated.

idnode.node.BaseNode.ID, default=None

To provide the id of this node.

typenode.node.BaseNode.Type, default=None

To specify the type of this node.

static get_node_list()

Provide the list of all nodes.

Note

There is no reason why user simulation needs to use this method to obtain all nodes, since user simulation creates all nodes in own scenario class and should keep a record of all nodes within the scenario class.

static reset()

Reset the global node list.

Note

User simulation should not call this method. This method is used for simulation engine to start a new simulation with proper initialization.

remove_from_simulation()

This method is used to remove this node from the simulation.

In the user simulation, if nodes are allocated dynamically, this method must be used to remove any unwanted node. Removing a node will inform the simulation engine to disable it so that it will not interact with other nodes in the simulation world and will not be shown on the animation.

The method does not immediately delete the node, it marks the node as disabled. The simulation will ignore the disabled nodes during the processing. In the simulation loop, when there is a substantial number of disabled nodes, the simulation will trigger garbage collector to remove the disabled nodes from the global list.

is_disabled()

Use this method to check if a node is disabled.

This method is used in the simulation engine. There is no reason to use it in the user simulation.

Returns
bool

Whether the node is disabled. Disabled nodes will not appear on the simulation world.

is_visible()

Use this method to check if a node is visible. Note that the drawing of user defined types is not managed by the simulation engine, thus the simulation engine simply ignores the flag. However, the user may use this flag for own indication to manually control the visibility of this node.

set_visible(flag=True)

Use this method to set the visibility of a node. Note that the drawing of user defined types is not managed by the simulation engine, thus the simulation engine simply ignores the flag. However, the user may use this flag for own indication to manually control the visibility of this node.

set_color(color)

Set the color to draw for this node.

Parameters
colorcolor constant defined in wx

The color to draw.

get_color()

Get the color that is used to draw this node.

Returns
Color constant defined in wx

The color for this node.

reset_color()

Reset the color for the node to the default color of wx.BLACK.

get(query_str: str)

Query the node to retrieve corresponding information.

The information can be queried includes:

  • “location”: the current location of the node, return an instance of sim.loc.LOC subclass.

  • “direction”: the current moving direction of the node, return an instance of sim.direction.DIR subclass.

  • “speed”: the targeted moving speed of the node, return a float. The actual speed can be lower due to other slow moving vehicles in front.

  • “displacement”: the distance travelled in the last simulation step, return a float. Displacement can be useful to calculate the actual speed. (Note: currently not supported, throws system error if called)

  • “mobility”: the mobility instance of the node, return an instance of node.mobility.BaseMobility subclass.

  • “transceiver”: the transceiver instance of the node.

  • “simworld”: the simulation world instance, user simulation is already given this instance during the creation event, so it is unnecessary to use this method to get the instance again.

Parameters
query_strstr

The query string. See also above.

set_mobility(mobility, direction=None)

Set the mobility for this instance. It is important to set a mobility to a node even it is stationary, as mobility contains location and direction information used in the simulation.

Parameters
mobilitynode.mobility.BaseMobility subclass

The mobility model for this node.

directionsim.direction.DIR subclass, optional, default=None

The direction of the node. If None is given, the default setting will be a constant north pointing direction.

set_transceiver(transceiver)

Use this method to set a transceiver for the node. A transceiver is needed for communications.

Parameters
transceivercomm.transceiver.Transceiver

The transceiver for this node. The transceiver provides a collection of functions for communications.

static lookup(id: node.node.BaseNode.ID)

This is a static method for performing a lookup for the node with id.

This method acts like a local DNS, but we use id instead of a DNS name, and we retrieve from the global lookup table instead of via network DNS query. Usage:

from node.node import BaseNode
my_node = BaseNode.lookup("vehicle1")
Parameters
idnode.node.BaseNode.ID

The id to lookup.

Returns
node.node.BaseNode subclass

The node instance that carries the id, or None if not found.