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
83 hazards_detected: "set[Hazard]"
84
[docs]
85 def __init__(self, hazards: "set[Hazard]", *args: object) -> None:
86 super().__init__(*args)
87 self.hazards_detected = hazards
88
89
[docs]
90class UpdatedPathException(LunaticAgentException):
91 """
92 Should be raised when the path has been updated and the agent should replan.
93
94 Rules that replan on Phase.DONE | END, should throw this exception at the end.
95 """
96
97
[docs]
98class _RuleResultException(LunaticAgentException):
99 """
100 Abstract class for exceptions that can be raised by rules
101 **that still are able to return a result**.
102
103 :meta public:
104 """
105
106 result: Any = RuleResult.NO_RESULT
107
[docs]
108 def __init__(self, result: Any = RuleResult.NO_RESULT, *args: object):
109 super().__init__(*args)
110 self.result = result
111
112
[docs]
113class NoFurtherRulesException(_RuleResultException):
114 """
115 Raised when no further rules should be executed in this phase.
116
117 Caught by :py:meth:`.LunaticAgent.execute_phase`.
118
119 The agent will continue at the phase where the :py:class:`BlockedRule` was triggered.
120 """
121
122
[docs]
123class DoNotEvaluateChildRules(_RuleResultException):
124 """
125 Can be raised in a :py:class:`MultiRule` to prevent the evaluation of child rules.
126
127 Can also be raised by child rules to prevent the evaluation of further child rules.
128 """
129
130
[docs]
131class UnblockRuleException(_RuleResultException):
132 """
133 Can be raised in a :py:class:`BlockedRule` to end it.
134
135 The agent will continue at the phase where the :py:class:`BlockedRule` was triggered.
136
137 Note:
138 Further rules that are in this phase can still be executed.
139 Alternatively, consider raising a :py:class:`NoFurtherRulesException`.
140 """