Writer.cpp
Go to the documentation of this file.
1 #include "Writer.h"
2 
5 
6 #include "../error.h"
7 
8 
9 namespace armarx::armem::client
10 {
11 
12  Writer::Writer(server::WritingMemoryInterfacePrx memory) : memory(memory)
13  {
14  }
15 
16  data::AddSegmentResult Writer::addSegment(const std::string& coreSegmentName, const std::string& providerSegmentName, bool clearWhenExists)
17  {
18  data::AddSegmentInput input;
19  input.coreSegmentName = coreSegmentName;
20  input.providerSegmentName = providerSegmentName;
21  input.clearWhenExists = clearWhenExists;
22  return addSegment(input);
23  }
24 
25  data::AddSegmentResult Writer::addSegment(const MemoryID& providerSegmentID, bool clearWhenExists)
26  {
27  return addSegment(providerSegmentID.coreSegmentName, providerSegmentID.providerSegmentName, clearWhenExists);
28  }
29 
30  data::AddSegmentResult Writer::addSegment(const std::pair<std::string, std::string>& names, bool clearWhenExists)
31  {
32  return addSegment(names.first, names.second, clearWhenExists);
33  }
34 
35  data::AddSegmentResult Writer::addSegment(const data::AddSegmentInput& input)
36  {
37  data::AddSegmentsResult results = addSegments({input});
38  ARMARX_CHECK_EQUAL(results.size(), 1);
39  return results.at(0);
40  }
41 
42  data::AddSegmentsResult Writer::addSegments(const data::AddSegmentsInput& inputs)
43  {
45  data::AddSegmentsResult results = memory->addSegments(inputs);
46  ARMARX_CHECK_EQUAL(results.size(), inputs.size());
47  return results;
48  }
49 
50 
52  {
54 
55  data::Commit commitIce;
56  toIce(commitIce, commit);
57 
58  data::CommitResult resultIce = this->_commit(commitIce);
59 
60  armem::CommitResult result;
61  fromIce(resultIce, result);
62 
63  return result;
64  }
65 
66 
67  data::CommitResult Writer::commit(const data::Commit& _commit)
68  {
69  data::Commit commit = _commit;
70  return this->_commit(commit);
71  }
72 
73 
75  {
77  commit.updates.push_back(update);
78 
79  armem::CommitResult result = this->commit(commit);
80  ARMARX_CHECK_EQUAL(result.results.size(), 1);
81  return result.results.at(0);
82  }
83 
85  const MemoryID& entityID,
86  const std::vector<aron::data::DictPtr>& instancesData,
87  Time referencedTime)
88  {
90  update.entityID = entityID;
91  update.instancesData = instancesData;
92  update.referencedTime = referencedTime;
93  return commit(update);
94  }
95 
96  void
97  Writer::setWritingMemory(server::WritingMemoryInterfacePrx memory)
98  {
99  this->memory = memory;
100  }
101 
102  data::CommitResult Writer::_commit(data::Commit& commit)
103  {
105 
106  /*
107  * This function sets the `timeSent` of each `EntityUpdate` before
108  * sending the data. To allow that, `commit` needs to bo non-const.
109  * Otherwise, the function would need to make a copy of `commit`,
110  * which is not necessary in all cases; e.g. when called by
111  * `commit(Const Commit&)`, which converts the commit to ice types
112  * anyway.
113  */
114 
115  const Time timeSent = armem::Time::Now();
116  for (data::EntityUpdate& update : commit.updates)
117  {
118  toIce(update.timeSent, timeSent);
119  }
120 
121  data::CommitResult result;
122  auto handleError = [&commit, &result](const std::string & what)
123  {
124  for (const auto& _ : commit.updates)
125  {
126  (void) _;
127  data::EntityUpdateResult& r = result.results.emplace_back();
128  r.success = false;
129  r.errorMessage = "Memory component not registered.\n" + std::string(what);
130  }
131  };
132 
133  try
134  {
135  result = memory->commit(commit);
136  }
137  catch (const Ice::ConnectionRefusedException& e)
138  {
139  handleError(e.what());
140  }
141  catch (const Ice::ConnectionLostException& e)
142  {
143  handleError(e.what());
144  }
145  catch (const Ice::NotRegisteredException& e)
146  {
147  handleError(e.what());
148  }
149 
150  return result;
151  }
152 }
153 
154 
155 std::ostream& armarx::armem::data::operator<<(std::ostream& os, const AddSegmentInput& rhs)
156 {
157  return os << "AddSegmentInput: "
158  << "\n- core segment: \t'" << rhs.coreSegmentName << "'"
159  << "\n- provider segment: \t'" << rhs.providerSegmentName << "'"
160  << "\n- clear when exists:\t" << rhs.clearWhenExists << ""
161  << "\n";
162 }
163 std::ostream& armarx::armem::data::operator<<(std::ostream& os, const AddSegmentsInput& rhs)
164 {
165  for (size_t i = 0; i < rhs.size(); ++i)
166  {
167  os << "[" << i << "] " << rhs[i];
168  }
169  return os;
170 }
171 std::ostream& armarx::armem::data::operator<<(std::ostream& os, const AddSegmentResult& rhs)
172 {
173  return os << "AddSegmentResult:"
174  << "\n- success: \t" << rhs.success
175  << "\n- segment ID: \t'" << rhs.segmentID << "'"
176  << "\n- error message: \t" << rhs.errorMessage
177  << "\n";
178 }
179 std::ostream& armarx::armem::data::operator<<(std::ostream& os, const AddSegmentsResult& rhs)
180 {
181  for (size_t i = 0; i < rhs.size(); ++i)
182  {
183  os << "[" << i << "] " << rhs[i];
184  }
185  return os;
186 }
armarx::armem::client::fromIce
void fromIce(const armem::query::data::Input &ice, QueryInput &input)
Definition: Query.cpp:53
Writer.h
armarx::armem::MemoryID::providerSegmentName
std::string providerSegmentName
Definition: MemoryID.h:52
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:72
armarx::armem::client::Writer::setWritingMemory
void setWritingMemory(server::WritingMemoryInterfacePrx memory)
Definition: Writer.cpp:97
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::client::Writer::addSegment
data::AddSegmentResult addSegment(const std::string &coreSegmentName, const std::string &providerSegmentName, bool clearWhenExists=false)
Definition: Writer.cpp:16
armarx::armem::client
This file is part of ArmarX.
Definition: forward_declarations.h:7
ice_conversions.h
armarx::memory
Brief description of class memory.
Definition: memory.h:39
armarx::armem::MemoryID::coreSegmentName
std::string coreSegmentName
Definition: MemoryID.h:51
armarx::armem::data::operator<<
std::ostream & operator<<(std::ostream &os, const AddSegmentInput &rhs)
Definition: Writer.cpp:155
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit)
Writes a Commit to the memory.
Definition: Writer.cpp:51
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::CommitResult
Result of a Commit.
Definition: Commit.h:110
armarx::armem::client::Writer::Writer
Writer(const Writer &)=default
Construct a memory writer.
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
ExpressionException.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
armarx::armem::CommitResult::results
std::vector< EntityUpdateResult > results
Definition: Commit.h:112
armarx::armem::client::Writer::addSegments
data::AddSegmentsResult addSegments(const data::AddSegmentsInput &input)
Definition: Writer.cpp:42
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::armem::client::toIce
void toIce(armem::query::data::Input &ice, const QueryInput &input)
Definition: Query.cpp:47
armarx::armem::client::Writer::memory
server::WritingMemoryInterfacePrx memory
Definition: Writer.h:72