SerializableJsonStateMachine.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-17
  6. * Changed: 2022-07-14
  7. *
  8. * */
  9. #include <ls_std/logic/serialization/SerializableJsonStateMachine.hpp>
  10. #include <ls_std/logic/serialization/SerializableJsonState.hpp>
  11. #include <ls_std/core/exception/IllegalArgumentException.hpp>
  12. ls::std::logic::SerializableJsonStateMachine::SerializableJsonStateMachine(const ::std::shared_ptr<ls::std::logic::StateMachine> &_value) : ls::std::core::Class("SerializableJsonStateMachine")
  13. {
  14. this->_assignValue(_value);
  15. }
  16. ls::std::core::type::byte_field ls::std::logic::SerializableJsonStateMachine::marshal()
  17. {
  18. this->_update();
  19. return this->jsonObject.dump();
  20. }
  21. void ls::std::logic::SerializableJsonStateMachine::unmarshal(const ls::std::core::type::byte_field &_data)
  22. {
  23. this->jsonObject = ls::std::core::type::json::parse(_data);
  24. this->_unmarshalStates();
  25. this->_unmarshalCurrentState();
  26. this->value->setMemory(this->jsonObject["memory"]);
  27. this->value->setName(this->jsonObject["name"]);
  28. }
  29. ::std::shared_ptr<ls::std::logic::StateMachine> ls::std::logic::SerializableJsonStateMachine::getValue()
  30. {
  31. return this->value;
  32. }
  33. void ls::std::logic::SerializableJsonStateMachine::setValue(const ::std::shared_ptr<ls::std::logic::StateMachine> &_value)
  34. {
  35. this->_assignValue(_value);
  36. }
  37. void ls::std::logic::SerializableJsonStateMachine::_assignValue(const ::std::shared_ptr<ls::std::logic::StateMachine> &_value)
  38. {
  39. if (_value == nullptr)
  40. {
  41. throw ls::std::core::IllegalArgumentException{};
  42. }
  43. this->value = _value;
  44. }
  45. void ls::std::logic::SerializableJsonStateMachine::_unmarshalCurrentState()
  46. {
  47. if (this->jsonObject.contains("currentState"))
  48. {
  49. this->value->setStartState(this->jsonObject["currentState"]);
  50. }
  51. }
  52. void ls::std::logic::SerializableJsonStateMachine::_unmarshalStates()
  53. {
  54. for (const auto &serializedState : this->jsonObject["states"])
  55. {
  56. ::std::shared_ptr<ls::std::logic::State> state = ::std::make_shared<ls::std::logic::State>("TMP_ID");
  57. ls::std::logic::SerializableJsonState{state}.unmarshal(serializedState.dump());
  58. this->value->addState(state);
  59. }
  60. }
  61. void ls::std::logic::SerializableJsonStateMachine::_update()
  62. {
  63. this->jsonObject = {{"memory", this->value->getMemory()},
  64. {"name", this->value->getName()}};
  65. this->_updateCurrentState();
  66. this->_updateStates();
  67. }
  68. void ls::std::logic::SerializableJsonStateMachine::_updateCurrentState()
  69. {
  70. if (this->value->getCurrentState() != nullptr)
  71. {
  72. this->jsonObject["currentState"] = this->value->getCurrentState()->getId();
  73. }
  74. }
  75. void ls::std::logic::SerializableJsonStateMachine::_updateStates()
  76. {
  77. ::std::string jsonString{};
  78. for (const auto &state : this->value->getStates())
  79. {
  80. jsonString = ls::std::logic::SerializableJsonState{state.second}.marshal();
  81. this->jsonObject["states"][state.first] = ls::std::core::type::json::parse(jsonString);
  82. }
  83. }