Added support for 'together' option in class diagrams without rendered namespaces

This commit is contained in:
Bartek Kryza
2023-02-04 00:23:51 +01:00
parent b910e9b02f
commit d7d80ab41e
14 changed files with 318 additions and 57 deletions

View File

@@ -479,14 +479,30 @@ void generator::generate(const package &p, std::ostream &ostr) const
} }
else if (dynamic_cast<class_ *>(subpackage.get()) != nullptr) { else if (dynamic_cast<class_ *>(subpackage.get()) != nullptr) {
if (m_model.should_include(*subpackage)) { if (m_model.should_include(*subpackage)) {
generate_alias(dynamic_cast<class_ &>(*subpackage), ostr); auto together_group =
generate(dynamic_cast<class_ &>(*subpackage), ostr); m_config.get_together_group(subpackage->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(subpackage.get());
}
else {
generate_alias(dynamic_cast<class_ &>(*subpackage), ostr);
generate(dynamic_cast<class_ &>(*subpackage), ostr);
}
} }
} }
else if (dynamic_cast<enum_ *>(subpackage.get()) != nullptr) { else if (dynamic_cast<enum_ *>(subpackage.get()) != nullptr) {
if (m_model.should_include(*subpackage)) { if (m_model.should_include(*subpackage)) {
generate_alias(dynamic_cast<enum_ &>(*subpackage), ostr); auto together_group =
generate(dynamic_cast<enum_ &>(*subpackage), ostr); m_config.get_together_group(subpackage->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(subpackage.get());
}
else {
generate_alias(dynamic_cast<enum_ &>(*subpackage), ostr);
generate(dynamic_cast<enum_ &>(*subpackage), ostr);
}
} }
} }
} }
@@ -536,37 +552,71 @@ void generator::generate(std::ostream &ostr) const
generate_plantuml_directives(ostr, m_config.puml().before); generate_plantuml_directives(ostr, m_config.puml().before);
for (const auto &p : m_model) { for (const auto &p : m_model) {
if (dynamic_cast<package *>(p.get()) != nullptr) { if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
const auto &sp = dynamic_cast<package &>(*p); if (!pkg->is_empty())
if (!sp.is_empty()) generate(*pkg, ostr);
generate(sp, ostr);
} }
else if (dynamic_cast<class_ *>(p.get()) != nullptr) { else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {
if (m_model.should_include(*p)) { if (m_model.should_include(*cls)) {
generate_alias(dynamic_cast<class_ &>(*p), ostr); auto together_group =
generate(dynamic_cast<class_ &>(*p), ostr); m_config.get_together_group(cls->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(cls);
}
else {
generate_alias(*cls, ostr);
generate(*cls, ostr);
}
} }
} }
else if (dynamic_cast<enum_ *>(p.get()) != nullptr) { else if (auto *enm = dynamic_cast<enum_ *>(p.get()); enm) {
if (m_model.should_include(*p)) { if (m_model.should_include(*enm)) {
generate_alias(dynamic_cast<enum_ &>(*p), ostr); auto together_group =
generate(dynamic_cast<enum_ &>(*p), ostr); m_config.get_together_group(enm->full_name(false));
if (together_group) {
current_level_together_groups_[together_group.value()]
.push_back(enm);
}
else {
generate_alias(*enm, ostr);
generate(*enm, ostr);
}
} }
} }
} }
for (const auto &p : m_model) { // Now generate any diagram elements which are in together groups
if (dynamic_cast<package *>(p.get()) != nullptr) { for (const auto &[group_name, group_elements] :
generate_relationships(dynamic_cast<package &>(*p), ostr); current_level_together_groups_) {
}
else if (dynamic_cast<class_ *>(p.get()) != nullptr) { ostr << "' together group for " << group_name << "\n";
if (m_model.should_include(*p)) { ostr << "together {\n";
generate_relationships(dynamic_cast<class_ &>(*p), ostr); for (auto *e : group_elements) {
if (auto *cls = dynamic_cast<class_ *>(e); cls) {
generate_alias(*cls, ostr);
generate(*cls, ostr);
}
if (auto *enm = dynamic_cast<enum_ *>(e); enm) {
generate_alias(*enm, ostr);
generate(*enm, ostr);
} }
} }
else if (dynamic_cast<enum_ *>(p.get()) != nullptr) { ostr << "}\n";
if (m_model.should_include(*p)) { }
generate_relationships(dynamic_cast<enum_ &>(*p), ostr);
for (const auto &p : m_model) {
if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
generate_relationships(*pkg, ostr);
}
else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {
if (m_model.should_include(*cls)) {
generate_relationships(*cls, ostr);
}
}
else if (auto *enm = dynamic_cast<enum_ *>(p.get()); enm) {
if (m_model.should_include(*enm)) {
generate_relationships(*enm, ostr);
} }
} }
} }

View File

@@ -83,6 +83,9 @@ public:
private: private:
std::string render_name(std::string name) const; std::string render_name(std::string name) const;
mutable std::map<std::string, std::vector<common::model::element *>>
current_level_together_groups_;
}; };
} // namespace plantuml } // namespace plantuml

View File

@@ -224,15 +224,22 @@ void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
for (const auto &[entity_name, hints] : m_config.layout()) { for (const auto &[entity_name, hints] : m_config.layout()) {
for (const auto &hint : hints) { for (const auto &hint : hints) {
std::stringstream hint_str; std::stringstream hint_str;
// 'together' layout hint is handled separately
if (hint.hint == config::hint_t::together)
continue;
const auto &hint_entity = std::get<std::string>(hint.entity);
try { try {
auto element_opt = m_model.get(entity_name); auto element_opt = m_model.get(entity_name);
if (!element_opt) if (!element_opt)
element_opt = m_model.get((uns | entity_name).to_string()); element_opt = m_model.get((uns | entity_name).to_string());
auto hint_element_opt = m_model.get(hint.entity); auto hint_element_opt = m_model.get(hint_entity);
if (!hint_element_opt) if (!hint_element_opt)
hint_element_opt = hint_element_opt =
m_model.get((uns | hint.entity).to_string()); m_model.get((uns | hint_entity).to_string());
if (!element_opt || !hint_element_opt) if (!element_opt || !hint_element_opt)
continue; continue;
@@ -244,7 +251,7 @@ void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
catch (clanguml::error::uml_alias_missing &e) { catch (clanguml::error::uml_alias_missing &e) {
LOG_DBG("=== Skipping layout hint from {} to {} due " LOG_DBG("=== Skipping layout hint from {} to {} due "
"to: {}", "to: {}",
entity_name, hint.entity, e.what()); entity_name, hint_entity, e.what());
} }
} }
} }

View File

@@ -142,6 +142,31 @@ std::vector<std::string> diagram::get_translation_units() const
return translation_units; return translation_units;
} }
std::optional<std::string> class_diagram::get_together_group(
const std::string &full_name) const
{
const auto relative_name = using_namespace().relative(full_name);
for (const auto &[hint_target, hints] : layout()) {
for (const auto &hint : hints) {
if (hint.hint == hint_t::together) {
const auto &together_others =
std::get<std::vector<std::string>>(hint.entity);
if ((full_name == hint_target) ||
util::contains(together_others, full_name))
return hint_target;
if ((relative_name == hint_target) ||
util::contains(together_others, relative_name))
return hint_target;
}
}
}
return std::nullopt;
}
void diagram::initialize_type_aliases() void diagram::initialize_type_aliases()
{ {
if (type_aliases().count("std::basic_string<char>") == 0U) { if (type_aliases().count("std::basic_string<char>") == 0U) {

View File

@@ -80,13 +80,13 @@ struct filter {
std::vector<std::filesystem::path> paths; std::vector<std::filesystem::path> paths;
}; };
enum class hint_t { up, down, left, right }; enum class hint_t { up, down, left, right, together };
std::string to_string(hint_t t); std::string to_string(hint_t t);
struct layout_hint { struct layout_hint {
hint_t hint{hint_t::up}; hint_t hint{hint_t::up};
std::string entity; std::variant<std::string, std::vector<std::string>> entity;
}; };
using layout_hints = std::map<std::string, std::vector<layout_hint>>; using layout_hints = std::map<std::string, std::vector<layout_hint>>;
@@ -184,6 +184,9 @@ struct class_diagram : public diagram {
option<layout_hints> layout{"layout"}; option<layout_hints> layout{"layout"};
std::optional<std::string> get_together_group(
const std::string &full_name) const;
void initialize_relationship_hints(); void initialize_relationship_hints();
}; };

View File

@@ -468,6 +468,10 @@ template <> struct convert<layout_hint> {
rhs.hint = hint_t::right; rhs.hint = hint_t::right;
rhs.entity = node["right"].as<std::string>(); rhs.entity = node["right"].as<std::string>();
} }
else if (node["together"]) {
rhs.hint = hint_t::together;
rhs.entity = node["together"].as<std::vector<std::string>>();
}
else else
return false; return false;
@@ -645,7 +649,8 @@ config load(
doc["git"] = git_config; doc["git"] = git_config;
} }
return doc.as<config>(); auto d = doc.as<config>();
return d;
} }
catch (YAML::BadFile &e) { catch (YAML::BadFile &e) {
throw std::runtime_error(fmt::format( throw std::runtime_error(fmt::format(

View File

@@ -159,7 +159,13 @@ YAML::Emitter &operator<<(
YAML::Emitter &operator<<(YAML::Emitter &out, const layout_hint &c) YAML::Emitter &operator<<(YAML::Emitter &out, const layout_hint &c)
{ {
out << YAML::BeginMap; out << YAML::BeginMap;
out << YAML::Key << c.hint << YAML::Value << c.entity;
out << YAML::Key << c.hint << YAML::Value;
if (std::holds_alternative<std::string>(c.entity))
out << std::get<std::string>(c.entity);
else if (std::holds_alternative<std::vector<std::string>>(c.entity))
out << std::get<std::vector<std::string>>(c.entity);
out << YAML::EndMap; out << YAML::EndMap;
return out; return out;
} }

19
tests/t00053/.clang-uml Normal file
View File

@@ -0,0 +1,19 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00053_class:
type: class
glob:
- ../../tests/t00053/t00053.cc
include:
namespaces:
- clanguml::t00053
using_namespace:
- clanguml::t00053
layout:
a:
- together: [c,e,f]
A:
- together: [C,E,F]
h:
- together: [j]

38
tests/t00053/t00053.cc Normal file
View File

@@ -0,0 +1,38 @@
namespace clanguml {
namespace t00053 {
struct a {
};
struct b {
};
struct c {
};
struct d {
};
struct e {
};
struct f {
};
struct g {
};
struct A {
};
struct B {
};
struct C {
};
struct D {
};
struct E {
};
struct F {
};
struct G {
};
enum class h { hhh };
enum class i { iii };
enum class j { jjj };
}
}

55
tests/t00053/test_case.h Normal file
View File

@@ -0,0 +1,55 @@
/**
* tests/t00053/test_case.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
TEST_CASE("t00053", "[test-case][class]")
{
auto [config, db] = load_config("t00053");
auto diagram = config.diagrams["t00053_class"];
REQUIRE(diagram->name == "t00053_class");
auto model = generate_class_diagram(*db, diagram);
REQUIRE(model->name() == "t00053_class");
auto puml = generate_class_puml(diagram, *model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
// Check if all classes exist
REQUIRE_THAT(puml, IsClass(_A("a")));
REQUIRE_THAT(puml, IsClass(_A("b")));
REQUIRE_THAT(puml, IsClass(_A("c")));
REQUIRE_THAT(puml, IsClass(_A("d")));
REQUIRE_THAT(puml, IsClass(_A("e")));
REQUIRE_THAT(puml, IsClass(_A("f")));
REQUIRE_THAT(puml, IsClass(_A("g")));
REQUIRE_THAT(puml, IsClass(_A("A")));
REQUIRE_THAT(puml, IsClass(_A("B")));
REQUIRE_THAT(puml, IsClass(_A("C")));
REQUIRE_THAT(puml, IsClass(_A("D")));
REQUIRE_THAT(puml, IsClass(_A("E")));
REQUIRE_THAT(puml, IsClass(_A("F")));
REQUIRE_THAT(puml, IsClass(_A("G")));
save_puml(config.output_directory() + "/" + diagram->name + ".puml", puml);
}

View File

@@ -246,6 +246,7 @@ using namespace clanguml::test::matchers;
#include "t00050/test_case.h" #include "t00050/test_case.h"
#include "t00051/test_case.h" #include "t00051/test_case.h"
#include "t00052/test_case.h" #include "t00052/test_case.h"
#include "t00053/test_case.h"
/// ///
/// Sequence diagram tests /// Sequence diagram tests

View File

@@ -153,6 +153,9 @@ test_cases:
- name: t00052 - name: t00052
title: Test case for template methods rendering title: Test case for template methods rendering
description: description:
- name: t00053
title: Test case for `together` layout hint
description:
Sequence diagrams: Sequence diagrams:
- name: t20001 - name: t20001
title: Basic sequence diagram test case title: Basic sequence diagram test case

View File

@@ -150,6 +150,8 @@ TEST_CASE("Test config includes", "[unit-test]")
TEST_CASE("Test config layout", "[unit-test]") TEST_CASE("Test config layout", "[unit-test]")
{ {
using namespace std::string_literals;
auto cfg = clanguml::config::load("./test_config_data/layout.yml"); auto cfg = clanguml::config::load("./test_config_data/layout.yml");
CHECK(cfg.diagrams.size() == 2); CHECK(cfg.diagrams.size() == 2);
@@ -157,35 +159,78 @@ TEST_CASE("Test config layout", "[unit-test]")
[[maybe_unused]] auto &def = static_cast<clanguml::config::class_diagram &>( [[maybe_unused]] auto &def = static_cast<clanguml::config::class_diagram &>(
*cfg.diagrams["class_main"]); *cfg.diagrams["class_main"]);
auto check_layout = [](const auto &diagram, auto check_class_layout =
const clanguml::common::model::diagram_t type) { [](const clanguml::config::class_diagram &diagram,
CHECK(diagram.type() == type); const clanguml::common::model::diagram_t type) {
CHECK(diagram.type() == type);
CHECK(diagram.layout().at("ABCD").size() == 2); const auto &ABCD = diagram.layout().at("ABCD");
CHECK(diagram.layout().at("ABCD")[0].hint ==
clanguml::config::hint_t::up);
CHECK(diagram.layout().at("ABCD")[0].entity == "ABCD_SUBCLASS");
CHECK(diagram.layout().at("ABCD")[1].hint ==
clanguml::config::hint_t::left);
CHECK(diagram.layout().at("ABCD")[1].entity == "ABCD_SIBLING");
CHECK(diagram.layout().at("ABCD_SIBLING").size() == 2); CHECK(ABCD.size() == 3);
CHECK(diagram.layout().at("ABCD_SIBLING")[0].hint ==
clanguml::config::hint_t::right);
CHECK(diagram.layout().at("ABCD_SIBLING")[0].entity ==
"ABCD_OTHER_SIBLING");
CHECK(diagram.layout().at("ABCD_SIBLING")[1].hint ==
clanguml::config::hint_t::down);
CHECK(diagram.layout().at("ABCD_SIBLING")[1].entity ==
"ABCD_SIBLING_SIBLING");
};
check_layout(static_cast<clanguml::config::class_diagram &>( CHECK(ABCD[0].hint == clanguml::config::hint_t::up);
*cfg.diagrams["class_main"]), CHECK(std::get<std::string>(ABCD[0].entity) == "ABCD_SUBCLASS");
CHECK(ABCD[1].hint == clanguml::config::hint_t::left);
CHECK(std::get<std::string>(ABCD[1].entity) == "ABCD_SIBLING");
CHECK(ABCD[2].hint == clanguml::config::hint_t::together);
CHECK(std::get<std::vector<std::string>>(ABCD[2].entity) ==
std::vector{"A"s, "B"s, "C"s, "D"s});
CHECK(diagram.get_together_group("ABCD").value() == "ABCD");
CHECK(
diagram.get_together_group("clanguml::ABCD").value() == "ABCD");
CHECK(diagram.get_together_group("B").value() == "ABCD");
CHECK(diagram.get_together_group("clanguml::B").value() == "ABCD");
CHECK(!diagram.get_together_group("clanguml::E"));
CHECK(!diagram.get_together_group("E"));
const auto &ABCD_SIBLING = diagram.layout().at("ABCD_SIBLING");
CHECK(ABCD_SIBLING.size() == 2);
CHECK(ABCD_SIBLING[0].hint == clanguml::config::hint_t::right);
CHECK(std::get<std::string>(ABCD_SIBLING[0].entity) ==
"ABCD_OTHER_SIBLING");
CHECK(ABCD_SIBLING[1].hint == clanguml::config::hint_t::down);
CHECK(std::get<std::string>(ABCD_SIBLING[1].entity) ==
"ABCD_SIBLING_SIBLING");
};
auto check_package_layout =
[](const clanguml::config::package_diagram &diagram,
const clanguml::common::model::diagram_t type) {
CHECK(diagram.type() == type);
const auto &ABCD = diagram.layout().at("ABCD");
CHECK(ABCD.size() == 2);
CHECK(ABCD[0].hint == clanguml::config::hint_t::up);
CHECK(std::get<std::string>(ABCD[0].entity) == "ABCD_SUBCLASS");
CHECK(ABCD[1].hint == clanguml::config::hint_t::left);
CHECK(std::get<std::string>(ABCD[1].entity) == "ABCD_SIBLING");
const auto &ABCD_SIBLING = diagram.layout().at("ABCD_SIBLING");
CHECK(ABCD_SIBLING.size() == 2);
CHECK(ABCD_SIBLING[0].hint == clanguml::config::hint_t::right);
CHECK(std::get<std::string>(ABCD_SIBLING[0].entity) ==
"ABCD_OTHER_SIBLING");
CHECK(ABCD_SIBLING[1].hint == clanguml::config::hint_t::down);
CHECK(std::get<std::string>(ABCD_SIBLING[1].entity) ==
"ABCD_SIBLING_SIBLING");
};
check_class_layout(static_cast<clanguml::config::class_diagram &>(
*cfg.diagrams["class_main"]),
clanguml::common::model::diagram_t::kClass); clanguml::common::model::diagram_t::kClass);
check_layout(static_cast<clanguml::config::package_diagram &>( check_package_layout(static_cast<clanguml::config::package_diagram &>(
*cfg.diagrams["package_main"]), *cfg.diagrams["package_main"]),
clanguml::common::model::diagram_t::kPackage); clanguml::common::model::diagram_t::kPackage);
} }

View File

@@ -13,6 +13,7 @@ diagrams:
ABCD: ABCD:
- up: ABCD_SUBCLASS - up: ABCD_SUBCLASS
- left: ABCD_SIBLING - left: ABCD_SIBLING
- together: [A, B, C, D]
ABCD_SIBLING: ABCD_SIBLING:
- right: ABCD_OTHER_SIBLING - right: ABCD_OTHER_SIBLING
- down: ABCD_SIBLING_SIBLING - down: ABCD_SIBLING_SIBLING