feat: added is_in_final_state method

This commit is contained in:
2026-07-16 06:40:55 +03:00
parent ac201c0854
commit 8e8471a7ab
2 changed files with 22 additions and 0 deletions
+6
View File
@@ -136,6 +136,12 @@ class Machine(Generic[Input]):
return None # The machine has not started yet
return self.__states[self.__current_state_hash]
def is_in_final_state(self) -> bool:
if self.__current_state_hash is None:
return False
assert self.__current_state_hash is not None
return self.__current_state_hash in self.__end_states
def __str__(self) -> str:
r = [f"=MACHINE=", "\tSTATES"]
for state in self.__states.values():
+16
View File
@@ -210,6 +210,22 @@ class TestMachine(unittest.TestCase):
with self.assertRaises(ValueError):
m.reset()
# IS_IN_FINAL_STATE
def test_is_in_final_state(self):
m = Machine()
m.add_state("A", True, False)
m.add_state("B", False, True)
m.add_transition("X", "A", "B", lambda context: print("A -> B"))
m.process_input("X")
self.assertTrue(m.is_in_final_state())
m.reset()
self.assertFalse(m.is_in_final_state())
def test_is_in_final_state_not_started(self):
m = Machine()
self.assertFalse(m.is_in_final_state())
# STATE
def test_state_str(self):