13 const std::string type = graph.getTypeName();
15 memoryx::RelationList relations;
16 for (
auto edge : graph.edges())
19 relation->setName(type);
22 relation->setSign(
true);
24 auto makeRef = [&](
const nlohmann::json& vertex)
27 ref->entityId = vertex.at(
"object").at(
"entityId").get<std::string>();
30 auto&
source = edge.source().attrib().json;
31 auto&
target = edge.target().attrib().json;
36 relation->setEntities({sourceRef, targetRef});
38 relation->setAttributes(edge.attrib().json.dump());
39 relation->setSourceAttributes(
source.dump());
40 relation->setTargetAttributes(
target.dump());
42 relations.push_back(relation);
48 semrel::AttributedGraph
49 semantic::fromMemory(
const memoryx::RelationList& relations)
53 std::map<std::string, nlohmann::json> id2attr;
54 std::map<std::pair<std::string, std::string>, nlohmann::json> edge2attr;
56 for (
const memoryx::RelationBasePtr& relationBase : relations)
58 type = relationBase->getName();
59 auto relation = memoryx::RelationPtr::dynamicCast(relationBase);
61 if (relation && relation->getSign() ==
true)
63 memoryx::EntityRefList entities = relation->getEntities();
64 if (entities.size() == 2)
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);
73 auto edgeAttrs = nlohmann::json::parse(relation->getAttributes());
74 edge2attr.emplace(std::make_pair(sourceId, targetId), edgeAttrs);
80 semrel::AttributedGraph result;
81 result.attrib().json = {{
"type", type}};
82 std::map<std::string, semrel::AttributedGraph::VertexDescriptor> id2desc;
84 for (
auto& [
id, attr] : id2attr)
86 semrel::AttributedGraph::Vertex vertex = result.addVertex(semrel::ShapeID(-1));
87 vertex.attrib().json = std::move(attr);
88 id2desc.emplace(
id, vertex.descriptor());
91 for (
auto& [pair, attr] : edge2attr)
93 auto& sourceId = pair.first;
94 auto& targetId = pair.second;
96 auto sourceDesc = id2desc.at(sourceId);
97 auto targetDesc = id2desc.at(targetId);
99 semrel::AttributedGraph::Edge edge = result.addEdge(sourceDesc, targetDesc);
100 edge.attrib().json = attr;