MemoryToDebugObserver.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::MemoryToDebugObserver
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2023
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "MemoryToDebugObserver.h"
24 
30 
32 {
33 
35  const Services& services) :
36  properties(properties), services(services)
37  {
39  }
40 
42  {
43  public:
45  {
46  }
47 
48  // void visitAronVariant(const data::DictPtr&) override;
49  // void visitAronVariant(const data::ListPtr&) override;
50  // void visitAronVariant(const data::NDArrayPtr&) override;
51  void
53  {
54  setDatafield(v->getValue());
55  }
56 
57  void
59  {
60  setDatafield(v->getValue());
61  }
62 
63  void
65  {
66  setDatafield(v->getValue());
67  }
68 
69  void
71  {
72  setDatafield(v->getValue());
73  }
74 
75  void
77  {
78  setDatafield(v->getValue());
79  }
80 
81  void
83  {
84  setDatafield(v->getValue());
85  }
86 
87  template <class ValueT>
88  void
89  setDatafield(const ValueT& value)
90  {
92  ++count;
93  }
94 
96  std::string channelName;
97  std::string datafieldName;
98 
99  int count = 0;
100  };
101 
102  void
104  {
105  Visitor visitor(services.debugObserver);
106 
107  std::stringstream log;
108 
109  // Group by memory segment to reduce number of queries.
110  std::map<MemoryID, std::vector<const MemoryValueID*>> valuesPerProviderSegment;
111  for (const MemoryValueID& valueId : properties.plottedValues)
112  {
113  valuesPerProviderSegment[valueId.entityID.getProviderSegmentID()].push_back(&valueId);
114  }
115 
116  for (const auto& [providerSegmentID, values] : valuesPerProviderSegment)
117  {
118  armem::client::Reader* reader = nullptr;
119  try
120  {
121  reader = getReader(providerSegmentID);
122  }
124  {
125  log << "\n" << e.what();
126  continue;
127  }
128  ARMARX_CHECK_NOT_NULL(reader);
129 
130  const QueryResult result = reader->getLatestSnapshotsIn(providerSegmentID);
131  if (not result.success)
132  {
133  log << "Query to provider segment " << providerSegmentID
134  << " failed: " << result.errorMessage;
135  continue;
136  }
137 
138  for (const MemoryValueID* valueId : values)
139  {
140  const wm::Entity* entity = result.memory.findEntity(valueId->entityID);
141  if (entity == nullptr)
142  {
143  log << "\nDid not find entity " << valueId->entityID << " in provider segment "
144  << providerSegmentID << ".";
145  continue;
146  }
147 
148  const wm::EntityInstance& instance = entity->getLatestInstance();
149  aron::data::VariantPtr valueVariant =
150  instance.data()->navigateAbsolute(valueId->aronPath);
151  if (not valueVariant)
152  {
153  log << "\nDid not find " << valueId->aronPath.toString()
154  << " in entity instance " << instance.id() << ".";
155  continue;
156  }
157 
158  visitor.channelName = makeChannelName(valueId->entityID);
159  visitor.datafieldName = makeDatafieldName(valueId->aronPath);
160 
161  aron::data::visit(visitor, valueVariant);
162  }
163  }
164 
166 
167  if (not log.str().empty())
168  {
170  << "Encountered issues while sending memory values to the debug observer "
171  "for plotting: "
172  << log.str();
173  }
174  }
175 
176  std::string
177  MemoryToDebugObserver::makeChannelName(const MemoryID& memoryID)
178  {
179  return simox::alg::replace_all(memoryID.str(), "/", ">");
180  }
181 
182  std::string
183  MemoryToDebugObserver::makeDatafieldName(const aron::Path& path)
184  {
185  // The first element is always "ARON->", which we can discard for readability.
186  std::string str = path.toString();
187  str = simox::alg::remove_prefix(str, path.getRootIdentifier() + path.getDelimeter());
188  str = simox::alg::replace_all(str, path.getDelimeter(), ">");
189  return str;
190  }
191 
192  Reader*
193  MemoryToDebugObserver::getReader(const MemoryID& memoryID)
194  {
195  auto it = memoryReaders.find(memoryID);
196  if (it != memoryReaders.end())
197  {
198  return &it->second;
199  }
200  else
201  {
203 
204  auto [it, _] = memoryReaders.emplace(memoryID, reader);
205  return &it->second;
206  }
207  }
208 
209 } // namespace armarx::armem::client::util
210 
211 namespace armarx::armem::client
212 {
213 
214  void
215  util::to_json(simox::json::json& j, const MemoryValueID& id)
216  {
217  j["entityID"] = id.entityID.getItems();
218  j["aronPath"] = id.aronPath.getPath();
219  }
220 
221  void
222  util::from_json(const simox::json::json& j, MemoryValueID& id)
223  {
224  id.entityID = MemoryID::fromItems(j.at("entityID").get<std::vector<std::string>>());
225  id.aronPath = {j.at("aronPath").get<std::vector<std::string>>()};
226  }
227 
228  void
229  util::to_json(simox::json::json& j, const MemoryToDebugObserver::Properties& p)
230  {
231  j["plottedValues"] = p.plottedValues;
232  }
233 
234  void
235  util::from_json(const simox::json::json& j, MemoryToDebugObserver::Properties& p)
236  {
237  j.at("plottedValues").get_to(p.plottedValues);
238  }
239 
240 
241 } // namespace armarx::armem::client
armarx::armem::client::util::Visitor::datafieldName
std::string datafieldName
Definition: MemoryToDebugObserver.cpp:97
armarx::armem::detail::SuccessHeader::success
bool success
Definition: SuccessHeader.h:20
armarx::armem::client::Reader
Reads data from a memory server.
Definition: Reader.h:24
Variant.h
armarx::armem::client::util::MemoryToDebugObserver::MemoryToDebugObserver
MemoryToDebugObserver(const Properties &properties, const Services &services)
Constructor.
Definition: MemoryToDebugObserver.cpp:34
armarx::aron::data::ConstVariantVisitor
Definition: VariantVisitor.h:39
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::armem::client::QueryResult::memory
wm::Memory memory
The slice of the memory that matched the query.
Definition: Query.h:58
armarx::armem::client::Reader::getLatestSnapshotsIn
QueryResult getLatestSnapshotsIn(const MemoryID &id, armem::query::DataMode dataMode=armem::query::DataMode::WithData) const
Get the latest snapshots under the given memory ID.
Definition: Reader.cpp:343
armarx::armem::client::util::MemoryToDebugObserver::Services::memoryNameSystem
MemoryNameSystem & memoryNameSystem
Definition: MemoryToDebugObserver.h:69
armarx::armem::base::detail::GetLatestInstanceMixin::getLatestInstance
auto & getLatestInstance(int instanceIndex=0)
Retrieve the latest entity instance.
Definition: lookup_mixins.h:163
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
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::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::FloatPtr &v) override
Definition: MemoryToDebugObserver.cpp:64
armarx::DebugObserverHelper::sendDebugObserverBatch
void sendDebugObserverBatch()
Definition: DebugObserverHelper.cpp:63
armarx::aron::Path::getDelimeter
std::string getDelimeter() const
Definition: Path.cpp:73
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem::client::util::MemoryToDebugObserver::Properties::plottedValues
std::vector< MemoryValueID > plottedValues
Definition: MemoryToDebugObserver.h:61
armarx::aron::data::LongPtr
std::shared_ptr< Long > LongPtr
Definition: forward_declarations.h:26
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
mns.h
armarx::armem::client
This file is part of ArmarX.
Definition: forward_declarations.h:7
armarx::aron::data::DoublePtr
std::shared_ptr< Double > DoublePtr
Definition: forward_declarations.h:32
armarx::armem::client::util::to_json
void to_json(simox::json::json &j, const MemoryValueID &id)
Definition: MemoryToDebugObserver.cpp:215
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::StringPtr &v) override
Definition: MemoryToDebugObserver.cpp:82
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::DoublePtr &v) override
Definition: MemoryToDebugObserver.cpp:70
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::armem::client::util::MemoryValueID
ID of an ARON value in the memory.
Definition: MemoryToDebugObserver.h:39
armarx::DebugObserverHelper::setDebugObserverBatchModeEnabled
void setDebugObserverBatchModeEnabled(bool enable)
Definition: DebugObserverHelper.cpp:58
json_conversions.h
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::armem::base::detail::GetFindEntityMixin::findEntity
auto * findEntity(const MemoryID &entityID)
Find an entity.
Definition: lookup_mixins.h:425
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::BoolPtr &v) override
Definition: MemoryToDebugObserver.cpp:76
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::armem::detail::SuccessHeader::errorMessage
std::string errorMessage
Definition: SuccessHeader.h:21
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::IntPtr &v) override
Definition: MemoryToDebugObserver.cpp:52
armarx::armem::client::util::Visitor::setDatafield
void setDatafield(const ValueT &value)
Definition: MemoryToDebugObserver.cpp:89
MemoryToDebugObserver.h
armarx::armem::client::util::MemoryToDebugObserver::Services::debugObserver
armarx::DebugObserverHelper & debugObserver
Definition: MemoryToDebugObserver.h:70
armarx::armem::MemoryID::fromItems
static MemoryID fromItems(const std::vector< std::string > &items)
Constructor memory ID from items as returned by getItems().
Definition: MemoryID.cpp:189
armarx::armem::client::util::from_json
void from_json(const simox::json::json &j, MemoryValueID &id)
Definition: MemoryToDebugObserver.cpp:222
armarx::armem::client::util::MemoryToDebugObserver::pollOnce
void pollOnce()
Query values from the memory and send them to the debug observer.
Definition: MemoryToDebugObserver.cpp:103
armarx::armem::client::util::MemoryToDebugObserver::Properties
Configuration.
Definition: MemoryToDebugObserver.h:59
armarx::armem::MemoryID::getProviderSegmentID
MemoryID getProviderSegmentID() const
Definition: MemoryID.cpp:297
armarx::armem::client::util::Visitor::debugObserver
armarx::DebugObserverHelper & debugObserver
Definition: MemoryToDebugObserver.cpp:95
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::LongPtr &v) override
Definition: MemoryToDebugObserver.cpp:58
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::DebugObserverHelper::setDebugObserverDatafield
void setDebugObserverDatafield(const std::string &channelName, const std::string &datafieldName, const TimedVariantPtr &value) const
Definition: DebugObserverHelper.cpp:74
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VariantVisitor.h
armarx::armem::client::util::Visitor::Visitor
Visitor(armarx::DebugObserverHelper &debugObserver)
Definition: MemoryToDebugObserver.cpp:44
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:29
armarx::armem::client::util
Definition: MemoryListener.cpp:13
armarx::armem::client::util::MemoryValueID::entityID
armem::MemoryID entityID
Definition: MemoryToDebugObserver.h:41
armarx::aron::data::visit
requires isVisitor< VisitorImplementation, typename VisitorImplementation::Input > void visit(VisitorImplementation &v, typename VisitorImplementation::Input &o)
Definition: Visitor.h:124
armarx::aron::data::FloatPtr
std::shared_ptr< Float > FloatPtr
Definition: forward_declarations.h:29
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::aron::data::StringPtr
std::shared_ptr< String > StringPtr
Definition: forward_declarations.h:35
armarx::aron::Path::getRootIdentifier
std::string getRootIdentifier() const
Definition: Path.cpp:61
armarx::armem::client::util::Visitor
Definition: MemoryToDebugObserver.cpp:41
armarx::armem::client::util::MemoryToDebugObserver::Services
Required services.
Definition: MemoryToDebugObserver.h:67
armarx::armem::client::util::Visitor::count
int count
Definition: MemoryToDebugObserver.cpp:99
armarx::aron::data::IntPtr
std::shared_ptr< Int > IntPtr
Definition: forward_declarations.h:23
armarx::armem::client::MemoryNameSystem::getReader
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Definition: MemoryNameSystem.cpp:177
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:129
armarx::DebugObserverHelper
Brief description of class DebugObserverHelper.
Definition: DebugObserverHelper.h:50
armarx::armem::client::util::Visitor::channelName
std::string channelName
Definition: MemoryToDebugObserver.cpp:96
armarx::armem::client::util::MemoryValueID::aronPath
aron::Path aronPath
Definition: MemoryToDebugObserver.h:42
armarx::aron::Path::toString
std::string toString() const
Definition: Path.cpp:125
All.h
armarx::aron::data::BoolPtr
std::shared_ptr< Bool > BoolPtr
Definition: forward_declarations.h:38