SerializableJSONStateMachine.cpp 2.5 KB

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