diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-01 11:49:53 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-01 11:49:53 +0200 |
| commit | 1f18e740539aed751865ecff9d0f3cba44230e54 (patch) | |
| tree | e23254751cc5a3be551233efb979a00571f40dc6 /src/philo/table.py | |
| parent | 763f02a8b1e69c0e26a088824981d23ba1e5386d (diff) | |
| download | philosophers_test-1f18e740539aed751865ecff9d0f3cba44230e54.tar.gz philosophers_test-1f18e740539aed751865ecff9d0f3cba44230e54.tar.bz2 philosophers_test-1f18e740539aed751865ecff9d0f3cba44230e54.zip | |
Refactoring file structure, Added summary
Diffstat (limited to 'src/philo/table.py')
| -rw-r--r-- | src/philo/table.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/philo/table.py b/src/philo/table.py new file mode 100644 index 0000000..ea93391 --- /dev/null +++ b/src/philo/table.py @@ -0,0 +1,67 @@ +# ############################################################################ # +# # +# ::: :::::::: # +# table.py :+: :+: :+: # +# +:+ +:+ +:+ # +# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2020/10/01 10:53:29 by cacharle #+# #+# # +# Updated: 2020/10/01 11:31:44 by cacharle ### ########.fr # +# # +# ############################################################################ # + + +import itertools + +from philo import Philo +from philo import error +from philo.event import Event + +class Table: + def __init__( + self, + philo_num: int, + timeout_die: int, + timeout_eat: int, + timeout_sleep: int, + meal_num: int + ): + self._philos = [Philo(id_, timeout_die, timeout_eat, timeout_sleep, meal_num) + for id_ in range(1, philo_num + 1)] + self._logs = [] + self._philo_num = philo_num + self.dead = False + + def add_log(self, log): + """ Add a log to the correct philosopher + Set the dead flag if it's a death log + """ + self._logs.append(log) + philo = next(p for p in self._philos if p.id == log.id) + philo.logs.append(log) + # move + if self.dead: + raise error.Log(self._logs, "should not output after death") + if log.event is Event.DIE: + self.dead = True + + def check(self): + """ Check global logs and all philosophers logs for errors + + - Should not output after one philosopher died + - Should not take non existant forks + - Timestamps should be in increasing order + """ + + if self.dead: + return + + for p in self._philos: + p.check() + + fork_used = sum([p.used_forks for p in self._philos]) + if fork_used > self._philo_num: + raise error.Log(self._logs, "using nonexistant forks") + for l1, l2 in zip(self._logs, self._logs[1:]): + if l1.timestamp > l2.timestamp: + raise error.Log(self._logs, "timestamps not in ordered") |
