Refactored sequence diagram model classes

This commit is contained in:
Bartek Kryza
2022-12-11 21:21:27 +01:00
parent 29b679b0a4
commit e5e7df43e8
12 changed files with 377 additions and 233 deletions

View File

@@ -31,7 +31,7 @@ common::model::diagram_t diagram::type() const
common::optional_ref<common::model::diagram_element> diagram::get(
const std::string &full_name) const
{
for (const auto &[id, participant] : participants) {
for (const auto &[id, participant] : participants_) {
if (participant->full_name(false) == full_name)
return {*participant};
}
@@ -42,8 +42,8 @@ common::optional_ref<common::model::diagram_element> diagram::get(
common::optional_ref<common::model::diagram_element> diagram::get(
const common::model::diagram_element::id_t id) const
{
if (participants.find(id) != participants.end())
return {*participants.at(id)};
if (participants_.find(id) != participants_.end())
return {*participants_.at(id)};
return {};
}
@@ -62,7 +62,7 @@ inja::json diagram::context() const
inja::json::array_t elements{};
// Add classes
for (const auto &[id, p] : participants) {
for (const auto &[id, p] : participants_) {
elements.emplace_back(p->context());
}
@@ -71,46 +71,29 @@ inja::json diagram::context() const
return ctx;
}
void diagram::print() const
void diagram::add_participant(std::unique_ptr<participant> p)
{
LOG_DBG(" --- Participants ---");
for (const auto &[id, participant] : participants) {
LOG_DBG("{} - {}", id, participant->to_string());
const auto participant_id = p->id();
if (participants_.find(participant_id) == participants_.end()) {
LOG_DBG("Adding '{}' participant: {}, {} [{}]", p->type_name(),
p->full_name(false), p->id(),
p->type_name() == "method"
? dynamic_cast<method *>(p.get())->method_name()
: "");
participants_.emplace(participant_id, std::move(p));
}
}
LOG_DBG(" --- Activities ---");
for (const auto &[from_id, act] : sequences) {
void diagram::add_active_participant(common::model::diagram_element::id_t id)
{
active_participants_.emplace(id);
}
LOG_DBG("Sequence id={}:", from_id);
const auto &from_activity = *(participants.at(from_id));
LOG_DBG(" Activity id={}, from={}:", act.from,
from_activity.full_name(false));
for (const auto &message : act.messages) {
if (participants.find(message.from) == participants.end())
continue;
const auto &from_participant = *participants.at(message.from);
if (participants.find(message.to) == participants.end()) {
LOG_DBG(" Message from={}, from_id={}, "
"to={}, to_id={}, name={}",
from_participant.full_name(false), from_participant.id(),
"__UNRESOLVABLE_ID__", message.to, message.message_name);
}
else {
const auto &to_participant = *participants.at(message.to);
LOG_DBG(" Message from={}, from_id={}, "
"to={}, to_id={}, name={}",
from_participant.full_name(false), from_participant.id(),
to_participant.full_name(false), to_participant.id(),
message.message_name);
}
}
}
activity &diagram::get_activity(common::model::diagram_element::id_t id)
{
return sequences_.at(id);
}
void diagram::add_for_stmt(
@@ -158,17 +141,12 @@ void diagram::add_loop_stmt(
if (current_caller_id == 0)
return;
if (sequences.find(current_caller_id) == sequences.end()) {
activity a;
a.from = current_caller_id;
sequences.insert({current_caller_id, std::move(a)});
if (sequences_.find(current_caller_id) == sequences_.end()) {
activity a{current_caller_id};
sequences_.insert({current_caller_id, std::move(a)});
}
message m;
m.from = current_caller_id;
m.type = type;
sequences[current_caller_id].messages.emplace_back(std::move(m));
get_activity(current_caller_id).add_message({type, current_caller_id});
}
void diagram::end_loop_stmt(
@@ -180,9 +158,7 @@ void diagram::end_loop_stmt(
if (current_caller_id == 0)
return;
message m;
m.from = current_caller_id;
m.type = type;
message m{type, current_caller_id};
message_t loop_type = message_t::kWhile;
@@ -191,10 +167,10 @@ void diagram::end_loop_stmt(
else if (type == message_t::kDoEnd)
loop_type = message_t::kDo;
if (sequences.find(current_caller_id) != sequences.end()) {
auto &current_messages = sequences[current_caller_id].messages;
if (sequences_.find(current_caller_id) != sequences_.end()) {
auto &current_messages = get_activity(current_caller_id).messages();
if (current_messages.back().type == loop_type) {
if (current_messages.back().type() == loop_type) {
current_messages.pop_back();
}
else {
@@ -209,16 +185,12 @@ void diagram::add_if_stmt(
{
using clanguml::common::model::message_t;
if (sequences.find(current_caller_id) == sequences.end()) {
activity a;
a.from = current_caller_id;
sequences.insert({current_caller_id, std::move(a)});
if (sequences_.find(current_caller_id) == sequences_.end()) {
activity a{current_caller_id};
sequences_.insert({current_caller_id, std::move(a)});
}
message m;
m.from = current_caller_id;
m.type = type;
sequences[current_caller_id].messages.emplace_back(std::move(m));
get_activity(current_caller_id).add_message({type, current_caller_id});
}
void diagram::end_if_stmt(
@@ -227,22 +199,20 @@ void diagram::end_if_stmt(
{
using clanguml::common::model::message_t;
message m;
m.from = current_caller_id;
m.type = message_t::kIfEnd;
message m{message_t::kIfEnd, current_caller_id};
if (sequences.find(current_caller_id) != sequences.end()) {
if (sequences_.find(current_caller_id) != sequences_.end()) {
auto &current_messages = sequences[current_caller_id].messages;
auto &current_messages = get_activity(current_caller_id).messages();
// Remove the if/else messages if there were no calls
// added to the diagram between them
auto last_if_it =
std::find_if(current_messages.rbegin(), current_messages.rend(),
[](const message &m) { return m.type == message_t::kIf; });
[](const message &m) { return m.type() == message_t::kIf; });
bool last_if_block_is_empty =
std::none_of(current_messages.rbegin(), last_if_it,
[](const message &m) { return m.type == message_t::kCall; });
[](const message &m) { return m.type() == message_t::kCall; });
if (!last_if_block_is_empty) {
current_messages.emplace_back(std::move(m));
@@ -254,6 +224,88 @@ void diagram::end_if_stmt(
}
}
bool diagram::started() const { return started_; }
void diagram::started(bool s) { started_ = s; }
std::map<common::model::diagram_element::id_t, activity> &diagram::sequences()
{
return sequences_;
}
const std::map<common::model::diagram_element::id_t, activity> &
diagram::sequences() const
{
return sequences_;
}
std::map<common::model::diagram_element::id_t, std::unique_ptr<participant>> &
diagram::participants()
{
return participants_;
}
const std::map<common::model::diagram_element::id_t,
std::unique_ptr<participant>> &
diagram::participants() const
{
return participants_;
}
std::set<common::model::diagram_element::id_t> &diagram::active_participants()
{
return active_participants_;
};
const std::set<common::model::diagram_element::id_t> &
diagram::active_participants() const
{
return active_participants_;
};
void diagram::print() const
{
LOG_DBG(" --- Participants ---");
for (const auto &[id, participant] : participants_) {
LOG_DBG("{} - {}", id, participant->to_string());
}
LOG_DBG(" --- Activities ---");
for (const auto &[from_id, act] : sequences_) {
LOG_DBG("Sequence id={}:", from_id);
const auto &from_activity = *(participants_.at(from_id));
LOG_DBG(" Activity id={}, from={}:", act.from(),
from_activity.full_name(false));
for (const auto &message : act.messages()) {
if (participants_.find(message.from()) == participants_.end())
continue;
const auto &from_participant = *participants_.at(message.from());
if (participants_.find(message.to()) == participants_.end()) {
LOG_DBG(" Message from={}, from_id={}, "
"to={}, to_id={}, name={}, type={}",
from_participant.full_name(false), from_participant.id(),
"__UNRESOLVABLE_ID__", message.to(), message.message_name(),
to_string(message.type()));
}
else {
const auto &to_participant = *participants_.at(message.to());
LOG_DBG(" Message from={}, from_id={}, "
"to={}, to_id={}, name={}, type={}",
from_participant.full_name(false), from_participant.id(),
to_participant.full_name(false), to_participant.id(),
message.message_name(), to_string(message.type()));
}
}
}
}
}
namespace clanguml::common::model {