Skip to content

Commit

Permalink
Added JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Mar 15, 2024
1 parent bb462bc commit b3e1aec
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
12 changes: 8 additions & 4 deletions eval/visitors/eval_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,23 @@ auto match_scc_visitor_ = Util::Overload {
auto match_scc_ts_visitor_ = Util::Overload {
[](LIB::BaseSBG a, Util::MD_NAT b, Util::MD_NAT c, bool d) {
LIB::BaseMatch match(a.copy(b[0]), d);
match.calculate(c[0]);
LIB::UnordSet match_res = match.calculate(c[0]).matched_edges();
LIB::BaseSCC scc(buildSCCFromMatching(match), d);
LIB::BasePWMap scc_res = scc.calculate();
LIB::BaseTopSort ts(buildSortFromSCC(scc, scc_res), d);
return InfoBaseType(ts.calculate());
LIB::BaseVO ts_res = ts.calculate();
buildJson(match_res, scc.transformResult(scc_res), ts_res);
return InfoBaseType(ts_res);
},
[](LIB::CanonSBG a, Util::MD_NAT b, Util::MD_NAT c, bool d) {
LIB::CanonMatch match(a.copy(b[0]), d);
match.calculate(c[0]);
LIB::OrdSet match_res = match.calculate(c[0]).matched_edges();
LIB::CanonSCC scc(buildSCCFromMatching(match), d);
LIB::CanonPWMap scc_res = scc.calculate();
LIB::CanonTopSort ts(buildSortFromSCC(scc, scc_res), d);
return InfoBaseType(ts.calculate());
LIB::CanonVO ts_res = ts.calculate();
buildJson(match_res, scc.transformResult(scc_res), ts_res);
return InfoBaseType(ts_res);
},
[](auto a, auto b, auto c, auto d) {
Util::ERROR("Wrong arguments for matching+scc+ts");
Expand Down
2 changes: 1 addition & 1 deletion sbg/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ bool SBGMap<Set>::operator!=(const SBGMap &other) const
template<typename Set>
std::ostream &operator<<(std::ostream &out, const SBGMap<Set> &sbgmap)
{
out << sbgmap.dom() << " " << sbgmap.exp();
out << sbgmap.dom() << " -> " << sbgmap.exp();

return out;
}
Expand Down
65 changes: 65 additions & 0 deletions sbg/sbg_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,23 @@ PWMap<Set> SBGSCC<Set>::calculate()
return rmap;
}

template<typename Set>
std::set<Set> SBGSCC<Set>::transformResult(PWMap<Set> scc)
{
Components res;

Set reps = scc.filterMap([](const SBGMap<Set> &sbgmap) {
return eqId(sbgmap);
}).image();

for (const SetPiece &mdi : reps) {
Set represented = scc.preImage(Set(mdi));
res.emplace(represented);
}

return res;
}

// -----------------------------------------------------------------------------
// Topological sort ------------------------------------------------------------
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -1105,6 +1122,54 @@ DSBGraph<Set> buildSortFromSCC(
template BaseDSBG buildSortFromSCC(const BaseSCC &scc, const BasePWMap &rmap);
template CanonDSBG buildSortFromSCC(const CanonSCC &scc, const CanonPWMap &rmap);

template<typename Set>
void buildJson(
const Set &matching, std::set<Set> scc, const VertexOrder<Set> &order
)
{
// 1. Parse a JSON string into DOM.
const char* json = "{\"matching\":\"\",\"scc\":\"\",\"order\":\"\"}";
rapidjson::Document d;
d.Parse(json);

// 2. Modify it by DOM.
rapidjson::Value &m = d["matching"];
std::stringstream ss1;
ss1 << matching;
m.SetString(ss1.str().c_str(), strlen(ss1.str().c_str()), d.GetAllocator());

rapidjson::Value &s = d["scc"];
std::stringstream ss2;
for (const Set &s : scc)
ss2 << s;
s.SetString(ss2.str().c_str(), strlen(ss2.str().c_str()), d.GetAllocator());

rapidjson::Value &o = d["order"];
std::stringstream ss3;
ss3 << order;
o.SetString(ss3.str().c_str(), strlen(ss3.str().c_str()), d.GetAllocator());

// 3. Stringify the DOM
FILE *fp = fopen("output.json", "w");
char write_buffer[65536];
rapidjson::FileWriteStream os(fp, write_buffer, sizeof(write_buffer));
rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(os);
d.Accept(writer);

fclose(fp);

return;
}

template void buildJson
(
const UnordSet &match, std::set<UnordSet> scc, const BaseVO &order
);
template void buildJson
(
const OrdSet &match, std::set<OrdSet> scc, const CanonVO &order
);

} // namespace LIB

} // namespace SBG
11 changes: 11 additions & 0 deletions sbg/sbg_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
#include <list>
#include <set>
#include <iostream>
#include <unordered_set>

#include "include/rapidjson/document.h"
#include "include/rapidjson/filewritestream.h"
#include "include/rapidjson/prettywriter.h"
#include "sbg/sbg.hpp"
#include "util/logger.hpp"

Expand Down Expand Up @@ -170,6 +174,7 @@ template<typename Set>
struct SBGSCC {
using Map = SBGMap<Set>;
using PW = PWMap<Set>;
using Components = std::set<Set>;

//*** SBG info, constant
member_class(DSBGraph<Set>, dsbg);
Expand All @@ -194,6 +199,7 @@ struct SBGSCC {
SBGSCC(DSBGraph<Set> dsbg, bool debug);

PW calculate();
Components transformResult(PW scc);

private:
PW sccStep();
Expand Down Expand Up @@ -283,6 +289,11 @@ DSBGraph<Set> buildSCCFromMatching(const SBGMatching<Set> &match);
template<typename Set>
DSBGraph<Set> buildSortFromSCC(const SBGSCC<Set> &scc, const PWMap<Set> &rmap);

template<typename Set>
void buildJson(
const Set &matching, std::set<Set> scc, const VertexOrder<Set> &order
);

} // namespace LIB

} // namespace SBG
Expand Down
2 changes: 1 addition & 1 deletion test/test3_ts.test
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ equation
end Test3;
*/

N = 10
N = 100000

F1 = N-2
F2 = N-2+F1
Expand Down

0 comments on commit b3e1aec

Please sign in to comment.