This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffers.py
89 lines (73 loc) · 2.88 KB
/
buffers.py
1
2
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
89
"""This module contains the classes for buffers."""
from machines import BORED
from collections import deque
class Buffer:
def __init__(self, factory, size):
self.factory = factory
assert size > 0
self.size = size
self.storage = []
self.providers = []
self.receivers = []
def enough_room(self, amount=1):
return len(self.storage) + amount <= self.size
def add_product(self, batch):
if len(self.storage) + len(batch) <= self.size:
self.storage.extend(batch)
# Since we have a non empty storage, try to activate all receivers
for machine in self.receivers:
if machine.status == BORED:
self.factory.schedule(0, machine.start_producing)
else:
raise Exception('Buffer overflow.')
def remove_product(self, amount=1):
if len(self.storage) >= amount:
# Since we have a non full storage, try to activate all providers
for machine in self.providers:
if machine.status == BORED:
machine.start_producing()
return [self.storage.pop() for _ in range(amount)]
else:
raise Exception('Buffer underflow.')
class AssemblyLine(Buffer):
halted = False
def __init__(self, factory, size):
super().__init__(factory, size)
self.line = deque()
self.next_batch = []
def add_product(self, batch):
if self.enough_room():
self.put_on_line(batch)
else:
raise Exception('Assembly line overflow.')
def put_on_line(self, batch):
"""Puts product on assembly line."""
total_queue_time = sum([time for time, batch in self.line])
assert total_queue_time <= 5
self.line.appendleft((5 - total_queue_time, batch))
self.halted = False
if len(self.line) == 1:
self.factory.schedule(5, self.put_in_crate)
def put_in_crate(self):
"""Puts product in crate after assembly line."""
if len(self.storage) < self.size:
self.storage.extend(self.next_batch)
# Schedule next product on the line
if len(self.line) > 0:
time, self.next_batch = self.line.pop()
self.factory.schedule(time, self.put_in_crate)
# If we have a full storage, try activate all receivers
if len(self.storage) == self.size:
for machine in self.receivers:
if machine.status == BORED:
self.factory.schedule(0, machine.start_producing)
else:
self.halted = True
def remove_product(self, amount=1):
if self.halted:
self.factory.schedule(0, self.put_in_crate)
return super().remove_product(amount)
if __name__ == '__main__':
import gui
print('buffers main')
gui.main()