Commit b3028f02 authored by Ahmad Reza's avatar Ahmad Reza
Browse files

some code cleaning based on the results of sonarqube

parent 26633e17
......@@ -43,8 +43,8 @@ tile_mm_size = 2
##Examples##
##Moving
scenario = lonely_particle.py
solution = random_walk.py
scenario = lonely_particle
solution = random_walk
#solution = round_walk.py
## Creating and Deleting
......
......@@ -12,7 +12,6 @@ def generate_gnuplot(directory):
plot.stdin.write('set xlabel "Rounds" \n')
for bla in data.columns:
if i >= 5:
# bla="Location"
plot.stdin.write('set ylabel "%s" \n' % (bla))
plot.stdin.write("set output '" + directory + "/rounds_%s.png' \n" % (bla))
plot.stdin.write("set term png giant size 800,600 font 'Helvetica,20' \n")
......@@ -32,7 +31,6 @@ def generate_gnuplot(directory):
plot.stdin.write('set xlabel "Particle" \n')
for bla in data.columns:
if i >= 3 :
# bla="Location"
plot.stdin.write('set ylabel "%s" \n' % (bla))
plot.stdin.write("set term png giant size 800,600 font 'Helvetica,20' \n")
plot.stdin.write("set output '" + directory + "/particle_%s.png' \n" % (bla))
......
......@@ -119,13 +119,6 @@ class Matter():
if (self.mm_limit == True and len( self._memory) < self.mm_size) or not self.mm_limit:
self._memory[key] = data
self.sim.csv_round_writer.update_metrics(memory_write=1)
# if self.memory_delay == True:
# self._tmp_memory[key] = data
# print("Wrote data at ", self.sim.sim.get_actual_round())
# self.memory_buffer[self.sim.sim.get_actual_round()+self.memory_delay_time] = self._tmp_memory.copy()
# self._tmp_memory.clear()
# else:
# self._memory[key] = data
return True
else:
return False
......
......@@ -118,10 +118,9 @@ class Particle(matter.Matter):
:return: True: Success Moving; False: Non moving
"""
dir_coord = self.sim.get_coords_in_dir(self.coords, dir)
#sim = self.sim_to_coords(dir_coord[0], dir_coord[1])
#print ("sim actual coord "+ str(sim))
if self.sim.border==1:
if abs(dir_coord[0]) > self.sim.get_sim_x_size() or abs(dir_coord[1]) > self.sim.get_sim_y_size() :
if self.sim.border==1 and (abs(dir_coord[0]) > self.sim.get_sim_x_size() \
or abs(dir_coord[1]) > self.sim.get_sim_y_size()) :
dir = dir - 3 if dir > 2 else dir + 3
dir_coord = self.sim.get_coords_in_dir(self.coords, dir)
if self.sim.check_coords(dir_coord[0], dir_coord[1]):
......@@ -244,15 +243,12 @@ class Particle(matter.Matter):
def get_matter_in_dir(self, matter="tile", dir=E):
if matter=="tile":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="tile" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
def get_tile_in(self, dir=E):
......@@ -312,28 +308,12 @@ class Particle(matter.Matter):
def get_matter_in_dir(self, matter="tile", dir=E):
if matter=="tile":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location":
if self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="tile" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_tile_map_coords():
return self.sim.get_tile_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="particle" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_particle_map_coords():
return self.sim.get_particle_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
if matter=="location" and self.sim.get_coords_in_dir(self.coords, dir) in self.sim.get_location_map_coords():
return self.sim.get_location_map_coords()[self.sim.get_coords_in_dir(self.coords, dir)]
def scan_for_matter_within(self, matter='all', hop=1):
"""
......@@ -377,33 +357,22 @@ class Particle(matter.Matter):
y_scan_coord_pos = self.coords[1] + cnt
if cnt == 0:
if matter == "particles":
# if self.coords in self.sim.particle_map_coords:
# hop_list.append(self.sim.particle_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.particle_map_coords:
hop_list.append(self.sim.particle_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.particle_map_coords:
hop_list.append(self.sim.particle_map_coords[(x_scan_neg, y_scan_coord_neg)])
elif matter == "locations":
# if self.coords in self.sim.location_map_coords:
# hop_list.append(self.sim.location_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.location_map_coords:
hop_list.append(self.sim.location_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.location_map_coords:
hop_list.append(self.sim.location_map_coords[(x_scan_neg, y_scan_coord_neg)])
elif matter == "tiles":
# if self.coords in self.sim.tile_map_coords:
# hop_list.append(self.sim.tile_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.tile_map_coords:
hop_list.append(self.sim.tile_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.tile_map_coords:
hop_list.append(self.sim.tile_map_coords[(x_scan_neg, y_scan_coord_neg)])
elif matter == "all":
# if self.coords in self.sim.tile_map_coords:
# hop_list.append(self.sim.tile_map_coords[self.coords])
# if self.coords in self.sim.location_map_coords:
# hop_list.append(self.sim.location_map_coords[self.coords])
# if self.coords in self.sim.particle_map_coords:
# hop_list.append(self.sim.particle_map_coords[self.coords])
if (x_scan_pos, y_scan_coord_neg) in self.sim.tile_map_coords:
hop_list.append(self.sim.tile_map_coords[(x_scan_pos, y_scan_coord_neg)])
if (x_scan_neg, y_scan_coord_neg) in self.sim.tile_map_coords:
......@@ -805,134 +774,112 @@ class Particle(matter.Matter):
self.__isCarried = False
self.touch()
def create_tile(self, color=gray, alpha=1):
"""
Creates a tile on the particles actual position
:return: None
"""
logging.info("Going to create a tile on position %s", str(self.coords))
self.sim.add_tile(self.coords[0], self.coords[1], color, alpha)
self.sim.tile_map_coords[self.coords[0], self.coords[1]].created = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
def create_tile_in(self, dir=None, color=gray, alpha=1):
"""
Creates a tile either in a given direction
:param dir: The direction on which the tile should be created. Options: E, SE, SW, W, NW, NE,
:return: None
"""
logging.info("particle with id %s is", self.get_id())
logging.info("Going to create a tile in %s ", str(dir))
if dir != None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
def create_tile(self, color=gray, alpha=1):
"""
Creates a tile on the particles actual position
:return: None
"""
logging.info("Going to create a tile on position %s", str(self.coords))
self.sim.add_tile(self.coords[0], self.coords[1], color, alpha)
self.sim.tile_map_coords[self.coords[0], self.coords[1]].created = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
def create_tile_in(self, dir=None, color=gray, alpha=1):
"""
Creates a tile either in a given direction
:param dir: The direction on which the tile should be created. Options: E, SE, SW, W, NW, NE,
:return: None
"""
logging.info("particle with id %s is", self.get_id())
logging.info("Going to create a tile in %s ", str(dir))
if dir != None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
if self.sim.add_tile(coords[0], coords[1], color, alpha) == True:
self.sim.tile_map_coords[coords[0], coords[1]].created = True
logging.info("Tile is created")
self.sim.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
else:
logging.info("Not created tile ")
def create_tile_on(self, x=None, y=None, color=gray, alpha=1):
"""
Creates a tile either on a given x,y coordinates
:param x: x coordinate
:param y: y coordinate
:return: None
"""
logging.info("particle with id %s is", self.get_id())
if x is not None and y is not None:
coords = (x, y)
if self.sim.check_coords(x, y):
logging.info("Going to create a tile on position \(%i , %i\)", x, y)
if self.sim.add_tile(coords[0], coords[1], color, alpha) == True:
self.sim.tile_map_coords[coords[0], coords[1]].created = True
logging.info("Tile is created")
self.sim.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
else:
logging.info("Not created tile ")
def create_tile_on(self, x=None, y=None, color=gray, alpha=1):
"""
Creates a tile either on a given x,y coordinates
:param x: x coordinate
:param y: y coordinate
:return: None
"""
logging.info("particle with id %s is", self.get_id())
if x is not None and y is not None:
coords = (x, y)
if self.sim.check_coords(x, y):
logging.info("Going to create a tile on position \(%i , %i\)", x, y)
if self.sim.add_tile(coords[0], coords[1], color, alpha) == True:
self.sim.tile_map_coords[coords[0], coords[1]].created = True
self.sim.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
self.sim.csv_round_writer.update_tiles_num(len(self.sim.get_tiles_list()))
self.sim.csv_round_writer.update_metrics(tile_created=1)
return True
else:
logging.info("Not created tile on coords \(%i , %i\)", y, x)
return False
else:
logging.info("Not created tile on coords \(%i , %i\)", y, x)
def delete_tile(self):
"""
Deletes a tile on current position
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile on current position")
if self.coords in self.sim.get_tile_map_coords():
if self.sim.remove_tile_on(self.coords):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Not created tile on coords \(%i , %i\)", y, x)
return False
else:
logging.info("Could not delet tile")
return False
logging.info("Not created tile on coords \(%i , %i\)", y, x)
def delete_tile_with(self, id):
"""
Deletes a tile with a given tile-id
def delete_tile(self):
"""
Deletes a tile on current position
:param tile_id: The id of the tile that should be deleted
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile with tile id %s", str(id))
if self.sim.remove_tile(id):
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile on current position")
if self.coords in self.sim.get_tile_map_coords():
if self.sim.remove_tile_on(self.coords):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile with tile id %s", str(id))
return False
else:
logging.info("Could not delet tile")
return False
def delete_tile_in(self, dir=E):
"""
Deletes a tile either in a given direction
:param dir: The direction on which the tile should be deleted. Options: E, SE, SW, W, NW, NE,
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if -1 < dir < 7:
coords = self.sim.get_coords_in_dir(self.coords, dir)
logging.info("Deleting tile in %s direction", str(dir))
if coords is not None:
if self.sim.remove_tile_on(coords):
logging.info("Deleted tile with tile on coords %s", str(coords))
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
def delete_tile_with(self, id):
"""
Deletes a tile with a given tile-id
def delete_tile_on(self, x=None, y=None):
"""
Deletes a tile either on a given x,y coordinates
,
:param x: x coordinate
:param y: y coordinate
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if x is not None and y is not None:
coords = (x, y)
:param tile_id: The id of the tile that should be deleted
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.get_id())
logging.info("is going to delete a tile with tile id %s", str(id))
if self.sim.remove_tile(id):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile with tile id %s", str(id))
return False
def delete_tile_in(self, dir=E):
"""
Deletes a tile either in a given direction
:param dir: The direction on which the tile should be deleted. Options: E, SE, SW, W, NW, NE,
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if -1 < dir < 7:
coords = self.sim.get_coords_in_dir(self.coords, dir)
logging.info("Deleting tile in %s direction", str(dir))
if coords is not None:
if self.sim.remove_tile_on(coords):
logging.info("Deleted tile with tile on coords %s", str(coords))
self.csv_particle_writer.write_particle(tile_deleted=1)
......@@ -940,77 +887,128 @@ class Particle(matter.Matter):
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
def delete_tile_on(self, x=None, y=None):
"""
Deletes a tile either on a given x,y coordinates
,
:param x: x coordinate
:param y: y coordinate
:return: True: Deleting successful; False: Deleting unsuccessful
"""
coords = ()
if x is not None and y is not None:
coords = (x, y)
if self.sim.remove_tile_on(coords):
logging.info("Deleted tile with tile on coords %s", str(coords))
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
else:
logging.info("Could not delet tile on coords %s", str(coords))
return False
def take_tile(self):
"""
Takes a tile on the actual position
def take_tile(self):
"""
Takes a tile on the actual position
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[self.coords]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile has been taken")
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile could not be taken")
return False
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[self.coords]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile has been taken")
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("No tile on the actual position not in the sim")
logging.info("Tile could not be taken")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
logging.info("No tile on the actual position not in the sim")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
return False
def take_tile_with(self, id):
"""
Takes a tile with a given tile id
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if id in self.sim.tile_map_id:
logging.info("Tile with tile id %s is in the sim", str(id))
self.carried_tile = self.sim.tile_map_id[id]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(id))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile with tile id %s could not be taken", str(id))
return False
def take_tile_with(self, id):
"""
Takes a tile with a given tile id
:param id: The id of the tile that should be taken
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if id in self.sim.tile_map_id:
logging.info("Tile with tile id %s is in the sim", str(id))
self.carried_tile = self.sim.tile_map_id[id]
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(id))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile with tile id %s is not in the sim", str(id))
logging.info("Tile with tile id %s could not be taken", str(id))
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle", str(id))
logging.info("Tile with tile id %s is not in the sim", str(id))
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle", str(id))
return False
def take_tile_in(self, dir):
"""
Takes a tile that is in a given direction
def take_tile_in(self, dir):
"""
Takes a tile that is in a given direction
:param dir: The direction on which the tile should be taken. Options: E, SE, SW, W, NW, NE,
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
:param dir: The direction on which the tile should be taken. Options: E, SE, SW, W, NW, NE,
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
coords = self.sim.get_coords_in_dir(self.coords, dir)
if coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[coords]
logging.info("Tile with tile id %s is in the sim", str(self.carried_tile.get_id()))
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
return True
else:
logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
return False
else:
logging.info("Tile is not in the sim")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
return False
def take_tile_on(self, x=None, y=None):
"""
Takes a tile that is in a given direction
:param x: x coordinate
:param y: y coordinate
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.sim.check_coords(x, y):
coords = (x, y)
if coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[coords]
logging.info("Tile with tile id %s is in the sim", str(self.carried_tile.get_id()))
if self.carried_tile.take(coords=self.coords):
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
return True
else:
logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
......@@ -1019,73 +1017,72 @@ class Particle(matter.Matter):
logging.info("Tile is not in the sim")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
logging.info("Coordinates are wrong")
return False
else:
logging.info("Tile cannot taken because particle is carrieng either a tile or a particle")
return False
def take_tile_on(self, x=None, y=None):
"""
Takes a tile that is in a given direction
def drop_tile(self):
"""
Drops the taken tile on the particles actual position
:param x: x coordinate
:param y: y coordinate
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.sim.check_coords(x, y):
coords = (x, y)
if coords in self.sim.tile_map_coords:
self.carried_tile = self.sim.tile_map_coords[coords]
logging.info("Tile with tile id %s is in the sim", str(self.carried_tile.get_id()))
if self.carried_tile.take(coords=self.coords):
self.sim.csv_round_writer.update_metrics(tiles_taken=1)
self.csv_particle_writer.write_particle(tiles_taken=1)
logging.info("Tile with tile id %s has been taken", str(self.carried_tile.get_id()))
return True
else:
logging.info("Tile with tile id %s could not be taken", str(self.carried_tile.get_id()))
return False
else:
logging.info("Tile is not in the sim")
return False
else:
logging.info("Coordinates are wrong")