Writer.cpp
Go to the documentation of this file.
1 #include "Writer.h"
2 
3 #include <IceUtil/Time.h>
4 #include <SimoxUtility/algorithm/get_map_keys_values.h>
5 #include <mutex>
6 #include <optional>
7 
9 
14 #include <RobotAPI/libraries/armem_robot/aron/RobotDescription.aron.generated.h>
15 #include <RobotAPI/libraries/armem_robot/aron/Robot.aron.generated.h>
18 
19 
21 {
23  memoryNameSystem(memoryNameSystem)
24  {}
25 
27  {
28  ARMARX_DEBUG << "Writer: registerPropertyDefinitions";
29 
30  const std::string prefix = propertyPrefix;
31 
32  def->optional(properties.memoryName, prefix + "MemoryName");
33 
34  def->optional(properties.coreAttachmentsSegmentName,
35  prefix + "CoreSegment",
36  "Name of the memory core segment to use for object attachments.");
37  def->optional(properties.providerName, prefix + "ProviderName");
38  }
39 
41  {
42  // Wait for the memory to become available and add it as dependency.
43  ARMARX_IMPORTANT << "Writer: Waiting for memory '" << properties.memoryName << "' ...";
44  try
45  {
46  memoryWriter = memoryNameSystem.useWriter(properties.memoryName);
47  memoryReader = memoryNameSystem.useReader(properties.memoryName);
48  ARMARX_IMPORTANT << "Writer: Connected to memory '" << properties.memoryName << "'";
49  }
51  {
52  ARMARX_ERROR << e.what();
53  return;
54  }
55  }
56 
57 
58  std::optional<armem::MemoryID> Writer::commit(const ObjectAttachment& attachment)
59  {
60  std::lock_guard g{memoryWriterMutex};
61 
62  const auto result = memoryWriter.addSegment(properties.coreAttachmentsSegmentName, properties.providerName);
63 
64  if (not result.success)
65  {
66  ARMARX_ERROR << "Creating core segment failed. Reason: " << result.errorMessage;
67  return std::nullopt;
68  }
69 
70  const auto& timestamp = attachment.timestamp;
71 
72  const auto providerId = armem::MemoryID(result.segmentID);
73  const auto entityID =
74  providerId
75  .withEntityName(attachment.object.entityName) // TODO check if meaningful
76  .withTimestamp(timestamp);
77 
79  update.entityID = entityID;
80 
81  arondto::attachment::ObjectAttachment aronAttachment;
82  toAron(aronAttachment, attachment);
83 
84  update.instancesData = {aronAttachment.toAron()};
85  update.referencedTime = timestamp;
86 
87  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
88  armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
89 
90  ARMARX_DEBUG << updateResult;
91 
92  if (not updateResult.success)
93  {
94  ARMARX_ERROR << updateResult.errorMessage;
95  return std::nullopt;
96  }
97 
98  return updateResult.snapshotID;
99 
100  }
101 
102  std::optional<armem::MemoryID> Writer::commit(const ArticulatedObjectAttachment& attachment)
103  {
104  std::lock_guard g{memoryWriterMutex};
105 
106  const auto result = memoryWriter.addSegment(properties.coreAttachmentsSegmentName, properties.providerName);
107 
108  if (not result.success)
109  {
110  ARMARX_ERROR << "Creating core segment failed. Reason: " << result.errorMessage;
111  return std::nullopt;
112  }
113 
114  const auto& timestamp = attachment.timestamp;
115 
116  const auto providerId = armem::MemoryID(result.segmentID);
117  const auto entityID =
118  providerId
119  .withEntityName(attachment.object.id.entityName) // TODO check if meaningful
120  .withTimestamp(timestamp);
121 
123  update.entityID = entityID;
124 
125  arondto::attachment::ArticulatedObjectAttachment aronAttachment;
126  toAron(aronAttachment, attachment);
127 
128  update.instancesData = {aronAttachment.toAron()};
129  update.referencedTime = timestamp;
130 
131  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
132  armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
133 
134  ARMARX_DEBUG << updateResult;
135 
136  if (not updateResult.success)
137  {
138  ARMARX_ERROR << updateResult.errorMessage;
139  return std::nullopt;
140  }
141 
142  return updateResult.snapshotID;
143  }
144 
145 
146 
147 } // namespace armarx::armem::attachment
armarx::armem::attachment::ObjectAttachment::object
ObjectID object
Definition: types.h:106
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::client::Writer::addSegment
data::AddSegmentResult addSegment(const std::string &coreSegmentName, const std::string &providerSegmentName, bool clearWhenExists=false)
Definition: Writer.cpp:16
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:40
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit)
Writes a Commit to the memory.
Definition: Writer.cpp:51
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:58
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::attachment
Definition: Reader.cpp:22
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
aron_conversions.h
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
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:22
Logging.h
robot_conversions.h
aron_conversions.h
armarx::armem::attachment::Writer::registerPropertyDefinitions
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition: Writer.cpp:26
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