MemoryBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 
5 
6 // BaseClass
7 #include "MemoryItem.h"
8 
9 // ChildType
10 
11 // ArmarX
14 #include <ArmarXCore/core/time.h>
15 
20 #include <RobotAPI/libraries/armem/server/test/ForgettingExperiments.h>
22 
24 {
25  /// @brief Interface functions for the longterm memory classes
26  template <class _CoreSegmentT>
27  class MemoryBase : public MemoryItem
28  {
29  public:
30  struct Statistics
31  {
34  bool firstStart = true;
35  bool firstStop = true;
40  };
41 
42  public:
43  using CoreSegmentT = _CoreSegmentT;
44 
45  MemoryBase(const std::string& exportName, const MemoryID& id) :
46  MemoryItem(exportName, id), enabled(false)
47  {
48  }
49 
50  /// initialize config
51  void
53  {
54  bool en = p.enabled_on_startup;
58 
59  try
60  {
61  const auto j = nlohmann::json::parse(p.configuration_on_startup);
62 
63  // Processors are shared. So we only need to configure the root
64  processors->configure(j);
65 
66  this->_configure(j);
67  }
68  catch (...)
69  {
70  ARMARX_WARNING << "Failed to parse `" << p.configuration_on_startup << "`";
71  en = false;
72  }
73 
74  if (en)
75  {
76  this->startRecording();
77  }
78  }
79 
80  /// enable this LTM
81  void
83  {
84  processors->resetFilterStatisticsForNewEpisode(); //make sure the statistics are
85  //resetted before the recording starts
87  ARMARX_INFO << "Enabling LTM " << id().str() << " at " << now.toDateTimeString();
88  ARMARX_INFO << "Storing information at " << this->getExportName();
89  enabled = true;
91  {
93  statistics.firstStart = false;
94  }
95  statistics.lastStarted = now;
96  this->_enable();
97  }
98 
99  /// disable this LTM
100  void
102  {
104  ARMARX_INFO << "Disabling LTM " << id().str() << " at " << now.toDateTimeString();
105  enabled = false;
106  if (statistics.firstStop)
107  {
108  statistics.firstStopped = now;
109  statistics.firstStop = false;
110  }
111  statistics.lastStopped = now;
112  this->_disable();
113  ARMARX_INFO << "Disabling of LTM finished";
114  }
115 
116  /// return the full ltm as a wm::Memory with only references
117  /// the ltm may be huge, use with caution
120  {
123  return ret;
124  }
125 
126  void
128  {
129  TIMING_START(LTM_Memory_LoadAll);
131  TIMING_END_STREAM(LTM_Memory_LoadAll, ARMARX_DEBUG);
132  }
133 
134  /// return the full ltm as a wm::Memory and resolves the references
135  /// the ltm may be huge, use with caution
138  {
141  return ret;
142  }
143 
144  void
146  {
147  TIMING_START(LTM_Memory_LoadAllAndResolve);
149  _resolve(memory);
150  TIMING_END_STREAM(LTM_Memory_LoadAllAndResolve, ARMARX_DEBUG);
151  }
152 
153  /// convert the references of the input into a wm::Memory
154  void
156  {
157  TIMING_START(LTM_Memory_Load);
158  _resolve(memory);
159  TIMING_END_STREAM(LTM_Memory_Load, ARMARX_DEBUG);
160  }
161 
162  /// append a wm::Memory instance to the ltm
163  void
165  {
166  TIMING_START(LTM_Memory_Append);
167 
168  for (auto& f : processors->memFilters)
169  {
170  if (!f->accept(memory))
171  {
173  << "Ignoring to put a Memory into the LTM because it got filtered.";
174  return;
175  }
176  }
178  TIMING_END_STREAM(LTM_Memory_Append, ARMARX_DEBUG);
179  }
180 
181  /// append a wm::Memory instance to the ltm
182  void
183  store(const armem::server::wm::Memory& serverMemory)
184  {
186  memory.update(armem::toCommit(serverMemory));
187  this->store(memory);
188  }
189 
190  /// iterate over all core segments of this ltm
191  virtual bool forEachCoreSegment(std::function<void(CoreSegmentT&)> func) const = 0;
192 
193  /// check if core segment exists
194  virtual bool hasCoreSegment(const std::string&) const = 0;
195 
196  /// find core segment
197  virtual std::unique_ptr<CoreSegmentT> findCoreSegment(const std::string&) const = 0;
198 
199  /// default parameters. Implementation should use the configuration to configure
200  virtual void
201  createPropertyDefinitions(PropertyDefinitionsPtr& defs, const std::string& prefix)
202  {
203  defs->optional(p.enabled_on_startup, prefix + "enabled");
204  defs->optional(p.configuration_on_startup, prefix + "configuration");
205  defs->optional(p.export_name, prefix + "exportName");
206  }
207 
208  /// enable/disable
209  void
211  {
213 
214  enable();
215  }
216 
217  void
219  {
220 
221  disable();
222  }
223 
224  bool
225  isRecording() const
226  {
227  return enabled;
228  }
229 
230  /// statistics
231  virtual void
233  {
234  // enabled stays the same
237  }
238 
239  Statistics
241  {
242  return statistics;
243  }
244 
245  std::map<std::string, processor::SnapshotFilter::FilterStatistics>
247  {
248  try
249  {
250  ARMARX_INFO << "Trying to save statistics";
251  auto stats = processors->getSnapshotFilterStatistics();
252  return stats;
253  }
254  catch (InvalidArgumentException& e)
255  { //no data was actually recorded
256  ARMARX_INFO << e.what();
257  }
258  catch (...)
259  {
260  ARMARX_WARNING << "Saving statistics did not work";
261  }
262  std::map<std::string, processor::SnapshotFilter::FilterStatistics> emptyStatistics;
263  return emptyStatistics;
264  }
265 
266  /// get level name1
267  static std::string
269  {
270  return "LT-Memory";
271  }
272 
273  protected:
274  /// configuration
275  virtual void
276  _configure(const nlohmann::json&)
277  {
278  }
279 
280  virtual void
282  {
283  }
284 
285  virtual void
287  {
288  }
289 
290  virtual void
291  _setExportName(const std::string&)
292  {
293  }
294 
295  virtual void _loadAllReferences(armem::wm::Memory& memory) = 0;
296  virtual void _resolve(armem::wm::Memory& memory) = 0;
297  virtual void _store(const armem::wm::Memory& memory) = 0;
298  virtual void _directlyStore(const armem::wm::Memory& memory) = 0;
299 
300  public:
301  // stuff for scenario parameters
302  struct Properties
303  {
304  bool enabled_on_startup = false;
306  "{ \"SnapshotFrequencyFilter\": {\"WaitingTimeInMsForFilter\" : 50}, "
307  "\"PngConverter\": {}, \"ExrConverter\": {}}"; //record with 20 fps as standard
308  std::string export_name = "MemoryExport";
309  std::string export_path = "/tmp/ltm";
310  } p;
311 
312  protected:
313  mutable std::recursive_mutex ltm_mutex;
314 
315  mutable Statistics statistics;
316 
317  std::atomic_bool enabled = false;
318  };
319 } // namespace armarx::armem::server::ltm::detail
armarx::armem::server::ltm::detail::MemoryBase::isRecording
bool isRecording() const
Definition: MemoryBase.h:225
armarx::armem::server::ltm::detail::MemoryBase::_disable
virtual void _disable()
Definition: MemoryBase.h:286
armarx::armem::server::ltm::detail::MemoryBase::getStatistics
Statistics getStatistics() const
Definition: MemoryBase.h:240
armarx::armem::server::ltm::detail::MemoryBase::Statistics
Definition: MemoryBase.h:30
TIMING_START
#define TIMING_START(name)
Definition: TimeUtil.h:280
armarx::armem::server::ltm::detail
Definition: CoreSegmentBase.cpp:3
armarx::armem::server::ltm::detail::MemoryBase::_resolve
virtual void _resolve(armem::wm::Memory &memory)=0
armarx::armem::server::ltm::detail::MemoryBase::Statistics::firstStopped
armarx::core::time::DateTime firstStopped
Definition: MemoryBase.h:37
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::armem::server::ltm::detail::MemoryItem::processors
std::shared_ptr< Processors > processors
Definition: MemoryItem.h:54
armarx::armem::server::ltm::detail::MemoryBase::disable
void disable()
disable this LTM
Definition: MemoryBase.h:101
armarx::armem::server::ltm::detail::MemoryBase::findCoreSegment
virtual std::unique_ptr< CoreSegmentT > findCoreSegment(const std::string &) const =0
find core segment
armarx::armem::server::ltm::detail::MemoryBase::Statistics::lastStarted
armarx::core::time::DateTime lastStarted
Definition: MemoryBase.h:38
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
MemoryID.h
armarx::armem::server::ltm::detail::MemoryBase::_loadAllReferences
virtual void _loadAllReferences(armem::wm::Memory &memory)=0
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem::server::ltm::detail::MemoryBase::Statistics::firstStart
bool firstStart
Definition: MemoryBase.h:34
armarx::armem::server::ltm::detail::MemoryBase::Statistics::firstStarted
armarx::core::time::DateTime firstStarted
Definition: MemoryBase.h:36
armarx::armem::server::ltm::detail::MemoryBase::CoreSegmentT
_CoreSegmentT CoreSegmentT
Definition: MemoryBase.h:43
armarx::armem::server::ltm::detail::MemoryBase::p
struct armarx::armem::server::ltm::detail::MemoryBase::Properties p
armarx::armem::server::ltm::detail::MemoryBase::Properties
Definition: MemoryBase.h:302
aron_conversions.h
armarx::armem::server::ltm::detail::MemoryBase::enabled
std::atomic_bool enabled
Definition: MemoryBase.h:317
armarx::armem::server::ltm::detail::MemoryBase::Properties::configuration_on_startup
std::string configuration_on_startup
Definition: MemoryBase.h:305
armarx::armem::server::ltm::detail::MemoryBase::getLevelName
static std::string getLevelName()
get level name1
Definition: MemoryBase.h:268
armarx::memory
Brief description of class memory.
Definition: memory.h:39
armarx::armem::server::ltm::detail::MemoryBase::MemoryBase
MemoryBase(const std::string &exportName, const MemoryID &id)
Definition: MemoryBase.h:45
armarx::armem::server::ltm::detail::MemoryBase::Statistics::recordedCoreSegments
long recordedCoreSegments
Definition: MemoryBase.h:33
TIMING_END_STREAM
#define TIMING_END_STREAM(name, os)
Definition: TimeUtil.h:300
armarx::armem::server::ltm::detail::MemoryBase::Properties::export_path
std::string export_path
Definition: MemoryBase.h:309
armarx::armem::server::ltm::detail::MemoryBase::statistics
Statistics statistics
Definition: MemoryBase.h:315
armarx::armem::server::ltm::detail::MemoryBase
Interface functions for the longterm memory classes.
Definition: MemoryBase.h:27
armarx::armem::server::ltm::detail::MemoryBase::hasCoreSegment
virtual bool hasCoreSegment(const std::string &) const =0
check if core segment exists
armarx::armem::server::ltm::detail::MemoryBase::enable
void enable()
enable this LTM
Definition: MemoryBase.h:82
armarx::armem::server::ltm::detail::MemoryBase::_enable
virtual void _enable()
Definition: MemoryBase.h:281
memory_definitions.h
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::armem::server::wm::Memory
Definition: memory_definitions.h:128
armarx::armem::server::ltm::detail::MemoryBase::resolve
void resolve(armem::wm::Memory &memory)
convert the references of the input into a wm::Memory
Definition: MemoryBase.h:155
armarx::armem::server::ltm::detail::MemoryBase::_configure
virtual void _configure(const nlohmann::json &)
configuration
Definition: MemoryBase.h:276
armarx::armem::server::ltm::detail::MemoryBase::loadAllAndResolve
void loadAllAndResolve(armem::wm::Memory &memory)
Definition: MemoryBase.h:145
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm::detail::MemoryBase::resetStatistics
virtual void resetStatistics()
statistics
Definition: MemoryBase.h:232
armarx::armem::server::ltm::detail::MemoryBase::store
void store(const armem::server::wm::Memory &serverMemory)
append a wm::Memory instance to the ltm
Definition: MemoryBase.h:183
armarx::armem::server::ltm::detail::MemoryBase::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: MemoryBase.h:313
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::server::ltm::detail::MemoryBase::Statistics::firstStop
bool firstStop
Definition: MemoryBase.h:35
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::server::ltm::detail::MemoryBase::forEachCoreSegment
virtual bool forEachCoreSegment(std::function< void(CoreSegmentT &)> func) const =0
iterate over all core segments of this ltm
armarx::armem::server::ltm::detail::MemoryBase::loadAllAndResolve
armem::wm::Memory loadAllAndResolve()
return the full ltm as a wm::Memory and resolves the references the ltm may be huge,...
Definition: MemoryBase.h:137
armarx::armem::server::ltm::detail::MemoryBase::stopRecording
void stopRecording()
Definition: MemoryBase.h:218
armarx::armem::server::ltm::detail::MemoryBase::createPropertyDefinitions
virtual void createPropertyDefinitions(PropertyDefinitionsPtr &defs, const std::string &prefix)
default parameters. Implementation should use the configuration to configure
Definition: MemoryBase.h:201
armarx::armem::server::ltm::detail::MemoryItem::getExportName
virtual std::string getExportName() const
Definition: MemoryItem.h:26
armarx::armem::server::ltm::detail::MemoryBase::Properties::enabled_on_startup
bool enabled_on_startup
Definition: MemoryBase.h:304
operations.h
armarx::armem::server::ltm::detail::MemoryBase::getFilterStatistics
std::map< std::string, processor::SnapshotFilter::FilterStatistics > getFilterStatistics()
Definition: MemoryBase.h:246
armarx::armem::server::ltm::detail::MemoryBase::startRecording
void startRecording()
enable/disable
Definition: MemoryBase.h:210
Component.h
memory_definitions.h
LoggingUtil.h
armarx::armem::server::ltm::detail::MemoryBase::loadAllReferences
void loadAllReferences(armem::wm::Memory &memory)
Definition: MemoryBase.h:127
armarx::armem::server::ltm::detail::MemoryBase::Statistics::lastEnabled
armarx::core::time::DateTime lastEnabled
Definition: MemoryBase.h:32
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::server::ltm::detail::MemoryBase::Statistics::lastStopped
armarx::core::time::DateTime lastStopped
Definition: MemoryBase.h:39
armarx::armem::server::ltm::detail::MemoryItem::id
MemoryID id() const
Definition: MemoryItem.cpp:37
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::server::ltm::CoreSegment
Definition: CoreSegment.h:13
armarx::armem::server::ltm::detail::MemoryBase::loadAllReferences
armem::wm::Memory loadAllReferences()
return the full ltm as a wm::Memory with only references the ltm may be huge, use with caution
Definition: MemoryBase.h:119
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::ltm::detail::MemoryItem::setExportName
void setExportName(const std::string &n)
Definition: MemoryItem.cpp:23
time.h
armarx::armem::server::ltm::detail::MemoryBase::_setExportName
virtual void _setExportName(const std::string &)
Definition: MemoryBase.h:291
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::server::ltm::detail::MemoryBase::_store
virtual void _store(const armem::wm::Memory &memory)=0
armarx::armem::toCommit
Commit toCommit(const ContainerT &container)
Definition: operations.h:27
armarx::armem::server::ltm::detail::MemoryItem
Interface functions for the longterm memory classes.
Definition: MemoryItem.h:13
MemoryItem.h
armarx::core::time::DateTime::Invalid
static DateTime Invalid()
Definition: DateTime.cpp:60
armarx::armem::server::ltm::detail::MemoryBase::Properties::export_name
std::string export_name
Definition: MemoryBase.h:308
armarx::armem::server::ltm::detail::MemoryBase::configure
void configure()
initialize config
Definition: MemoryBase.h:52
armarx::armem::server::ltm::detail::MemoryBase::store
void store(const armem::wm::Memory &memory)
append a wm::Memory instance to the ltm
Definition: MemoryBase.h:164
armarx::armem::server::ltm::detail::MemoryBase::_directlyStore
virtual void _directlyStore(const armem::wm::Memory &memory)=0