Skip to content

Commit

Permalink
exceptions for set_start_state
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Oct 29, 2024
1 parent 657d3fe commit 91d5afd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
15 changes: 11 additions & 4 deletions yasmin/src/yasmin/state_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void StateMachine::add_state(std::string name, std::shared_ptr<State> state) {
}

void StateMachine::set_start_state(std::string state_name) {

if (state_name.empty()) {
throw std::invalid_argument("Initial state cannot be empty");

} else if (this->states.find(state_name) == this->states.end()) {
throw std::invalid_argument("Initial state '" + state_name +
"' is not in the state machine");
}

this->start_state = state_name;
}

Expand Down Expand Up @@ -117,10 +126,6 @@ void StateMachine::validate() {
// check initial state
if (this->start_state.empty()) {
throw std::runtime_error("No initial state set");

} else if (this->states.find(this->start_state) == this->states.end()) {
throw std::runtime_error("Initial state label: '" + this->start_state +
"' is not in the state machine");
}

std::set<std::string> terminal_outcomes;
Expand Down Expand Up @@ -189,6 +194,8 @@ void StateMachine::validate() {
std::string
StateMachine::execute(std::shared_ptr<blackboard::Blackboard> blackboard) {

this->validate();

this->current_state_mutex->lock();
this->current_state = this->start_state;
this->current_state_mutex->unlock();
Expand Down
12 changes: 12 additions & 0 deletions yasmin/tests/python/test_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def test_state_machine_get_current_state(self):
def test_state_call(self):
self.assertEqual("outcome4", self.sm())

def test_set_start_state_empty(self):
with self.assertRaises(Exception) as context:
self.sm.set_start_state("")
self.assertEqual(str(context.exception), "Initial state cannot be empty")

def test_set_start_state_wrong_state(self):
with self.assertRaises(Exception) as context:
self.sm.set_start_state("FOO1")
self.assertEqual(
str(context.exception), "Initial state 'FOO1' is not in the state machine"
)

def test_add_repeated_state(self):
with self.assertRaises(Exception) as context:
self.sm.add_state(
Expand Down
13 changes: 8 additions & 5 deletions yasmin/yasmin/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def add_state(
self._start_state = name

def set_start_state(self, name: str) -> None:

if not name:
raise Exception("Initial state cannot be empty")

elif name not in self._states:
raise Exception(f"Initial state '{name}' is not in the state machine")

self._start_state = name

def get_start_state(self) -> str:
Expand All @@ -77,12 +84,8 @@ def cancel_state(self) -> None:
def validate(self) -> None:

# check initial state
if self._start_state is None:
if not self._start_state:
raise Exception("No initial state set")
elif self._start_state not in self._states:
raise Exception(
f"Initial state label: '{self._start_state}' is not in the state machine"
)

terminal_outcomes = []

Expand Down

0 comments on commit 91d5afd

Please sign in to comment.