-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
47 lines (44 loc) · 1.95 KB
/
controller.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
class Controller:
def __init__(self):
self.trigger = None
self.conditions = []
self.and_or = 0 # 0 is and, 1 is or
self.if_action = []
self.else_action = []
self.triggered = False # when it's triggered we will detect the changes
def initialize(self, data):
self.trigger = data[0]
self.conditions = data[1]
self.and_or = data[2] # 0 is and, 1 is or
self.if_action = data[3]
self.else_action = data[4]
def check_udpate(self, trigger, all_states):
# TODO (Zhuoyue) this function is similar (well...basically the logic is the same) to the
# process_programm function in simulate.py we might want to create a generic function for both of them
if trigger == self.trigger:
self.triggered = True
results = []
is_satisfied = True
if self.conditions:
for condition in self.conditions:
local_state = int(condition.split()[0])
local_idx = int(condition.split()[1])
results.append(all_states[local_idx] == local_state)
if self.and_or == 0: # and
is_satisfied = all(results)
else: # or
is_satisfied = any(results)
if is_satisfied:
for local_action in self.if_action:
local_state = int(local_action.split()[0])
local_idx = int(local_action.split()[1])
all_states[local_idx] = local_state
else:
for local_action in self.else_action:
local_state = int(local_action.split()[0])
local_idx = int(local_action.split()[1])
all_states[local_idx] = local_state
# updated_script.append(action)
return all_states
else: # if the current action is not a trigger
return None