MemoryBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <list>
5 
6 
7 // BaseClass
8 #include "MemoryItem.h"
9 
10 // ChildType
11 
12 // ArmarX
15 #include <ArmarXCore/core/time.h>
16 
21 #include <RobotAPI/libraries/armem/server/test/ForgettingExperiments.h>
23 
25 {
26  /// @brief Interface functions for the longterm memory classes
27  template <class _CoreSegmentT>
28  class MemoryBase : public MemoryItem
29  {
30  public:
31  struct Statistics
32  {
35  bool firstStart = true;
36  bool firstStop = true;
41  };
42 
43  public:
44  using CoreSegmentT = _CoreSegmentT;
45 
46  MemoryBase(const std::string& exportName, const MemoryID& id) :
47  MemoryItem(exportName, id), enabled(false)
48  {
49  }
50 
51  /// initialize config
52  void
54  {
55  bool en = p.enabled_on_startup;
59 
61 
62  try
63  {
64  const auto j = nlohmann::json::parse(p.configuration_on_startup);
65 
66  // Processors are shared. So we only need to configure the root
67  processors->configure(j);
68 
69  this->_configure(j);
70  }
71  catch (...)
72  {
73  ARMARX_WARNING << "Failed to parse `" << p.configuration_on_startup << "`";
74  en = false;
75  }
76 
77  if (en)
78  {
79  this->startRecording();
80  }
81 
82  }
83 
84  /// enable this LTM
85  void
87  {
88  processors->resetFilterStatisticsForNewEpisode(); //make sure the statistics are
89  //resetted before the recording starts
91  ARMARX_INFO << "Enabling LTM " << id().str() << " at " << now.toDateTimeString();
92  ARMARX_INFO << "Storing information at " << this->getExportName();
93  enabled = true;
95  {
97  statistics.firstStart = false;
98  }
99  statistics.lastStarted = now;
100  this->_enable();
101  }
102 
103  /// disable this LTM
104  void
106  {
108  ARMARX_INFO << "Disabling LTM " << id().str() << " at " << now.toDateTimeString();
109  enabled = false;
110  if (statistics.firstStop)
111  {
112  statistics.firstStopped = now;
113  statistics.firstStop = false;
114  }
115  statistics.lastStopped = now;
116  this->_disable();
117  ARMARX_INFO << "Disabling of LTM finished";
118  }
119 
120  /// return the full ltm as a wm::Memory with only references
121  /// the ltm may be huge, use with caution
124  {
127  return ret;
128  }
129 
130  void
132  {
133  TIMING_START(LTM_Memory_LoadAll);
135  TIMING_END_STREAM(LTM_Memory_LoadAll, ARMARX_DEBUG);
136  }
137 
138  //return the newest part of the ltm as a wm::Memory with only references
139  //will return the newest n snapshots of each entity
144  return ret;
145  }
146 
147  //return the newest part of the ltm as a wm::Memory with only references
148  //will return the newest n snapshots of each entity for only the CoreSegments matching one of the name sin the list
149  void
150  loadLatestNReferences(int n, armem::wm::Memory& memory, std::list<std::string> coreSegNames){
151  TIMING_START(LTM_Memory_loadNewestn_someCoreSegs);
152  _loadLatestNReferences(n, memory, coreSegNames);
153  TIMING_END_STREAM(LTM_Memory_loadNewestn_someCoreSegs, ARMARX_DEBUG);
154  }
155 
156  void
158  TIMING_START(LTM_Memory_loadNewestn);
160  TIMING_END_STREAM(LTM_Memory_loadNewestn, ARMARX_DEBUG);
161  }
162 
163 
164 
165  /// return the full ltm as a wm::Memory and resolves the references
166  /// the ltm may be huge, use with caution
169  {
172  return ret;
173  }
174 
175  void
177  {
178  TIMING_START(LTM_Memory_LoadAllAndResolve);
180  _resolve(memory);
181  TIMING_END_STREAM(LTM_Memory_LoadAllAndResolve, ARMARX_DEBUG);
182  }
183 
184  /// convert the references of the input into a wm::Memory
185  void
187  {
188  TIMING_START(LTM_Memory_Load);
189  _resolve(memory);
190  TIMING_END_STREAM(LTM_Memory_Load, ARMARX_DEBUG);
191  }
192 
193  /// append a wm::Memory instance to the ltm
194  void
196  {
197  TIMING_START(LTM_Memory_Append);
198 
199  for (auto& f : processors->memFilters)
200  {
201  if (!f->accept(memory))
202  {
204  << "Ignoring to put a Memory into the LTM because it got filtered.";
205  return;
206  }
207  }
209  TIMING_END_STREAM(LTM_Memory_Append, ARMARX_DEBUG);
210  }
211 
212  /// append a wm::Memory instance to the ltm
213  void
214  store(const armem::server::wm::Memory& serverMemory)
215  {
217  memory.update(armem::toCommit(serverMemory));
218  this->store(memory);
219  }
220 
221  /// iterate over all core segments of this ltm
222  virtual bool forEachCoreSegment(std::function<void(CoreSegmentT&)> func) const = 0;
223 
224  /// check if core segment exists
225  virtual bool hasCoreSegment(const std::string&) const = 0;
226 
227  /// find core segment
228  virtual std::unique_ptr<CoreSegmentT> findCoreSegment(const std::string&) const = 0;
229 
230  /// default parameters. Implementation should use the configuration to configure
231  virtual void
232  createPropertyDefinitions(PropertyDefinitionsPtr& defs, const std::string& prefix)
233  {
234  defs->optional(p.enabled_on_startup, prefix + "enabled");
235  defs->optional(p.configuration_on_startup, prefix + "configuration");
236  defs->optional(p.export_name, prefix + "exportName");
237 
238  defs->optional(p.importOnStartUp, prefix + "importOnStartUp");
239  defs->optional(p.maxAmountOfSnapshotsLoaded, prefix + "maxAmountSnapshotsLoaded");
240  defs->optional(p.coreSegmentsToLoad, prefix + "loadedCoreSegments");
241 
242  }
243 
244  /// enable/disable
245  void
247  {
249 
250  enable();
251  }
252 
253  void
255  {
256 
257  disable();
258  }
259 
260  bool
261  isRecording() const
262  {
263  return enabled;
264  }
265 
266  /// statistics
267  virtual void
269  {
270  // enabled stays the same
273  }
274 
275  Statistics
277  {
278  return statistics;
279  }
280 
281  std::map<std::string, processor::SnapshotFilter::FilterStatistics>
283  {
284  try
285  {
286  ARMARX_INFO << "Trying to save statistics";
287  auto stats = processors->getSnapshotFilterStatistics();
288  return stats;
289  }
290  catch (InvalidArgumentException& e)
291  { //no data was actually recorded
292  ARMARX_INFO << e.what();
293  }
294  catch (...)
295  {
296  ARMARX_WARNING << "Saving statistics did not work";
297  }
298  std::map<std::string, processor::SnapshotFilter::FilterStatistics> emptyStatistics;
299  return emptyStatistics;
300  }
301 
302  /// get level name1
303  static std::string
305  {
306  return "LT-Memory";
307  }
308 
309  void
311  _loadOnStartup();
312  }
313 
314 
315  protected:
316  /// configuration
317  virtual void
318  _configure(const nlohmann::json&)
319  {
320  }
321 
322  virtual void
324  {
325  }
326 
327  virtual void
329  {
330  }
331 
332  virtual void
333  _setExportName(const std::string&)
334  {
335  }
336 
337  virtual void _loadAllReferences(armem::wm::Memory& memory) = 0;
338  virtual void _resolve(armem::wm::Memory& memory) = 0;
339  virtual void _store(const armem::wm::Memory& memory) = 0;
340  virtual void _directlyStore(const armem::wm::Memory& memory) = 0;
341  virtual void _loadOnStartup() = 0;
342  virtual void _loadLatestNReferences(int n, armem::wm::Memory& memory) = 0;
343  virtual void _loadLatestNReferences(int n, armem::wm::Memory& memory, std::list<std::string> coreSegNames) = 0;
344 
345  public:
346  // stuff for scenario parameters
347  struct Properties
348  {
349  bool enabled_on_startup = false;
351  "{ \"SnapshotFrequencyFilter\": {\"WaitingTimeInMsForFilter\" : 50}, "
352  "\"PngConverter\": {}, \"ExrConverter\": {}}"; //record with 20 fps as standard
353  std::string export_name = "MemoryExport";
354  std::string export_path = "/tmp/ltm";
355 
356  //belong more to WM, but no good solution for that currently:
357  bool importOnStartUp = false;
359  std::string coreSegmentsToLoad = "Localization";
360  } p;
361 
362 
363  protected:
364  mutable std::recursive_mutex ltm_mutex;
365 
366  mutable Statistics statistics;
367 
368  std::atomic_bool enabled = true;
369  };
370 } // namespace armarx::armem::server::ltm::detail
armarx::armem::server::ltm::detail::MemoryBase::isRecording
bool isRecording() const
Definition: MemoryBase.h:261
armarx::armem::server::ltm::detail::MemoryBase::_disable
virtual void _disable()
Definition: MemoryBase.h:328
armarx::armem::server::ltm::detail::MemoryBase::getStatistics
Statistics getStatistics() const
Definition: MemoryBase.h:276
armarx::armem::server::ltm::detail::MemoryBase::Statistics
Definition: MemoryBase.h:31
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:38
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::loadLatestNReferences
void loadLatestNReferences(int n, armem::wm::Memory &memory)
Definition: MemoryBase.h:157
armarx::armem::server::ltm::detail::MemoryBase::disable
void disable()
disable this LTM
Definition: MemoryBase.h:105
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:39
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::server::ltm::detail::MemoryBase::Properties::coreSegmentsToLoad
std::string coreSegmentsToLoad
Definition: MemoryBase.h:359
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:35
armarx::armem::server::ltm::detail::MemoryBase::Statistics::firstStarted
armarx::core::time::DateTime firstStarted
Definition: MemoryBase.h:37
armarx::armem::server::ltm::detail::MemoryBase::Properties::importOnStartUp
bool importOnStartUp
Definition: MemoryBase.h:357
armarx::armem::server::ltm::detail::MemoryBase::CoreSegmentT
_CoreSegmentT CoreSegmentT
Definition: MemoryBase.h:44
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:347
aron_conversions.h
armarx::armem::server::ltm::detail::MemoryBase::enabled
std::atomic_bool enabled
Definition: MemoryBase.h:368
armarx::armem::server::ltm::detail::MemoryBase::Properties::configuration_on_startup
std::string configuration_on_startup
Definition: MemoryBase.h:350
armarx::armem::server::ltm::detail::MemoryBase::getLevelName
static std::string getLevelName()
get level name1
Definition: MemoryBase.h:304
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:46
armarx::armem::server::ltm::detail::MemoryBase::Statistics::recordedCoreSegments
long recordedCoreSegments
Definition: MemoryBase.h:34
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:354
armarx::armem::server::ltm::detail::MemoryBase::statistics
Statistics statistics
Definition: MemoryBase.h:366
armarx::armem::server::ltm::detail::MemoryBase
Interface functions for the longterm memory classes.
Definition: MemoryBase.h:28
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:86
armarx::armem::server::ltm::detail::MemoryBase::_enable
virtual void _enable()
Definition: MemoryBase.h:323
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:136
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:186
armarx::armem::server::ltm::detail::MemoryBase::_configure
virtual void _configure(const nlohmann::json &)
configuration
Definition: MemoryBase.h:318
armarx::armem::server::ltm::detail::MemoryBase::loadAllAndResolve
void loadAllAndResolve(armem::wm::Memory &memory)
Definition: MemoryBase.h:176
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::server::ltm::detail::MemoryBase::resetStatistics
virtual void resetStatistics()
statistics
Definition: MemoryBase.h:268
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:214
armarx::armem::server::ltm::detail::MemoryBase::_loadOnStartup
virtual void _loadOnStartup()=0
armarx::armem::server::ltm::detail::MemoryBase::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: MemoryBase.h:364
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::server::ltm::detail::MemoryBase::loadLatestNReferences
armem::wm::Memory loadLatestNReferences(int n)
Definition: MemoryBase.h:141
armarx::armem::server::ltm::detail::MemoryBase::Statistics::firstStop
bool firstStop
Definition: MemoryBase.h:36
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::Properties::maxAmountOfSnapshotsLoaded
int maxAmountOfSnapshotsLoaded
Definition: MemoryBase.h:358
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:168
armarx::armem::server::ltm::detail::MemoryBase::stopRecording
void stopRecording()
Definition: MemoryBase.h:254
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:232
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:349
operations.h
armarx::armem::server::ltm::detail::MemoryBase::getFilterStatistics
std::map< std::string, processor::SnapshotFilter::FilterStatistics > getFilterStatistics()
Definition: MemoryBase.h:282
armarx::armem::server::ltm::detail::MemoryBase::startRecording
void startRecording()
enable/disable
Definition: MemoryBase.h:246
Component.h
memory_definitions.h
LoggingUtil.h
armarx::armem::server::ltm::detail::MemoryBase::loadAllReferences
void loadAllReferences(armem::wm::Memory &memory)
Definition: MemoryBase.h:131
armarx::armem::server::ltm::detail::MemoryBase::Statistics::lastEnabled
armarx::core::time::DateTime lastEnabled
Definition: MemoryBase.h:33
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:40
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:123
armarx::armem::server::ltm::detail::MemoryBase::loadOnStartup
void loadOnStartup()
Definition: MemoryBase.h:310
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:333
armarx::armem::server::ltm::detail::MemoryBase::loadLatestNReferences
void loadLatestNReferences(int n, armem::wm::Memory &memory, std::list< std::string > coreSegNames)
Definition: MemoryBase.h:150
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::_loadLatestNReferences
virtual void _loadLatestNReferences(int n, armem::wm::Memory &memory)=0
armarx::armem::server::ltm::detail::MemoryBase::Properties::export_name
std::string export_name
Definition: MemoryBase.h:353
armarx::armem::server::ltm::detail::MemoryBase::configure
void configure()
initialize config
Definition: MemoryBase.h:53
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:195
armarx::armem::server::ltm::detail::MemoryBase::_directlyStore
virtual void _directlyStore(const armem::wm::Memory &memory)=0