-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehaviour.py
60 lines (48 loc) · 1.36 KB
/
behaviour.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
import threading
class Behaviour:
def __init__(self, name):
self.lock = threading.Lock()
self.lock.locked()
self.name = name
self._stop = threading.Event()
self._end = threading.Event()
def stop(self):
"Stops the behavior of the agent"
self._stop.set()
if not self.lock.locked():
self.lock.acquire()
@property
def stopped(self):
return self._stop.is_set()
def end(self):
"Ends the behavior of the agent"
self._end.set()
@property
def ended(self):
return self._end.is_set()
def restart(self):
"Allows the behaviour to be explicitly restarted"
if not self._stop.is_set():
return
self._stop.clear()
if self.lock.locked():
self.lock.release()
def on_start(self, *args):
"Function called before the behavior is started"
pass
def run(self, *args):
"Starts the excecution of the agent"
pass
def done(self) -> bool:
"""Returns True if the behaviour has finished
else returns False"""
pass
def run_cyclic(func):
def wrapper(self, *args):
while True:
if self.ended:
return
if self.stopped:
self.lock.acquire()
func(self, *args)
return wrapper