matter.py 3.52 KB
Newer Older
1

Ahmad Reza's avatar
Ahmad Reza committed
2
3
4
import uuid
from datetime import datetime

5
6
from lib.swarm_sim_header import eprint

Ahmad Reza's avatar
Ahmad Reza committed
7

Ahmad Reza's avatar
Ahmad Reza committed
8
class Matter:
Karol Actun's avatar
Karol Actun committed
9
    def __init__(self, world, coordinates, color, type=None, mm_size=100):
10
        """Initializing the matter constructor"""
11
        self.coordinates = coordinates
Karol Actun's avatar
Karol Actun committed
12
        self.color = color
Ahmad Reza's avatar
Ahmad Reza committed
13
        self.__id = str(uuid.uuid4())
Ahmad Reza's avatar
Ahmad Reza committed
14
        self.world = world
Karol Actun's avatar
Karol Actun committed
15
        self._memory = {}
Ahmad Reza's avatar
Ahmad Reza committed
16
        self.type = type
17
        self.memory_limitation = world.config_data.memory_limitation
Ahmad Reza's avatar
Ahmad Reza committed
18
        self.mm_size = mm_size
19
        self.created = False
Ahmad Reza's avatar
Ahmad Reza committed
20
21
22
23
24
25
26
27
28

    def read_memory_with(self, key):
        """
        Read all its own memory based on a give keywoard

        :param key: Keywoard
        :return: The founded memory; None: When nothing is written based on the keywoard
        """
        tmp_memory = None
29
        if key in self._memory:
Ahmad Reza's avatar
Ahmad Reza committed
30
            tmp_memory = self._memory[key]
Ahmad Reza's avatar
Ahmad Reza committed
31
            self.world.csv_round.update_metrics( memory_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
32
33
34
35
36
37
38
39
        if isinstance(tmp_memory, list) and len(str(tmp_memory)) == 0:
            return None
        if isinstance(tmp_memory, str) and len(str(tmp_memory)) == 0:
            return None
        return tmp_memory

    def read_whole_memory(self):
        """
40
        Reads all  matters own memory based on a give keywoard
Ahmad Reza's avatar
Ahmad Reza committed
41
42
43
44
45

        :param key: Keywoard
        :return: The founded memory; None: When nothing is written based on the keywoard
        """
        if self._memory != None :
Ahmad Reza's avatar
Ahmad Reza committed
46
            self.world.csv_round.update_metrics(memory_read=1)
47
            return self._memory
Ahmad Reza's avatar
Ahmad Reza committed
48
49
50
51
52
53
54
55
56
57
58
59
        else:
            return None

    def write_memory_with(self, key, data):
        """
        Write on its own memory a data with a keywoard

        :param key: A string keyword for orderring the data into the memory
        :param data: The data that should be stored into the memory
        :return: True: Successful written into the memory; False: Unsuccessful
        """

60
        if (self.memory_limitation == True and len( self._memory) < self.mm_size) or not self.memory_limitation:
Ahmad Reza's avatar
Ahmad Reza committed
61
            self._memory[key] = data
Ahmad Reza's avatar
Ahmad Reza committed
62
            self.world.csv_round.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
            return True
        else:
            return False
            #write csv

    def write_memory(self, data):
        """
        Write on its own memory a data with a keywoard

        :param key: A string keyword for orderring the data into the memory
        :param data: The data that should be stored into the memory
        :return: True: Successful written into the memory; False: Unsuccessful
        """

77
        if (self.memory_limitation == True and len( self._memory) < self.mm_size) or not self.memory_limitation:
Ahmad Reza's avatar
Ahmad Reza committed
78
                self._memory[datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-1]] = data
Ahmad Reza's avatar
Ahmad Reza committed
79
                self.world.csv_round.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
80
81
82
83
84
85
86
87
88
89
90
91
92
                return True
        else:
            return False
            #write csv

    def delete_memeory_with(self, key):
        del self._memory[key]

    def delete_whole_memeory(self):
         self._memory.clear()

    def get_id(self):
        """
93
94
        Gets the matter id
        :return: matter id
Ahmad Reza's avatar
Ahmad Reza committed
95
96
97
98
99
        """
        return self.__id

    def set_color(self, color):
        """
100
        Sets the matter color
Ahmad Reza's avatar
Ahmad Reza committed
101

102
        :param color: matter color
Ahmad Reza's avatar
Ahmad Reza committed
103
104
        :return: None
        """
105
106
107
108
        if len(color) != 4:
            eprint("invalid color format!\ncolor has to be in rgba format => (float, float, float, float)")
        else:
            self.color = color
Ahmad Reza's avatar
Ahmad Reza committed
109

110
111
    def get_color(self):
        """
Karol Actun's avatar
Karol Actun committed
112
        :return: color
113
        """
Karol Actun's avatar
Karol Actun committed
114
        return self.color