mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-29 13:57:28 +01:00
Add vehicle graph core: .vhg loader, logic and cab blocks, -graph-run and -graph-catalog
This commit is contained in:
273
vehicle/graph/graph_catalog.cpp
Normal file
273
vehicle/graph/graph_catalog.cpp
Normal file
@@ -0,0 +1,273 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "vehicle/graph/graph_catalog.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace vgraph
|
||||
{
|
||||
|
||||
std::string_view to_string(block_origin const Origin)
|
||||
{
|
||||
switch (Origin)
|
||||
{
|
||||
case block_origin::cab:
|
||||
return "cab";
|
||||
case block_origin::simulator:
|
||||
return "simulator";
|
||||
case block_origin::extracted:
|
||||
return "extracted";
|
||||
case block_origin::native:
|
||||
return "native";
|
||||
case block_origin::interface_profile:
|
||||
return "interface";
|
||||
case block_origin::script:
|
||||
return "script";
|
||||
default:
|
||||
return "primitive";
|
||||
}
|
||||
}
|
||||
|
||||
int block_type::port_index(std::string_view const Key) const
|
||||
{
|
||||
for (std::size_t idx = 0; idx < ports.size(); ++idx)
|
||||
{
|
||||
if (ports[idx].key == Key)
|
||||
{
|
||||
return static_cast<int>(idx);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int block_type::param_index(std::string_view const Key) const
|
||||
{
|
||||
for (std::size_t idx = 0; idx < params.size(); ++idx)
|
||||
{
|
||||
if (params[idx].key == Key)
|
||||
{
|
||||
return static_cast<int>(idx);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
catalog const &catalog::get()
|
||||
{
|
||||
static catalog const instance{[]() {
|
||||
catalog result;
|
||||
register_logic_blocks(result);
|
||||
register_cab_blocks(result);
|
||||
return result;
|
||||
}()};
|
||||
return instance;
|
||||
}
|
||||
|
||||
block_type const *catalog::find(std::string_view const Id) const
|
||||
{
|
||||
for (auto const &type : m_types)
|
||||
{
|
||||
if (type.id == Id)
|
||||
{
|
||||
return &type;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void catalog::add(block_type Type)
|
||||
{
|
||||
m_types.emplace_back(std::move(Type));
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
nlohmann::json to_json(param_value const &Value)
|
||||
{
|
||||
switch (Value.type)
|
||||
{
|
||||
case param_type::boolean:
|
||||
return Value.boolean;
|
||||
case param_type::text:
|
||||
case param_type::choice:
|
||||
return Value.text;
|
||||
default:
|
||||
return Value.number;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string catalog::export_json() const
|
||||
{
|
||||
nlohmann::ordered_json root;
|
||||
root["format"] = "maszyna-vehicle-graph-catalog";
|
||||
root["version"] = 1;
|
||||
root["graph_format"] = std::string{file_format};
|
||||
root["graph_version"] = file_version;
|
||||
|
||||
auto &domains = (root["domains"] = nlohmann::ordered_json::array());
|
||||
for (std::size_t idx = 0; idx < static_cast<std::size_t>(domain::count_); ++idx)
|
||||
{
|
||||
auto const &domaininfo{info(static_cast<domain>(idx))};
|
||||
nlohmann::ordered_json item;
|
||||
item["id"] = std::string{domaininfo.id};
|
||||
item["physical"] = domaininfo.physical;
|
||||
item["forward"] = std::string{domaininfo.forward};
|
||||
item["forward_unit"] = std::string{domaininfo.forward_unit};
|
||||
if (domaininfo.physical)
|
||||
{
|
||||
item["back"] = std::string{domaininfo.back};
|
||||
item["back_unit"] = std::string{domaininfo.back_unit};
|
||||
}
|
||||
domains.push_back(std::move(item));
|
||||
}
|
||||
|
||||
auto &blocks = (root["blocks"] = nlohmann::ordered_json::array());
|
||||
for (auto const &type : m_types)
|
||||
{
|
||||
nlohmann::ordered_json item;
|
||||
item["id"] = type.id;
|
||||
item["category"] = type.category;
|
||||
item["name"] = type.name;
|
||||
item["description"] = type.description;
|
||||
item["origin"] = std::string{to_string(type.origin)};
|
||||
item["deferred"] = type.deferred;
|
||||
auto &ports = (item["ports"] = nlohmann::ordered_json::array());
|
||||
for (auto const &port : type.ports)
|
||||
{
|
||||
nlohmann::ordered_json portitem;
|
||||
portitem["key"] = port.key;
|
||||
portitem["direction"] = (port.direction == port_direction::in ? "in" : "out");
|
||||
portitem["domain"] = std::string{info(port.dom).id};
|
||||
if (port.dom == domain::signal)
|
||||
{
|
||||
portitem["type"] = std::string{to_string(port.type)};
|
||||
}
|
||||
if (false == port.unit.empty())
|
||||
{
|
||||
portitem["unit"] = port.unit;
|
||||
}
|
||||
if (port.direction == port_direction::in)
|
||||
{
|
||||
portitem["optional"] = port.optional;
|
||||
if (port.dom == domain::signal)
|
||||
{
|
||||
portitem["default"] = port.default_value;
|
||||
}
|
||||
}
|
||||
ports.push_back(std::move(portitem));
|
||||
}
|
||||
auto ¶ms = (item["params"] = nlohmann::ordered_json::array());
|
||||
for (auto const ¶m : type.params)
|
||||
{
|
||||
nlohmann::ordered_json paramitem;
|
||||
paramitem["key"] = param.key;
|
||||
paramitem["type"] = std::string{to_string(param.type)};
|
||||
if (false == param.unit.empty())
|
||||
{
|
||||
paramitem["unit"] = param.unit;
|
||||
}
|
||||
paramitem["default"] = to_json(param.default_value);
|
||||
if (param.type == param_type::choice)
|
||||
{
|
||||
paramitem["choices"] = param.choices;
|
||||
}
|
||||
params.push_back(std::move(paramitem));
|
||||
}
|
||||
blocks.push_back(std::move(item));
|
||||
}
|
||||
return root.dump(1, '\t');
|
||||
}
|
||||
|
||||
port_def signal_in(std::string Key, value_type const Type, double const Default, std::string Unit, bool const Optional)
|
||||
{
|
||||
port_def result;
|
||||
result.key = std::move(Key);
|
||||
result.direction = port_direction::in;
|
||||
result.dom = domain::signal;
|
||||
result.type = Type;
|
||||
result.unit = std::move(Unit);
|
||||
result.optional = Optional;
|
||||
result.default_value = Default;
|
||||
return result;
|
||||
}
|
||||
|
||||
port_def signal_out(std::string Key, value_type const Type, std::string Unit)
|
||||
{
|
||||
port_def result;
|
||||
result.key = std::move(Key);
|
||||
result.direction = port_direction::out;
|
||||
result.dom = domain::signal;
|
||||
result.type = Type;
|
||||
result.unit = std::move(Unit);
|
||||
return result;
|
||||
}
|
||||
|
||||
port_def physical_in(std::string Key, domain const Domain, bool const Optional)
|
||||
{
|
||||
port_def result;
|
||||
result.key = std::move(Key);
|
||||
result.direction = port_direction::in;
|
||||
result.dom = Domain;
|
||||
result.optional = Optional;
|
||||
return result;
|
||||
}
|
||||
|
||||
port_def physical_out(std::string Key, domain const Domain)
|
||||
{
|
||||
port_def result;
|
||||
result.key = std::move(Key);
|
||||
result.direction = port_direction::out;
|
||||
result.dom = Domain;
|
||||
return result;
|
||||
}
|
||||
|
||||
param_def number_param(std::string Key, double const Default, std::string Unit)
|
||||
{
|
||||
param_def result;
|
||||
result.key = std::move(Key);
|
||||
result.type = param_type::number;
|
||||
result.unit = std::move(Unit);
|
||||
result.default_value = param_value::make_number(Default);
|
||||
return result;
|
||||
}
|
||||
|
||||
param_def boolean_param(std::string Key, bool const Default)
|
||||
{
|
||||
param_def result;
|
||||
result.key = std::move(Key);
|
||||
result.type = param_type::boolean;
|
||||
result.default_value = param_value::make_boolean(Default);
|
||||
return result;
|
||||
}
|
||||
|
||||
param_def text_param(std::string Key, std::string Default)
|
||||
{
|
||||
param_def result;
|
||||
result.key = std::move(Key);
|
||||
result.type = param_type::text;
|
||||
result.default_value = param_value::make_text(std::move(Default));
|
||||
return result;
|
||||
}
|
||||
|
||||
param_def choice_param(std::string Key, std::vector<std::string> Choices, std::string Default)
|
||||
{
|
||||
param_def result;
|
||||
result.key = std::move(Key);
|
||||
result.type = param_type::choice;
|
||||
result.choices = std::move(Choices);
|
||||
result.default_value = param_value::make_choice(std::move(Default));
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace vgraph
|
||||
Reference in New Issue
Block a user