HAL
grouping_serializer.cpp
Go to the documentation of this file.
5 #include <filesystem>
6 #include <fstream>
10 #include "hal_core/netlist/net.h"
12 #include "hal_core/utilities/log.h"
14 #include "rapidjson/filereadstream.h"
15 
16 namespace hal {
17  GroupingSerializer* GroupingSerializer::instance = new GroupingSerializer();
18 
20  : ProjectSerializer("groupings")
21  {;}
22 
23  std::string GroupingSerializer::serialize(Netlist* netlist, const std::filesystem::path& savedir, bool)
24  {
25  std::filesystem::path groupingFilePath(savedir);
26  groupingFilePath.append("groupings.json");
27 
29 
30  JsonWriteArray& grpArr = doc.add_array("groupings");
31 
32  for (Grouping* grp : netlist->get_groupings())
33  {
34  JsonWriteObject& grpObj = grpArr.add_object();
35  grpObj["id"] = (int)grp->get_id();
36  grpObj["name"] = grp->get_name();
37 
38  JsonWriteArray& grpMod = grpObj.add_array("modules");
39  std::vector<Module*> sortMod = grp->get_modules();
40  std::sort(sortMod.begin(), sortMod.end(), [](Module* lhs, Module* rhs) { return lhs->get_id() < rhs->get_id(); });
41  for (const Module* m : sortMod)
42  grpMod << m->get_id();
43  grpMod.close();
44 
45  JsonWriteArray& grpGat = grpObj.add_array("gates");
46  std::vector<Gate*> sortGat = grp->get_gates();
47  std::sort(sortGat.begin(), sortGat.end(), [](Gate* lhs, Gate* rhs) { return lhs->get_id() < rhs->get_id(); });
48  for (const Gate* g : sortGat)
49  grpGat << g->get_id();
50  grpGat.close();
51 
52  JsonWriteArray& grpNet = grpObj.add_array("nets");
53  std::vector<Net*> sortNet = grp->get_nets();
54  std::sort(sortNet.begin(), sortNet.end(), [](Net* lhs, Net* rhs) { return lhs->get_id() < rhs->get_id(); });
55  for (const Net* n : sortNet)
56  grpNet << n->get_id();
57  grpNet.close();
58 
59  grpObj.close();
60  }
61 
62  grpArr.close();
63 
64  doc.serialize(groupingFilePath.string());
65 
66  return groupingFilePath.filename().string();
67  }
68 
69  void GroupingSerializer::deserialize(Netlist* netlist, const std::filesystem::path &loaddir)
70  {
71  std::string relname = ProjectManager::instance()->get_filename(m_name);
72  if (relname.empty()) return;
73  std::filesystem::path groupingFilePath(loaddir);
74  groupingFilePath.append(relname);
75 
76  FILE* grpFile = fopen(groupingFilePath.string().c_str(), "rb");
77  if (grpFile == NULL)
78  {
79  log_error("GroupingSerializer::deserialize", "unable to open '{}'.", groupingFilePath.string());
80  return;
81  }
82 
83  char buffer[65536];
84  rapidjson::FileReadStream frs(grpFile, buffer, sizeof(buffer));
85  rapidjson::Document document;
86  document.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(frs);
87 
88  if (document.HasMember("groupings"))
89  {
90  for (const rapidjson::Value& grpVal : document["groupings"].GetArray())
91  {
92  Grouping* grouping = netlist->create_grouping(grpVal["id"].GetUint(), grpVal["name"].GetString());
93  if (grouping == nullptr)
94  {
95  return;
96  }
97 
98  if (grpVal.HasMember("modules"))
99  {
100  for (auto& module_node : grpVal["modules"].GetArray())
101  {
102  grouping->assign_module(netlist->get_module_by_id(module_node.GetUint()));
103  }
104  }
105 
106  if (grpVal.HasMember("gates"))
107  {
108  for (auto& gate_node : grpVal["gates"].GetArray())
109  {
110  grouping->assign_gate(netlist->get_gate_by_id(gate_node.GetUint()));
111  }
112  }
113 
114  if (grpVal.HasMember("nets"))
115  {
116  for (auto& net_node : grpVal["nets"].GetArray())
117  {
118  grouping->assign_net(netlist->get_net_by_id(net_node.GetUint()));
119  }
120  }
121  }
122  }
123  }
124 }
Definition: gate.h:58
bool assign_module(Module *module, bool force=false)
Definition: grouping.cpp:243
bool assign_gate(Gate *gate, bool force=false)
Definition: grouping.cpp:69
bool assign_net(Net *net, bool force=false)
Definition: grouping.cpp:156
std::string serialize(Netlist *netlist, const std::filesystem::path &savedir, bool isAutosave) override
void deserialize(Netlist *netlist, const std::filesystem::path &loaddir) override
JsonWriteObject & add_object()
bool serialize(const std::string &filename)
JsonWriteArray & add_array(const std::string &tag)
Definition: net.h:58
static ProjectManager * instance()
std::string get_filename(const std::string &serializer_name)
#define log_error(channel,...)
Definition: log.h:78
n
Definition: test.py:6