-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.py
125 lines (115 loc) · 3.76 KB
/
fsm.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Event1FSM():
def __init__(self):
self.event_label = 1
self.state = 's0'
self.s1_counter = 0
def update_state(self, input):
if self.state == 's0':
if input == 'flush_toilet':
self.state = 's1'
return 0
elif self.state == 's1':
if input == 'wash':
self.state = 's0'
self.s1_counter = 0
return 0
elif input == 'flush_toilet':
return 0
elif input == 'walk':
self.s1_counter += 1
if self.s1_counter > 12:
self.state = 's0'
self.s1_counter = 0
return self.event_label
return 0
else:
self.state = 's0'
self.s1_counter = 0
return self.event_label
raise Exception("Failed to return.")
class Event2FSM():
def __init__(self):
self.event_label = 2
self.state = 's0'
self.s1_counter = 0
self.s2_counter = 0
def update_state(self, input):
if self.state == 's0':
if input == 'wash':
self.state = 's1'
return 0
elif input == 'eat':
self.state = 's2'
return self.event_label
else:
return 0
elif self.state == 's1':
if input == 'wash':
return 0
elif input == 'eat':
self.state = 's2'
return 0
elif input == 'flush_toilet':
self.state = 's0'
self.s1_counter = 0
return 0
else:
# count other activities
self.s1_counter += 1
if self.s1_counter > 12:
self.state = 's0'
self.s1_counter = 0
return 0
else:
return 0
elif self.state == 's2':
if input == 'eat':
self.s2_counter = 0
return 0
else:
self.s2_counter += 1
if self.s2_counter > 36:
self.state = 's0'
self.s2_counter = 0
return 0
raise Exception("Failed to return.")
class Event3FSM():
def __init__(self):
self.event_label = 3
self.state = 's0'
self.s1_counter = 0 # count the total time of brush actions
self.s2_counter = 0 # count the time of other actions after the last brush action happens
def update_state(self, input):
if self.state == 's0':
if input == 'brush_teeth':
self.state = 's1'
self.s1_counter += 1
return 0
elif self.state == 's1':
if input == 'brush_teeth':
self.s1_counter += 1
else:
self.state = 's2'
self.s2_counter += 1
return 0
elif self.state == 's2':
if input == 'brush':
# reinitialize s2_counter
self.state = 's1'
self.s1_counter += 1
self.s2_counter = 0
return 0
else:
self.s2_counter += 1
s1_record = self.s1_counter
if self.s2_counter > 2:
self.state = 's0'
self.s1_counter = 0
self.s2_counter = 0
if s1_record < 24:
return self.event_label
else:
return 0
else:
return 0
raise Exception("Failed to return.")