EntitySnapshot.cpp
Go to the documentation of this file.
1// Header
2#include "EntitySnapshot.h"
3
4#include <memory>
5
6// ArmarX
9
12
14{
16 const std::string& exportName,
17 const armem::MemoryID& memoryId /* UNESCAPED */,
18 const std::shared_ptr<Processors>& filters,
19 const std::shared_ptr<persistence::MemoryPersistenceStrategy>& persistenceStrategy) :
20 EntitySnapshotBase(exportName, memoryId, filters), persistenceStrategy_(persistenceStrategy)
21 {
22 }
23
24 bool
25 EntitySnapshot::_implForEachInstance(std::function<void(EntityInstance&)> func) const
26 {
28
29 ARMARX_DEBUG << "For each entity instance (entity snapshot= " << id().cleanID().str()
30 << ")";
31
32 for (auto& entityInstanceKey : persistenceStrategy_->getContainerKeys(id()))
33 {
34 std::shared_ptr<persistence::MemoryPersistenceStrategy>
35 entityInstancePersistenceStrategy(persistenceStrategy_);
36 MemoryID entityInstanceId = id().withInstanceIndex(std::stoi(entityInstanceKey));
38 getExportName(), entityInstanceId, processors, entityInstancePersistenceStrategy);
39
40 func(c);
41 }
42
43 return true;
44 }
45
46 bool
47 EntitySnapshot::_implHasInstance(const int entityInstanceIndex) const
48 {
50
51 return persistenceStrategy_->containsContainer(id(), std::to_string(entityInstanceIndex));
52 }
53
54 std::shared_ptr<EntityInstance>
55 EntitySnapshot::_implFindInstance(const int entityInstanceIndex) const
56 {
58
59 if (!_implHasInstance(entityInstanceIndex)) // Call _impl version
60 {
61 return nullptr;
62 }
63
64 return std::make_shared<EntityInstance>(getExportName(),
65 id().withInstanceIndex(entityInstanceIndex),
67 persistenceStrategy_);
68 }
69
70 void
72 {
74
75 wmEntitySnapshot.id() = id().getEntitySnapshotID().cleanID();
76
77 _implForEachInstance([&wmEntitySnapshot](armem::server::ltm::EntityInstance& ltmEntityInstance)
78 { ltmEntityInstance.loadAllReferences(wmEntitySnapshot); });
79 }
80
81 void
83 {
85
86 ARMARX_DEBUG << "Resolve entity snapshot id=" << id().cleanID().str();
87
88 wmEntitySnapshot.forEachInstance(
89 [&](auto& wmEntityInstance)
90 {
91 std::shared_ptr<persistence::MemoryPersistenceStrategy>
92 entityInstancePersistanceStrategy(persistenceStrategy_);
93
94 EntityInstance ltmEntityInstance(
96 id().withInstanceIndex(wmEntityInstance.id().instanceIndex),
98 entityInstancePersistanceStrategy);
99 try
100 {
101 ltmEntityInstance.resolve(wmEntityInstance);
102 }
103 catch (...)
104 {
105 ARMARX_WARNING << "Could not resolve enity instance";
106 }
107 });
108 }
109
110 void
112 {
114
115 if (id().timestamp.isInvalid())
116 {
118 << "During storage of segment '" << wmEntitySnapshot.id().str()
119 << "' I noticed that the corresponding LTM has no id set. "
120 << "I set the id of the LTM to the same name, however this should not happen!";
121 id().timestamp = wmEntitySnapshot.id().timestamp;
122 }
123
124 ARMARX_DEBUG << "Directly store entity snapshot";
125
126
127 /*if (!connected())
128 {
129 ARMARX_WARNING << "LTM ENTITY SNAPSHOT NOT CONNECTED ALTHOUGH ENABLED " << id().str();
130 return;
131 }*/
132
133 wmEntitySnapshot.forEachInstance(
134 [&](const auto& wmEntityInstance)
135 {
136 std::shared_ptr<persistence::MemoryPersistenceStrategy>
137 entityInstancePersistenceStrategy(persistenceStrategy_);
138
140 id().withInstanceIndex(wmEntityInstance.id().instanceIndex),
142 entityInstancePersistenceStrategy);
143 c.store(wmEntityInstance);
144
145 statistics.recordedInstances++;
146 });
147 }
148} // namespace armarx::armem::server::ltm
std::string timestamp()
constexpr T c
MemoryID getEntitySnapshotID() const
Definition MemoryID.cpp:318
MemoryID cleanID() const
Definition MemoryID.cpp:133
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition MemoryID.cpp:102
MemoryID withInstanceIndex(int index) const
Definition MemoryID.cpp:441
bool forEachInstance(InstanceFunctionT &&func)
void _store(const armem::wm::EntitySnapshot &wmEntitySnapshot) override
EntitySnapshot(const std::string &exportName, const MemoryID &memoryId, const std::shared_ptr< Processors > &filters, const std::shared_ptr< persistence::MemoryPersistenceStrategy > &persistenceStrategy)
bool _implForEachInstance(std::function< void(EntityInstance &)> func) const override
void _resolve(armem::wm::EntitySnapshot &wmEntitySnapshot) const override
bool _implHasInstance(const int entityInstanceIndex) const override
std::shared_ptr< EntityInstance > _implFindInstance(const int entityInstanceIndex) const override
void _loadAllReferences(armem::wm::EntitySnapshot &wmEntitySnapshot) const override
void resolve(armem::wm::EntityInstance &e) const
convert the references of the input into a wm::Memory
void loadAllReferences(armem::wm::EntitySnapshot &e) const
return the full sub-ltm as a wm::EntitySnapshot with only references the ltm may be huge,...
virtual std::string getExportName() const
Definition MemoryItem.h:27
std::shared_ptr< Processors > processors
Definition MemoryItem.h:54
Client-side working memory entity snapshot.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_TRACE
Definition trace.h:77