EntityInstance.cpp
Go to the documentation of this file.
1 // Header
2 #include "EntityInstance.h"
3 
4 // STD / STL
5 #include <fstream>
6 #include <iostream>
7 
8 // ArmarX
11 
13 
15 {
18 
19  const std::string& exportName,
20  const armem::MemoryID& id /* UNESCAPED */,
21  const std::shared_ptr<Processors>& filters) :
22  EntityInstanceBase(exportName, id, filters),
23  DiskMemoryItemMixin(p, exportName, id),
24  MongoDBStorageMixin(s, exportName, id)
25  {
26  //start();
27  }
28 
29  void
31  {
32  std::lock_guard l(ltm_mutex);
33  // assure that we put the right index to the snapshot
34  //ARMARX_CHECK(e.size() == (size_t)id().instanceIndex);
35  //int index = e.getInstanceIndices().size();
36 
37  //ARMARX_INFO << VAROUT(getFullPath());
38  //ARMARX_INFO << VAROUT(getMemoryID().getEntityInstanceID().getLeafItem());
39  int index = std::stoi(getMemoryID().getEntityInstanceID().getLeafItem());
40 
42 
43  // add instance. Do not set data, since we only return references
44  e.addInstance(i);
45  }
46 
47  void
49  {
50  std::lock_guard l(ltm_mutex);
51  //ARMARX_INFO << "resolve for entity instance " << e.id().str();
52  auto& dictConverter = processors->defaultObjectConverter;
53 
54  aron::data::DictPtr datadict = nullptr;
55  aron::data::DictPtr metadatadict = nullptr;
56 
57  if (fullPathExists())
58  {
59 
60  std::string dataFilename = (DATA_FILENAME + dictConverter.suffix);
61  std::string metadataFilename = (METADATA_FILENAME + dictConverter.suffix);
62  std::filesystem::path dataPath = getFullPath() / dataFilename;
63  std::filesystem::path metadataPath = getFullPath() / metadataFilename;
64 
65  if (util::fs::fileExists(dataPath))
66  {
67  //ARMARX_INFO << "Convert data for entity " << id();
68  auto datafilecontent = readDataFromFile(dataFilename);
69  auto dataaron = dictConverter.convert({datafilecontent, ""}, {});
70  datadict = aron::data::Dict::DynamicCastAndCheck(dataaron);
71  }
72  else
73  {
74  ARMARX_ERROR << "Could not find the data file '" << dataPath.string()
75  << "'. Continuing without data.";
76  }
77 
78  //ARMARX_INFO << "Convert metadata for entity " << id();
79  if (util::fs::fileExists(metadataPath))
80  {
81  auto metadatafilecontent = readDataFromFile(metadataFilename);
82  auto metadataaron = dictConverter.convert({metadatafilecontent, ""}, {});
83  metadatadict = aron::data::Dict::DynamicCastAndCheck(metadataaron);
84  }
85  else
86  {
87  ARMARX_ERROR << "Could not find the metadata file '" << metadataPath.string()
88  << "'. Continuing without metadata.";
89  }
90  }
91 
92  // check for special members TODO: only allowed for direct children?
93  auto allFilesInIndexFolder = getAllFiles();
94  for (const auto& [key, m] : datadict->getElements())
95  {
96  for (auto& f : processors->converters)
97  {
98  // iterate over all files and search for matching ones.
99  // We cannot simply check for the existence of a file because we do not know the
100  // mode (filename = memberName.mode.suffix)
101  for (const auto& filepath : allFilesInIndexFolder)
102  {
103  if (simox::alg::starts_with(filepath.filename(), key) and
104  simox::alg::ends_with(filepath, f->suffix))
105  {
106  std::string mode = simox::alg::remove_suffix(
107  simox::alg::remove_prefix(filepath.filename(), key), f->suffix);
108 
109  auto memberfilecontent = readDataFromFile(filepath.filename());
110  auto memberaron = f->convert(
111  {memberfilecontent, mode},
112  armarx::aron::Path(datadict->getPath(), std::vector<std::string>{key}));
113  datadict->setElement(key, memberaron);
114  break;
115  }
116  }
117  }
118  }
119 
120  from_aron(metadatadict, datadict, e);
121  }
122 
123  nlohmann::json
125  {
126  std::lock_guard l(ltm_mutex);
127  if (id().instanceIndex < 0)
128  {
130  << "During storage of segment '" << e.id().str()
131  << "' I noticed that the corresponding LTM has no id set. "
132  << "I set the id of the LTM to the same name, however this should not happen!";
133  id().timestamp = e.id().timestamp;
134  }
135 
136  auto& dictConverter = processors->defaultObjectConverter;
137 
138  /*if (!connected())
139  {
140  ARMARX_WARNING << "LTM ENTITY INSTANCE NOT CONNECTED ALTHOUGH ENABLED " << id().str();
141  return {};
142  }*/
143 
144  ensureFullPathExists(true);
145 
146  // data
147  auto dataAron = std::make_shared<aron::data::Dict>();
148  auto metadataAron = std::make_shared<aron::data::Dict>();
149  to_aron(metadataAron, dataAron, e);
150 
151  std::shared_ptr<aron::data::Dict> source;
152 
153  bool saveAndExtract = 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:67
armarx::armem::MemoryID::timestamp
Time timestamp
Definition: MemoryID.h:54
armarx::armem::server::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:64
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:124
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:137
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:167
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::METADATA_FILENAME
static const constexpr char * METADATA_FILENAME
Definition: DiskStorageMixin.h:68
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::aron::data::detail::SpecializedVariantBase< data::dto::Dict, Dict >::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:135
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
Dict.h
armarx::armem::server::ltm::EntityInstance::_loadAllReferences
void _loadAllReferences(armem::wm::EntitySnapshot &) const override
Definition: EntityInstance.cpp:30
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:39
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:153
filename
std::string filename
Definition: VisualizationRobot.cpp:83
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
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:681
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
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:16
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:110
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
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:16
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
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:48
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:136
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::getFullPath
Path getFullPath() const
Definition: DiskStorageMixin.cpp:87
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:145
armarx::armem::server::ltm::detail::mixin::DiskMemoryItemMixin::ensureFullPathExists
void ensureFullPathExists(bool createIfNotExistent=false) const
Definition: DiskStorageMixin.cpp:130