Skip to content

Commit bb159af

Browse files
committed
Redefined JSON causalization output
1 parent 1ce2d6e commit bb159af

30 files changed

Lines changed: 352 additions & 122 deletions

algorithms/misc/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ target_sources(
22
sbgraph
33
PRIVATE
44
causalization_builders.cpp
5-
# causalization_json.cpp
5+
causalization_json.cpp
66
)

algorithms/misc/causalization_json.cpp

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,117 +17,78 @@
1717
1818
******************************************************************************/
1919

20+
#include "algorithms/misc/causalization_json.hpp"
21+
2022
#include "rapidjson/document.h"
2123
#include "rapidjson/filewritestream.h"
2224
#include "rapidjson/prettywriter.h"
2325

24-
#include "algorithms/misc/causalization_json.hpp"
25-
26-
namespace MISC {
26+
namespace misc {
2727

2828
using namespace SBG::LIB;
2929

30-
rapidjson::Value setJson(const Set &s
31-
, rapidjson::Document::AllocatorType &alloc)
32-
{
33-
rapidjson::Value result(rapidjson::kArrayType);
34-
35-
for (const SetPiece &mdi : s) {
36-
rapidjson::Value inter_array(rapidjson::kArrayType);
37-
for (const Interval &i : mdi) {
38-
rapidjson::Value inter(rapidjson::kArrayType);
39-
40-
rapidjson::Value beg;
41-
beg.SetInt(i.begin());
42-
inter.PushBack(beg, alloc);
43-
rapidjson::Value st;
44-
st.SetInt(i.step());
45-
inter.PushBack(st, alloc);
46-
rapidjson::Value end;
47-
end.SetInt(i.end());
48-
inter.PushBack(end, alloc);
49-
50-
inter_array.PushBack(inter, alloc);
51-
}
52-
rapidjson::Value mdi_obj(rapidjson::kObjectType);
53-
mdi_obj.AddMember("interval", inter_array, alloc);
54-
result.PushBack(mdi_obj, alloc);
55-
}
56-
57-
return result;
58-
}
59-
60-
rapidjson::Value expJson(Exp exp, rapidjson::Document::AllocatorType &alloc)
61-
{
62-
rapidjson::Value result(rapidjson::kArrayType);
63-
64-
for (const LExp &le : exp) {
65-
rapidjson::Value le_array(rapidjson::kArrayType);
66-
67-
std::stringstream ssm;
68-
ssm << le.slope();
69-
rapidjson::Value m;
70-
m.SetString(ssm.str().c_str(), strlen(ssm.str().c_str()), alloc);
71-
le_array.PushBack(m, alloc);
30+
////////////////////////////////////////////////////////////////////////////////
31+
// Causalization result --------------------------------------------------------
32+
////////////////////////////////////////////////////////////////////////////////
7233

73-
std::stringstream ssh;
74-
ssh << le.offset();
75-
rapidjson::Value h;
76-
h.SetString(ssh.str().c_str(), strlen(ssh.str().c_str()), alloc);
77-
le_array.PushBack(h, alloc);
34+
CausalizationResult::CausalizationResult(SBG::LIB::Set horizontal_sorting
35+
, SBG::LIB::PWMap algebraic_loops
36+
, SBG::LIB::Set mfvs
37+
, SBG::LIB::PWMap vertical_sorting) : _horizontal_sorting(horizontal_sorting)
38+
, _algebraic_loops(algebraic_loops)
39+
, _mfvs(mfvs)
40+
, _vertical_sorting(vertical_sorting) {}
7841

79-
result.PushBack(le_array, alloc);
80-
}
81-
82-
return result;
42+
const SBG::LIB::Set& CausalizationResult::horizontal_sorting() const
43+
{
44+
return _horizontal_sorting;
8345
}
8446

85-
rapidjson::Value mapJson(
86-
const PWMap &pw, rapidjson::Document::AllocatorType &alloc
87-
)
47+
const SBG::LIB::PWMap& CausalizationResult::algebraic_loops() const
8848
{
89-
rapidjson::Value result(rapidjson::kArrayType);
90-
91-
for (const Map &map : pw) {
92-
rapidjson::Value ith(rapidjson::kObjectType);
93-
94-
ith.AddMember("dom", setJson(map.dom(), alloc), alloc);
95-
ith.AddMember("exp", expJson(map.exp(), alloc), alloc);
49+
return _algebraic_loops;
50+
}
9651

97-
result.PushBack(ith, alloc);
98-
}
52+
const SBG::LIB::Set& CausalizationResult::mfvs() const { return _mfvs; }
9953

100-
return result;
54+
const SBG::LIB::PWMap& CausalizationResult::vertical_sorting() const
55+
{
56+
return _vertical_sorting;
10157
}
10258

103-
void buildJson(const Set &matching, const PWMap &scc, const PWMap &order)
59+
////////////////////////////////////////////////////////////////////////////////
60+
// Build JSON file -------------------------------------------------------------
61+
////////////////////////////////////////////////////////////////////////////////
62+
63+
void toJSON(const CausalizationResult& causalized)
10464
{
105-
rapidjson::Document d;
106-
d.SetObject();
107-
rapidjson::Document::AllocatorType& alloc = d.GetAllocator();
65+
// Initialize rapidJSON
66+
rapidjson::Document document;
67+
document.SetObject();
68+
rapidjson::Document::AllocatorType& alloc = document.GetAllocator();
10869

109-
// Create matching information
110-
rapidjson::Value edges = setJson(matching, alloc);
111-
d.AddMember("matching", edges, alloc);
70+
// Save causalization results
71+
document.AddMember("horizontal_sorting"
72+
, causalized.horizontal_sorting().toJSON(alloc), alloc);
11273

113-
// Create SCC information
114-
rapidjson::Value scc_rmap = mapJson(scc, alloc);
115-
d.AddMember("scc", scc_rmap, alloc);
74+
document.AddMember("algebraic_loops"
75+
, causalized.algebraic_loops().toJSON(alloc), alloc);
11676

117-
// Create sort information
118-
rapidjson::Value order_rmap = mapJson(order, alloc);
119-
d.AddMember("sort", order_rmap, alloc);
77+
document.AddMember("mfvs", causalized.mfvs().toJSON(alloc), alloc);
12078

79+
document.AddMember("vertical_sorting"
80+
, causalized.vertical_sorting().toJSON(alloc), alloc);
81+
82+
// Write file with rapidJSON
12183
FILE *fp = fopen("output.json", "w");
12284
char write_buffer[65536];
12385
rapidjson::FileWriteStream os(fp, write_buffer, sizeof(write_buffer));
12486
rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(os);
12587
rapidjson::PrettyFormatOptions opt = rapidjson::kFormatSingleLineArray;
12688
writer.SetFormatOptions(opt);
127-
d.Accept(writer);
89+
document.Accept(writer);
12890

12991
fclose(fp);
13092
}
13193

132-
} // namespace MISC
133-
94+
} // namespace misc

algorithms/misc/causalization_json.hpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,35 @@
2525
2626
******************************************************************************/
2727

28-
#ifndef MISC_CAUSALIZATION_JSON_HPP
29-
#define MISC_CAUSALIZAITON_JSON_HPP
28+
#ifndef SBGRAPH_ALGORITHMS_MISC_CAUSALIZATION_JSON_HPP_
29+
#define SBGRAPH_ALGORITHMS_MISC_CAUSALIZAITON_JSON_HPP_
3030

31-
#include <sbg/sbg.hpp>
31+
#include "sbg/pw_map.hpp"
32+
#include "sbg/set.hpp"
3233

33-
namespace MISC {
34+
namespace misc {
3435

35-
void buildJson(const SBG::LIB::Set &matching, const SBG::LIB::PWMap &scc
36-
, const SBG::LIB::PWMap &order);
36+
class CausalizationResult {
37+
public:
38+
CausalizationResult(SBG::LIB::Set horizontal_sorting
39+
, SBG::LIB::PWMap algebraic_loops
40+
, SBG::LIB::Set mfvs
41+
, SBG::LIB::PWMap vertical_sorting);
3742

38-
} // namespace MISC
43+
const SBG::LIB::Set& horizontal_sorting() const;
44+
const SBG::LIB::PWMap& algebraic_loops() const;
45+
const SBG::LIB::Set& mfvs() const;
46+
const SBG::LIB::PWMap& vertical_sorting() const;
3947

40-
#endif
48+
private:
49+
SBG::LIB::Set _horizontal_sorting;
50+
SBG::LIB::PWMap _algebraic_loops;
51+
SBG::LIB::Set _mfvs;
52+
SBG::LIB::PWMap _vertical_sorting;
53+
};
54+
55+
void toJSON(const CausalizationResult& causalized);
56+
57+
} // namespace misc
58+
59+
#endif // SBGRAPH_ALGORITHMS_MISC_CAUSALIZATION_JSON_HPP_

eval/visitors/func_evaluator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "algorithms/matching/matching.hpp"
2525
#include "algorithms/matching/matching_fact.hpp"
2626
#include "algorithms/misc/causalization_builders.hpp"
27+
#include "algorithms/misc/causalization_json.hpp"
2728
#include "algorithms/scc/scc.hpp"
2829
#include "algorithms/scc/scc_fact.hpp"
2930
#include "algorithms/sorting/topological/topological_sorting.hpp"
@@ -749,6 +750,11 @@ ExprBaseType BuiltInFunctions::causalizationEvaluator(const EBTList& args)
749750
dsbg = misc::buildVerticalSortingSBG(scc_result, mfvs_result);
750751
LIB::TopologicalSorting ts_impl = LIB::TS_FACT.createTSAlgorithm();
751752
LIB::PWMap ts_result = ts_impl.calculate(dsbg, scc_result.rmap());
753+
754+
misc::CausalizationResult causalized{match_result.M(), scc_result.rmap()
755+
, mfvs_result, ts_result};
756+
misc::toJSON(causalized);
757+
752758
return ExprBaseType{ts_result};
753759
}
754760

sbg/dom_ord_pwmap.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,21 @@ void DomOrdPWMap::compact()
836836
_pieces = std::move(result._pieces);
837837
}
838838

839+
// Non-member functions --------------------------------------------------------
840+
841+
rapidjson::Value toJSON(DomOrdPWMap pw
842+
, rapidjson::Document::AllocatorType& alloc)
843+
{
844+
rapidjson::Value result{rapidjson::kArrayType};
845+
846+
for (const MapEntry& entry : pw) {
847+
rapidjson::Value jth = toJSON(entry.map(), alloc);
848+
result.PushBack(jth, alloc);
849+
}
850+
851+
return result;
852+
}
853+
839854
} // namespace detail
840855

841856
} // namespace LIB

sbg/dom_ord_pwmap.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "sbg/map_entry.hpp"
3333
#include "sbg/set.hpp"
3434

35+
#include "rapidjson/document.h"
36+
3537
#include <vector>
3638
#include <iosfwd>
3739

@@ -172,6 +174,11 @@ inline void DomOrdPWMap::emplaceBack(Args&&... args)
172174
template<typename OrdCollection1, typename OrdCollection2, typename Core>
173175
Core traverse(const OrdCollection1& lhs, const OrdCollection2& rhs, Core op);
174176

177+
// Non-member functions --------------------------------------------------------
178+
179+
rapidjson::Value toJSON(DomOrdPWMap pw
180+
, rapidjson::Document::AllocatorType& alloc);
181+
175182
} // namespace detail
176183

177184
} // namespace LIB

sbg/expression.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,19 @@ Expression Expression::cartesianProduct(const Expression& other) const
225225
return result;
226226
}
227227

228+
rapidjson::Value Expression::toJSON(rapidjson::Document::AllocatorType& alloc)
229+
const
230+
{
231+
rapidjson::Value result{rapidjson::kArrayType};
232+
233+
for (const detail::LinearExpr& le : _impl) {
234+
rapidjson::Value jth = detail::toJSON(le, alloc);
235+
result.PushBack(jth, alloc);
236+
}
237+
238+
return result;
239+
}
240+
228241
} // namespace LIB
229242

230243
} // namespace SBG

sbg/expression.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "sbg/linear_expr.hpp"
3333
#include "sbg/multidim_inter.hpp"
3434

35+
#include "rapidjson/document.h"
36+
3537
#include <vector>
3638

3739
namespace SBG {
@@ -125,6 +127,8 @@ class Expression {
125127
*/
126128
Expression cartesianProduct(const Expression& other) const;
127129

130+
rapidjson::Value toJSON(rapidjson::Document::AllocatorType& alloc) const;
131+
128132
private:
129133
detail::ExpressionImpl _impl;
130134

sbg/interval.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ MaybeInterval Interval::compact(const Interval& other) const
192192
return {};
193193
}
194194

195+
// Non-member functions --------------------------------------------------------
196+
197+
rapidjson::Value toJSON(Interval i, rapidjson::Document::AllocatorType& alloc)
198+
{
199+
rapidjson::Value result{rapidjson::kArrayType};
200+
201+
rapidjson::Value begin{static_cast<uint64_t>(i.begin())};
202+
result.PushBack(begin, alloc);
203+
rapidjson::Value step{static_cast<uint64_t>(i.step())};
204+
result.PushBack(step, alloc);
205+
rapidjson::Value end{static_cast<uint64_t>(i.end())};
206+
result.PushBack(end, alloc);
207+
208+
return result;
209+
}
210+
195211
} // namespace detail
196212

197213
} // namespace LIB

sbg/interval.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "sbg/natural.hpp"
3535
#include "sbg/perimeter.hpp"
3636

37+
#include "rapidjson/document.h"
38+
3739
#include <iosfwd>
3840
#include <optional>
3941

@@ -105,6 +107,10 @@ bool operator!=(const Interval& lhs, const Interval& rhs);
105107

106108
std::ostream& operator<<(std::ostream& out, const Interval& i);
107109

110+
// Non-member functions --------------------------------------------------------
111+
112+
rapidjson::Value toJSON(Interval i, rapidjson::Document::AllocatorType& alloc);
113+
108114
} // namespace detail
109115

110116
} // namespace LIB

0 commit comments

Comments
 (0)