Skip to content

Commit a097dcc

Browse files
committed
Refactored relationships from diagram elements to common diagram model (#427)
1 parent 4cc542a commit a097dcc

29 files changed

+450
-305
lines changed

src/class_diagram/generators/json/class_diagram_generator.cc

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,6 @@ void to_json(nlohmann::json &j, const class_ &c)
104104

105105
j["members"] = c.members();
106106
j["methods"] = c.methods();
107-
auto bases = nlohmann::json::array();
108-
109-
for (const auto &rel : c.relationships()) {
110-
if (rel.type() != common::model::relationship_t::kExtension)
111-
continue;
112-
113-
auto base = nlohmann::json::object();
114-
base["is_virtual"] = rel.is_virtual();
115-
base["id"] = std::to_string(rel.destination().value());
116-
if (rel.access() != common::model::access_t::kNone)
117-
base["access"] = to_string(rel.access());
118-
bases.push_back(std::move(base));
119-
}
120-
j["bases"] = std::move(bases);
121107

122108
set_module(j, c);
123109

@@ -132,25 +118,6 @@ void to_json(nlohmann::json &j, const objc_interface &c)
132118

133119
j["members"] = c.members();
134120
j["methods"] = c.methods();
135-
auto bases = nlohmann::json::array();
136-
auto protocols = nlohmann::json::array();
137-
138-
for (const auto &rel : c.relationships()) {
139-
if (rel.type() == common::model::relationship_t::kExtension) {
140-
auto base = nlohmann::json::object();
141-
base["id"] = std::to_string(rel.destination().value());
142-
if (rel.access() != common::model::access_t::kNone)
143-
base["access"] = to_string(rel.access());
144-
bases.push_back(std::move(base));
145-
}
146-
else if (rel.type() == common::model::relationship_t::kInstantiation) {
147-
auto protocol = nlohmann::json::object();
148-
protocol["id"] = std::to_string(rel.destination().value());
149-
protocols.push_back(std::move(protocol));
150-
}
151-
}
152-
j["bases"] = std::move(bases);
153-
j["protocols"] = std::move(protocols);
154121
}
155122

156123
void to_json(nlohmann::json &j, const enum_ &c)
@@ -261,6 +228,22 @@ void generator::generate(const class_ &c, nlohmann::json &parent) const
261228
{
262229
nlohmann::json object = c;
263230

231+
auto bases = nlohmann::json::array();
232+
233+
for (const auto &rel : model().relationships(c.id())) {
234+
if (rel.type() != common::model::relationship_t::kExtension)
235+
continue;
236+
237+
auto base = nlohmann::json::object();
238+
base["is_virtual"] = rel.is_virtual();
239+
base["id"] = std::to_string(rel.destination().value());
240+
if (rel.access() != common::model::access_t::kNone)
241+
base["access"] = to_string(rel.access());
242+
bases.push_back(std::move(base));
243+
}
244+
245+
object["bases"] = std::move(bases);
246+
264247
// Perform config dependent postprocessing on generated class
265248
if (!config().generate_fully_qualified_name())
266249
object["display_name"] =
@@ -307,6 +290,27 @@ void generator::generate(const objc_interface &c, nlohmann::json &parent) const
307290
{
308291
nlohmann::json object = c;
309292

293+
auto bases = nlohmann::json::array();
294+
auto protocols = nlohmann::json::array();
295+
296+
for (const auto &rel : model().relationships(c.id())) {
297+
if (rel.type() == common::model::relationship_t::kExtension) {
298+
auto base = nlohmann::json::object();
299+
base["id"] = std::to_string(rel.destination().value());
300+
if (rel.access() != common::model::access_t::kNone)
301+
base["access"] = to_string(rel.access());
302+
bases.push_back(std::move(base));
303+
}
304+
else if (rel.type() == common::model::relationship_t::kInstantiation) {
305+
auto protocol = nlohmann::json::object();
306+
protocol["id"] = std::to_string(rel.destination().value());
307+
protocols.push_back(std::move(protocol));
308+
}
309+
}
310+
311+
object["bases"] = std::move(bases);
312+
object["protocols"] = std::move(protocols);
313+
310314
// Perform config dependent postprocessing on generated class
311315
if (!config().generate_fully_qualified_name())
312316
object["display_name"] = display_name_adapter(c).full_name_no_ns();

src/class_diagram/generators/json/class_diagram_generator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void generator::generate_relationships(const T &c, nlohmann::json &parent) const
151151
const auto &model =
152152
common_generator<diagram_config, diagram_model>::model();
153153

154-
for (const auto &r : c.relationships()) {
154+
for (const auto &r : model.relationships(c.id())) {
155155
auto target_element = model.get(r.destination());
156156
if (!target_element.has_value()) {
157157
LOG_DBG("Skipping {} relation from '{}' to '{}' due "

src/class_diagram/generators/mermaid/class_diagram_generator.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
8989
std::set<std::string> rendered_relations;
9090

9191
std::stringstream all_relations_str;
92-
for (const auto &r : c.relationships()) {
92+
for (const auto &r : model().relationships(c.id())) {
9393
try {
9494
generate_relationship(r, rendered_relations);
9595
}
@@ -403,7 +403,7 @@ void generator::generate_relationships(
403403
std::stringstream all_relations_str;
404404
std::set<std::string> unique_relations;
405405

406-
for (const auto &r : c.relationships()) {
406+
for (const auto &r : model().relationships(c.id())) {
407407
LOG_DBG("== Processing relationship {}", to_string(r.type()));
408408

409409
std::stringstream relstr;
@@ -487,7 +487,7 @@ void generator::generate_relationships(
487487
std::stringstream all_relations_str;
488488
std::set<std::string> unique_relations;
489489

490-
for (const auto &r : c.relationships()) {
490+
for (const auto &r : model().relationships(c.id())) {
491491
LOG_DBG("== Processing relationship {}", to_string(r.type()));
492492

493493
std::stringstream relstr;
@@ -556,7 +556,7 @@ void generator::generate_relationships(
556556

557557
void generator::generate_relationships(const enum_ &e, std::ostream &ostr) const
558558
{
559-
for (const auto &r : e.relationships()) {
559+
for (const auto &r : model().relationships(e.id())) {
560560
eid_t destination{};
561561
std::stringstream relstr;
562562
try {
@@ -648,7 +648,7 @@ void generator::generate(const objc_interface &c, std::ostream &ostr) const
648648
std::set<std::string> rendered_relations;
649649

650650
std::stringstream all_relations_str;
651-
for (const auto &r : c.relationships()) {
651+
for (const auto &r : model().relationships(c.id())) {
652652
try {
653653
generate_relationship(r, rendered_relations);
654654
}
@@ -706,7 +706,7 @@ void generator::generate_relationships(
706706
std::stringstream all_relations_str;
707707
std::set<std::string> unique_relations;
708708

709-
for (const auto &r : c.relationships()) {
709+
for (const auto &r : model().relationships(c.id())) {
710710
LOG_DBG("== Processing relationship {}", to_string(r.type()));
711711

712712
std::stringstream relstr;

src/class_diagram/generators/plantuml/class_diagram_generator.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
184184
std::set<std::string> rendered_relations;
185185

186186
std::stringstream all_relations_str;
187-
for (const auto &r : c.relationships()) {
187+
for (const auto &r : model().relationships(c.id())) {
188188
try {
189189
generate_relationship(r, rendered_relations);
190190
}
@@ -417,7 +417,7 @@ void generator::generate(const objc_interface &c, std::ostream &ostr) const
417417
std::set<std::string> rendered_relations;
418418

419419
std::stringstream all_relations_str;
420-
for (const auto &r : c.relationships()) {
420+
for (const auto &r : model().relationships(c.id())) {
421421
try {
422422
generate_relationship(r, rendered_relations);
423423
}
@@ -585,7 +585,7 @@ void generator::generate_relationships(
585585
std::stringstream all_relations_str;
586586
std::set<std::string> unique_relations;
587587

588-
for (const auto &r : c.relationships()) {
588+
for (const auto &r : model().relationships(c.id())) {
589589
LOG_TRACE("== Processing relationship {}",
590590
plantuml_common::to_plantuml(r, config()));
591591

@@ -664,7 +664,7 @@ void generator::generate_relationships(
664664
std::stringstream all_relations_str;
665665
std::set<std::string> unique_relations;
666666

667-
for (const auto &r : c.relationships()) {
667+
for (const auto &r : model().relationships(c.id())) {
668668
if (!model().should_include(r.type()))
669669
continue;
670670

@@ -751,7 +751,7 @@ void generator::generate(const enum_ &e, std::ostream &ostr) const
751751

752752
void generator::generate_relationships(const enum_ &e, std::ostream &ostr) const
753753
{
754-
for (const auto &r : e.relationships()) {
754+
for (const auto &r : model().relationships(e.id())) {
755755
eid_t destination{};
756756
std::stringstream relstr;
757757
try {
@@ -803,7 +803,7 @@ void generator::generate_relationships(
803803
std::stringstream all_relations_str;
804804
std::set<std::string> unique_relations;
805805

806-
for (const auto &r : c.relationships()) {
806+
for (const auto &r : model().relationships(c.id())) {
807807
LOG_TRACE("== Processing relationship {}",
808808
plantuml_common::to_plantuml(r, config()));
809809

src/class_diagram/model/diagram.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ void diagram::get_parents(
161161
{
162162
bool found_new{false};
163163
for (const auto &parent : parents) {
164-
for (const auto &rel : parent.get().relationships()) {
164+
for (const auto &rel : relationships(parent.get().id())) {
165165
if (rel.type() != common::model::relationship_t::kExtension)
166166
continue;
167167

@@ -246,16 +246,19 @@ void diagram::remove_redundant_dependencies()
246246
for (const auto &el : elements_view) {
247247
std::set<eid_t> dependency_relationships_to_remove;
248248

249-
for (auto &r : el.get().relationships()) {
249+
for (auto &r : relationships(el.get().id())) {
250250
if (r.type() != relationship_t::kDependency)
251251
dependency_relationships_to_remove.emplace(r.destination());
252252
}
253253

254-
util::erase_if(el.get().relationships(),
254+
util::erase_if(relationships(),
255255
[&dependency_relationships_to_remove, &el](const auto &r) {
256256
if (r.type() != relationship_t::kDependency)
257257
return false;
258258

259+
if (r.source() != el.get().id())
260+
return false;
261+
259262
auto has_another_relationship_to_destination =
260263
dependency_relationships_to_remove.count(
261264
r.destination()) > 0;
@@ -270,6 +273,8 @@ void diagram::remove_redundant_dependencies()
270273

271274
void diagram::apply_filter()
272275
{
276+
common::model::apply_filter(relationships(), filter());
277+
273278
// First find all element ids which should be removed
274279
std::set<eid_t> to_remove;
275280

@@ -299,6 +304,14 @@ void diagram::apply_filter()
299304
for (const auto &el : elements_view)
300305
el.get().apply_filter(filter(), to_remove);
301306
});
307+
308+
auto &rels = relationships();
309+
rels.erase(std::remove_if(std::begin(rels), std::end(rels),
310+
[&to_remove](auto &&r) {
311+
return to_remove.count(r.source()) > 0 ||
312+
to_remove.count(r.destination()) > 0;
313+
}),
314+
std::end(rels));
302315
}
303316

304317
bool diagram::is_empty() const

0 commit comments

Comments
 (0)