Skip to content

Commit ace077d

Browse files
committed
Support agent selection via the run() function
1 parent c81d183 commit ace077d

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

benchbot_api/benchbot.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,11 @@ def __init__(self,
7676
supervisor_address='http://' + DEFAULT_ADDRESS + ':' +
7777
str(DEFAULT_PORT) + '/',
7878
auto_start=True):
79-
if agent is not None and not isinstance(agent, Agent):
80-
raise ValueError("BenchBot received an agent of type '%s' "
81-
"which is not an instance of '%s'." %
82-
(agent.__class__.__name__, Agent.__name__))
83-
self.agent = agent
84-
79+
self.agent = None
8580
self.supervisor_address = supervisor_address
8681
self._connection_callbacks = {}
87-
if auto_start:
88-
self.start()
82+
83+
self.set_agent(agent, auto_start)
8984

9085
def _build_address(self, route_name, route_type=RouteType.CONNECTION):
9186
"""Builds an address for communication with a running instance of
@@ -304,11 +299,13 @@ def results_functions(self):
304299
for r in self._query('/', BenchBot.RouteType.RESULTS)
305300
}
306301

307-
def run(self):
302+
def run(self, agent=None):
308303
"""Helper function that runs the robot according to the agent given.
309304
Generally, you should use this function and implement your object in
310305
your own custom agent class.
311306
"""
307+
if agent is not None:
308+
self.set_agent(agent)
312309
if self.agent is None:
313310
raise RuntimeError(
314311
"Can't call Benchbot.run() without an agent attached. Either "
@@ -332,6 +329,21 @@ def scene_fn():
332329
self.agent.save_result(self.result_filename, self.empty_results(),
333330
self.results_functions())
334331

332+
def set_agent(self, agent, auto_start=True):
333+
"""Updates the current agent, and starts its connection with a BenchBot
334+
Supervisor if requested"""
335+
if agent is None:
336+
self.agent = None
337+
return
338+
339+
if agent is not None and not isinstance(agent, Agent):
340+
raise ValueError("BenchBot received an agent of type '%s' "
341+
"which is not an instance of '%s'." %
342+
(agent.__class__.__name__, Agent.__name__))
343+
self.agent = agent
344+
if auto_start:
345+
self.start()
346+
335347
def start(self):
336348
"""Establishes a connect to the Supervisor, and then uses this to
337349
establish a connection with a running robot. It then initialises all

0 commit comments

Comments
 (0)