memory_conversions.cpp
Go to the documentation of this file.
2
4
5namespace armarx::armem::wm
6{
7 void
8 toMemory(Memory& m, const std::vector<CoreSegment>& e)
9 {
10 m.clear();
11 for (const auto& s : e)
12 {
13 m.addCoreSegment(s);
14 }
15 }
16
17 void
18 toMemory(Memory& m, const std::vector<ProviderSegment>& e)
19 {
20 m.clear();
21 for (const auto& s : e)
22 {
23 if (!m.hasCoreSegment(s.id().coreSegmentName))
24 {
25 m.addCoreSegment(s.id().coreSegmentName);
26 }
27 auto* c = m.findCoreSegment(s.id().coreSegmentName);
28 c->addProviderSegment(s);
29 }
30 }
31
32 void
33 toMemory(Memory& m, const std::vector<Entity>& e)
34 {
35 m.clear();
36 for (const auto& s : e)
37 {
38 if (!m.hasCoreSegment(s.id().coreSegmentName))
39 {
40 m.addCoreSegment(s.id().coreSegmentName);
41 }
42 auto* c = m.findCoreSegment(s.id().coreSegmentName);
43
44 if (!c->hasProviderSegment(s.id().providerSegmentName))
45 {
46 c->addProviderSegment(s.id().providerSegmentName);
47 }
48 auto* p = c->findProviderSegment(s.id().providerSegmentName);
49 p->addEntity(s);
50 }
51 }
52
53 void
54 toMemory(Memory& m, const std::vector<EntitySnapshot>& e)
55 {
56 m.clear();
57
58 for (const auto& s : e)
59 {
60 if (!m.hasCoreSegment(s.id().coreSegmentName))
61 {
62 m.addCoreSegment(s.id().coreSegmentName);
63 }
64 auto* c = m.findCoreSegment(s.id().coreSegmentName);
65
66 if (!c->hasProviderSegment(s.id().providerSegmentName))
67 {
68 c->addProviderSegment(s.id().providerSegmentName);
69 }
70 auto* p = c->findProviderSegment(s.id().providerSegmentName);
71
72 if (!p->hasEntity(s.id().entityName))
73 {
74 p->addEntity(s.id().entityName);
75 }
76 auto* en = p->findEntity(s.id().entityName);
77 en->addSnapshot(s);
78 }
79 }
80
81 void
82 toMemory(Memory& m, const std::vector<EntityInstance>& e)
83 {
84 m.clear();
85 for (const auto& s : e)
86 {
87 if (!m.hasCoreSegment(s.id().coreSegmentName))
88 {
89 m.addCoreSegment(s.id().coreSegmentName);
90 }
91 auto* c = m.findCoreSegment(s.id().coreSegmentName);
92
93 if (!c->hasProviderSegment(s.id().providerSegmentName))
94 {
95 c->addProviderSegment(s.id().providerSegmentName);
96 }
97 auto* p = c->findProviderSegment(s.id().providerSegmentName);
98
99 if (!p->hasEntity(s.id().entityName))
100 {
101 p->addEntity(s.id().entityName);
102 }
103 auto* en = p->findEntity(s.id().entityName);
104
105 if (!en->hasSnapshot(s.id().timestamp))
106 {
107 en->addSnapshot(s.id().timestamp);
108 }
109 auto* sn = p->findSnapshot(s.id());
110
111 sn->addInstance(s);
112 }
113 }
114
115 void
117 const armarx::armem::server::wm::Memory& structure,
118 const std::vector<EntitySnapshot>& e)
119 {
120 // create an empty working memory:
121 m.clear();
122
123 for (const auto& s : e)
124 {
125 //get name of the core segment:
126 auto coreSegmentName = s.id().coreSegmentName;
127 auto* coreStructure = structure.findCoreSegment(coreSegmentName);
128
129 // The structure memory may not know about this core segment
130 // (version skew, partial load, snapshot from a different
131 // schema, ...). Skip the snapshot with a warning instead of
132 // dereferencing a nullptr below.
133 if (coreStructure == nullptr)
134 {
135 ARMARX_WARNING << "Cannot convert snapshot " << s.id().str()
136 << ": core segment '" << coreSegmentName
137 << "' is not part of the structure memory. Skipping.";
138 continue;
139 }
140
141 //if m does not have this core segment yet add the segment but copy the structure from the server wm:
142 if (!m.hasCoreSegment(coreSegmentName))
143 {
144 m.addCoreSegment(coreSegmentName, coreStructure->aronType());
145 }
146 auto* c = m.findCoreSegment(coreSegmentName);
147
148 //get name of provider segment:
149 auto providerSegmentName = s.id().providerSegmentName;
150 auto* providerStructure = coreStructure->findProviderSegment(providerSegmentName);
151
152 // Same defensive check for the provider segment.
153 if (providerStructure == nullptr)
154 {
155 ARMARX_WARNING << "Cannot convert snapshot " << s.id().str()
156 << ": provider segment '" << providerSegmentName
157 << "' is not part of the structure memory under core segment '"
158 << coreSegmentName << "'. Skipping.";
159 continue;
160 }
161
162 if (!c->hasProviderSegment(providerSegmentName))
163 {
164 c->addProviderSegment(providerSegmentName, providerStructure->aronType());
165 }
166 auto* p = c->findProviderSegment(providerSegmentName);
167
168 if (!p->hasEntity(s.id().entityName))
169 {
170 p->addEntity(s.id().entityName);
171 }
172 auto* en = p->findEntity(s.id().entityName);
173 en->addSnapshot(s);
174 }
175 }
176
177} // namespace armarx::armem::wm
constexpr T c
CoreSegmentT * findCoreSegment(const std::string &name)
Definition MemoryBase.h:121
bool hasCoreSegment(const std::string &name) const
Definition MemoryBase.h:107
CoreSegmentT & addCoreSegment(const std::string &name, aron::type::ObjectPtr coreSegmentType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Add an empty core segment with the given name, type and prediction engines.
Definition MemoryBase.h:261
Client-side working memory.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
void toMemory(Memory &m, const std::vector< CoreSegment > &e)