Writer.cpp
Go to the documentation of this file.
1 #include "Writer.h"
2 
3 #include <mutex>
4 #include <optional>
5 
6 #include <IceUtil/Time.h>
7 
8 #include <SimoxUtility/algorithm/get_map_keys_values.h>
9 
12 
17 #include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
18 #include <RobotAPI/libraries/armem_robot_state/aron/RobotDescription.aron.generated.h>
21 
23 {
25  memoryNameSystem(memoryNameSystem)
26  {
27  }
28 
29  void
31  {
32  ARMARX_DEBUG << "Writer: registerPropertyDefinitions";
33 
34  const std::string prefix = propertyPrefix;
35 
36  def->optional(properties.memoryName, prefix + "MemoryName");
37 
38  def->optional(properties.coreAttachmentsSegmentName,
39  prefix + "CoreSegment",
40  "Name of the memory core segment to use for object attachments.");
41  def->optional(properties.providerName, prefix + "ProviderName");
42  }
43 
44  void
46  {
47  // Wait for the memory to become available and add it as dependency.
48  ARMARX_IMPORTANT << "Writer: Waiting for memory '" << properties.memoryName << "' ...";
49  try
50  {
51  memoryWriter = memoryNameSystem.useWriter(properties.memoryName);
52  memoryReader = memoryNameSystem.useReader(properties.memoryName);
53  ARMARX_IMPORTANT << "Writer: Connected to memory '" << properties.memoryName << "'";
54  }
56  {
57  ARMARX_ERROR << e.what();
58  return;
59  }
60  }
61 
62  std::optional<armem::MemoryID>
63  Writer::commit(const ObjectAttachment& attachment)
64  {
65  std::lock_guard g{memoryWriterMutex};
66 
67  const auto result =
68  memoryWriter.addSegment(properties.coreAttachmentsSegmentName, properties.providerName);
69 
70  if (not result.success)
71  {
72  ARMARX_ERROR << "Creating core segment failed. Reason: " << result.errorMessage;
73  return std::nullopt;
74  }
75 
76  const auto& timestamp = attachment.timestamp;
77 
78  const auto providerId = armem::MemoryID(result.segmentID);
79  const auto entityID =
80  providerId
81  .withEntityName(attachment.object.entityName) // TODO check if meaningful
82  .withTimestamp(timestamp);
83 
85  update.entityID = entityID;
86 
87  arondto::attachment::ObjectAttachment aronAttachment;
88  toAron(aronAttachment, attachment);
89 
90  update.instancesData = {aronAttachment.toAron()};
91  update.referencedTime = timestamp;
92 
93  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
94  armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
95 
96  ARMARX_DEBUG << updateResult;
97 
98  if (not updateResult.success)
99  {
100  ARMARX_ERROR << updateResult.errorMessage;
101  return std::nullopt;
102  }
103 
104  return updateResult.snapshotID;
105  }
106 
107  std::optional<armem::MemoryID>
109  {
110  std::lock_guard g{memoryWriterMutex};
111 
112  const auto result =
113  memoryWriter.addSegment(properties.coreAttachmentsSegmentName, properties.providerName);
114 
115  if (not result.success)
116  {
117  ARMARX_ERROR << "Creating core segment failed. Reason: " << result.errorMessage;
118  return std::nullopt;
119  }
120 
121  const auto& timestamp = attachment.timestamp;
122 
123  const auto providerId = armem::MemoryID(result.segmentID);
124  const auto entityID =
125  providerId
126  .withEntityName(attachment.object.id.entityName) // TODO check if meaningful
127  .withTimestamp(timestamp);
128 
130  update.entityID = entityID;
131 
132  arondto::attachment::ArticulatedObjectAttachment aronAttachment;
133  toAron(aronAttachment, attachment);
134 
135  update.instancesData = {aronAttachment.toAron()};
136  update.referencedTime = timestamp;
137 
138  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
139  armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
140 
141  ARMARX_DEBUG << updateResult;
142 
143  if (not updateResult.success)
144  {
145  ARMARX_ERROR << updateResult.errorMessage;
146  return std::nullopt;
147  }
148 
149  return updateResult.snapshotID;
150  }
151 
152 
153 } // namespace armarx::armem::attachment
armarx::armem::attachment::ObjectAttachment::object
ObjectID object
Definition: types.h:106
armarx::armem::client::Writer::addSegment
data::AddSegmentResult addSegment(const std::string &coreSegmentName, const std::string &providerSegmentName, bool clearWhenExists=false) const
Definition: Writer.cpp:16
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::armem::attachment::ObjectAttachment
ObjectAttachment describes a fixed transformation between an agent and an object.
Definition: types.h:100
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:72
MemoryID.h
armarx::armem::attachment::ArticulatedObjectAttachment::object
AgentDescription object
Definition: types.h:127
armarx::armem::toAron
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
Definition: aron_conversions.cpp:19
armarx::armem::client::MemoryNameSystem::useWriter
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
Definition: MemoryNameSystem.cpp:276
armarx::armem::attachment::Writer::connect
void connect()
Definition: Writer.cpp:45
armarx::armem::attachment::ArticulatedObjectAttachment
ArticulatedObjectAttachment describes a fixed transformation between an agent and an articulated obje...
Definition: types.h:121
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
error.h
armarx::armem::client::MemoryNameSystem::useReader
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Definition: MemoryNameSystem.cpp:184
armarx::armem::attachment::Writer::commit
std::optional< armem::MemoryID > commit(const ObjectAttachment &attachment)
Definition: Writer.cpp:63
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
aron_conversions.h
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
armarx::armem::attachment
Definition: Reader.cpp:22
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::MemoryID::entityName
std::string entityName
Definition: MemoryID.h:53
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
aron_conversions.h
PropertyDefinitionContainer.h
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::attachment::ObjectAttachment::timestamp
armem::Time timestamp
Definition: types.h:108
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::armem::attachment::Writer::Writer
Writer(armem::client::MemoryNameSystem &memoryNameSystem)
Definition: Writer.cpp:24
Logging.h
robot_conversions.h
aron_conversions.h
armarx::armem::attachment::Writer::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: Writer.cpp:30
armarx::armem::attachment::AgentDescription::id
AgentID id
id either pointing to a arondto::Robot or arondto::ArticulatedObject
Definition: types.h:87
armarx::armem::attachment::ArticulatedObjectAttachment::timestamp
armem::Time timestamp
Definition: types.h:129
Writer.h
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29