memory_serialization.cpp
Go to the documentation of this file.
1 #include "memory_serialization.h"
2 
3 #ifdef MEMORYX_FOUND
4 
7 
8 namespace armarx
9 {
10  memoryx::RelationList
11  semantic::toMemory(const semrel::AttributedGraph& graph)
12  {
13  const std::string type = graph.getTypeName();
14 
15  memoryx::RelationList relations;
16  for (auto edge : graph.edges())
17  {
19  relation->setName(type);
20  // sign == true: the relation holds
21  // sign == false: the relation does not hold
22  relation->setSign(true);
23 
24  auto makeRef = [&](const nlohmann::json& vertex)
25  {
27  ref->entityId = vertex.at("object").at("entityId").get<std::string>();
28  return ref;
29  };
30  auto& source = edge.source().attrib().json;
31  auto& target = edge.target().attrib().json;
32  memoryx::EntityRefPtr sourceRef = makeRef(source);
33  memoryx::EntityRefPtr targetRef = makeRef(target);
34 
35  // Order matters! The relation is directed: source -> target
36  relation->setEntities({sourceRef, targetRef});
37 
38  relation->setAttributes(edge.attrib().json.dump());
39  relation->setSourceAttributes(source.dump());
40  relation->setTargetAttributes(target.dump());
41 
42  relations.push_back(relation);
43  }
44 
45  return relations;
46  }
47 
48  semrel::AttributedGraph
49  semantic::fromMemory(const memoryx::RelationList& relations)
50  {
51  // Find all unique entities in the graph and store their attributes.
52  std::string type;
53  std::map<std::string, nlohmann::json> id2attr;
54  std::map<std::pair<std::string, std::string>, nlohmann::json> edge2attr;
55 
56  for (const memoryx::RelationBasePtr& relationBase : relations)
57  {
58  type = relationBase->getName();
59  auto relation = memoryx::RelationPtr::dynamicCast(relationBase);
60  // sign == true: The relation holds
61  if (relation && relation->getSign() == true)
62  {
63  memoryx::EntityRefList entities = relation->getEntities();
64  if (entities.size() == 2)
65  {
66  auto& sourceId = entities[0]->entityId;
67  auto& targetId = entities[1]->entityId;
68  auto sourceAttrs = nlohmann::json::parse(relation->getSourceAttributes());
69  auto targetAttrs = nlohmann::json::parse(relation->getTargetAttributes());
70  id2attr.emplace(sourceId, sourceAttrs);
71  id2attr.emplace(targetId, targetAttrs);
72 
73  auto edgeAttrs = nlohmann::json::parse(relation->getAttributes());
74  edge2attr.emplace(std::make_pair(sourceId, targetId), edgeAttrs);
75  }
76  }
77  }
78 
79  // Create an attributed graph from the two maps id2attr and edge2attr.
80  semrel::AttributedGraph result;
81  result.attrib().json = {{"type", type}};
82  std::map<std::string, semrel::AttributedGraph::VertexDescriptor> id2desc;
83  {
84  for (auto& [id, attr] : id2attr)
85  {
86  semrel::AttributedGraph::Vertex vertex = result.addVertex(semrel::ShapeID(-1));
87  vertex.attrib().json = std::move(attr);
88  id2desc.emplace(id, vertex.descriptor());
89  }
90 
91  for (auto& [pair, attr] : edge2attr)
92  {
93  auto& sourceId = pair.first;
94  auto& targetId = pair.second;
95 
96  auto sourceDesc = id2desc.at(sourceId);
97  auto targetDesc = id2desc.at(targetId);
98 
99  semrel::AttributedGraph::Edge edge = result.addEdge(sourceDesc, targetDesc);
100  edge.attrib().json = attr;
101  }
102  }
103 
104  return result;
105  }
106 } // namespace armarx
107 
108 #endif
memory_serialization.h
boost::target
Vertex target(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:688
armarx::armem::wm::toMemory
void toMemory(Memory &m, const std::vector< CoreSegment > &e)
Definition: memory_conversions.cpp:6
memoryx::Relation
Relation class represents a directed relation between entities.
Definition: Relation.h:42
memoryx::EntityRef
The EntityRef class is used to store references to Entities as values in other Entity instances.
Definition: EntityRef.h:45
Relation.h
IceInternal::Handle
Definition: forward_declarations.h:8
EntityRef.h
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28