matter.py 5.33 KB
Newer Older
1
"""The marker module provides the interface to the markers. A marker is any point on
2
 the coordinate system of the simulators sim"""
Ahmad Reza's avatar
Ahmad Reza committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


import uuid
import logging
from datetime import datetime


black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
violett = 9

color_map = {
    black: [0.0, 0.0, 0.0],
    gray: [0.3, 0.3, 0.3],
    red: [0.8, 0.0, 0.0],
    green: [0.0, 0.8, 0.0],
    blue: [0.0, 0.0, 0.8],
    yellow: [0.8, 0.8, 0.0],
    orange: [0.8, 0.3, 0.0],
    cyan: [0.0, 0.8, 0.8],
    violett: [0.8, 0.2, 0.6]
}


33
class Matter():
34
    """In the classe marker all the methods for the characterstic of a marker is included"""
Ahmad Reza's avatar
Ahmad Reza committed
35

Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
36
    def __init__(self, sim, coords, color=black, alpha=1, type=None, mm_size=100):
37
        """Initializing the marker constructor"""
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
38
        self.coords = coords
Ahmad Reza's avatar
Ahmad Reza committed
39
40
41
42
43
44
        self.color = color_map[color]
        self.__id = str(uuid.uuid4())
        self.memory_delay_time=3
        self.memory_delay=True
        self.memory_buffer=[]
        self._tmp_memory=[]
45
        self.sim = sim
Ahmad Reza's avatar
Ahmad Reza committed
46
47
48
49
        self._memory={}
        self.__modified=False
        self.__alpha=alpha
        self.type = type
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
50
        self.mm_limit = sim.config_data.mm_limitation
Ahmad Reza's avatar
Ahmad Reza committed
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
        self.mm_size = mm_size

    def set_alpha(self, alpha):
        """
        Set the alpha value of the particle

        :param alpha: The alpha of the particle
        :return: None
        """
        if (0 <= alpha <= 1):
            self.__alpha = round(alpha,2)
            self.touch()
        elif alpha < 0:
            self.__alpha = 0
            self.touch()
        elif alpha > 1:
            self.__alpha = 1
            self.touch()

    def get_alpha(self):
        """
        Returns the alpha value of the particle

        :return: alpha
        """
        return round(self.__alpha,2)

    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
        # if self.memory_delay == True:
        #     for key in self._tmp_memory:
        #         if key ==
89
        if key in self._memory:
Ahmad Reza's avatar
Ahmad Reza committed
90
            tmp_memory = self._memory[key]
91
            self.sim.csv_round_writer.update_metrics( memory_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
92
93
94
95
96
97
98
99
        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):
        """
100
        Reads all  markers own memory based on a give keywoard
Ahmad Reza's avatar
Ahmad Reza committed
101
102
103
104
105

        :param key: Keywoard
        :return: The founded memory; None: When nothing is written based on the keywoard
        """
        if self._memory != None :
106
107
            self.sim.csv_round_writer.update_metrics(memory_read=1)
            return self._memory
Ahmad Reza's avatar
Ahmad Reza committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        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
        """

        if (self.mm_limit == True and len( self._memory) < self.mm_size) or not self.mm_limit:
            self._memory[key] = data
122
            self.sim.csv_round_writer.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
            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
        """

        if (self.mm_limit == True and len( self._memory) < self.mm_size) or not self.mm_limit:
                self._memory[datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-1]] = data
140
                self.sim.csv_round_writer.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
                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):
        """
156
157
        Gets the marker id
        :return: marker id
Ahmad Reza's avatar
Ahmad Reza committed
158
159
160
161
162
        """
        return self.__id

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

165
        :param color: marker color
Ahmad Reza's avatar
Ahmad Reza committed
166
167
168
169
170
171
        :return: None
        """
        if type (color) == int:
            self.color = color_map[color]
        else:
            self.color = color
172
        self.touch()
Ahmad Reza's avatar
Ahmad Reza committed
173
174


175
176
    def get_color(self):
        """
177
        Sets the marker color
178

179
        :param color: marker color
180
181
182
183
184
        :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
185
186
187
188
189
190
191
192
193

    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