json_conversions.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::components::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 "json_conversions.h"
24 
25 #include <SimoxUtility/algorithm/string/string_tools.h>
26 #include <SimoxUtility/json.h>
27 
29 
30 namespace armarx::armem
31 {
32 
33  static const char* nowhereText = "[Nowhere]";
34 
35  void
36  id_graph::to_json(nlohmann::json& json, const MemoryVertex& vertex)
37  {
38  json["memoryID"] = vertex.memoryID;
39  json["memoryID.str()"] = vertex.memoryID.str();
40  json["depth"] = vertex.depth;
41 
42  if (not vertex.memoryID.hasMemoryName())
43  {
44  json["style"]["label"] = nowhereText;
45  json["style"]["fill-color"] = simox::Color::gray().to_vector4i();
46  }
47  else
48  {
49  std::vector<std::string> items = vertex.memoryID.getItems();
50  std::string del = "/";
51  if (items.size() > 3)
52  {
53  del = static_cast<std::string>("/\n");
54  }
55  json["style"]["label"] = simox::alg::join(items, del);
56  json["style"]["fill-color"] = vertex.fillColor.to_vector4i();
57  }
58  }
59 
60  void
61  id_graph::from_json(const nlohmann::json& json, MemoryVertex& vertex)
62  {
63  json.at("memoryID").get_to(vertex.memoryID);
64  json.at("depth").get_to(vertex.depth);
65  }
66 
67  void
68  id_graph::to_json(nlohmann::json& json, const MemoryEdge& edge)
69  {
70  json["key"] = edge.referenceKey;
71  json["isNowhereLink"] = edge.isNowhereLink;
72 
73  json["style"]["label"] = edge.referenceKey;
74 
75  if (edge.isNowhereLink)
76  {
77  json["style"]["color"] = simox::Color::kit_red().to_vector4i();
78  }
79  }
80 
81  void
82  id_graph::from_json(const nlohmann::json& json, MemoryEdge& edge)
83  {
84  json.at("key").get_to(edge.referenceKey);
85  json.at("isNowhereLink").get_to(edge.isNowhereLink);
86  }
87 
88  void
89  id_graph::to_json(nlohmann::json& json, const MemoryGraphAttributes& graph)
90  {
91  json["initialMemoryID"] = graph.initialMemoryID;
92  }
93 
94  void
95  id_graph::from_json(const nlohmann::json& json, MemoryGraphAttributes& graph)
96  {
97  json.at("initialMemoryID").get_to(graph.initialMemoryID);
98  }
99 } // namespace armarx::armem
armarx::armem::id_graph::MemoryVertex::memoryID
MemoryID memoryID
Definition: MemoryGraph.h:35
armarx::armem::id_graph::MemoryVertex::fillColor
simox::Color fillColor
Definition: MemoryGraph.h:37
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:32
json_conversions.h
armarx::armem::id_graph::MemoryGraphAttributes
Definition: MemoryGraph.h:46
armarx::armem::id_graph::MemoryGraphAttributes::initialMemoryID
MemoryID initialMemoryID
Definition: MemoryGraph.h:48
armarx::armem::id_graph::MemoryVertex
Definition: MemoryGraph.h:33
armarx::armem::id_graph::from_json
void from_json(const nlohmann::json &j, MemoryVertex &vertex)
Definition: json_conversions.cpp:61
armarx::armem::id_graph::MemoryEdge
Definition: MemoryGraph.h:40
armarx::armem::MemoryID::hasMemoryName
bool hasMemoryName() const
Definition: MemoryID.h:103
armarx::armem::MemoryID::getItems
std::vector< std::string > getItems(bool escapeDelimiters=false) const
Get the levels from root to first not defined level (excluding).
Definition: MemoryID.cpp:233
armarx::armem::id_graph::to_json
void to_json(nlohmann::json &j, const MemoryVertex &vertex)
Definition: json_conversions.cpp:36
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