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