feat: added is_in_final_state method
This commit is contained in:
@@ -136,6 +136,12 @@ class Machine(Generic[Input]):
|
|||||||
return None # The machine has not started yet
|
return None # The machine has not started yet
|
||||||
return self.__states[self.__current_state_hash]
|
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:
|
def __str__(self) -> str:
|
||||||
r = [f"=MACHINE=", "\tSTATES"]
|
r = [f"=MACHINE=", "\tSTATES"]
|
||||||
for state in self.__states.values():
|
for state in self.__states.values():
|
||||||
|
|||||||
@@ -210,6 +210,22 @@ class TestMachine(unittest.TestCase):
|
|||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
m.reset()
|
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
|
# STATE
|
||||||
|
|
||||||
def test_state_str(self):
|
def test_state_str(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user