-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaton.py
48 lines (43 loc) · 1.6 KB
/
automaton.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
class Automaton:
START_STATE = 'q0'
END_STATE = 'q3'
instances = {}
def create_instance(self, instance_id):
self.instances[instance_id] = self.START_STATE
# q0 = before login
# q1 = logged in (maybe selected and bought item)
# q2 = selected item
# q3 = logged out
# A(B|BC)*D
def make_action(self, instance_id, action):
if not self.instance_is_running(instance_id) and action == 'A':
self.create_instance(instance_id)
instance_state = self.instances[instance_id]
if instance_state == 'q0':
if action == 'A':
self.instances[instance_id] = 'q1'
else:
raise Exception('Invalid action')
elif instance_state == 'q1':
if action == 'B':
self.instances[instance_id] = 'q2'
elif action == 'D':
del self.instances[instance_id]
else:
raise Exception('Invalid action')
elif instance_state == 'q2':
if action == 'C':
self.instances[instance_id] = 'q1'
elif action == 'D':
del self.instances[instance_id]
elif action != 'B':
raise Exception('Invalid action')
elif instance_state == 'q3':
del self.instances[instance_id]
raise Exception('Invalid action for logged out')
else:
raise Exception('Invalid state')
def instance_is_running(self, instance_id):
return instance_id in self.instances
def number_of_instances(self):
return len(self.instances)