diff --git a/lib/include/irritator/modeling.hpp b/lib/include/irritator/modeling.hpp index e38f29cd..6f994520 100644 --- a/lib/include/irritator/modeling.hpp +++ b/lib/include/irritator/modeling.hpp @@ -765,6 +765,26 @@ struct component { return ret; } + vector get_x_starts_with(std::string_view str) const noexcept + { + vector ret; + + x.for_each([&](const auto id, const auto& name) noexcept { + if (name.sv() == str) { + ret.emplace_back(id); + } else { + const auto str = name.sv(); + const auto pos = str.find_last_of('_'); + + if (pos != std::string_view::npos and + str.substr(0, pos) == name) + ret.emplace_back(id); + } + }); + + return ret; + } + port_id get_y(std::string_view str) const noexcept { auto ret = undefined(); @@ -777,6 +797,26 @@ struct component { return ret; } + vector get_y_starts_with(std::string_view str) const noexcept + { + vector ret; + + y.for_each([&](const auto id, const auto& name) noexcept { + if (name.sv() == str) { + ret.emplace_back(id); + } else { + const auto str = name.sv(); + const auto pos = str.find_last_of('_'); + + if (pos != std::string_view::npos and + str.substr(0, pos) == name) + ret.emplace_back(id); + } + }); + + return ret; + } + port_id get_or_add_x(std::string_view str) noexcept { const auto id = get_x(str); diff --git a/lib/src/modeling-grid.cpp b/lib/src/modeling-grid.cpp index 302d6261..e2ee2ef6 100644 --- a/lib/src/modeling-grid.cpp +++ b/lib/src/modeling-grid.cpp @@ -126,6 +126,23 @@ static auto build_grid_children(modeling& mod, grid_component& grid) noexcept return ret; } +struct split_name { + split_name(std::string_view str) noexcept + { + const auto pos = str.find_last_of('_'); + if (pos != std::string_view::npos and + std::cmp_less(pos + 1, str.size())) { + left = str.substr(0, pos); + right = str.substr(pos + 1, std::string::npos); + } else { + left = str; + } + } + + std::string_view left; + std::string_view right; +}; + static void connection_add(modeling& mod, grid_component& grid, child_id src, @@ -133,32 +150,62 @@ static void connection_add(modeling& mod, child_id dst, std::string_view port_dst) noexcept { - auto port_src_real = undefined(); - if_data_exists_do(grid.cache, src, [&](auto& child) noexcept { - debug::ensure(child.type == child_type::component); - - if (child.type == child_type::component) { - if_data_exists_do( - mod.components, child.id.compo_id, [&](auto& compo) noexcept { - port_src_real = compo.get_y(port_src); - }); - } - }); - - auto port_dst_real = undefined(); - if_data_exists_do(grid.cache, dst, [&](auto& child) noexcept { - debug::ensure(child.type == child_type::component); - - if (child.type == child_type::component) { - if_data_exists_do( - mod.components, child.id.compo_id, [&](auto& compo) noexcept { - port_dst_real = compo.get_x(port_dst); - }); - } - }); - - if (is_defined(port_src_real) && is_defined(port_dst_real)) - grid.cache_connections.alloc(src, port_src_real, dst, port_dst_real); + auto* child_src = grid.cache.try_to_get(src); + auto* child_dst = grid.cache.try_to_get(dst); + + debug::ensure(child_src == nullptr or + child_src->type != child_type::component); + debug::ensure(child_dst == nullptr or + child_dst->type != child_type::component); + + auto* compo_src = mod.components.try_to_get(child_src->id.compo_id); + auto* compo_dst = mod.components.try_to_get(child_dst->id.compo_id); + debug::ensure(compo_src and compo_dst); + + compo_src->y.for_each( + [&](const auto sid, const auto& sname) noexcept { + split_name p_src(sname.sv()); + if (port_src == p_src.left) { + + compo_dst->x.for_each( + [&](const auto did, const auto dname) noexcept { + split_name p_dst(dname.sv()); + + if (port_dst == p_dst.left) { + + if (p_src.right == p_dst.right) + grid.cache_connections.alloc(src, sid, dst, did); + } + }); + } + }); + + // auto port_src_real = undefined(); + // if_data_exists_do(grid.cache, src, [&](auto& child) noexcept { + // debug::ensure(child.type == child_type::component); + + // if (child.type == child_type::component) { + // if_data_exists_do( + // mod.components, child.id.compo_id, [&](auto& compo) noexcept { + // ports_src = compo.get_x_starts_with(port_src); + // }); + // } + // }); + + // auto port_dst_real = undefined(); + // if_data_exists_do(grid.cache, dst, [&](auto& child) noexcept { + // debug::ensure(child.type == child_type::component); + + // if (child.type == child_type::component) { + // if_data_exists_do( + // mod.components, child.id.compo_id, [&](auto& compo) noexcept { + // port_dst_real = compo.get_x(port_dst); + // }); + // } + // }); + + // if (is_defined(port_src_real) && is_defined(port_dst_real)) + // grid.cache_connections.alloc(src, port_src_real, dst, port_dst_real); } void build_grid_connections(modeling& mod,