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 
25 #include <SimoxUtility/algorithm/string/string_tools.h>
26 
32 
34 {
35 
37  const Services& services) :
38  properties(properties), services(services)
39  {
41  }
42 
44  {
45  public:
47  {
48  }
49 
50  // void visitAronVariant(const data::DictPtr&) override;
51  // void visitAronVariant(const data::ListPtr&) override;
52  // void visitAronVariant(const data::NDArrayPtr&) override;
53  void
55  {
56  setDatafield(v->getValue());
57  }
58 
59  void
61  {
62  setDatafield(v->getValue());
63  }
64 
65  void
67  {
68  setDatafield(v->getValue());
69  }
70 
71  void
73  {
74  setDatafield(v->getValue());
75  }
76 
77  void
79  {
80  setDatafield(v->getValue());
81  }
82 
83  void
85  {
86  setDatafield(v->getValue());
87  }
88 
89  template <class ValueT>
90  void
91  setDatafield(const ValueT& value)
92  {
94  ++count;
95  }
96 
98  std::string channelName;
99  std::string datafieldName;
100 
101  int count = 0;
102  };
103 
104  void
106  {
107  Visitor visitor(services.debugObserver);
108 
109  std::stringstream log;
110 
111  // Group by memory segment to reduce number of queries.
112  std::map<MemoryID, std::vector<const MemoryValueID*>> valuesPerProviderSegment;
113  for (const MemoryValueID& valueId : properties.plottedValues)
114  {
115  valuesPerProviderSegment[valueId.entityID.getProviderSegmentID()].push_back(&valueId);
116  }
117 
118  for (const auto& [providerSegmentID, values] : valuesPerProviderSegment)
119  {
120  armem::client::Reader* reader = nullptr;
121  try
122  {
123  reader = getReader(providerSegmentID);
124  }
126  {
127  log << "\n" << e.what();
128  continue;
129  }
130  ARMARX_CHECK_NOT_NULL(reader);
131 
132  const QueryResult result = reader->getLatestSnapshotsIn(providerSegmentID);
133  if (not result.success)
134  {
135  log << "Query to provider segment " << providerSegmentID
136  << " failed: " << result.errorMessage;
137  continue;
138  }
139 
140  for (const MemoryValueID* valueId : values)
141  {
142  const wm::Entity* entity = result.memory.findEntity(valueId->entityID);
143  if (entity == nullptr)
144  {
145  log << "\nDid not find entity " << valueId->entityID << " in provider segment "
146  << providerSegmentID << ".";
147  continue;
148  }
149 
150  const wm::EntityInstance& instance = entity->getLatestInstance();
151  aron::data::VariantPtr valueVariant =
152  instance.data()->navigateAbsolute(valueId->aronPath);
153  if (not valueVariant)
154  {
155  log << "\nDid not find " << valueId->aronPath.toString()
156  << " in entity instance " << instance.id() << ".";
157  continue;
158  }
159 
160  visitor.channelName = makeChannelName(valueId->entityID);
161  visitor.datafieldName = makeDatafieldName(valueId->aronPath);
162 
163  aron::data::visit(visitor, valueVariant);
164  }
165  }
166 
168 
169  if (not log.str().empty())
170  {
172  << "Encountered issues while sending memory values to the debug observer "
173  "for plotting: "
174  << log.str();
175  }
176  }
177 
178  std::string
179  MemoryToDebugObserver::makeChannelName(const MemoryID& memoryID)
180  {
181  return simox::alg::replace_all(memoryID.str(), "/", ">");
182  }
183 
184  std::string
185  MemoryToDebugObserver::makeDatafieldName(const aron::Path& path)
186  {
187  // The first element is always "ARON->", which we can discard for readability.
188  std::string str = path.toString();
189  str = simox::alg::remove_prefix(str, path.getRootIdentifier() + path.getDelimeter());
190  str = simox::alg::replace_all(str, path.getDelimeter(), ">");
191  return str;
192  }
193 
194  Reader*
195  MemoryToDebugObserver::getReader(const MemoryID& memoryID)
196  {
197  auto it = memoryReaders.find(memoryID);
198  if (it != memoryReaders.end())
199  {
200  return &it->second;
201  }
202  else
203  {
205 
206  auto [it, _] = memoryReaders.emplace(memoryID, reader);
207  return &it->second;
208  }
209  }
210 
211 } // namespace armarx::armem::client::util
212 
213 namespace armarx::armem::client
214 {
215 
216  void
217  util::to_json(simox::json::json& j, const MemoryValueID& id)
218  {
219  j["entityID"] = id.entityID.getItems();
220  j["aronPath"] = id.aronPath.getPath();
221  }
222 
223  void
224  util::from_json(const simox::json::json& j, MemoryValueID& id)
225  {
226  id.entityID = MemoryID::fromItems(j.at("entityID").get<std::vector<std::string>>());
227  id.aronPath = {j.at("aronPath").get<std::vector<std::string>>()};
228  }
229 
230  void
231  util::to_json(simox::json::json& j, const MemoryToDebugObserver::Properties& p)
232  {
233  j["plottedValues"] = p.plottedValues;
234  }
235 
236  void
237  util::from_json(const simox::json::json& j, MemoryToDebugObserver::Properties& p)
238  {
239  j.at("plottedValues").get_to(p.plottedValues);
240  }
241 
242 
243 } // namespace armarx::armem::client
armarx::armem::client::util::Visitor::datafieldName
std::string datafieldName
Definition: MemoryToDebugObserver.cpp:99
armarx::armem::detail::SuccessHeader::success
bool success
Definition: SuccessHeader.h:19
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:36
armarx::aron::data::ConstVariantVisitor
Definition: VariantVisitor.h:39
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
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:377
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:66
armarx::DebugObserverHelper::sendDebugObserverBatch
void sendDebugObserverBatch()
Definition: DebugObserverHelper.cpp:71
armarx::aron::Path::getDelimeter
std::string getDelimeter() const
Definition: Path.cpp:75
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: Configurator.cpp:5
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:217
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:84
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::DoublePtr &v) override
Definition: MemoryToDebugObserver.cpp:72
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:27
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:64
json_conversions.h
armarx::aron::Path
The Path class.
Definition: Path.h:35
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:75
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::BoolPtr &v) override
Definition: MemoryToDebugObserver.cpp:78
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::armem::detail::SuccessHeader::errorMessage
std::string errorMessage
Definition: SuccessHeader.h:20
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:54
armarx::armem::client::util::Visitor::setDatafield
void setDatafield(const ValueT &value)
Definition: MemoryToDebugObserver.cpp:91
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:194
armarx::armem::client::util::from_json
void from_json(const simox::json::json &j, MemoryValueID &id)
Definition: MemoryToDebugObserver.cpp:224
armarx::armem::client::util::MemoryToDebugObserver::pollOnce
void pollOnce()
Query values from the memory and send them to the debug observer.
Definition: MemoryToDebugObserver.cpp:105
armarx::armem::client::util::MemoryToDebugObserver::Properties
Configuration.
Definition: MemoryToDebugObserver.h:59
armarx::armem::MemoryID::getProviderSegmentID
MemoryID getProviderSegmentID() const
Definition: MemoryID.cpp:302
armarx::armem::client::util::Visitor::debugObserver
armarx::DebugObserverHelper & debugObserver
Definition: MemoryToDebugObserver.cpp:97
armarx::armem::client::util::Visitor::visitAronVariant
void visitAronVariant(const aron::data::LongPtr &v) override
Definition: MemoryToDebugObserver.cpp:60
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:25
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:83
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
VariantVisitor.h
armarx::armem::client::util::Visitor::Visitor
Visitor(armarx::DebugObserverHelper &debugObserver)
Definition: MemoryToDebugObserver.cpp:46
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:28
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:136
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:24
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:63
armarx::armem::client::util::Visitor
Definition: MemoryToDebugObserver.cpp:43
armarx::armem::client::util::MemoryToDebugObserver::Services
Required services.
Definition: MemoryToDebugObserver.h:67
armarx::armem::client::util::Visitor::count
int count
Definition: MemoryToDebugObserver.cpp:101
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:260
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:132
armarx::DebugObserverHelper
Brief description of class DebugObserverHelper.
Definition: DebugObserverHelper.h:51
armarx::armem::client::util::Visitor::channelName
std::string channelName
Definition: MemoryToDebugObserver.cpp:98
armarx::armem::client::util::MemoryValueID::aronPath
aron::Path aronPath
Definition: MemoryToDebugObserver.h:42
armarx::aron::Path::toString
std::string toString() const
Definition: Path.cpp:127
All.h
armarx::aron::data::BoolPtr
std::shared_ptr< Bool > BoolPtr
Definition: forward_declarations.h:38