matter.py 5.02 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
Ahmad Reza's avatar
Ahmad Reza committed
5
from lib.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"""
Ahmad Reza's avatar
Ahmad Reza committed
10
    def __init__(self, world, coords, color=black, alpha=1, type=None, mm_size=100):
11
        """Initializing the marker constructor"""
Ahmad Reza Cheraghi's avatar
Ahmad Reza Cheraghi committed
12
        self.coords = coords
Ahmad Reza's avatar
Ahmad Reza committed
13
14
15
16
17
18
        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=[]
Ahmad Reza's avatar
Ahmad Reza committed
19
        self.world = world
Ahmad Reza's avatar
Ahmad Reza committed
20
21
22
23
        self._memory={}
        self.__modified=False
        self.__alpha=alpha
        self.type = type
24
        self.memory_limitation = world.config_data.memory_limitation
Ahmad Reza's avatar
Ahmad Reza committed
25
        self.mm_size = mm_size
Ahmad Reza's avatar
Ahmad Reza committed
26
        self.modified = False
27
        self.created = False
Ahmad Reza's avatar
Ahmad Reza committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41

    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
42
        self.touch()
Ahmad Reza's avatar
Ahmad Reza committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    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 ==
62
        if key in self._memory:
Ahmad Reza's avatar
Ahmad Reza committed
63
            tmp_memory = self._memory[key]
Ahmad Reza's avatar
Ahmad Reza committed
64
            self.world.csv_round.update_metrics( memory_read=1)
Ahmad Reza's avatar
Ahmad Reza committed
65
66
67
68
69
70
71
72
        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):
        """
73
        Reads all  markers own memory based on a give keywoard
Ahmad Reza's avatar
Ahmad Reza committed
74
75
76
77
78

        :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
79
            self.world.csv_round.update_metrics(memory_read=1)
80
            return self._memory
Ahmad Reza's avatar
Ahmad Reza committed
81
82
83
84
85
86
87
88
89
90
91
92
        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
        """

93
        if (self.memory_limitation == True and len( self._memory) < self.mm_size) or not self.memory_limitation:
Ahmad Reza's avatar
Ahmad Reza committed
94
            self._memory[key] = data
Ahmad Reza's avatar
Ahmad Reza committed
95
            self.world.csv_round.update_metrics(memory_write=1)
Ahmad Reza's avatar
Ahmad Reza committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
            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
        """

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

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

135
        :param color: marker color
Ahmad Reza's avatar
Ahmad Reza committed
136
137
138
139
140
141
        :return: None
        """
        if type (color) == int:
            self.color = color_map[color]
        else:
            self.color = color
142
        self.touch()
Ahmad Reza's avatar
Ahmad Reza committed
143

144
145
    def get_color(self):
        """
146
        Sets the marker color
147

148
        :param color: marker color
149
150
151
152
153
        :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
154
155
156
157
158
159
160
161
162

    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