Commit 0d2a755f authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

WIP

parent fcd2a3bf
Pipeline #40683 failed with stage
in 1 minute and 7 seconds
......@@ -5,7 +5,7 @@ set(gui_sources
application.cpp application.hpp dialog.cpp dialog.hpp imnodes.cpp
imnodes.h imnodes_internal.h implot.h implot.cpp internal.hpp internal.cpp
node-editor.cpp simulation-editor.cpp sources.cpp window-logger.cpp
${PROJECT_SOURCE_DIR}/../../lib/modeling.cpp
${PROJECT_SOURCE_DIR}/../../lib/src/modeling.cpp
${PROJECT_SOURCE_DIR}/../../external/imgui/imgui.cpp
${PROJECT_SOURCE_DIR}/../../external/imgui/imgui.h
${PROJECT_SOURCE_DIR}/../../external/imgui/imgui_demo.cpp
......
......@@ -10,11 +10,11 @@
namespace irt {
enum class component_id = u64;
enum class component_ref_id = u64;
enum class parameter_id = u64;
enum class component_id : u64;
enum class component_ref_id : u64;
enum class parameter_id : u64;
enum class vertice_type : i8
enum class child_type : i8
{
model,
component
......@@ -44,16 +44,16 @@ struct parameter
};
template<typename DataArray, typename T, typename Function>
void for_each(const DataArra& data, vector<T>& vec, Function&& f)
void for_each(const DataArray& data, vector<T>& vec, Function&& f)
{
i64 i = 0, e = v.size();
i64 i = 0, e = vec.size();
while (i != e) {
if (auto* ptr = data.try_to_get(vec[i]); ptr) {
f(*ptr);
++i;
} else {
v.swap_pop_back(i);
vec.swap_pop_back(i);
}
}
}
......@@ -62,8 +62,8 @@ struct connection
{
u64 src; // model_id or component_id
u64 dst; // model_id or component_id
node_type type_src; // model or component
node_type type_dst; // model or component
child_type type_src; // model or component
child_type type_dst; // model or component
i8 port_src; // output port index
i8 port_dst; // input port index
connection_type type;
......@@ -83,8 +83,8 @@ struct child
child(model_id model) noexcept;
child(component_ref_id component) noexcept;
u64 id;
node_type type;
u64 id;
child_type type;
};
struct port
......@@ -92,9 +92,9 @@ struct port
port(model_id model, i8 port) noexcept;
port(component_ref_id component, i8 port) noexcept;
u64 id;
node_type type;
i8 port;
u64 id; // model_id or component_ref_id
child_type type; // allow to choose id
i8 index; // index of the port
};
struct component
......@@ -111,7 +111,7 @@ struct component
struct modeling
{
data_array<model, model_id> models;
data_array<component_ref, component_ref_id> components;
data_array<component_ref, component_ref_id> component_refs;
data_array<component, component_id> components;
data_array<parameter, parameter_id> parameters;
data_array<observer, observer_id> observers;
......@@ -133,16 +133,16 @@ inline child::child(component_ref_id component) noexcept
, type{ child_type::component }
{}
inline port::port(model_id model, i8 port) noexcept
inline port::port(model_id model, i8 port_) noexcept
: id{ ordinal(model) }
, type{ child_type::model }
, port{ port_ }
, index{ port_ }
{}
inline port::port(component_ref_id component, i8 port) noexcept
inline port::port(component_ref_id component, i8 port_) noexcept
: id{ ordinal(component) }
, type{ child_type::component }
, port{ port_ }
, index{ port_ }
{}
} // namespace irt
......
......@@ -6,17 +6,17 @@
namespace irt {
static status build_models_recursively(const modeling& mod,
const component_ref& comp_ref,
simulation& sim)
static status build_models_recursively(const modeling& mod,
component_ref& comp_ref,
simulation& sim)
{
comp_ref.mappers.clear();
comp_ref.mappers.data.clear();
auto* comp = mod.components.try_to_get(comp_ref.id);
if (!comp)
return status::success; /* @TODO certainly an error in API */
for (i64 i = 0, e = comp->children.size(); i != e; ++i) {
for (i32 i = 0, e = comp->children.ssize(); i != e; ++i) {
u64 id = comp->children[i].id;
if (comp->children[i].type == child_type::model) {
......@@ -28,7 +28,7 @@ static status build_models_recursively(const modeling& mod,
irt_return_if_fail(sim.models.can_alloc(1),
status::simulation_not_enough_model);
auto& dst = sim.models.clone(*src);
auto& dst = sim.clone(*src);
auto dst_id = sim.models.get_id(dst);
irt_return_if_bad(
......@@ -46,30 +46,29 @@ static status build_models_recursively(const modeling& mod,
return status::success;
}
static status build_simulation_connection(const component& src,
model_id mdl_src,
i8 port_src,
const component& dst,
model_id mdl_dst,
i8 port_dst,
simulation& sim)
static status build_simulation_connection(const component_ref& src,
model_id mdl_src,
i8 port_src,
const component_ref& dst,
model_id mdl_dst,
i8 port_dst,
simulation& sim)
{
auto* src_map_id = src.mappers.get(mdl_src);
auto* dst_map_id = dst.mappers.get(mdl_dst);
irt_return_if_fail(src_map_id && dst_map_id,
status::model_connect_unknown_dynamics);
status::model_connect_bad_dynamics);
auto* src_sim = sim.models.try_to_get(*src_map_id);
auto* dst_sim = sim.models.try_to_get(*dst_map_id);
irt_return_if_fail(src_sim && dst_sim,
status::model_connect_unknown_dynamics);
irt_return_if_fail(src_sim && dst_sim, status::model_connect_bad_dynamics);
return sim.connect(*src_sim, port_src, *dst_sim, port_dst);
}
static status build_model_to_model_connection(const modeling& mod,
static status build_model_to_model_connection(modeling& mod,
const component_ref& comp_ref,
simulation& sim,
int i)
......@@ -116,6 +115,8 @@ static bool found_input_port(const modeling& mod,
model_found = *mapped_id;
port_found = c->x[port].port;
return true;
} else {
return false;
}
}
......@@ -124,6 +125,42 @@ static bool found_input_port(const modeling& mod,
}
}
static bool found_output_port(const modeling& mod,
component_ref_id compo_ref,
i8 port,
model_id& model_found,
i8& port_found)
{
for (;;) {
auto* c_ref = mod.component_ref.try_to_get(compo_ref);
if (!c_ref)
return false;
auto* c = mod.components.try_to_get(c_ref->id);
if (!c)
return false;
if (!(0 <= port && port < c->y.size()))
return false;
if (c->y[port].type == child_type::model) {
auto id = enum_cast<model_id>(c->y[port].id);
auto* mapped_id = c_ref->mappers.find(model_found);
if (mapped_id) {
model_found = *mapped_id;
port_found = c->y[port].port;
return true;
} else {
return false;
}
}
compo_ref = enum_cast<component_ref_id>(c->y[port].id);
port = c->y[port].port;
}
}
static status build_model_to_component_connection(
const modeling& mod,
const component_ref& compo_ref,
......@@ -158,17 +195,17 @@ static status build_model_to_component_connection(
return status::success; // @todo fail
}
static status build_connections_recursively(const modeling& mod,
const component_ref& compo_ref,
simulation& sim)
static status build_connections_recursively(modeling& mod,
component_ref& c_ref,
simulation& sim)
{
auto* compo = m.components.try_to_get(compo_ref.id);
auto* compo = mod.components.try_to_get(c_ref.id);
if (!compo)
return status::success; // @todo certainly an error
// Build connections from the leaf to the head of the component
// hierarchy.
for (i64 i = 0, e = compo->children.size(); i != e; ++i) {
for (i32 i = 0, e = compo->children.ssize(); i != e; ++i) {
if (compo->children[i].type == child_type::component) {
u64 id = compo->children[i].id;
auto child_c_ref_id = enum_cast<component_ref_id>(id);
......@@ -180,25 +217,22 @@ static status build_connections_recursively(const modeling& mod,
}
}
for (i64 i = 0, e = compo->connections.size(); i != e; ++i) {
auto src = compo->connections[i].src;
auto dst = compo->connections[i].dst;
for (i32 i = 0, e = compo->connections.ssize(); i != e; ++i) {
if (compo->connections[i].type_src == child_type::model) {
if (compo->connections[i].type_dst == child_type::model) {
irt_return_if_bad(
build_model_to_model_connection(mod, comp, sim, i));
build_model_to_model_connection(mod, compo, sim, i));
} else {
irt_return_if_bad(
build_model_to_component_connection(mod, comp, sim, i));
build_model_to_component_connection(mod, compo, sim, i));
}
} else {
if (compo->connections[i].type_dst == child_type::model) {
irt_return_if_bad(
buid_component_to_model_connection(mod, comp, sim, i));
buid_component_to_model_connection(mod, compo, sim, i));
} else {
irt_return_if_bad(
buid_component_to_component_connection(mod, comp, sim, i));
buid_component_to_component_connection(mod, compo, sim, i));
}
}
}
......@@ -206,23 +240,20 @@ static status build_connections_recursively(const modeling& mod,
return status::success;
}
static status build_models(const modeling& mod,
const component_ref& comp,
simulation& sim)
static status build_models(modeling& mod, component_ref& c_ref, simulation& sim)
{
irt_return_if_bad(build_models_recursively(mod, component_ref, sim));
irt_return_if_bad(build_connections_recursively(mod, component_ref, sim));
irt_return_if_bad(build_models_recursively(mod, c_ref, sim));
irt_return_if_bad(build_connections_recursively(mod, c_ref, sim));
return status::success;
}
status build_simulation(const modeling& mod, simulation& sim)
status build_simulation(modeling& mod, simulation& sim)
{
auto* component_ref = mod.component_refs.try_to_get(mod.head);
if (!component_refs)
return status::success;
if (auto* c_ref = mod.component_refs.try_to_get(mod.head); c_ref)
return build_models(mod, *c_ref, sim);
return build_models_from_component(mod, *component_ref, sim);
return status::success;
}
} // namespace irt
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment