std::vector<Uuid> ModelTool::get_endpoints_with_role(const attribute::Array<Uuid>& endpoints_uuids, const EntityRole role) { std::vector<Uuid> filtered_endpoints_uuids; std::copy_if(endpoints_uuids.begin(), endpoints_uuids.end(), std::back_inserter(filtered_endpoints_uuids), [role](Uuid endpoint_uuid) { Endpoint endpoint = get_manager<Endpoint>().get_entry(endpoint_uuid); if (endpoint.get_connected_entities().size() > 0) { return endpoint.get_connected_entities()[0].get_entity_role() == role; } return false; }); return filtered_endpoints_uuids; }
const std::string PncKeyGenerator::generate_key(const Endpoint& endpoint, const std::vector<Port>& in_endpoint_ports) { auto connected_entities = endpoint.get_connected_entities(); std::vector<Port> endpoint_ports = in_endpoint_ports; if (connected_entities.empty()) { if (endpoint_ports.empty()) { throw KeyValueMissingError("No connected entities and unique key ports."); } throw KeyValueMissingError("No connected entities."); } using ConnectedEntity = agent_framework::model::attribute::ConnectedEntity; auto entity_lexicographical_compare = [](ConnectedEntity lhs, ConnectedEntity rhs) -> bool { if (!lhs.get_entity().has_value() || !rhs.get_entity().has_value()) { return lhs.get_entity() < rhs.get_entity(); } else { return lhs.get_entity_role().value().to_string() < rhs.get_entity_role().value().to_string(); } }; auto port_lexicographical_compare = [](const Port& lhs, const Port& rhs) -> bool { return lhs.get_unique_key() < rhs.get_unique_key(); }; std::sort(endpoint_ports.begin(), endpoint_ports.end(), port_lexicographical_compare); std::sort(connected_entities.begin(), connected_entities.end(), entity_lexicographical_compare); std::vector<std::string> connected_entities_data{}; constexpr std::size_t data_multiplier = 2u; constexpr std::size_t uuid_string_length = 36u; connected_entities_data.reserve(data_multiplier * connected_entities.size()); std::size_t unique_key_size = 0u; for (const auto& connected_entity : connected_entities) { if (connected_entity.get_entity().has_value()) { connected_entities_data.push_back(connected_entity.get_entity().value()); unique_key_size += uuid_string_length; } if (connected_entity.get_entity_role().has_value()) { connected_entities_data.push_back(connected_entity.get_entity_role().value().to_string()); unique_key_size += connected_entities_data.back().size(); } } for (const auto& endpoint_port : endpoint_ports) { unique_key_size += endpoint_port.get_unique_key().has_value() ? endpoint_port.get_unique_key().value().size() : 0u; } std::string endpoint_unique_key{generate_key_base(endpoint)}; endpoint_unique_key.reserve(endpoint_unique_key.size() + unique_key_size); for (const auto& connected_entity_data_chunk : connected_entities_data) { endpoint_unique_key += connected_entity_data_chunk; } for (const auto& endpoint_port : endpoint_ports) { endpoint_unique_key += endpoint_port.get_unique_key().has_value() ? endpoint_port.get_unique_key().value() : std::string(); } return endpoint_unique_key; }