RTC Toolkit  2.0.0
modelBuilderBase.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_MODELBUILDERBASE_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_MODELBUILDERBASE_HPP
14 
20 
21 #include <fstream>
22 #include <iostream>
23 #include <map>
24 #include <tuple>
25 #include <vector>
26 
27 namespace rtctk::componentFramework {
28 
33 public:
34  ModelBuilderBase(StateMachineEngine& engine) : sm("StateMachine"), mm(sm), m_engine(engine) {
35  }
36 
37  virtual ~ModelBuilderBase() = default;
38 
39  void RegisterModel() {
40  LOG4CPLUS_DEBUG(GetLogger(), "StateMachine.DEBUG\n" + Export(sm, "debug").str());
41  LOG4CPLUS_DEBUG(GetLogger(), "StateMachine.SCXML\n" + Export(sm, "scxml").str());
42 
43  // TODO: validate model
44 
45  m_engine.RegisterModel("model", Export(sm, "scxml").str());
46  }
47 
48  void ExportModel(std::string const& file_name) {
49  auto ends_with = [](std::string const& value, std::string const& ending) {
50  if (ending.size() > value.size())
51  return false;
52  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
53  };
54 
55  if (ends_with(file_name, "scxml")) {
56  std::ofstream out(file_name);
57  out << Export(sm, "scxml").str();
58  } else if (ends_with(file_name, "smcat")) {
59  std::ofstream out(file_name);
60  out << Export(sm, "smcat").str();
61  } else if (ends_with(file_name, "debug")) {
62  std::ofstream out(file_name);
63  out << Export(sm, "debug").str();
64  } else {
65  CII_THROW(RtctkException, "Unsupported export format: '" + file_name + "'");
66  }
67  }
68 
69 protected:
70  using LayerOptions = std::map<std::string, bool>;
71  using Layer = std::tuple<std::string, LayerOptions>;
72  using Layers = std::vector<Layer>;
73 
74  void RegisterLayer(Layer layer) {
75  std::ostringstream msg;
76  msg << "Applying Layer '" << std::get<0>(layer) << "' with Options:";
77  for (const auto& [key, value] : std::get<1>(layer)) {
78  msg << " " << key << ":" << value;
79  }
80  LOG4CPLUS_DEBUG(GetLogger(), msg.str());
81 
82  layers.push_back(layer);
83  }
84 
88 
89 private:
90  Layers layers;
91 };
92 
93 } // namespace rtctk::componentFramework
94 
95 #endif
modelExport.hpp
Export the state machine model in various formats.
rtctk::componentFramework::ModelBuilderBase::Layer
std::tuple< std::string, LayerOptions > Layer
Definition: modelBuilderBase.hpp:71
rtctk::componentFramework::StateMachineEngine::RegisterModel
void RegisterModel(const std::string &name, const std::string &model)
Register a new state machine model.
Definition: stateMachineEngine.cpp:52
stateMachineEngine.hpp
Wrapper around the SCXML State Machine Engine.
rtctk::componentFramework::ModelBuilderBase::~ModelBuilderBase
virtual ~ModelBuilderBase()=default
rtctk::componentFramework::StateMachine
Definition: model.hpp:53
rtctk::componentFramework::ModelBuilderBase::sm
StateMachine sm
Definition: modelBuilderBase.hpp:85
rtctk::componentFramework::ModelBuilderBase::RegisterModel
void RegisterModel()
Definition: modelBuilderBase.hpp:39
rtctk::componentFramework::ModelBuilderBase::ExportModel
void ExportModel(std::string const &file_name)
Definition: modelBuilderBase.hpp:48
rtctk::componentFramework::ModelBuilderBase::mm
ModelManipulator mm
Definition: modelBuilderBase.hpp:86
model.hpp
In-memory representation of the state machine model.
rtctk::componentFramework
Definition: commandReplier.cpp:20
modelManipulator.hpp
Manipulate the in-memory state machine model.
rtctk::componentFramework::RtctkException
The RtctkException class is the base class for all Rtctk exceptions.
Definition: exceptions.hpp:207
rtctk::componentFramework::StateMachineEngine
Definition: stateMachineEngine.hpp:31
rtctk::componentFramework::ModelManipulator
Class that provides methods to manipulate the state machine model.
Definition: modelManipulator.hpp:24
rtctk::componentFramework::ModelBuilderBase
Base class of the ModelBuilder.
Definition: modelBuilderBase.hpp:32
rtctk::componentFramework::GetLogger
log4cplus::Logger & GetLogger(const std::string &name="")
Get handle to a specific logger (used with logging macros)
rtctk::componentFramework::Export
std::stringstream Export(StateMachine &sm, std::string const &format)
Export the in-memory state machine model in various formats.
Definition: modelExport.cpp:536
rtctk::componentFramework::ModelBuilderBase::Layers
std::vector< Layer > Layers
Definition: modelBuilderBase.hpp:72
rtctk::componentFramework::ModelBuilderBase::RegisterLayer
void RegisterLayer(Layer layer)
Definition: modelBuilderBase.hpp:74
rtctk::componentFramework::ModelBuilderBase::m_engine
StateMachineEngine & m_engine
Definition: modelBuilderBase.hpp:87
logger.hpp
Logging Support Library based on log4cplus.
rtctk::componentFramework::ModelBuilderBase::ModelBuilderBase
ModelBuilderBase(StateMachineEngine &engine)
Definition: modelBuilderBase.hpp:34
rtctk::componentFramework::ModelBuilderBase::LayerOptions
std::map< std::string, bool > LayerOptions
Definition: modelBuilderBase.hpp:70