EntityInstance.cpp
Go to the documentation of this file.
1 // Header
2 #include "EntityInstance.h"
3 
4 // STD / STL
5 #include <SimoxUtility/algorithm/string/string_tools.h>
6 
7 // ArmarX
10 
12 
14 {
17 
18  const std::string& exportName,
19  const armem::MemoryID& id /* UNESCAPED */,
20  const std::shared_ptr<Processors>& filters) :
21  EntityInstanceBase(exportName, id, filters),
22  DiskMemoryItemMixin(p, exportName, id),
23  MongoDBStorageMixin(s, exportName, id)
24  {
25  //start();
26  }
27 
28  void
30  {
31  std::lock_guard l(ltm_mutex);
32  // assure that we put the right index to the snapshot
33  //ARMARX_CHECK(e.size() == (size_t)id().instanceIndex);
34  //int index = e.getInstanceIndices().size();
35 
36  //ARMARX_INFO << VAROUT(getFullPath());
37  //ARMARX_INFO << VAROUT(getMemoryID().getEntityInstanceID().getLeafItem());
38  int index = std::stoi(getMemoryID().getEntityInstanceID().getLeafItem());
39 
41 
42  // add instance. Do not set data, since we only return references
43  e.addInstance(i);
44  }
45 
46  void
48  {
49  std::lock_guard l(ltm_mutex);
50  //ARMARX_INFO << "resolve for entity instance " << e.id().str();
51  auto& dictConverter = processors->defaultObjectConverter;
52 
53  aron::data::DictPtr datadict = nullptr;
54  aron::data::DictPtr metadatadict = nullptr;
55 
56  if (fullPathExists())
57  {
58 
59  std::string dataFilename = (DATA_FILENAME + dictConverter.suffix);
60  std::string metadataFilename = (METADATA_FILENAME + dictConverter.suffix);
61  std::filesystem::path dataPath = getFullPath() / dataFilename;
62  std::filesystem::path metadataPath = getFullPath() / metadataFilename;
63 
64  if (util::fs::fileExists(dataPath))
65  {
66  //ARMARX_INFO << "Convert data for entity " << id();
67  auto datafilecontent = readDataFromFile(dataFilename);
68  auto dataaron = dictConverter.convert({datafilecontent, ""}, {});
69  datadict = aron::data::Dict::DynamicCastAndCheck(dataaron);
70  }
71  else
72  {
73  ARMARX_ERROR << "Could not find the data file '" << dataPath.string()
74  << "'. Continuing without data.";
75  }
76 
77  //ARMARX_INFO << "Convert metadata for entity " << id();
78  if (util::fs::fileExists(metadataPath))
79  {
80  auto metadatafilecontent = readDataFromFile(metadataFilename);
81  auto metadataaron = dictConverter.convert({metadatafilecontent, ""}, {});
82  metadatadict = aron::data::Dict::DynamicCastAndCheck(metadataaron);
83  }
84  else
85  {
86  ARMARX_ERROR << "Could not find the metadata file '" << metadataPath.string()
87  << "'. Continuing without metadata.";
88  }
89  }
90 
91  // check for special members TODO: only allowed for direct children?
92  auto allFilesInIndexFolder = getAllFiles();
93  for (const auto& [key, m] : datadict->getElements())
94  {
95  for (auto& f : processors->converters)
96  {
97  // iterate over all files and search for matching ones.
98  // We cannot simply check for the existence of a file because we do not know the
99  // mode (filename = memberName.mode.suffix)
100  for (const auto& filepath : allFilesInIndexFolder)
101  {
102  if (simox::alg::starts_with(filepath.filename(), key) and
103  simox::alg::ends_with(filepath, f->suffix))
104  {
105  std::string mode = simox::alg::remove_suffix(
106  simox::alg::remove_prefix(filepath.filename(), key), f->suffix);
107 
108  auto memberfilecontent = readDataFromFile(filepath.filename());
109  auto memberaron = f->convert(
110  {memberfilecontent, mode},
111  armarx::aron::Path(datadict->getPath(), std::vector<std::string>{key}));
112  datadict->setElement(key, memberaron);
113  break;
114  }
115  }
116  }
117  }
118 
119  from_aron(metadatadict, datadict, e);
120  }
121 
122  nlohmann::json
124  {
125  std::lock_guard l(ltm_mutex);
126  if (id().instanceIndex < 0)
127  {
129  << "During storage of segment '" << e.id().str()
130  << "' I noticed that the corresponding LTM has no id set. "
131  << "I set the id of the LTM to the same name, however this should not happen!";
132  id().timestamp = e.id().timestamp;
133  }
134 
135  auto& dictConverter = processors->defaultObjectConverter;
136 
137  /*if (!connected())
138  {
139  ARMARX_WARNING << "LTM ENTITY INSTANCE NOT CONNECTED ALTHOUGH ENABLED " << id().str();
140  return {};
141  }*/
142 
143  ensureFullPathExists(true);
144 
145  // data
146  auto dataAron = std::make_shared<aron::data::Dict>();
147  auto metadataAron = std::make_shared<aron::data::Dict>();
148  to_aron(metadataAron, dataAron, e);
149 
150  std::shared_ptr<aron::data::Dict> source;
151 
152  bool saveAndExtract =
153  false; //if true the data is saved in extracted form and in data.aron.json
154  // this is needed if several LTMs are recorded at once because otherwise the data from the data.aron.json
155  // is not there anymore to extract from
156 
157  if (saveAndExtract)
158  {
159  source = dataAron->clone();
160  }
161  else
162  {
163  source = dataAron;
164  }
165 
166  //ARMARX_INFO << "save and extract is " << saveAndExtract;
167 
168 
169  // check special members for special converters
170  for (auto& c : processors->converters)
171  {
173  ARMARX_CHECK_NOT_NULL(c->extractor);
174  auto dataExt = c->extractor->extract(source);
175 
176  for (const auto& [memberName, var] : dataExt.extraction)
177  {
179 
180  auto [memberDataVec, memberDataModeSuffix] = c->convert(var);
181 
182  std::string filename = (memberName + memberDataModeSuffix + c->suffix);
183 
184  ensureFileExists(filename, true);
185  writeDataToFile(filename, memberDataVec);
186  }
187  }
188 
189  // convert dict and metadata
190  auto [dataVec, dataVecModeSuffix] = dictConverter.convert(dataAron);
191  auto [metadataVec, metadataVecModeSuffix] = dictConverter.convert(metadataAron);
192  ARMARX_CHECK_EMPTY(dataVecModeSuffix);
193  ARMARX_CHECK_EMPTY(metadataVecModeSuffix);
194 
195  auto dataToReturn = nlohmann::json::parse(std::string(dataVec.begin(), dataVec.end()));
196 
197  {
198  std::string dataFilename = (DATA_FILENAME + dictConverter.suffix);
199  std::string metadataFilename = (METADATA_FILENAME + dictConverter.suffix);
200  std::filesystem::path dataPath = getFullPath() / dataFilename;
201  std::filesystem::path metadataPath = getFullPath() / metadataFilename;
202 
203  writeDataToFile(dataFilename, dataVec);
204  writeDataToFile(metadataFilename, metadataVec);
205  }
206 
209 
210  return dataToReturn;
211  }
212 } // namespace armarx::armem::server::ltm
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::DATA_FILENAME
static const constexpr char * DATA_FILENAME
Definition: DiskStorageMixin.h:68
armarx::armem::MemoryID::timestamp
Time timestamp
Definition: MemoryID.h:54
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:65
armarx::armem::server::ltm::detail::MemoryItem::getMemoryID
MemoryID getMemoryID() const
Definition: MemoryItem.h:33
armarx::armem::server::ltm::detail::MemoryItem::processors
std::shared_ptr< Processors > processors
Definition: MemoryItem.h:54
armarx::armem::server::ltm::EntityInstance::_store
nlohmann::json _store(const armem::wm::EntityInstance &) override
Definition: EntityInstance.cpp:123
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFileExists
void ensureFileExists(const std::string &filename, bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:142
armarx::armem::base::EntitySnapshotBase::addInstance
EntityInstanceT & addInstance(const EntityInstanceT &instance)
Add a single instance with data.
Definition: EntitySnapshotBase.h:264
ARMARX_CHECK_EMPTY
#define ARMARX_CHECK_EMPTY(c)
Definition: ExpressionException.h:218
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
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::mixin::DiskMemoryItemMixin::getAllFiles
std::vector< Path > getAllFiles() const
Definition: DiskStorageMixin.cpp:229
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::METADATA_FILENAME
static const constexpr char * METADATA_FILENAME
Definition: DiskStorageMixin.h:69
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::aron::data::detail::SpecializedVariantBase< data::dto::Dict, Dict >::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:134
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:47
Dict.h
armarx::armem::server::ltm::EntityInstance::_loadAllReferences
void _loadAllReferences(armem::wm::EntitySnapshot &) const override
Definition: EntityInstance.cpp:29
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::to_aron
void to_aron(aron::data::DictPtr &metadata, aron::data::DictPtr &data, const wm::EntityInstance &)
convert to metadata and aron instance
Definition: aron_conversions.cpp:44
armarx::armem::server::ltm::detail::EntityInstanceBase::ltm_mutex
std::recursive_mutex ltm_mutex
Definition: EntityInstanceBase.h:71
EntityInstance.h
armarx::armem::server::ltm::detail::EntityInstanceBase::Statistics::recordedData
long recordedData
Definition: EntityInstanceBase.h:20
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::readDataFromFile
std::vector< unsigned char > readDataFromFile(const std::string &filename) const
Definition: DiskStorageMixin.cpp:215
filename
std::string filename
Definition: VisualizationRobot.cpp:86
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::armem::wm::EntitySnapshot
Client-side working memory entity snapshot.
Definition: memory_definitions.h:80
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:25
armarx::armem::server::ltm
Definition: forward_declarations.h:20
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::armem::from_aron
void from_aron(const aron::data::DictPtr &metadata, const aron::data::DictPtr &data, wm::EntityInstance &)
convert from metadata and data aron instance
Definition: aron_conversions.cpp:15
armarx::armem::server::ltm::detail::MemoryItem::id
MemoryID id() const
Definition: MemoryItem.cpp:37
TimeUtil.h
armarx::armem::server::ltm::detail::EntityInstanceBase::Statistics::recordedMetaData
long recordedMetaData
Definition: EntityInstanceBase.h:21
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::fullPathExists
bool fullPathExists() const
Definition: DiskStorageMixin.cpp:113
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:53
armarx::armem::server::ltm::detail::mixin::MongoDBSettings
Definition: MongoDBStorageMixin.h:15
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::armem::server::ltm::EntityInstance::EntityInstance
EntityInstance(const detail::mixin::Path &, const detail::mixin::MongoDBSettings &, const std::string &, const MemoryID &id, const std::shared_ptr< Processors > &p)
Definition: EntityInstance.cpp:15
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::ltm::detail::EntityInstanceBase::statistics
Statistics statistics
Definition: EntityInstanceBase.h:73
armarx::armem::server::ltm::EntityInstance::_resolve
void _resolve(armem::wm::EntityInstance &) const override
Definition: EntityInstance.cpp:47
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::server::ltm::util::fs::fileExists
bool fileExists(const std::filesystem::path &p)
Definition: filesystem.cpp:139
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getFullPath
Path getFullPath() const
Definition: DiskStorageMixin.cpp:89
armarx::armem::wm::EntityInstanceBase
base::EntityInstanceBase< AronDtoT, EntityInstanceMetadata > EntityInstanceBase
Entity instance with a concrete ARON DTO type as data.
Definition: memory_definitions.h:74
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::writeDataToFile
void writeDataToFile(const std::string &filename, const std::vector< unsigned char > &data) const
Definition: DiskStorageMixin.cpp:200
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFullPathExists
void ensureFullPathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:133