matter.py 5.27 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
        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)
        elif alpha < 0:
            self.__alpha = 0
        elif alpha > 1:
            self.__alpha = 1
66
        self.touch()
Ahmad Reza's avatar
Ahmad Reza committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    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 ==
86
        if key in self._memory:
Ahmad Reza's avatar
Ahmad Reza committed
87
            tmp_memory = self._memory[key]
88
            self.sim.csv_round_writer.update_metrics( memory_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
89
90
91
92
93
94
95
96
        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):
        """
97
        Reads all  markers own memory based on a give keywoard
Ahmad Reza's avatar
Ahmad Reza committed
98
99
100
101
102

        :param key: Keywoard
        :return: The founded memory; None: When nothing is written based on the keywoard
        """
        if self._memory != None :
103
104
            self.sim.csv_round_writer.update_metrics(memory_read=1)
            return self._memory
Ahmad Reza's avatar
Ahmad Reza committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        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
119
            self.sim.csv_round_writer.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
            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
137
                self.sim.csv_round_writer.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                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):
        """
153
154
        Gets the marker id
        :return: marker id
Ahmad Reza's avatar
Ahmad Reza committed
155
156
157
158
159
        """
        return self.__id

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

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


172
173
    def get_color(self):
        """
174
        Sets the marker color
175

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

    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