matter.py 4.92 KB
Newer Older
1
"""The marker module provides the interface to the markers. A marker is any point on
Ahmad Reza's avatar
Ahmad Reza committed
2
 the coordinate system of the simulators world"""
Ahmad Reza's avatar
Ahmad Reza committed
3
4
import uuid
from datetime import datetime
5
from lib.swarm_sim_header import *
Ahmad Reza's avatar
Ahmad Reza committed
6
7


Ahmad Reza's avatar
Ahmad Reza committed
8
class Matter:
9
    """In the classe marker all the methods for the characterstic of a marker is included"""
10
    def __init__(self, world, coordinates, color=black, transparency=1, type=None, mm_size=100):
11
        """Initializing the marker constructor"""
12
        self.coordinates = coordinates
Ahmad Reza's avatar
Ahmad Reza committed
13
14
        self.color = color_map[color]
        self.__id = str(uuid.uuid4())
Ahmad Reza's avatar
Ahmad Reza committed
15
        self.world = world
Ahmad Reza's avatar
Ahmad Reza committed
16
        self._memory={}
17
        self.__transparency=transparency
Ahmad Reza's avatar
Ahmad Reza committed
18
        self.type = type
19
        self.memory_limitation = world.config_data.memory_limitation
Ahmad Reza's avatar
Ahmad Reza committed
20
        self.mm_size = mm_size
Ahmad Reza's avatar
Ahmad Reza committed
21
        self.modified = False
22
        self.created = False
Ahmad Reza's avatar
Ahmad Reza committed
23

24
    def set_transparency(self, transparency):
Ahmad Reza's avatar
Ahmad Reza committed
25
        """
26
        Set the transparency value of the particle
Ahmad Reza's avatar
Ahmad Reza committed
27

28
        :param transparency: The transparency of the particle
Ahmad Reza's avatar
Ahmad Reza committed
29
30
        :return: None
        """
31
32
33
34
35
36
        if (0 <= transparency <= 1):
            self.__transparency = round(transparency,2)
        elif transparency < 0:
            self.__transparency = 0
        elif transparency > 1:
            self.__transparency = 1
37
        self.touch()
38

39
    def get_transparency(self):
Ahmad Reza's avatar
Ahmad Reza committed
40
        """
41
        Returns the transparency value of the particle
Ahmad Reza's avatar
Ahmad Reza committed
42

43
        :return: transparency
Ahmad Reza's avatar
Ahmad Reza committed
44
        """
45
        return round(self.__transparency,2)
Ahmad Reza's avatar
Ahmad Reza committed
46
47
48
49
50
51
52
53
54

    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
55
        if key in self._memory:
Ahmad Reza's avatar
Ahmad Reza committed
56
            tmp_memory = self._memory[key]
Ahmad Reza's avatar
Ahmad Reza committed
57
            self.world.csv_round.update_metrics( memory_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
58
59
60
61
62
63
64
65
        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):
        """
66
        Reads all  markers own memory based on a give keywoard
Ahmad Reza's avatar
Ahmad Reza committed
67
68
69
70
71

        :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
72
            self.world.csv_round.update_metrics(memory_read=1)
73
            return self._memory
Ahmad Reza's avatar
Ahmad Reza committed
74
75
76
77
78
79
80
81
82
83
84
85
        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
        """

86
        if (self.memory_limitation == True and len( self._memory) < self.mm_size) or not self.memory_limitation:
Ahmad Reza's avatar
Ahmad Reza committed
87
            self._memory[key] = data
Ahmad Reza's avatar
Ahmad Reza committed
88
            self.world.csv_round.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
            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
        """

103
        if (self.memory_limitation == True and len( self._memory) < self.mm_size) or not self.memory_limitation:
Ahmad Reza's avatar
Ahmad Reza committed
104
                self._memory[datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-1]] = data
Ahmad Reza's avatar
Ahmad Reza committed
105
                self.world.csv_round.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
106
107
108
109
110
111
112
113
114
115
116
117
118
                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):
        """
119
120
        Gets the marker id
        :return: marker id
Ahmad Reza's avatar
Ahmad Reza committed
121
122
123
124
125
        """
        return self.__id

    def set_color(self, color):
        """
126
        Sets the marker color
Ahmad Reza's avatar
Ahmad Reza committed
127

128
        :param color: marker color
Ahmad Reza's avatar
Ahmad Reza committed
129
130
131
132
133
134
        :return: None
        """
        if type (color) == int:
            self.color = color_map[color]
        else:
            self.color = color
135
        self.touch()
Ahmad Reza's avatar
Ahmad Reza committed
136

137
138
    def get_color(self):
        """
139
        Sets the marker color
140

141
        :param color: marker color
142
143
144
145
146
        :return: None
        """
        for color, code in color_map.items():    # for name, age in dictionary.iteritems():  (for Python 2.x)
         if code == self.color:
           return(color)
Ahmad Reza's avatar
Ahmad Reza committed
147
148
149
150
151
152
153
154
155

    def touch(self):
        """Tells the visualization that something has been modified and that it shoud changed it"""
        self.modified = True

    def untouch(self):
        """Tells the visualization that something has been modified and that it shoud changed it"""
        self.modified = False