First working version of JSON sequence diagram generator

This commit is contained in:
Bartek Kryza
2023-03-19 18:29:45 +01:00
parent e620c86f31
commit f0497e934d
42 changed files with 1165 additions and 56 deletions

View File

@@ -20,59 +20,6 @@
#include "util/error.h"
namespace clanguml::common::model {
using nlohmann::json;
void to_json(nlohmann::json &j, const source_location &sl)
{
j = json{{"file", sl.file_relative()}, {"line", sl.line()}};
}
void to_json(nlohmann::json &j, const element &c)
{
j = json{{"id", std::to_string(c.id())}, {"name", c.name()},
{"namespace", c.get_namespace().to_string()}, {"type", c.type_name()},
{"display_name", c.full_name(false)}};
if (const auto &comment = c.comment(); comment)
j["comment"] = comment.value();
if (!c.file().empty()) {
j["source_location"] =
dynamic_cast<const common::model::source_location &>(c);
}
}
void to_json(nlohmann::json &j, const template_parameter &c)
{
j["kind"] = to_string(c.kind());
if (c.type())
j["type"] = c.type().value();
if (c.name())
j["name"] = c.name().value();
if (c.default_value())
j["default"] = c.default_value().value();
j["is_variadic"] = c.is_variadic();
}
void to_json(nlohmann::json &j, const relationship &c)
{
j["type"] = to_string(c.type());
j["destination"] = std::to_string(c.destination());
if (!c.multiplicity_source().empty())
j["multiplicity_source"] = c.multiplicity_source();
if (!c.multiplicity_destination().empty())
j["multiplicity_destination"] = c.multiplicity_destination();
if (c.access() != access_t::kNone)
j["access"] = to_string(c.access());
if (!c.label().empty())
j["label"] = c.label();
if (const auto &comment = c.comment(); comment)
j["comment"] = comment.value();
}
} // namespace clanguml::common::model
namespace clanguml::class_diagram::model {
using nlohmann::json;
void to_json(nlohmann::json &j, const class_element &c)
@@ -170,6 +117,9 @@ void generator::generate(std::ostream &ostr) const
generate_relationships(json_);
json_["name"] = m_model.name();
json_["diagram_type"] = "class";
ostr << json_;
}

View File

@@ -60,6 +60,8 @@ class generator : public common_generator<diagram_config, diagram_model> {
public:
generator(diagram_config &config, diagram_model &model);
void generate(std::ostream &ostr) const override;
void generate(const class_ &c, nlohmann::json &parent) const;
void generate(const enum_ &c, nlohmann::json &parent) const;
@@ -70,8 +72,6 @@ public:
void generate_top_level_elements(nlohmann::json &parent) const;
void generate(std::ostream &ostr) const override;
void generate_relationships(nlohmann::json &parent) const;
void generate_relationships(const class_ &c, nlohmann::json &parent) const;

View File

@@ -25,6 +25,7 @@
#include "config/config.h"
#include "include_diagram/generators/plantuml/include_diagram_generator.h"
#include "package_diagram/generators/plantuml/package_diagram_generator.h"
#include "sequence_diagram/generators/json/sequence_diagram_generator.h"
#include "sequence_diagram/generators/plantuml/sequence_diagram_generator.h"
#include "util/util.h"
#include "version.h"

View File

@@ -0,0 +1,72 @@
/**
* src/common/generators/json/generator.cc
*
* 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.
*/
#include "generator.h"
namespace clanguml::common::model {
using nlohmann::json;
void to_json(nlohmann::json &j, const source_location &sl)
{
j = json{{"file", sl.file_relative()}, {"line", sl.line()}};
}
void to_json(nlohmann::json &j, const element &c)
{
j = json{{"id", std::to_string(c.id())}, {"name", c.name()},
{"namespace", c.get_namespace().to_string()}, {"type", c.type_name()},
{"display_name", c.full_name(false)}};
if (const auto &comment = c.comment(); comment)
j["comment"] = comment.value();
if (!c.file().empty()) {
j["source_location"] =
dynamic_cast<const common::model::source_location &>(c);
}
}
void to_json(nlohmann::json &j, const template_parameter &c)
{
j["kind"] = to_string(c.kind());
if (c.type())
j["type"] = c.type().value();
if (c.name())
j["name"] = c.name().value();
if (c.default_value())
j["default"] = c.default_value().value();
j["is_variadic"] = c.is_variadic();
}
void to_json(nlohmann::json &j, const relationship &c)
{
j["type"] = to_string(c.type());
j["destination"] = std::to_string(c.destination());
if (!c.multiplicity_source().empty())
j["multiplicity_source"] = c.multiplicity_source();
if (!c.multiplicity_destination().empty())
j["multiplicity_destination"] = c.multiplicity_destination();
if (c.access() != access_t::kNone)
j["access"] = to_string(c.access());
if (!c.label().empty())
j["label"] = c.label();
if (const auto &comment = c.comment(); comment)
j["comment"] = comment.value();
}
} // namespace clanguml::common::model

View File

@@ -29,6 +29,18 @@
#include <ostream>
namespace clanguml::common::model {
using nlohmann::json;
void to_json(nlohmann::json &j, const source_location &sl);
void to_json(nlohmann::json &j, const element &c);
void to_json(nlohmann::json &j, const template_parameter &c);
void to_json(nlohmann::json &j, const relationship &c);
} // namespace clanguml::common::model
namespace clanguml::common::generators::json {
using clanguml::common::model::access_t;

View File

@@ -136,6 +136,19 @@ std::string to_string(const diagram_t t)
}
}
std::string to_string(const message_scope_t t)
{
switch (t) {
case message_scope_t::kNormal:
return "normal";
case message_scope_t::kCondition:
return "condition";
default:
assert(false);
return "";
}
}
diagram_t from_string(const std::string &s)
{
if (s == "class")

View File

@@ -80,6 +80,8 @@ std::string to_string(message_t m);
std::string to_string(diagram_t r);
std::string to_string(message_scope_t);
diagram_t from_string(const std::string &s);
} // namespace clanguml::common::model

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,81 @@
/**
* src/sequence_diagram/generators/json/sequence_diagram_generator.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.
*/
#pragma once
#include "common/generators/json/generator.h"
#include "config/config.h"
#include "sequence_diagram/model/diagram.h"
#include "util/util.h"
#include <glob/glob.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <sstream>
namespace clanguml::sequence_diagram::generators::json {
using diagram_config = clanguml::config::sequence_diagram;
using diagram_model = clanguml::sequence_diagram::model::diagram;
template <typename C, typename D>
using common_generator = clanguml::common::generators::json::generator<C, D>;
class generator : public common_generator<diagram_config, diagram_model> {
public:
generator(diagram_config &config, diagram_model &model);
void generate_call(const sequence_diagram::model::message &m,
nlohmann::json &parent) const;
common::id_t generate_participant(
nlohmann::json &parent, common::id_t id, bool force = false) const;
void generate_participant(
nlohmann::json &parent, const std::string &name) const;
void generate_activity(common::model::diagram_element::id_t activity_id,
const sequence_diagram::model::activity &a, nlohmann::json &parent,
std::vector<common::model::diagram_element::id_t> &visited,
std::optional<nlohmann::json> nested_block) const;
void generate(std::ostream &ostr) const override;
nlohmann::json &current_block_statement() const
{
assert(!block_statements_stack_.empty());
return block_statements_stack_.back().get();
}
private:
bool is_participant_generated(common::id_t id) const;
std::string render_name(std::string name) const;
mutable std::set<common::id_t> generated_participants_;
mutable nlohmann::json json_;
mutable std::vector<std::reference_wrapper<nlohmann::json>>
block_statements_stack_;
};
} // namespace clanguml::sequence_diagram::generators::json

View File

@@ -433,6 +433,10 @@ void generator::generate(std::ostream &ostr) const
render_mode =
model::function::message_render_mode::no_arguments;
// For methods or functions in diagrams where they are combined into
// file participants, we need to add an 'entry' point call to know
// which method relates to the first activity for this 'start_from'
// condition
if (from.value().type_name() == "method" ||
m_config.combine_free_functions_into_file_participants()) {
ostr << "[->"