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 <SemanticObjectRelations/Shapes/Shape.h>
26 #include <SimoxUtility/color/cmaps.h>
27 
28 #include <RobotAPI/libraries/armem/aron/MemoryID.aron.generated.h>
39 
40 #include "MemoryGraph.h"
41 
43 {
44 
46  {
47  }
48 
49 
52  {
53  int shapeIDCounter = 0;
54  MemoryGraph graph;
55  graph.attrib().initialMemoryID = memoryID;
56 
57  // Breadth-first search
58  std::map<armarx::armem::MemoryID, int> shapeIDs;
59  std::vector<std::tuple<int, int, std::pair<std::string, bool>>> edges;
60  std::vector<armarx::armem::MemoryID> currentLevel{memoryID};
61  std::vector<armarx::armem::MemoryID> nextLevel;
62  int depth = 0;
63 
64  shapeIDs[memoryID] = shapeIDCounter++;
65 
66  while (!currentLevel.empty())
67  {
68  for (const armarx::armem::MemoryID& vertID : currentLevel)
69  {
70  MemoryVertex vert;
71  vert.memoryID = vertID;
72  vert.depth = depth;
73  graph.addVertex(semrel::ShapeID{shapeIDs[vertID]}, vert);
74 
75  auto memory = armem::resolveID(mns, memoryID);
76  if (!memory)
77  {
78  continue;
79  }
80  auto objectAndType = armem::extractDataAndType(memory.value(), vertID);
81  if (!objectAndType)
82  {
83  ARMARX_WARNING << "Could not extract Aron data and type from memory object.";
84  continue;
85  }
86  auto [aronData, aronType] = objectAndType.value();
87 
88  auto ids = getMemoryIDs(aronData, aronType);
89  for (const auto& ref : ids)
90  {
91  if (!ref.second.hasMemoryName())
92  {
93  // Nowheres are duplicated to make the graph more legible.
94  MemoryVertex nowhere;
95  nowhere.memoryID = {};
96  nowhere.depth = depth + 1;
97  int nowhereID = shapeIDCounter++;
98  graph.addVertex(semrel::ShapeID{nowhereID}, nowhere);
99  edges.emplace_back(
100  shapeIDs[vertID], nowhereID, std::make_pair(ref.first, true));
101  continue;
102  }
103 
104  if (shapeIDs.find(ref.second) == shapeIDs.end())
105  {
106  shapeIDs[ref.second] = shapeIDCounter++;
107  nextLevel.push_back(ref.second);
108  }
109  edges.emplace_back(
110  shapeIDs[vertID], shapeIDs[ref.second], std::make_pair(ref.first, false));
111  }
112  }
113  currentLevel = std::move(nextLevel);
114  nextLevel.clear();
115  ++depth;
116  }
117 
118  for (const auto& [vert1, vert2, edgeProps] : edges)
119  {
120  MemoryEdge memEdge;
121  memEdge.referenceKey = edgeProps.first;
122  memEdge.isNowhereLink = edgeProps.second;
123  graph.addEdge(semrel::ShapeID{vert1}, semrel::ShapeID{vert2}, memEdge);
124  }
125 
126  bool style = true;
127  if (style)
128  {
129  int maxDepth = 1;
130  for (auto vertex : graph.vertices())
131  {
132  maxDepth = std::max(maxDepth, vertex.attrib().depth);
133  }
134 
135  simox::ColorMap cmap = simox::color::cmaps::GnBu();
136  cmap.set_vlimits(0, maxDepth + 1);
137  for (auto vertex : graph.vertices())
138  {
139  MemoryVertex& attrib = vertex.attrib();
140  attrib.fillColor = cmap(attrib.depth);
141  }
142  }
143 
144  return graph;
145  }
146 
147 
148  std::map<std::string, armarx::armem::MemoryID>
149  MemoryIDResolver::getMemoryIDs(const armarx::aron::data::DictPtr& obj,
150  const armarx::aron::type::ObjectPtr& type)
151  {
153  armarx::aron::data::visitRecursive(finder, obj, type);
154  return finder.getFoundObjects();
155  }
156 
157 } // namespace armarx::armem::id_graph
armarx::armem::id_graph
Definition: json_conversions.h:30
armarx::armem::id_graph::MemoryVertex::memoryID
MemoryID memoryID
Definition: MemoryGraph.h:36
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:38
MemoryID.h
MemoryIDResolver.h
semrel::RelationGraph
Definition: forward_declarations.h:28
mns.h
All.h
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::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:48
armarx::armem::id_graph::MemoryVertex
Definition: MemoryGraph.h:34
aron_conversions.h
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::armem::id_graph::MemoryEdge
Definition: MemoryGraph.h:41
armarx::armem::index::memoryID
const MemoryID memoryID
Definition: memory_ids.cpp:29
armarx::aron::data::visitRecursive
requires isRecursiveVisitor< RecursiveVisitorImplementation, typename RecursiveVisitorImplementation::Input > void visitRecursive(RecursiveVisitorImplementation &v, typename RecursiveVisitorImplementation::Input &o)
Definition: RecursiveVisitor.h:146
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::armem::id_graph::MemoryIDResolver::resolveToGraph
MemoryGraph resolveToGraph(const armarx::armem::MemoryID &memoryID)
Definition: MemoryIDResolver.cpp:51
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:186
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
armarx::armem::id_graph::MemoryEdge::referenceKey
std::string referenceKey
Definition: MemoryGraph.h:43
armarx::armem::id_graph::MemoryEdge::isNowhereLink
bool isNowhereLink
Definition: MemoryGraph.h:44
armarx::armem::id_graph::MemoryVertex::depth
int depth
Definition: MemoryGraph.h:37