MemoryIDResolver.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 VisionX::ArmarXObjects::MemoryGrapher
17 * @author [Author Name] ( [Author Email] )
18 * @date 2021
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23#include "MemoryIDResolver.h"
24
25#include <SimoxUtility/color/cmaps.h>
26
27#include <RobotAPI/libraries/armem/aron/MemoryID.aron.generated.h>
38
39#include "MemoryGraph.h"
40#include <SemanticObjectRelations/Shapes/Shape.h>
41
43{
44
48
51 {
52 int shapeIDCounter = 0;
53 MemoryGraph graph;
54 graph.attrib().initialMemoryID = memoryID;
55
56 // Breadth-first search
57 std::map<armarx::armem::MemoryID, int> shapeIDs;
58 std::vector<std::tuple<int, int, std::pair<std::string, bool>>> edges;
59 std::vector<armarx::armem::MemoryID> currentLevel{memoryID};
60 std::vector<armarx::armem::MemoryID> nextLevel;
61 int depth = 0;
62
63 shapeIDs[memoryID] = shapeIDCounter++;
64
65 while (!currentLevel.empty())
66 {
67 for (const armarx::armem::MemoryID& vertID : currentLevel)
68 {
69 MemoryVertex vert;
70 vert.memoryID = vertID;
71 vert.depth = depth;
72 graph.addVertex(semrel::ShapeID{shapeIDs[vertID]}, vert);
73
74 auto memory = armem::resolveID(mns, memoryID);
75 if (!memory)
76 {
77 continue;
78 }
79 auto objectAndType = armem::extractDataAndType(memory.value(), vertID);
80 if (!objectAndType)
81 {
82 ARMARX_WARNING << "Could not extract Aron data and type from memory object.";
83 continue;
84 }
85 auto [aronData, aronType] = objectAndType.value();
86
87 auto ids = getMemoryIDs(aronData, aronType);
88 for (const auto& ref : ids)
89 {
90 if (!ref.second.hasMemoryName())
91 {
92 // Nowheres are duplicated to make the graph more legible.
93 MemoryVertex nowhere;
94 nowhere.memoryID = {};
95 nowhere.depth = depth + 1;
96 int nowhereID = shapeIDCounter++;
97 graph.addVertex(semrel::ShapeID{nowhereID}, nowhere);
98 edges.emplace_back(
99 shapeIDs[vertID], nowhereID, std::make_pair(ref.first, true));
100 continue;
101 }
102
103 if (shapeIDs.find(ref.second) == shapeIDs.end())
104 {
105 shapeIDs[ref.second] = shapeIDCounter++;
106 nextLevel.push_back(ref.second);
107 }
108 edges.emplace_back(
109 shapeIDs[vertID], shapeIDs[ref.second], std::make_pair(ref.first, false));
110 }
111 }
112 currentLevel = std::move(nextLevel);
113 nextLevel.clear();
114 ++depth;
115 }
116
117 for (const auto& [vert1, vert2, edgeProps] : edges)
118 {
119 MemoryEdge memEdge;
120 memEdge.referenceKey = edgeProps.first;
121 memEdge.isNowhereLink = edgeProps.second;
122 graph.addEdge(semrel::ShapeID{vert1}, semrel::ShapeID{vert2}, memEdge);
123 }
124
125 bool style = true;
126 if (style)
127 {
128 int maxDepth = 1;
129 for (auto vertex : graph.vertices())
130 {
131 maxDepth = std::max(maxDepth, vertex.attrib().depth);
132 }
133
134 simox::ColorMap cmap = simox::color::cmaps::GnBu();
135 cmap.set_vlimits(0, maxDepth + 1);
136 for (auto vertex : graph.vertices())
137 {
138 MemoryVertex& attrib = vertex.attrib();
139 attrib.fillColor = cmap(attrib.depth);
140 }
141 }
142
143 return graph;
144 }
145
146 std::map<std::string, armarx::armem::MemoryID>
147 MemoryIDResolver::getMemoryIDs(const armarx::aron::data::DictPtr& obj,
149 {
152 return finder.getFoundObjects();
153 }
154
155} // namespace armarx::armem::id_graph
The memory name system (MNS) client.
MemoryIDResolver(armarx::armem::client::MemoryNameSystem &mns)
MemoryGraph resolveToGraph(const armarx::armem::MemoryID &memoryID)
Finds aron objects with a given type name prefix in aron variants and returns them as BOs.
const std::map< std::string, BOType > & getFoundObjects()
Get the objects that have been found.
Brief description of class memory.
Definition memory.h:39
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
semrel::RelationGraph< MemoryVertex, MemoryEdge, MemoryGraphAttributes > MemoryGraph
Definition MemoryGraph.h:51
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:34
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
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
void visitRecursive(RecursiveVisitorImplementation &v, typename RecursiveVisitorImplementation::Input &o)
std::shared_ptr< Object > ObjectPtr
Definition Object.h:36