util.h
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  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #pragma once
23 
24 #include <vector>
25 #include <optional>
26 
28 
32 
33 
34 namespace armarx::armem
35 {
36 
37  /**
38  * @brief Tries to cast a armem::EntityInstance to AronClass
39  *
40  * @tparam AronClass class name. Needs to be derived from armarx::aron::cppcodegenerator::AronCppClass
41  * @param item
42  * @return std::optional<AronClass>
43  */
44  template <typename AronClass>
45  std::optional<AronClass> tryCast(const wm::EntityInstance& item)
46  {
48 
49 
50  if (!item.data())
51  {
52  return std::nullopt;
53  }
54 
55 #if 0
56  // item.data() is by definition a DictNavigator
57  if (item.data()->getDescriptor() != aron::data::Descriptor::DICT)
58  {
59  return std::nullopt;
60  }
61 #endif
62 
63  try
64  {
65  return AronClass::FromAron(item.data());
66  }
68  {
69  return std::nullopt;
70  }
71  }
72 
73  /**
74  * @brief Returns all entities that can be cast to AronClass
75  *
76  * @tparam AronClass class name. Needs to be derived from armarx::aron::cppcodegenerator::AronCppClass
77  * @param entities collection of entities
78  * @return std::vector<AronClass>
79  */
80  template <typename AronClass>
81  std::vector<AronClass>
82  allOfType(const std::map<std::string, wm::Entity>& entities)
83  {
85 
86  std::vector<AronClass> outV;
87 
88  // loop over all entities and their snapshots
89  for (const auto &[s, entity] : entities)
90  {
91  entity.forEachInstance([&outV](const wm::EntityInstance& entityInstance)
92  {
93  const auto o = tryCast<AronClass>(entityInstance);
94 
95  if (o)
96  {
97  outV.push_back(*o);
98  }
99  });
100  }
101 
102  return outV;
103  }
104 
105  /**
106  * @brief filter + transform for entities.
107  *
108  * Can be used instead of
109  *
110  * std::vector<Bar> ret;
111  *
112  * const auto allOf = allOfType<Foo>(entities);
113  * std::transform(allOf.begin(), allOf.end(), std::back_inserter(ret), pred)
114  *
115  * This function has the benefit that the transform function will be applied directly.
116  * No intermediate vector has to be created (e.g. "allOf" in the example above).
117  *
118  * @tparam AronClass class name. Needs to be derived from armarx::aron::cppcodegenerator::AronCppClass
119  * @param entities collection of entities
120  * @param pred binary predicate function, applied to all entity instances
121  * @return vector of "pred"-transformed elements that can be cast to AronClass
122  */
123  template <typename AronClass>
124  auto transformAllOfType(const std::map<std::string, wm::Entity>& entities,
125  auto pred) -> std::vector<decltype(pred(AronClass()))>
126  {
128 
129  std::vector<decltype(pred(AronClass()))> outV;
130 
131  if (entities.empty())
132  {
133  ARMARX_WARNING << "No entities!";
134  }
135 
136  // loop over all entities and their snapshots
137  for (const auto &[s, entity] : entities)
138  {
139  entity.forEachInstance([&outV, &pred](const wm::EntityInstance& entityInstance)
140  {
141  const auto o = tryCast<AronClass>(entityInstance);
142 
143  if (o)
144  {
145  outV.push_back(pred(*o));
146  }
147  });
148  }
149 
150  return outV;
151  }
152 
153  /**
154  * @brief resolve a single MemoryID with the given MemoryNameSystem.
155  *
156  * Returns a Memory containing only the desired segment / entity if it was
157  * successfully queried. If the query was unsuccessful (the MNS could not
158  * resolve the memory server, the MemoryID does not exist, etc.),
159  * nothing is returned.
160  *
161  * @param mns the MemoryNameSystem to use for the query
162  * @param memoryID the MemoryID to query for
163  * @return memory containing the object referenced by the MemoryID if available
164  */
165  std::optional<armarx::armem::wm::Memory>
168 
169 
170  /**
171  * @brief get the data and type of the given MemoryID in the given Memory.
172  *
173  * Returns the data and the type of the latest instance corresponding to
174  * the given MemoryID (whatever `memory.findLatestInstance(memoryID)` returns).
175  * If no such instance exists in the memory or no type for the instance is available,
176  * nothing is returned.
177  *
178  * @param memory the Memory to extract the data and type from
179  * @param memoryID the MemoryID of the instance to extract
180  * @return pair of data and type for the instance if available
181  */
182  std::optional<std::pair<armarx::aron::data::DictPtr, armarx::aron::type::ObjectPtr>>
185 
186 } // namespace armarx::armem
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::armem::transformAllOfType
auto transformAllOfType(const std::map< std::string, wm::Entity > &entities, auto pred) -> std::vector< decltype(pred(AronClass()))>
filter + transform for entities.
Definition: util.h:124
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
armarx::armem::allOfType
std::vector< AronClass > allOfType(const std::map< std::string, wm::Entity > &entities)
Returns all entities that can be cast to AronClass.
Definition: util.h:82
armarx::memory
Brief description of class memory.
Definition: memory.h:39
armarx::armem::resolveID
std::optional< armarx::armem::wm::Memory > resolveID(armarx::armem::client::MemoryNameSystem &mns, const armarx::armem::MemoryID &memoryID)
resolve a single MemoryID with the given MemoryNameSystem.
Definition: util.cpp:10
armarx::aron::data::Descriptor::DICT
@ DICT
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
memory_definitions.h
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:29
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
MemoryNameSystem.h
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:129
armarx::armem::extractDataAndType
std::optional< std::pair< armarx::aron::data::DictPtr, armarx::aron::type::ObjectPtr > > extractDataAndType(const armarx::armem::wm::Memory &memory, const armarx::armem::MemoryID &memoryID)
get the data and type of the given MemoryID in the given Memory.
Definition: util.cpp:35
AronGeneratedClass.h
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::tryCast
std::optional< AronClass > tryCast(const wm::EntityInstance &item)
Tries to cast a armem::EntityInstance to AronClass.
Definition: util.h:45