Skip to content

Circular

Bases: ComponentManager

Class responsible for simulating circular access patterns functionality.

Source code in edge_sim_py/components/user_access_patterns/circular_duration_and_interval_access_pattern.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class CircularDurationAndIntervalAccessPattern(ComponentManager):
    """Class responsible for simulating circular access patterns functionality."""

    # Class attributes that allow this class to use helper methods from ComponentManager
    _instances = []
    _object_count = 0

    def __init__(
        self,
        obj_id: int = None,
        user: object = None,
        app: object = None,
        start: int = 1,
        duration_values: list = [],
        interval_values: list = [],
    ) -> object:
        """Creates a CircularDurationAndIntervalAccessPattern object.

        Args:
            obj_id (int, optional): Object identifier.
            user (object, optional): User to whom the access pattern belongs to. Defaults to None.
            start (int, optional): Time step of the first user access. Defaults to 1.
            duration_values (list, optional): List of valid values for the access durations. Defaults to [].
            interval_values (list, optional): List of valid values for the access intervals. Defaults to [].

        Returns:
            object: Created CircularDurationAndIntervalAccessPattern object.
        """
        # Adding the new object to the list of instances of its class
        self.__class__._instances.append(self)

        # Object's class instance ID
        self.__class__._object_count += 1
        if obj_id is None:
            obj_id = self.__class__._object_count
        self.id = obj_id

        # Information about the user to whom the access pattern belongs to
        self.user = user if user else None
        self.app = app if app else None

        # List of valid access pattern values
        self.duration_values = duration_values
        self.interval_values = interval_values

        # History of user accesses
        self.history = []

        # Updating the user "making_request" for the steps prior to user's first access based on the "start" attribute
        if self.user:
            self.user.access_patterns[str(app.id)] = self
            self.user.making_requests[str(app.id)] = {}
            for step in range(1, start):
                self.user.making_requests[str(app.id)][str(step)] = False
            self.user.making_requests[str(app.id)][str(start)] = True

            # Generating the initial user request
            self.get_next_access(start=start)

        # Model-specific attributes
        self.model = ComponentManager._ComponentManager__model

    def _to_dict(self) -> dict:
        """Method that overrides the way the object is formatted to JSON."

        Returns:
            dict: JSON-friendly representation of the object as a dictionary.
        """
        dictionary = {
            "attributes": {
                "id": self.id,
                "duration_values": self.duration_values,
                "interval_values": self.interval_values,
                "history": self.history,
            },
            "relationships": {
                "user": {"class": type(self.user).__name__, "id": self.user.id} if self.user else None,
                "app": {"class": type(self.app).__name__, "id": self.app.id} if self.app else None,
            },
        }
        return dictionary

    def get_next_access(self, start: int) -> dict:
        """Gets the next access.

        Args:
            start (int): Starting point for the new access.


        Returns:
            access (dict): Next access pattern.
        """
        # As this type of access patterns needs a never-ending circular reference to the duration and interval attributes, we need
        # to create generators from these attributes (initially defined as lists). However, as we also want to keep track of the
        # duration and interval values that are cycled in the generator, we create new generator attributes representing duration
        # and interval rather than simply transforming the original list attributes into generators
        if not hasattr(self, "duration_generator"):
            self.duration_generator = cycle(self.duration_values)
        if not hasattr(self, "interval_generator"):
            self.interval_generator = cycle(self.interval_values)

        duration = next(self.duration_generator)
        interval = next(self.interval_generator)

        access = {
            "start": start,
            "end": start + duration - 1,
            "duration": duration,
            "waiting_time": 0,
            "access_time": 0,
            "interval": interval,
            "next_access": start + duration + interval,
        }
        self.history.append(access)

        return access

__init__(obj_id=None, user=None, app=None, start=1, duration_values=[], interval_values=[])

Creates a CircularDurationAndIntervalAccessPattern object.

Parameters:

Name Type Description Default
obj_id int

Object identifier.

None
user object

User to whom the access pattern belongs to. Defaults to None.

None
start int

Time step of the first user access. Defaults to 1.

1
duration_values list

List of valid values for the access durations. Defaults to [].

[]
interval_values list

List of valid values for the access intervals. Defaults to [].

[]

Returns:

Name Type Description
object object

Created CircularDurationAndIntervalAccessPattern object.

Source code in edge_sim_py/components/user_access_patterns/circular_duration_and_interval_access_pattern.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def __init__(
    self,
    obj_id: int = None,
    user: object = None,
    app: object = None,
    start: int = 1,
    duration_values: list = [],
    interval_values: list = [],
) -> object:
    """Creates a CircularDurationAndIntervalAccessPattern object.

    Args:
        obj_id (int, optional): Object identifier.
        user (object, optional): User to whom the access pattern belongs to. Defaults to None.
        start (int, optional): Time step of the first user access. Defaults to 1.
        duration_values (list, optional): List of valid values for the access durations. Defaults to [].
        interval_values (list, optional): List of valid values for the access intervals. Defaults to [].

    Returns:
        object: Created CircularDurationAndIntervalAccessPattern object.
    """
    # Adding the new object to the list of instances of its class
    self.__class__._instances.append(self)

    # Object's class instance ID
    self.__class__._object_count += 1
    if obj_id is None:
        obj_id = self.__class__._object_count
    self.id = obj_id

    # Information about the user to whom the access pattern belongs to
    self.user = user if user else None
    self.app = app if app else None

    # List of valid access pattern values
    self.duration_values = duration_values
    self.interval_values = interval_values

    # History of user accesses
    self.history = []

    # Updating the user "making_request" for the steps prior to user's first access based on the "start" attribute
    if self.user:
        self.user.access_patterns[str(app.id)] = self
        self.user.making_requests[str(app.id)] = {}
        for step in range(1, start):
            self.user.making_requests[str(app.id)][str(step)] = False
        self.user.making_requests[str(app.id)][str(start)] = True

        # Generating the initial user request
        self.get_next_access(start=start)

    # Model-specific attributes
    self.model = ComponentManager._ComponentManager__model

get_next_access(start)

Gets the next access.

Parameters:

Name Type Description Default
start int

Starting point for the new access.

required

Returns:

Name Type Description
access dict

Next access pattern.

Source code in edge_sim_py/components/user_access_patterns/circular_duration_and_interval_access_pattern.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def get_next_access(self, start: int) -> dict:
    """Gets the next access.

    Args:
        start (int): Starting point for the new access.


    Returns:
        access (dict): Next access pattern.
    """
    # As this type of access patterns needs a never-ending circular reference to the duration and interval attributes, we need
    # to create generators from these attributes (initially defined as lists). However, as we also want to keep track of the
    # duration and interval values that are cycled in the generator, we create new generator attributes representing duration
    # and interval rather than simply transforming the original list attributes into generators
    if not hasattr(self, "duration_generator"):
        self.duration_generator = cycle(self.duration_values)
    if not hasattr(self, "interval_generator"):
        self.interval_generator = cycle(self.interval_values)

    duration = next(self.duration_generator)
    interval = next(self.interval_generator)

    access = {
        "start": start,
        "end": start + duration - 1,
        "duration": duration,
        "waiting_time": 0,
        "access_time": 0,
        "interval": interval,
        "next_access": start + duration + interval,
    }
    self.history.append(access)

    return access