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 
46  {
47  }
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,
148  const armarx::aron::type::ObjectPtr& type)
149  {
151  armarx::aron::data::visitRecursive(finder, obj, type);
152  return finder.getFoundObjects();
153  }
154 
155 } // namespace armarx::armem::id_graph
armarx::armem::id_graph
Definition: json_conversions.h:29
armarx::armem::id_graph::MemoryVertex::memoryID
MemoryID memoryID
Definition: MemoryGraph.h:35
armarx::aron::BOSubObjectFinder
Finds aron objects with a given type name prefix in aron variants and returns them as BOs.
Definition: object_finders.h:83
Reader.h
armarx::armem::id_graph::MemoryIDResolver::MemoryIDResolver
MemoryIDResolver(armarx::armem::client::MemoryNameSystem &mns)
Definition: MemoryIDResolver.cpp:45
client.h
armarx::armem::id_graph::MemoryVertex::fillColor
simox::Color fillColor
Definition: MemoryGraph.h:37
MemoryID.h
MemoryIDResolver.h
semrel::RelationGraph
Definition: forward_declarations.h:27
mns.h
All.h
armarx::memory
Brief description of class memory.
Definition: memory.h:38
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::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
RecursiveVisitor.h
object_finders.h
All.h
armarx::aron::BOSubObjectFinder::getFoundObjects
const std::map< std::string, BOType > & getFoundObjects()
Get the objects that have been found.
Definition: object_finders.h:97
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::armem::id_graph::MemoryVertex
Definition: MemoryGraph.h:33
aron_conversions.h
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::armem::id_graph::MemoryEdge
Definition: MemoryGraph.h:40
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:28
armarx::aron::data::visitRecursive
requires isRecursiveVisitor< RecursiveVisitorImplementation, typename RecursiveVisitorImplementation::Input > void visitRecursive(RecursiveVisitorImplementation &v, typename RecursiveVisitorImplementation::Input &o)
Definition: RecursiveVisitor.h:161
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:68
armarx::armem::id_graph::MemoryIDResolver::resolveToGraph
MemoryGraph resolveToGraph(const armarx::armem::MemoryID &memoryID)
Definition: MemoryIDResolver.cpp:50
MemoryGraph.h
util.h
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
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:34
armarx::armem::id_graph::MemoryEdge::referenceKey
std::string referenceKey
Definition: MemoryGraph.h:42
armarx::armem::id_graph::MemoryEdge::isNowhereLink
bool isNowhereLink
Definition: MemoryGraph.h:43
armarx::armem::id_graph::MemoryVertex::depth
int depth
Definition: MemoryGraph.h:36