Skip to content

Commit

Permalink
engine(databroker): Add signals_autocompletion.lua to simulation result
Browse files Browse the repository at this point in the history
  • Loading branch information
clsim authored and cassava committed Apr 22, 2024
1 parent 969b2a1 commit bac7d4f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
26 changes: 25 additions & 1 deletion engine/src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1459,13 +1459,35 @@ cloe::Json dump_signals(cloe::DataBroker& db) {
std::copy(signal->names().begin(), signal->names().end(),
std::back_inserter(signalreport.names));

const auto& metadata = signal->metadata();
const auto& metadata = signal->metadatas();
}

auto json = cloe::Json{report};
return json;
}

std::vector<std::string> dump_signals_autocompletion(cloe::DataBroker& db) {
auto result = std::vector<std::string>{};
result.emplace_back("---@meta");
result.emplace_back("---@class signals");

const auto& signals = db.signals();
for (const auto& [key, signal] : signals) {
const auto* tag = signal->metadata<cloe::LuaAutocompletionTag>();
if (tag) {
const auto* lua_type = to_ASCIIZ(tag->datatype);
const auto& lua_helptext = tag->text;
auto line = fmt::format("---@field {} {} {}", key, lua_type, lua_helptext);
result.emplace_back(std::move(line));
} else {
auto line = fmt::format("---@field {}", key);
result.emplace_back(std::move(line));
}
}
//
return result;
}

SimulationResult Simulation::run() {
// Input:
SimulationContext ctx{lua_.lua_state()};
Expand Down Expand Up @@ -1579,6 +1601,7 @@ SimulationResult Simulation::run() {
r.triggers = ctx.coordinator->history();
r.report = sol::object(ctx.lua["cloe"]["state"]["report"]);
r.signals = dump_signals(*ctx.db);
r.signals_autocompletion = dump_signals_autocompletion(*ctx.db);

abort_fn_ = nullptr;
return r;
Expand All @@ -1605,6 +1628,7 @@ size_t Simulation::write_output(const SimulationResult& r) const {
write_file(r.config.engine.output_file_config, r.config);
write_file(r.config.engine.output_file_triggers, r.triggers);
write_file(r.config.engine.output_file_signals, r.signals);
write_file(r.config.engine.output_file_signals_autocompletion, r.signals_autocompletion);
logger()->info("Wrote {} output files.", files_written);

return files_written;
Expand Down
2 changes: 2 additions & 0 deletions engine/src/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ struct SimulationResult {
cloe::Json triggers;
cloe::Json report;
cloe::Json signals; // dump of all signals in DataBroker right before the simulation started
std::vector<std::string>
signals_autocompletion; // pseudo lua file used for vscode autocompletion
boost::optional<boost::filesystem::path> output_dir;

public:
Expand Down
2 changes: 2 additions & 0 deletions engine/src/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ struct EngineConf : public Confable {
boost::optional<boost::filesystem::path> output_file_result{"result.json"};
boost::optional<boost::filesystem::path> output_file_triggers{"triggers.json"};
boost::optional<boost::filesystem::path> output_file_signals{"signals.json"};
boost::optional<boost::filesystem::path> output_file_signals_autocompletion{
"signals_autocompletion.lua"};
boost::optional<boost::filesystem::path> output_file_data_stream;
bool output_clobber_files{true};

Expand Down
32 changes: 29 additions & 3 deletions runtime/include/cloe/data_broker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,33 @@ class MetaInformation {
* Signal-Metainformation for generation of Lua documentation
*/
struct LuaAutocompletionTag : MetaInformation::Tag<LuaAutocompletionTag> {
/**
* X-Macro style enum-to-string conversion
*/
#define LUADATATYPE_LIST \
X(Number, 1) \
X(String, 2)

enum class LuaDatatype {
Number,
String,
#define X(name, value) name = value,
LUADATATYPE_LIST
#undef X
};

friend const char* to_ASCIIZ(LuaDatatype type) {
switch (type) {
#define X(name, value) \
case LuaDatatype::name: \
return #name;
LUADATATYPE_LIST
#undef X
default:
return {};
}
}

#undef LUADATATYPE_LIST

/**
* Lua datatype of the signal
*/
Expand Down Expand Up @@ -874,9 +896,13 @@ class Signal {
* \returns bool expressing the presence of the tag, if typename T::tag_type == void
*/
template <typename T>
auto get_metadata() -> decltype(metainformations_.get<T>()) {
auto metadata() -> decltype(metainformations_.get<T>()) {
return metainformations_.get<T>();
}
/**
* Get all tags of the signal
*/
const MetaInformation& metadatas() const { return metainformations_; }

private:
/**
Expand Down
8 changes: 4 additions & 4 deletions runtime/src/cloe/data_broker_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,10 @@ TEST(metainformations, metainformation_3) {
signal->add_metadata<principal_tag>(3.1415);
signal->add_metadata<frivolous_tag>();
// expected I
EXPECT_EQ(*signal->get_metadata<important_tag>(), 1);
EXPECT_EQ(*signal->get_metadata<paramount_tag>(), "Hello World");
EXPECT_EQ(*signal->get_metadata<principal_tag>(), 3.1415);
EXPECT_TRUE(signal->get_metadata<frivolous_tag>());
EXPECT_EQ(*signal->metadata<important_tag>(), 1);
EXPECT_EQ(*signal->metadata<paramount_tag>(), "Hello World");
EXPECT_EQ(*signal->metadata<principal_tag>(), 3.1415);
EXPECT_TRUE(signal->metadata<frivolous_tag>());
}

// Test Scenario: positive-test
Expand Down

0 comments on commit bac7d4f

Please sign in to comment.