From 51e0275db3cbe856ef8a0e86a82df48802a43b32 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Thu, 31 Aug 2023 22:28:25 +0200 Subject: [PATCH] Added '--print-to' cli option to print all possible 'to' constraints in sequence diagrams --- src/cli/cli_handler.cc | 18 +++++++++-------- src/cli/cli_handler.h | 6 ++++-- src/common/generators/generators.cc | 17 ++++++++++++---- src/sequence_diagram/model/diagram.cc | 29 ++++++++++++++++++++++++--- src/sequence_diagram/model/diagram.h | 13 +++++++++--- 5 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/cli/cli_handler.cc b/src/cli/cli_handler.cc index 0d6a31a9..1079aeba 100644 --- a/src/cli/cli_handler.cc +++ b/src/cli/cli_handler.cc @@ -129,8 +129,10 @@ cli_flow_t cli_handler::parse(int argc, const char **argv) "instead of actual location of `.clang-uml` file."); app.add_flag("--no-metadata", no_metadata, "Skip metadata (e.g. clang-uml version) from diagrams"); - app.add_flag("--print-start-from", print_start_from, - "Print all possible 'start_from' values for a given diagram"); + app.add_flag("--print-from,--print-start-from", print_from, + "Print all possible 'from' values for a given diagram"); + app.add_flag("--print-to", print_to, + "Print all possible 'to' values for a given diagram"); app.add_flag("--no-validate", no_validate, "Do not perform configuration file schema validation"); app.add_flag("--validate-only", validate_only, @@ -149,7 +151,7 @@ cli_flow_t cli_handler::parse(int argc, const char **argv) exit(app.exit(e)); // NOLINT(concurrency-mt-unsafe) } - if (quiet || dump_config || print_start_from) + if (quiet || dump_config || print_from || print_to) verbose = 0; else verbose++; @@ -202,11 +204,10 @@ cli_flow_t cli_handler::handle_pre_config_options() return cli_flow_t::kError; } - if (print_start_from) { + if (print_from || print_to) { if (diagram_names.size() != 1) { - LOG_ERROR( - "ERROR: '--print-start-from' requires specifying one diagram " - "name using '-n' option"); + LOG_ERROR("ERROR: '--print-from' and '--print-to' require " + "specifying one diagram name using '-n' option"); return cli_flow_t::kError; } @@ -346,7 +347,8 @@ runtime_config cli_handler::get_runtime_config() const runtime_config cfg; cfg.generators = generators; cfg.verbose = verbose; - cfg.print_start_from = print_start_from; + cfg.print_from = print_from; + cfg.print_to = print_to; cfg.progress = progress; cfg.thread_count = thread_count; diff --git a/src/cli/cli_handler.h b/src/cli/cli_handler.h index 5287dce8..4f0dc2e6 100644 --- a/src/cli/cli_handler.h +++ b/src/cli/cli_handler.h @@ -33,7 +33,8 @@ namespace clanguml::cli { struct runtime_config { int verbose{}; std::vector generators{}; - bool print_start_from{}; + bool print_from{}; + bool print_to{}; bool progress{}; unsigned int thread_count{}; }; @@ -173,7 +174,8 @@ public: std::optional add_include_diagram; std::optional add_diagram_from_template; bool dump_config{false}; - bool print_start_from{false}; + bool print_from{false}; + bool print_to{false}; std::optional paths_relative_to_pwd{}; std::vector template_variables{}; bool list_templates{false}; diff --git a/src/common/generators/generators.cc b/src/common/generators/generators.cc index 582540ac..d417f113 100644 --- a/src/common/generators/generators.cc +++ b/src/common/generators/generators.cc @@ -95,11 +95,20 @@ void generate_diagram_impl(const std::string &od, const std::string &name, std::move(progress)); if constexpr (std::is_same_v) { - if (rc.print_start_from) { - auto start_from_values = model->list_start_from_values(); + if (rc.print_from) { + auto from_values = model->list_from_values(); - for (const auto &start_from : start_from_values) { - std::cout << start_from << std::endl; + for (const auto &from : from_values) { + std::cout << from << std::endl; + } + + return; + } + if (rc.print_to) { + auto to_values = model->list_to_values(); + + for (const auto &to : to_values) { + std::cout << "|" << to << "|" << std::endl; } return; diff --git a/src/sequence_diagram/model/diagram.cc b/src/sequence_diagram/model/diagram.cc index 963843bd..00afcb19 100644 --- a/src/sequence_diagram/model/diagram.cc +++ b/src/sequence_diagram/model/diagram.cc @@ -193,18 +193,41 @@ bool diagram::should_include( dynamic_cast(p)); } -std::vector diagram::list_start_from_values() const +std::vector diagram::list_from_values() const { std::vector result; for (const auto &[from_id, act] : sequences_) { const auto &from_activity = *(participants_.at(from_id)); - - result.push_back(from_activity.full_name(false)); + const auto &full_name = from_activity.full_name(false); + if (!full_name.empty()) + result.push_back(full_name); } std::sort(result.begin(), result.end()); + result.erase(std::unique(result.begin(), result.end()), result.end()); + + return result; +} + +std::vector diagram::list_to_values() const +{ + std::vector result; + + for (const auto &[from_id, act] : sequences_) { + for (const auto &m : act.messages()) { + if (participants_.count(m.to()) > 0) { + const auto &to_activity = *(participants_.at(m.to())); + const auto &full_name = to_activity.full_name(false); + if (!full_name.empty()) + result.push_back(full_name); + } + } + } + + std::sort(result.begin(), result.end()); + result.erase(std::unique(result.begin(), result.end()), result.end()); return result; } diff --git a/src/sequence_diagram/model/diagram.h b/src/sequence_diagram/model/diagram.h index 6e1a128d..897c7791 100644 --- a/src/sequence_diagram/model/diagram.h +++ b/src/sequence_diagram/model/diagram.h @@ -232,11 +232,18 @@ public: bool should_include(const sequence_diagram::model::participant &p) const; /** - * @brief Get list of all possible 'start_from' values in the model + * @brief Get list of all possible 'from' values in the model * - * @return List of all possible 'start_from' values + * @return List of all possible 'from' values */ - std::vector list_start_from_values() const; + std::vector list_from_values() const; + + /** + * @brief Get list of all possible 'to' values in the model + * + * @return List of all possible 'to' values + */ + std::vector list_to_values() const; /** * @brief Generate a list of message chains matching a from_to constraint