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,
96  const std::string& origin) const
97  {
99  update.entityID = entityID;
100  update.instancesData = instancesData;
101  update.referencedTime = referencedTime;
102  update.origin = origin;
103  return commit(update);
104  }
105 
107  {
108  memory->clearWorkingMemory();
109  }
110 
111  void
112  Writer::setWritingMemory(server::WritingMemoryInterfacePrx memory)
113  {
114  this->memory = memory;
115  }
116 
117  data::CommitResult
118  Writer::_commit(data::Commit& commit) const
119  {
121 
122  /*
123  * This function sets the `timeSent` of each `EntityUpdate` before
124  * sending the data. To allow that, `commit` needs to bo non-const.
125  * Otherwise, the function would need to make a copy of `commit`,
126  * which is not necessary in all cases; e.g. when called by
127  * `commit(Const Commit&)`, which converts the commit to ice types
128  * anyway.
129  */
130 
131  const Time timeSent = armem::Time::Now();
132  for (data::EntityUpdate& update : commit.updates)
133  {
134  toIce(update.timeSent, timeSent);
135  }
136 
137  data::CommitResult result;
138  auto handleError = [&commit, &result](const std::string& what)
139  {
140  for (const auto& _ : commit.updates)
141  {
142  (void)_;
143  data::EntityUpdateResult& r = result.results.emplace_back();
144  r.success = false;
145  r.errorMessage = "Memory component not registered.\n" + std::string(what);
146  }
147  };
148 
149  try
150  {
151  result = memory->commit(commit);
152  }
153  catch (const Ice::ConnectionRefusedException& e)
154  {
155  handleError(e.what());
156  }
157  catch (const Ice::ConnectionLostException& e)
158  {
159  handleError(e.what());
160  }
161  catch (const Ice::NotRegisteredException& e)
162  {
163  handleError(e.what());
164  }
165 
166  return result;
167  }
168 } // namespace armarx::armem::client
169 
170 std::ostream&
171 armarx::armem::data::operator<<(std::ostream& os, const AddSegmentInput& rhs)
172 {
173  return os << "AddSegmentInput: "
174  << "\n- core segment: \t'" << rhs.coreSegmentName << "'"
175  << "\n- provider segment: \t'" << rhs.providerSegmentName << "'"
176  << "\n- clear when exists:\t" << rhs.clearWhenExists << ""
177  << "\n";
178 }
179 
180 std::ostream&
181 armarx::armem::data::operator<<(std::ostream& os, const AddSegmentsInput& rhs)
182 {
183  for (size_t i = 0; i < rhs.size(); ++i)
184  {
185  os << "[" << i << "] " << rhs[i];
186  }
187  return os;
188 }
189 
190 std::ostream&
191 armarx::armem::data::operator<<(std::ostream& os, const AddSegmentResult& rhs)
192 {
193  return os << "AddSegmentResult:"
194  << "\n- success: \t" << rhs.success << "\n- segment ID: \t'" << rhs.segmentID
195  << "'"
196  << "\n- error message: \t" << rhs.errorMessage << "\n";
197 }
198 
199 std::ostream&
200 armarx::armem::data::operator<<(std::ostream& os, const AddSegmentsResult& rhs)
201 {
202  for (size_t i = 0; i < rhs.size(); ++i)
203  {
204  os << "[" << i << "] " << rhs[i];
205  }
206  return os;
207 }
armarx::armem::client::fromIce
void fromIce(const armem::query::data::Input &ice, QueryInput &input)
Definition: Query.cpp:56
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:51
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:74
armarx::armem::client::Writer::setWritingMemory
void setWritingMemory(server::WritingMemoryInterfacePrx memory)
Definition: Writer.cpp:112
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: Configurator.cpp:5
ice_conversions.h
armarx::memory
Brief description of class memory.
Definition: memory.h:38
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:171
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:12
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:25
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:68
armarx::armem::client::Writer::clearWorkingMemory
void clearWorkingMemory()
Clears the servers working memory (uses the WM-Clear feature)
Definition: Writer.cpp:106
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:13
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:49
armarx::armem::client::Writer::memory
server::WritingMemoryInterfacePrx memory
Definition: Writer.h:82