1"""Helper module that contains all the custom exceptions used in the project"""
2
3from typing import Any
4
5import carla
6
7from classes.constants import Hazard, RuleResult
8
9__all__ = [
10 "AgentDoneException",
11 "ContinueLoopException",
12 "DoNotEvaluateChildRules",
13 "EmergencyStopException",
14 "LunaticAgentException",
15 "NoFurtherRulesException",
16 "SkipInnerLoopException",
17 "UnblockRuleException",
18 "UpdatedPathException",
19 "UserInterruption",
20 "_RuleResultException",
21]
22
23
[docs]
24class UserInterruption(Exception):
25 """
26 Terminate the loop if user input is detected.
27 Allows the scenario runner and Leaderboard_ to exit gracefully, if
28 handled appropriately, e.g. by directly returning.
29
30 Thrown by
31 :py:meth:`LunaticAgent.parse_keyboard_input <agents.lunatic_agent.LunaticAgent.parse_keyboard_input>`.
32
33 Note:
34 Is not a :py:class:`LunaticAgentException`.
35 """
36
37
[docs]
38class LunaticAgentException(Exception):
39 """
40 Base class for all custom exceptions that influence the Workflow of the :py:class:`.LunaticAgent`.
41 """
42
43
[docs]
44class AgentDoneException(LunaticAgentException):
45 """
46 Raised when there is no more waypoint in the queue to follow and no rule set a new destination.
47
48 When the a :py:class:`.GameFramework` instance is used as context manager will set
49 :py:attr:`game_framework.continue_loop <classes.worldmodel.GameFramework.continue_loop>`
50 to :python:`False`.
51 """
52
53
[docs]
54class ContinueLoopException(LunaticAgentException):
55 """
56 Raise when :py:meth:`.LunaticAgent.run_step` action of the agent should not be continued further.
57
58 The agent returns the current :python:`ctx.control` to the caller of :code:`run_step`.
59
60 Note:
61 Handled in :py:meth:`.LunaticAgent.run_step`, this exception should not propagate outside.
62 It can be caught by :py:class:`.GameFramework` and skip the current loop and not apply any controls,
63 an error will be logged.
64 """
65
66
[docs]
67class SkipInnerLoopException(LunaticAgentException):
68 """
69 Can be raised in `LunaticAgent._inner_step`. A new control object must be provided.
70 """
71
72 planned_control: carla.VehicleControl
73
[docs]
74 def __init__(self, planned_control: carla.VehicleControl, *args: object) -> None:
75 if not isinstance(planned_control, carla.VehicleControl):
76 raise TypeError("Must provide a carla.VehicleControl instance to raise a SkipInnerLoopException")
77 super().__init__(*args)
78 self.planned_control = planned_control
79
80
[docs]
81class EmergencyStopException(LunaticAgentException):
82 hazards_detected: "set[Hazard]"
83
[docs]
84 def __init__(self, hazards: "set[Hazard]", *args: object) -> None:
85 super().__init__(*args)
86 self.hazards_detected = hazards
87
88
[docs]
89class UpdatedPathException(LunaticAgentException):
90 """
91 Should be raised when the path has been updated and the agent should replan.
92
93 Rules that replan on Phase.DONE | END, should throw this exception at the end.
94 """
95
96
[docs]
97class _RuleResultException(LunaticAgentException):
98 """
99 Abstract class for exceptions that can be raised by rules
100 **that still are able to return a result**.
101
102 :meta public:
103 """
104
105 result: Any = RuleResult.NO_RESULT
106
[docs]
107 def __init__(self, result: Any = RuleResult.NO_RESULT, *args: object):
108 super().__init__(*args)
109 self.result = result
110
111
[docs]
112class NoFurtherRulesException(_RuleResultException):
113 """
114 Raised when no further rules should be executed in this phase.
115
116 Caught by :py:meth:`.LunaticAgent.execute_phase`.
117
118 The agent will continue at the phase where the :py:class:`BlockedRule` was triggered.
119 """
120
121
[docs]
122class DoNotEvaluateChildRules(_RuleResultException):
123 """
124 Can be raised in a :py:class:`MultiRule` to prevent the evaluation of child rules.
125
126 Can also be raised by child rules to prevent the evaluation of further child rules.
127 """
128
129
[docs]
130class UnblockRuleException(_RuleResultException):
131 """
132 Can be raised in a :py:class:`BlockedRule` to end it.
133
134 The agent will continue at the phase where the :py:class:`BlockedRule` was triggered.
135
136 Note:
137 Further rules that are in this phase can still be executed.
138 Alternatively, consider raising a :py:class:`NoFurtherRulesException`.
139 """