Source code for agents.rules.stopped_long_trigger
1"""Example of a :py:class:`MultiRule` if the agent does not perform any actions for a certain time."""
2
3from typing import List
4
5from agents.tools.logs import logger
6from classes.constants import AgentState, Phase
7from classes.rule import ConditionFunction, Context, MultiRule, Rule
8
9
[docs]
10class StoppedTooLongTrigger(MultiRule):
11 """Triggers child rules if the agent has stopped for a too long time"""
12 phase = Phase.UPDATE_INFORMATION | Phase.END
13
14 _warning_given = False
15
16 stop_time_threshold = 60
17 """Time in seconds the agent is allowed to stop before triggering the rule."""
18
[docs]
19 @ConditionFunction
20 def condition(self, ctx: Context) -> bool:
21 # time stopped in seconds # NOTE: Only in sync mode!
22 s_stopped = ctx.agent.current_states[AgentState.STOPPED] * ctx.config.planner.dt
23 if s_stopped < self.stop_time_threshold:
24 self._warning_given = False
25 return False
26 return True
27
[docs]
28 def action(self, ctx: Context) -> None:
29 if not self._warning_given:
30 self._warning_given = True
31 logger.warning("Agent has stopped for too long.")
32
33 # ... Child rules to be executed
34 rules: List[Rule] = []