Forráskód Böngészése

Extended StateMachine class

- added memory functionality: state machine is now able to remember entered states
- added tests for StateMachine class
pcmattulat 3 éve
szülő
commit
82a6c7a6ec

+ 11 - 1
source/logic/StateMachine.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-05
- * Changed:         2020-09-10
+ * Changed:         2020-09-11
  *
  * */
 
@@ -29,6 +29,10 @@ std::shared_ptr<ls_std::State> ls_std::StateMachine::getCurrentState() {
   return this->currentState;
 }
 
+std::vector<ls_std::StateId> ls_std::StateMachine::getMemory() {
+  return this->memory;
+}
+
 std::string ls_std::StateMachine::getName() {
   return this->name;
 }
@@ -39,6 +43,7 @@ bool ls_std::StateMachine::proceed() {
 
   if(condition) {
     this->currentState = this->states[nextValidStates.at(0)];
+    this->_remember(nextValidStates.at(0));
   }
 
   return condition;
@@ -49,6 +54,7 @@ bool ls_std::StateMachine::setStartState(const ls_std::StateId&_id) {
 
   if(exists) {
     this->currentState = this->states[_id];
+    this->_remember(_id);
   }
 
   return exists;
@@ -66,6 +72,10 @@ std::vector<ls_std::StateId> ls_std::StateMachine::_getNextValidStates() {
   return validStates;
 }
 
+void ls_std::StateMachine::_remember(const ls_std::StateId &_id) {
+  this->memory.push_back(_id);
+}
+
 bool ls_std::StateMachine::_stateExists(const ls_std::StateId &_id) {
   return this->states.find(_id) != this->states.end();
 }

+ 4 - 1
source/logic/StateMachine.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-05
- * Changed:         2020-09-10
+ * Changed:         2020-09-11
  *
  * */
 
@@ -27,6 +27,7 @@ namespace ls_std {
 
       bool addState(const std::shared_ptr<State>& _state);
       std::shared_ptr<State> getCurrentState();
+      std::vector<ls_std::StateId> getMemory();
       std::string getName();
       bool proceed();
       bool setStartState(const StateId& _id);
@@ -34,10 +35,12 @@ namespace ls_std {
     private:
 
       std::shared_ptr<State> currentState {};
+      std::vector<ls_std::StateId> memory {};
       std::string name {};
       std::unordered_map<StateId, std::shared_ptr<State>> states {};
 
       std::vector<StateId> _getNextValidStates();
+      void _remember(const StateId& _id);
       bool _stateExists(const StateId& _id);
   };
 }

+ 37 - 1
test/cases/logic/StateMachineTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-09-09
- * Changed:         2020-09-10
+ * Changed:         2020-09-11
  *
  * */
 
@@ -71,6 +71,42 @@ namespace {
     ASSERT_TRUE(stateMachine.getCurrentState() == nullptr);
   }
 
+  TEST_F(StateMachineTest, getMemory)
+  {
+    ls_std::StateMachine stateMachine = _createStateMachine();
+    ASSERT_STREQ("test_machine", stateMachine.getName().c_str());
+    ASSERT_EQ(0, stateMachine.getMemory().size());
+
+    stateMachine.setStartState("A");
+    ASSERT_EQ(1, stateMachine.getMemory().size());
+    ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
+
+    ASSERT_FALSE(stateMachine.proceed());
+    ASSERT_EQ(1, stateMachine.getMemory().size());
+    ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
+
+    stateMachine.getCurrentState()->getConnectedStates().at("AB")->updatePassCondition(true);
+    ASSERT_TRUE(stateMachine.proceed());
+    ASSERT_EQ(2, stateMachine.getMemory().size());
+    ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
+    ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
+
+    stateMachine.getCurrentState()->getConnectedStates().at("BC")->updatePassCondition(true);
+    ASSERT_TRUE(stateMachine.proceed());
+    ASSERT_EQ(3, stateMachine.getMemory().size());
+    ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
+    ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
+    ASSERT_STREQ("C", stateMachine.getMemory().at(2).c_str());
+
+    stateMachine.getCurrentState()->getConnectedStates().at("CB")->updatePassCondition(true);
+    ASSERT_TRUE(stateMachine.proceed());
+    ASSERT_EQ(4, stateMachine.getMemory().size());
+    ASSERT_STREQ("A", stateMachine.getMemory().at(0).c_str());
+    ASSERT_STREQ("B", stateMachine.getMemory().at(1).c_str());
+    ASSERT_STREQ("C", stateMachine.getMemory().at(2).c_str());
+    ASSERT_STREQ("B", stateMachine.getMemory().at(3).c_str());
+  }
+
   TEST_F(StateMachineTest, getName)
   {
     ls_std::StateMachine stateMachine {"test_machine"};