From 3ec1aac7732e232119b04d8ca67150843f9df3d0 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:11:59 -0600 Subject: [PATCH 01/20] (pgdata_fetchers) Functions that are called by the getters --- include/cpp_common/pgdata_fetchers.hpp | 83 ++++++ src/cpp_common/pgdata_fetchers.cpp | 369 +++++++++++++++++++++++++ 2 files changed, 452 insertions(+) create mode 100644 include/cpp_common/pgdata_fetchers.hpp create mode 100644 src/cpp_common/pgdata_fetchers.cpp diff --git a/include/cpp_common/pgdata_fetchers.hpp b/include/cpp_common/pgdata_fetchers.hpp new file mode 100644 index 000000000..75d334b5f --- /dev/null +++ b/include/cpp_common/pgdata_fetchers.hpp @@ -0,0 +1,83 @@ +/*PGR-GNU***************************************************************** + +File: pgdata_fetchers.hpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +---- + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_CPP_COMMON_PGDATA_FETCHERS_HPP_ +#define INCLUDE_CPP_COMMON_PGDATA_FETCHERS_HPP_ +#pragma once + +/* for HeapTuple, TupleDesc */ +extern "C" { +#include +#include +} + +#include +#include "cpp_common/undefPostgresDefine.hpp" + +namespace vrprouting { + +class Info; +class Matrix_cell_t; +class Orders_t; +class Vehicle_t; +class Time_multipliers_t; +class Vroom_break_t; +class Vroom_job_t; +class Vroom_matrix_t; +class Vroom_shipment_t; +class Vroom_time_window_t; +class Vroom_vehicle_t; + +namespace pgget { + + +namespace pickdeliver { + +Matrix_cell_t fetch_matrix(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Orders_t fetch_orders(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Time_multipliers_t fetch_timeMultipliers(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Vehicle_t fetch_vehicles(const HeapTuple, const TupleDesc&, const std::vector&, bool); + +} // namespace pickdeliver + +namespace vroom { + +Vroom_matrix_t fetch_matrix(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Vroom_time_window_t fetch_timewindows(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Vroom_job_t fetch_jobs(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Vroom_break_t fetch_breaks(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Vroom_shipment_t fetch_shipments(const HeapTuple, const TupleDesc&, const std::vector&, bool); +Vroom_vehicle_t fetch_vehicles(const HeapTuple, const TupleDesc&, const std::vector&, bool); + +} // namespace vroom + +} // namespace pgget +} // namespace vrprouting + +#endif // INCLUDE_CPP_COMMON_PGDATA_FETCHERS_HPP_ diff --git a/src/cpp_common/pgdata_fetchers.cpp b/src/cpp_common/pgdata_fetchers.cpp new file mode 100644 index 000000000..d1349b57a --- /dev/null +++ b/src/cpp_common/pgdata_fetchers.cpp @@ -0,0 +1,369 @@ +/*PGR-GNU***************************************************************** + +File: pgdata_fetchers.cpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "cpp_common/pgdata_fetchers.hpp" + +#include +#include +#include + +#include +#include +#include + +#include "cpp_common/info.hpp" +#include "cpp_common/check_get_data.hpp" + +#include "cpp_common/orders_t.hpp" +#include "cpp_common/matrix_cell_t.hpp" +#include "cpp_common/time_multipliers_t.hpp" +#include "cpp_common/vehicle_t.hpp" + +#include "cpp_common/vroom_break_t.hpp" +#include "cpp_common/vroom_job_t.hpp" +#include "cpp_common/vroom_matrix_t.hpp" +#include "cpp_common/vroom_shipment_t.hpp" +#include "cpp_common/vroom_time_window_t.hpp" +#include "cpp_common/vroom_vehicle_t.hpp" + +namespace { + +void check_pairs(vrprouting::Info lhs, vrprouting::Info rhs) { + if (!(vrprouting::column_found(lhs)) && vrprouting::column_found(rhs)) { + throw std::string("Column found: '") + rhs.name + "', missing column: '" + lhs.name + "'"; + } else if (!(vrprouting::column_found(rhs)) && vrprouting::column_found(lhs)) { + throw std::string("Column found: '") + lhs.name + "', missing column: '" + rhs.name + "'"; + } +} + +} // namespace + +namespace vrprouting { +namespace pgget { + +namespace vroom { + +Vroom_break_t +fetch_breaks( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Vroom_break_t vroom_break; + vroom_break.id = get_value(tuple, tupdesc, info[0], 0); + vroom_break.vehicle_id = get_value(tuple, tupdesc, info[1], 0); + vroom_break.service = get_value(tuple, tupdesc, info[2], 0); + vroom_break.data = get_jsonb(tuple, tupdesc, info[3]); + return vroom_break; +} + +Vroom_matrix_t +fetch_matrix( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Vroom_matrix_t matrix; + matrix.start_id = get_value(tuple, tupdesc, info[0], -1); + matrix.end_id = get_value(tuple, tupdesc, info[1], -1); + matrix.duration = get_value(tuple, tupdesc, info[2], 0); + matrix.cost = get_value(tuple, tupdesc, info[3], matrix.duration); + return matrix; +} + +Vroom_time_window_t +fetch_timewindows( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool is_shipment) { + Vroom_time_window_t time_window; + + time_window.id = get_value(tuple, tupdesc, info[0], 0); + time_window.kind = is_shipment? get_char(tuple, tupdesc, info[3], ' ') : ' '; + + if (is_shipment) { + if (time_window.kind != 'p' && time_window.kind != 'd') { + throw std::string("Invalid kind '") + time_window.kind + "', Expecting 'p' or 'd'"; + } + } + + Duration tw_open = get_value(tuple, tupdesc, info[1], 0); + Duration tw_close = get_value(tuple, tupdesc, info[2], 0); + + if (tw_open > tw_close) { + throw std::string("Invalid time window found: '") + info[2].name + "' < '" + info[1].name + "'"; + } + + time_window.tw = ::vroom::TimeWindow(tw_open, tw_close); + + return time_window; +} + +Vroom_job_t +fetch_jobs( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Vroom_job_t job; + + job.id = get_value(tuple, tupdesc, info[0], 0); + job.location_id = get_value(tuple, tupdesc, info[1], 0); + + job.setup = get_value(tuple, tupdesc, info[2], 0); + job.service = get_value(tuple, tupdesc, info[3], 0); + + auto pickup = get_array(tuple, tupdesc, info[5]); + auto delivery = get_array(tuple, tupdesc, info[4]); + + for (const auto &e : pickup) { + job.pickup.push_back(e); + } + + for (const auto &e : delivery) { + job.delivery.push_back(e); + } + + job.skills = get_uint_unordered_set(tuple, tupdesc, info[6]); + job.priority = get_value(tuple, tupdesc, info[7], 0); + job.data = get_jsonb(tuple, tupdesc, info[8]); + + if (job.priority > 100) { + throw std::string("Invalid value in column '") + info[7].name + "'. Maximum value allowed 100"; + } + return job; +} + +Vroom_shipment_t +fetch_shipments( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Vroom_shipment_t shipment; + + shipment.id = get_value(tuple, tupdesc, info[0], 0); + + shipment.p_location_id = get_value(tuple, tupdesc, info[1], 0); + shipment.d_location_id = get_value(tuple, tupdesc, info[4], 0); + + shipment.p_setup = get_value(tuple, tupdesc, info[2], 0); + shipment.p_service = get_value(tuple, tupdesc, info[3], 0); + shipment.d_setup = get_value(tuple, tupdesc, info[5], 0); + shipment.d_service = get_value(tuple, tupdesc, info[6], 0); + + auto amount = get_array(tuple, tupdesc, info[7]); + + for (const auto &a : amount) { + shipment.amount.push_back(a); + } + + shipment.skills = get_uint_unordered_set(tuple, tupdesc, info[8]); + + shipment.priority = get_value(tuple, tupdesc, info[9], 0); + + shipment.p_data = get_jsonb(tuple, tupdesc, info[10]); + shipment.d_data = get_jsonb(tuple, tupdesc, info[11]); + + if (shipment.priority > 100) { + throw std::string("Invalid value in column '") + info[9].name + "'. Maximum value allowed 100"; + } + return shipment; +} + +Vroom_vehicle_t +fetch_vehicles( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Vroom_vehicle_t vehicle; + vehicle.id = get_value(tuple, tupdesc, info[0], 0); + vehicle.start_id = get_value(tuple, tupdesc, info[1], -1); + vehicle.end_id = get_value(tuple, tupdesc, info[2], -1); + + auto capacity = get_array(tuple, tupdesc, info[3]); + + for (const auto &c : capacity) { + vehicle.capacity.push_back(c); + } + + vehicle.skills = get_uint_unordered_set(tuple, tupdesc, info[4]); + + Duration tw_open = get_value(tuple, tupdesc, info[5], 0); + Duration tw_close = get_value(tuple, tupdesc, info[6], UINT_MAX); + + if (tw_open > tw_close) { + throw std::string("Invalid time window found: '") + info[6].name + "' < '" + info[5].name + "'"; + } + + vehicle.tw = ::vroom::TimeWindow(tw_open, tw_close); + + vehicle.speed_factor = get_anynumerical(tuple, tupdesc, info[7], 1.0); + vehicle.max_tasks = get_value(tuple, tupdesc, info[8], INT_MAX); + vehicle.data = get_jsonb(tuple, tupdesc, info[9]); + + if (!(column_found(info[1]) || column_found(info[2]))) { + throw std::string("Missing column(s): '") + info[1].name + "' and/or '" + info[2].name + "' must exist"; + } + + if (vehicle.speed_factor <= 0.0) { + throw std::string("Invalid negative or zero value in column '") + info[7].name + "'"; + } + return vehicle; +} + + +} // namespace vroom + +namespace pickdeliver { + +Matrix_cell_t +fetch_matrix( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Matrix_cell_t row; + row.from_vid = get_value(tuple, tupdesc, info[0], -1); + row.to_vid = get_value(tuple, tupdesc, info[1], -1); + row.cost = get_value(tuple, tupdesc, info[2], 0); + return row; +} + +Time_multipliers_t +fetch_timeMultipliers( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool) { + Time_multipliers_t row; + row.start_time = get_value(tuple, tupdesc, info[0], 0); + row.multiplier = get_anynumerical(tuple, tupdesc, info[1], 1); + return row; +} + +Orders_t +fetch_orders( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool is_euclidean) { + Orders_t pd_order; + + if (is_euclidean) { + check_pairs(info[3], info[4]); + check_pairs(info[9], info[10]); + } + + pd_order.id = get_value(tuple, tupdesc, info[0], -1); + pd_order.demand = get_value(tuple, tupdesc, info[1], 0); + if (pd_order.demand == 0) { + throw std::make_pair( + std::string("Unexpected zero value found on column'") + info[1].name + "' of orders", + std::string("Check order id #:") + std::to_string(pd_order.id)); + } + + pd_order.pick_node_id = is_euclidean? 0 : get_value(tuple, tupdesc, info[2], -1); + pd_order.pick_x = is_euclidean? get_anynumerical(tuple, tupdesc, info[3], 0) : 0; + pd_order.pick_y = is_euclidean? get_anynumerical(tuple, tupdesc, info[4], 0) : 0; + pd_order.pick_open_t = get_value(tuple, tupdesc, info[5], -1); + pd_order.pick_close_t = get_value(tuple, tupdesc, info[6], -1); + if (pd_order.pick_close_t < pd_order.pick_open_t) { + throw std::make_pair( + std::string("Invalid time window found: '") + info[6].name + "' < '" + info[5].name + "'", + std::string("Check order id #:") + std::to_string(pd_order.id)); + } + + pd_order.pick_service_t = get_value(tuple, tupdesc, info[7], 0); + if (pd_order.pick_service_t < 0) { + throw std::make_pair( + std::string("Unexpected negative value found on column'") + info[7].name + "' of orders", + std::string("Check order id #:") + std::to_string(pd_order.id)); + } + + pd_order.deliver_node_id = is_euclidean? 0 : get_value(tuple, tupdesc, info[8], -1); + pd_order.deliver_x = is_euclidean? get_anynumerical(tuple, tupdesc, info[9], 0) : 0; + pd_order.deliver_y = is_euclidean? get_anynumerical(tuple, tupdesc, info[10], 0) : 0; + pd_order.deliver_open_t = get_value(tuple, tupdesc, info[11], -1); + pd_order.deliver_close_t = get_value(tuple, tupdesc, info[12], -1); + if (pd_order.deliver_close_t < pd_order.deliver_open_t) { + throw std::make_pair( + std::string("Invalid time window found: '") + info[12].name + "' < '" + info[11].name + "'", + std::string("Check order id #:") + std::to_string(pd_order.id)); + } + + pd_order.deliver_service_t = get_value(tuple, tupdesc, info[13], 0); + if (pd_order.pick_service_t < 0) { + throw std::make_pair( + std::string("Unexpected negative value found on column'") + info[13].name + "' of orders", + std::string("Check order id #:") + std::to_string(pd_order.id)); + } + + return pd_order; +} + +Vehicle_t +fetch_vehicles( + const HeapTuple tuple, const TupleDesc &tupdesc, + const std::vector &info, + bool is_euclidean) { + Vehicle_t vehicle; + + if (is_euclidean) { + check_pairs(info[5], info[6]); + check_pairs(info[11], info[12]); + } + + vehicle.id = get_value(tuple, tupdesc, info[0], -1); + vehicle.capacity = get_value(tuple, tupdesc, info[1], UINT32_MAX); + vehicle.cant_v = get_value(tuple, tupdesc, info[2], 1); + vehicle.speed = get_anynumerical(tuple, tupdesc, info[3], 1); + + /* + * start values + */ + vehicle.start_node_id = is_euclidean? 0 : get_value(tuple, tupdesc, info[4], -1); + vehicle.start_x = is_euclidean? get_anynumerical(tuple, tupdesc, info[5], 0) : 0; + vehicle.start_y = is_euclidean? get_anynumerical(tuple, tupdesc, info[6], 0) : 0; + vehicle.start_open_t = get_value(tuple, tupdesc, info[7], 0); + vehicle.start_close_t = get_value(tuple, tupdesc, info[8], INT64_MAX); + vehicle.start_service_t = get_value(tuple, tupdesc, info[9], 0); + + /* + * end values + */ + vehicle.end_node_id = is_euclidean? 0 : get_value(tuple, tupdesc, info[10], vehicle.start_node_id); + vehicle.end_x = is_euclidean? get_anynumerical(tuple, tupdesc, info[11], vehicle.start_x) : 0; + vehicle.end_y = is_euclidean? get_anynumerical(tuple, tupdesc, info[12], vehicle.start_y) : 0; + vehicle.end_open_t = get_value(tuple, tupdesc, info[13], vehicle.start_open_t); + vehicle.end_close_t = get_value(tuple, tupdesc, info[14], vehicle.start_close_t); + vehicle.end_service_t = get_value(tuple, tupdesc, info[15], 0); + + vehicle.stops = get_array(tuple, tupdesc, info[16]); + + return vehicle; +} + +} // namespace pickdeliver + +} // namespace pgget +} // namespace vrprouting From 2af6417393cf0defd5f7e36994a30e8ce2e2be6e Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:12:26 -0600 Subject: [PATCH 02/20] (pgdata_getters) Functions that are called by the drivers --- include/cpp_common/pgdata_getters.hpp | 101 +++++++ src/cpp_common/pgdata_getters.cpp | 396 ++++++++++++++++++++++++++ 2 files changed, 497 insertions(+) create mode 100644 include/cpp_common/pgdata_getters.hpp create mode 100644 src/cpp_common/pgdata_getters.cpp diff --git a/include/cpp_common/pgdata_getters.hpp b/include/cpp_common/pgdata_getters.hpp new file mode 100644 index 000000000..0a8594818 --- /dev/null +++ b/include/cpp_common/pgdata_getters.hpp @@ -0,0 +1,101 @@ +/*PGR-GNU***************************************************************** + +File: pgdata_getters.hpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_CPP_COMMON_PGDATA_GETTERS_HPP_ +#define INCLUDE_CPP_COMMON_PGDATA_GETTERS_HPP_ +#pragma once + +extern "C" { +#include +#include +} + +#include +#include +#include +#include +#include +#include +#include "cpp_common/undefPostgresDefine.hpp" + +#include "cpp_common/matrix_cell_t.hpp" +#include "cpp_common/orders_t.hpp" +#include "cpp_common/time_multipliers_t.hpp" +#include "cpp_common/vehicle_t.hpp" + +#include "cpp_common/vroom_break_t.hpp" +#include "cpp_common/vroom_job_t.hpp" +#include "cpp_common/vroom_matrix_t.hpp" +#include "cpp_common/vroom_shipment_t.hpp" +#include "cpp_common/vroom_time_window_t.hpp" +#include "cpp_common/vroom_vehicle_t.hpp" + +namespace vrprouting { +namespace pgget { +namespace pickdeliver { + +/** @brief Get the matrix */ +std::vector get_matrix(const std::string&, bool); + +/** @brief Reads the pick-Deliver shipments for timestams and intervals*/ +std::vector get_orders(const std::string&, bool, bool); + +/** @brief Reads the vehicles information */ +std::vector get_vehicles(const std::string&, bool, bool, bool); + +/** @brief Get the time multipliers using interval*/ +std::vector get_timeMultipliers(const std::string&, bool); + +} // namespace pickdeliver + +namespace vroom { + +/** @brief Reads the VROOM matrix */ +std::vector get_matrix(const std::string&, bool); + +/** @brief Reads the VROOM breaks */ +std::vector get_breaks(const std::string&, bool); + +/** @brief Reads the VROOM time windows */ +std::map, std::vector<::vroom::TimeWindow>> get_timewindows(const std::string&, bool, bool); + +/** @brief Reads the VROOM jobs */ +std::vector get_jobs(const std::string&, bool); + +/** @brief Reads the VROOM shipments */ +std::vector get_shipments(const std::string&, bool); + +/** @brief Reads the VROOM vehicles */ +std::vector get_vehicles(const std::string&, bool); + +} // namespace vroom +} // namespace pgget +} // namespace vrprouting + +#endif // INCLUDE_CPP_COMMON_PGDATA_GETTERS_HPP_ diff --git a/src/cpp_common/pgdata_getters.cpp b/src/cpp_common/pgdata_getters.cpp new file mode 100644 index 000000000..3fe38282f --- /dev/null +++ b/src/cpp_common/pgdata_getters.cpp @@ -0,0 +1,396 @@ +/*PGR-GNU***************************************************************** + +File: pgdata_getters.cpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "cpp_common/pgdata_getters.hpp" + +#include +#include +#include +#include + +#include "cpp_common/get_data.hpp" +#include "cpp_common/check_get_data.hpp" +#include "cpp_common/pgdata_fetchers.hpp" +#include "cpp_common/info.hpp" + +namespace vrprouting { +namespace pgget { + + +namespace vroom { +/** + ~~~~{.c} + SELECT start_id, end_id, duration, cost + FROM matrix; + ~~~~ + * @param[in] sql SQL query to execute + * @param[in] use_timestamps When true postgres Time datatypes are used + * @returns vector of Vroom_matrix_t containing the matrix cell contents + */ +std::vector +get_matrix( + const std::string &sql, + bool use_timestamps) { + using vrprouting::Info; + std::vector info{ + {-1, 0, true, "start_id", vrprouting::MATRIX_INDEX}, + {-1, 0, true, "end_id", vrprouting::MATRIX_INDEX}, + {-1, 0, true, "duration", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "cost", vrprouting::INTEGER}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_matrix); +} + +/** + ~~~~{.c} + SELECT id, vehicle_id, service, data + FROM breaks; + ~~~~ + * @param[in] sql SQL query to execute + * @param [in] use_timestamps When true postgres Time datatypes are used + * @returns vector of Vroom_break_t containing the breaks information + */ +std::vector +get_breaks( + const std::string &sql, + bool use_timestamps) { + if (sql.empty()) return std::vector(); + using vrprouting::Info; + std::vector info{ + {-1, 0, true, "id", vrprouting::IDX}, + {-1, 0, true, "vehicle_id", vrprouting::IDX}, + {-1, 0, false, "service", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "data", vrprouting::JSONB}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_breaks); +} + +/** + ~~~~{.c} + SELECT id, tw_open, tw_close, kind + FROM shipment_tws; + ~~~~ + * @param[in] sql SQL query to execute + * @param [in] use_timestamps When true postgres Time datatypes are used + * @param [in] is_shipment When true c$the kind is compulsory + * @returns vector of Vroom_time_window_t containing the time windows information + */ +std::map, std::vector<::vroom::TimeWindow>> +get_timewindows( + const std::string &sql, + bool use_timestamps, + bool is_shipment) { + using vrprouting::Info; + std::map, std::vector<::vroom::TimeWindow>> time_windows; + if (sql.empty()) return time_windows; + + std::vector info{ + {-1, 0, true, "id", vrprouting::ANY_INTEGER}, + {-1, 0, true, "tw_open", use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, true, "tw_close", use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, is_shipment, "kind", vrprouting::CHAR1}}; + + auto data = pgget::get_data(sql, is_shipment, info, &fetch_timewindows); + + for (const auto &tw : data) { + time_windows[std::make_pair(tw.id, tw.kind)]; + time_windows[std::make_pair(tw.id, tw.kind)].push_back(tw.tw); + } + return time_windows; +} + +/** + ~~~~{.c} + SELECT id, location_id, setup, service, delivery, delivery, skills, priority, data + FROM jobs; + ~~~~ + * @param[in] sql SQL query to execute + * @param [in] use_timestamps When true postgres Time datatypes are used + * @returns vector of Vroom_job_t containing the jobs information + */ +std::vector get_jobs( + const std::string &sql, + bool use_timestamps) { + if (sql.empty()) return std::vector(); + using vrprouting::Info; + std::vector info { + {-1, 0, true, "id", vrprouting::IDX}, + {-1, 0, true, "location_id", vrprouting::MATRIX_INDEX}, + {-1, 0, false, "setup", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "service", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "delivery", vrprouting::ANY_POSITIVE_ARRAY}, + {-1, 0, false, "pickup", vrprouting::ANY_POSITIVE_ARRAY}, + {-1, 0, false, "skills", vrprouting::ANY_UINT_ARRAY}, + {-1, 0, false, "priority", vrprouting::POSITIVE_INTEGER}, + {-1, 0, false, "data", vrprouting::JSONB}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_jobs); +} + +/** + ~~~~{.c} + SELECT id, + p_location_id, p_setup, p_service, p_data, + d_location_id, d_setup, d_service, d_data, + amount, skills, priority, data + FROM jobs; + ~~~~ + * @param[in] sql SQL query to execute + * @param[in] use_timestamps When true postgres Time datatypes are used + * @returns vector of Vroom_shipment_t containing the shipment information + */ +std::vector +get_shipments( + const std::string &sql, + bool use_timestamps) { + if (sql.empty()) return std::vector(); + using vrprouting::Info; + std::vector info{ + {-1, 0, true, "id", vrprouting::IDX}, + {-1, 0, true, "p_location_id", vrprouting::MATRIX_INDEX}, + {-1, 0, false, "p_setup", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "p_service", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, true, "d_location_id", vrprouting::MATRIX_INDEX}, + {-1, 0, false, "d_setup", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "d_service", use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + {-1, 0, false, "amount", vrprouting::ANY_POSITIVE_ARRAY}, + {-1, 0, false, "skills", vrprouting::ANY_UINT_ARRAY}, + {-1, 0, false, "priority", vrprouting::POSITIVE_INTEGER}, + {-1, 0, false, "p_data", vrprouting::JSONB}, + {-1, 0, false, "d_data", vrprouting::JSONB}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_shipments); +} + +/** + ~~~~{.c} + SELECT id, + start_id, end_id, capacity, + skills, tw_open, tw_close, speed_factor, max_tasks, data + FROM jobs; + ~~~~ + * @param[in] sql SQL query to execute + * @param[in] use_timestamps When true postgres Time datatypes are used + * @returns vector of Vroom_vehicle_t containing the vehicle information + */ +std::vector +get_vehicles( + const std::string &sql, + bool use_timestamps) { + using vrprouting::Info; + std::vector info{ + {-1, 0, true, "id", vrprouting::IDX}, + {-1, 0, false, "start_id", vrprouting::MATRIX_INDEX}, + {-1, 0, false, "end_id", vrprouting::MATRIX_INDEX}, + {-1, 0, false, "capacity", vrprouting::ANY_POSITIVE_ARRAY}, + {-1, 0, false, "skills", vrprouting::ANY_UINT_ARRAY}, + {-1, 0, false, "tw_open", use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, "tw_close", use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, "speed_factor", vrprouting::ANY_NUMERICAL}, + {-1, 0, false, "max_tasks", vrprouting::POSITIVE_INTEGER}, + {-1, 0, false, "data", vrprouting::JSONB}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_vehicles); +} + +} // namespace vroom + + +namespace pickdeliver { + +std::vector get_timeMultipliers( + const std::string &sql, + bool use_timestamps) { + using vrprouting::Info; + std::vector info{ + {-1, 0, true, + use_timestamps? "start_time" : "start_value", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, true, "multiplier", vrprouting::ANY_NUMERICAL}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_timeMultipliers); +} + +/** + ~~~~{.c} + SELECT start_vid, end_vid, [travel_time|agg_cost] + FROM matrix; + ~~~~ + * @param[in] sql SQL query to execute + * @param [in] use_timestamps When true postgres Time datatypes are used + * @returns vector of Matrix_cell_t containing the matrix cell contents + */ +std::vector get_matrix( + const std::string &sql, + bool use_timestamps) { + using vrprouting::Info; + std::vector info{ + {-1, 0, true, "start_vid", vrprouting::ID}, + {-1, 0, true, "end_vid", vrprouting::ID}, + {-1, 0, true, + use_timestamps? "travel_time" : "agg_cost", + use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}}; + + return pgget::get_data(sql, use_timestamps, info, &fetch_matrix); +} + + +/** + For queries comming from pgRouting + ~~~~{.c} + SELECT id, demand + [p_id | p_x, p_y], p_open, p_close, p_service, + [d_id | d_x, d_y], d_open, d_close, d_service, + FROM orders; + ~~~~ + For timestamps + ~~~~{.c} + SELECT id, demand + [p_id | p_x, p_y], p_tw_open, p_tw_close, p_t_service, + [d_id | d_x, d_y], d_tw_open, d_tw_close, d_t_service, + FROM orders; + ~~~~ + + @param[in] sql The orders query + @param [in] is_euclidean When true coordintes are going to be used + @param [in] use_timestamps When true data use postgres timestamps + @returns vector of Orders_t + */ +std::vector get_orders( + const std::string &sql, + bool is_euclidean, + bool use_timestamps) { + using vrprouting::Info; + + std::vector info{ + {-1, 0, true, "id", vrprouting::ID}, + {-1, 0, true, "amount", vrprouting::PAMOUNT}, + + {-1, 0, !is_euclidean, "p_id", vrprouting::ID}, + {-1, 0, is_euclidean, "p_x", vrprouting::COORDINATE}, + {-1, 0, is_euclidean, "p_y", vrprouting::COORDINATE}, + + {-1, 0, true, + use_timestamps? "p_tw_open" : "p_open", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, true, + use_timestamps? "p_tw_close" : "p_close", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, + use_timestamps? "p_t_service" : "p_service", + use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + + {-1, 0, !is_euclidean, "d_id", vrprouting::ID}, + {-1, 0, is_euclidean, "d_x", vrprouting::COORDINATE}, + {-1, 0, is_euclidean, "d_y", vrprouting::COORDINATE}, + {-1, 0, true, + use_timestamps? "d_tw_open" : "d_open", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, true, + use_timestamps? "d_tw_close" : "d_close", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, + use_timestamps? "d_t_service" : "d_service", + use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}}; + + return pgget::get_data(sql, is_euclidean, info, &fetch_orders); +} + + +/** + + For queries of the type: + ~~~~{.c} + SELECT id, capacity, speed, number + [s_id | s_x, s_y], s_open, s_close, s_service, + [e_id | e_x, e_y], e_open, e_close, e_service, + FROM orders; + ~~~~ + + for timestamps: + ~~~~{.c} + SELECT id, capacity, speed, number + [s_id | s_x, s_y], s_tw_open, s_tw_close, s_t_service, + [e_id | e_x, e_y], e_tw_open, e_tw_close, e_t_service, + FROM orders; + ~~~~ + + @param[in] sql The vehicles query + @param[in] is_euclidean when true: use coordinates + @param[in] use_timestamps When true data use postgres timestamps + @param[in] with_stops use stops information otherwise ignore + @returns vector of Vehicle_t + */ +std::vector get_vehicles( + const std::string &sql, + bool is_euclidean, + bool use_timestamps, + bool with_stops) { + using vrprouting::Info; + + std::vector info{ + {-1, 0, true, "id", vrprouting::ID}, + {-1, 0, true, "capacity", vrprouting::PAMOUNT}, + {-1, 0, false, "number", vrprouting::PAMOUNT}, + {-1, 0, false, "speed", vrprouting::SPEED}, + + {-1, 0, !is_euclidean, "s_id", vrprouting::ID}, + {-1, 0, is_euclidean, "s_x", vrprouting::COORDINATE}, + {-1, 0, is_euclidean, "s_y", vrprouting::COORDINATE}, + {-1, 0, false, + use_timestamps? "s_tw_open" : "s_open", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, + use_timestamps? "s_tw_close" : "s_close", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, + use_timestamps? "s_t_service" : "s_service", + use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + + {-1, 0, false, "e_id", vrprouting::ID}, + {-1, 0, false, "e_x", vrprouting::COORDINATE}, + {-1, 0, false, "e_y", vrprouting::COORDINATE}, + {-1, 0, false, + use_timestamps? "e_tw_open" : "e_open", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, + use_timestamps? "e_tw_close" : "e_close", + use_timestamps? vrprouting::TIMESTAMP : vrprouting::TTIMESTAMP}, + {-1, 0, false, + use_timestamps? "e_t_service" : "e_service", + use_timestamps? vrprouting::INTERVAL : vrprouting::TINTERVAL}, + + {-1, 0, with_stops, "stops", vrprouting::ANY_POSITIVE_ARRAY}}; + + return get_data(sql, is_euclidean, info, &fetch_vehicles); +} +} // namespace pickdeliver + +} // namespace pgget +} // namespace vrprouting From 110fd51306b64d8a30f8bb815595884635763071 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:12:51 -0600 Subject: [PATCH 03/20] (get_data) template for general data retrival --- include/cpp_common/get_data.hpp | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 include/cpp_common/get_data.hpp diff --git a/include/cpp_common/get_data.hpp b/include/cpp_common/get_data.hpp new file mode 100644 index 000000000..c52298bf7 --- /dev/null +++ b/include/cpp_common/get_data.hpp @@ -0,0 +1,93 @@ +/*PGR-GNU***************************************************************** + +File: get_data.hpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_CPP_COMMON_GET_DATA_HPP_ +#define INCLUDE_CPP_COMMON_GET_DATA_HPP_ +#pragma once + +#include +#include + +#include "c_common/postgres_connection.h" +#include "cpp_common/info.hpp" +#include "cpp_common/check_get_data.hpp" +#include "cpp_common/alloc.hpp" + +namespace vrprouting { +namespace pgget { + +/** @brief Retrives the tuples + * @tparam Data_type Scructure of data + * @tparam Func fetcher function + * @param[in] sql Query to be processed + * @param[in] flag useful flag depending on data + * @param[in] info information about the data + * @param[in] func fetcher function to be used + */ +template +std::vector +get_data(const std::string& sql, bool flag, std::vector info, Func func) { + const int tuple_limit = 1000000; + + size_t total_tuples = 0; + + auto SPIplan = vrp_SPI_prepare(sql.c_str()); + auto SPIportal = vrp_SPI_cursor_open(SPIplan); + + bool moredata = true; + std::vector tuples; + + while (moredata == true) { + SPI_cursor_fetch(SPIportal, true, tuple_limit); + auto tuptable = SPI_tuptable; + auto tupdesc = SPI_tuptable->tupdesc; + if (total_tuples == 0) fetch_column_info(tupdesc, info); + + size_t ntuples = SPI_processed; + total_tuples += ntuples; + + if (ntuples > 0) { + tuples.reserve(total_tuples); + for (size_t t = 0; t < ntuples; t++) { + tuples.push_back(func(tuptable->vals[t], tupdesc, info, flag)); + } + SPI_freetuptable(tuptable); + } else { + moredata = false; + } + } + + SPI_cursor_close(SPIportal); + return tuples; +} + +} // namespace pgget +} // namespace vrprouting + +#endif // INCLUDE_CPP_COMMON_GET_DATA_HPP_ From f337a7e58886514959fba90c83051a4c438763be Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:13:30 -0600 Subject: [PATCH 04/20] (undefPostgresDefine) Need to undef some postgres defines --- include/cpp_common/undefPostgresDefine.hpp | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 include/cpp_common/undefPostgresDefine.hpp diff --git a/include/cpp_common/undefPostgresDefine.hpp b/include/cpp_common/undefPostgresDefine.hpp new file mode 100644 index 000000000..06cb93ba4 --- /dev/null +++ b/include/cpp_common/undefPostgresDefine.hpp @@ -0,0 +1,79 @@ +/*PGR-GNU***************************************************************** + +File: undefPostgresDefine.hpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_CPP_COMMON_UNDEFPOSTGRESDEFINE_HPP_ +#define INCLUDE_CPP_COMMON_UNDEFPOSTGRESDEFINE_HPP_ + +/** @file undefPostgresDefine.hpp + +https://doxygen.postgresql.org/port_8h.html +Has the line +`#define snprintf pg_snprintf` + +Being `snprintf` part of [stdio.h](https://en.cppreference.com/w/cpp/header/cstdio) since C++11 +Work arount is to undef it. + +It's affecting boost 83+ + +Placing after including extern C postgres files +Example +~~~ c +extern "C" { +#include +#include +} + +#include + +#include "cpp_common/undefPostgresDefine.hpp" + + +~~~ + +*/ +#ifdef __cplusplus +#ifdef sprintf +#undef sprintf +#endif + +#ifdef snprintf +#undef snprintf +#endif + +#ifdef vsprintf +#undef vsprintf +#endif + +#ifdef vsnprintf +#undef vsnprintf +#endif + +#endif + +#endif // INCLUDE_CPP_COMMON_UNDEFPOSTGRESDEFINE_HPP_ From aeb8461ceeda0bf7084ab7f6fc5dcf8c5487e9a0 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:14:02 -0600 Subject: [PATCH 05/20] (check_get_data) checks and gets data from postgres --- include/cpp_common/check_get_data.hpp | 159 +++++ src/cpp_common/check_get_data.cpp | 824 ++++++++++++++++++++++++++ 2 files changed, 983 insertions(+) create mode 100644 include/cpp_common/check_get_data.hpp create mode 100644 src/cpp_common/check_get_data.cpp diff --git a/include/cpp_common/check_get_data.hpp b/include/cpp_common/check_get_data.hpp new file mode 100644 index 000000000..92249bb90 --- /dev/null +++ b/include/cpp_common/check_get_data.hpp @@ -0,0 +1,159 @@ +/*PGR-GNU***************************************************************** + * +FILE: check_get_data.hpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_CPP_COMMON_GET_CHECK_DATA_HPP_ +#define INCLUDE_CPP_COMMON_GET_CHECK_DATA_HPP_ +#pragma once + + +extern "C" { +#include +#include +#include +#include +} + +#include +#include +#include +#include +#include +#include "cpp_common/undefPostgresDefine.hpp" + +#include "c_types/typedefs.h" +#include "cpp_common/info.hpp" + + +namespace vrprouting { + +/** @brief Function will check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUTE). */ +bool column_found(const Info&); + +TTimestamp get_timestamp_without_timezone(TTimestamp timestamp); + +namespace detail { + +std::vector get_any_positive_array(const HeapTuple, const TupleDesc&, const Info&); +std::vector get_uint_array(const HeapTuple, const TupleDesc&, const Info&); +TInterval get_interval(const HeapTuple, const TupleDesc&, const Info&, TInterval); +TTimestamp get_timestamp(const HeapTuple, const TupleDesc&, const Info&, TTimestamp); +int64_t get_anyinteger(const HeapTuple, const TupleDesc&, const Info&, int64_t); + +template +T get_integral(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, T opt_value) { + static_assert(std::is_integral::value, "Integral required."); + return static_cast(get_anyinteger(tuple, tupdesc, info, static_cast(opt_value))); +} + +template +T get_positive(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, T opt_value) { + static_assert(std::is_integral::value, "Integral required."); + + if (!column_found(info)) return opt_value; + + auto value = get_anyinteger(tuple, tupdesc, info, 0); + if (value < 0) throw std::string("Unexpected negative value in column '") + info.name + "'"; + return static_cast(value); +} + +} // namespace detail + + + +/** @brief Function tells expected type of each column and then check the correspondence type of each column. */ +void fetch_column_info(const TupleDesc&, std::vector&); + +/** @brief Function gets the C string of a JSONB */ +std::string get_jsonb(const HeapTuple, const TupleDesc&, const Info&); + +/** @brief Function gets the @b double of a Postgres floating point */ +double get_anynumerical(const HeapTuple, const TupleDesc&, const Info&, double); + +/** @brief Function get a char of a CHAR*/ +char get_char(const HeapTuple, const TupleDesc&, const Info&, char); + +/** @brief Function get an unordered_set of uint32_t*/ +std::unordered_set get_uint_unordered_set(const HeapTuple, const TupleDesc&, const Info&); + +template +T get_value(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, T opt_value) { + switch (info.eType) { + case ANY_INTEGER : + return static_cast(detail::get_integral(tuple, tupdesc, info, static_cast(opt_value))); + break; + case INTEGER: + return static_cast(detail::get_integral(tuple, tupdesc, info, opt_value)); + break; + case ANY_UINT : + case TINTERVAL : + case POSITIVE_INTEGER: + return static_cast(detail::get_positive(tuple, tupdesc, info, opt_value)); + break; + case TIMESTAMP : + return static_cast(detail::get_timestamp(tuple, tupdesc, info, static_cast(opt_value))); + break; + case INTERVAL : + return static_cast(detail::get_interval(tuple, tupdesc, info, static_cast(opt_value))); + break; + default: + throw std::string("Missing case value ") + info.name; + break; + } +} + + +template +std::vector get_array(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) { + switch (info.eType) { + case ANY_POSITIVE_ARRAY: + return detail::get_any_positive_array(tuple, tupdesc, info); + break; + default: + throw std::string("Missing case value on array ") + info.name; + break; + } +} + +template +std::vector get_uint_array(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) { + switch (info.eType) { + case ANY_UINT_ARRAY: + return detail::get_uint_array(tuple, tupdesc, info); + break; + default: + throw std::string("Missing case value on Uint array ") + info.name; + break; + } +} + + +} // namespace vrprouting + + +#endif // INCLUDE_CPP_COMMON_GET_CHECK_DATA_HPP_ diff --git a/src/cpp_common/check_get_data.cpp b/src/cpp_common/check_get_data.cpp new file mode 100644 index 000000000..d17c94a85 --- /dev/null +++ b/src/cpp_common/check_get_data.cpp @@ -0,0 +1,824 @@ +/*PGR-GNU***************************************************************** + +File: check_get_data.cpp + +Copyright (c) 2024 pgRouting developers +Mail: pgrouting-dev@discourse.osgeo.org + +Developer: +Copyright (c) 2024 Celia Virginia Vergara Castillo +Mail: vicky at erosion.dev + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "cpp_common/check_get_data.hpp" + +extern "C" { +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +} + +#include +#include +#include +#include +#include +#include "cpp_common/undefPostgresDefine.hpp" + +#include "cpp_common/alloc.hpp" +#include "cpp_common/info.hpp" + + +namespace { + +void +check_interval_type(const vrprouting::Info &info) { + if (!(info.type == 1186)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected INTERVAL"; + } +} + +void +check_jsonb_type(vrprouting::Info info) { + if (!(info.type == JSONBOID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected JSONB"; + } +} + +void +check_integer_type(vrprouting::Info info) { + if (!(info.type == INT2OID || info.type == INT4OID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected SMALLINT or INTEGER"; + } +} + +void +check_integerarray_type(vrprouting::Info info) { + if (!(info.type == INT2ARRAYOID || info.type == INT4ARRAYOID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected INTEGER-ARRAY"; + } +} + +/** + * @brief The function check whether column type is ANY-INTEGER-ARRAY or not. + * + * Where ANY-INTEGER-ARRAY is SQL type: + * SMALLINT[], INTEGER[], BIGINT[] + * + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected ANY-INTEGER-ARRAY. + */ + +void +check_any_integerarray_type(vrprouting::Info info) { + if (!(info.type == INT2ARRAYOID + || info.type == INT4ARRAYOID + || info.type == 1016)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected ANY-INTEGER-ARRAY"; + } +} + +void +check_timestamp_type(vrprouting::Info info) { + if (!(info.type == 1114)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected TIMESTAMP"; + } +} + + +/** @brief Function tells expected type of each column and then check the correspondence type of each column. + * + * [SPI_fnumber](https://www.postgresql.org/docs/current/spi-spi-fnumber.html) + * [SPI_gettypeid](https://www.postgresql.org/docs/9.1/static/spi-spi-gettypeid.html) + * @param[in] tupdesc tuple description. + * @param[in] info contain one or more column information. + * @throw column not found. + * @throw ERROR Unknown type of column. + * @return @b TRUE when column exist. + * @b FALSE when column was not found. + */ +bool +get_column_info(const TupleDesc &tupdesc, vrprouting::Info &info) { + info.colNumber = SPI_fnumber(tupdesc, info.name.c_str()); + if (info.strict && info.colNumber == SPI_ERROR_NOATTRIBUTE) { + throw std::string("Column '") + info.name + "' not Found"; + } + + if (info.colNumber != SPI_ERROR_NOATTRIBUTE) { + info.type = SPI_gettypeid(tupdesc, info.colNumber); + if (info.type == InvalidOid) { + throw std::string("Type of column '") + info.name + "' not Found"; + } + return true; + } + return false; +} + +/** @brief The function check whether column type is ANY-INTEGER or not. + * + * Where ANY-INTEGER is SQL type: SMALLINT, INTEGER, BIGINT + * + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected column type is ANY-INTEGER. + */ +void +check_any_integer_type(const vrprouting::Info &info) { + if (!(info.type == INT2OID + || info.type == INT4OID + || info.type == INT8OID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected ANY-INTEGER"; + } +} + +/** + * @brief The function check whether column type is ANY-NUMERICAL. + * Where ANY-NUMERICAL is SQL type: + * SMALLINT, INTEGER, BIGINT, REAL, FLOAT + * + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected column type is ANY-NUMERICAL. + */ +void check_any_numerical_type(const vrprouting::Info &info) { + if (!(info.type == INT2OID + || info.type == INT4OID + || info.type == INT8OID + || info.type == FLOAT4OID + || info.type == FLOAT8OID + || info.type == NUMERICOID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected ANY-NUMERICAL"; + } +} + +/** + * @brief The function check whether column type is TEXT or not. + * Where TEXT is SQL type: + * TEXT + * + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected column type is TEXT. + */ +void +check_text_type(const vrprouting::Info &info) { + if (!(info.type == TEXTOID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected TEXT"; + } +} + +/** + * @brief The function check whether column type is CHAR or not. + * Where CHAR is SQL type: + * CHARACTER + * + * [BPCHAROID](https://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html#afa7749dbe36d31874205189d9d6b21d7) + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected column type is CHAR. + */ +void +check_char_type(const vrprouting::Info &info) { + if (!(info.type == BPCHAROID)) { + throw std::string("Unexpected type in column '") + info.name + "'. Expected TEXT"; + } +} + +TInterval +getInterval(const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) { + Datum binval; + bool isnull; + Interval* interval; + + binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull); + + if (isnull) throw std::string("Unexpected Null value in column ") + info.name; + + switch (info.type) { + case INTERVALOID: + interval = DatumGetIntervalP(binval); + break; + default: + throw std::string("Unexpected type value in column '") + info.name + "'. Expected INTERVALOID"; + } + + return interval->time / 1000000 + + interval->day * SECS_PER_DAY + + static_cast( + interval->month * ((DAYS_PER_YEAR / static_cast(MONTHS_PER_YEAR)) * SECS_PER_DAY)); +} + + +TTimestamp +getTimeStamp(const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) { + Datum binval; + bool isnull; + TTimestamp value = 0; + binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull); + + if (isnull) throw std::string("Unexpected Null value in column ") + info.name; + + switch (info.type) { + case 1114: + value = vrprouting::get_timestamp_without_timezone((TTimestamp) Int64GetDatum(binval)); + break; + default: + throw std::string("Unexpected type value in column '") + info.name + "'. Expected 1114"; + } + return value; +} + +/** + * @param[in] tuple input row to be examined. + * @param[in] tupdesc tuple descriptor + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected column type is ANY-INTEGER. + * @throw ERROR When value of column is NULL. + * + * @return Integer type of column value is returned. + */ +int64_t getBigInt( + const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) { + Datum binval; + bool isnull; + int64_t value = 0; + binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull); + if (isnull) + throw std::string("Unexpected Null value in column ") + info.name; + switch (info.type) { + case INT2OID: + value = static_cast(DatumGetInt16(binval)); + break; + case INT4OID: + value = static_cast(DatumGetInt32(binval)); + break; + case INT8OID: + value = DatumGetInt64(binval); + break; + default: + throw std::string("Unexpected type in column type of ") + info.name + ". Expected ANY-INTEGER"; + } + return value; +} + +/** + * @param[in] tuple input row to be examined. + * @param[in] tupdesc tuple descriptor + * @param[in] info contain column information. + * @throw ERROR Unexpected type in column. Expected column type is ANY-NUMERICAL. + * @throw ERROR When value of column is NULL. + * @return Double type of column value is returned. + */ +double getFloat8( + const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) { + Datum binval; + bool isnull = false; + binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isnull); + if (isnull) + throw std::string("Unexpected Null value in column ") + info.name; + + switch (info.type) { + case INT2OID: + return static_cast(DatumGetInt16(binval)); + break; + case INT4OID: + return static_cast(DatumGetInt32(binval)); + break; + case INT8OID: + return static_cast(DatumGetInt64(binval)); + break; + case FLOAT4OID: + return static_cast(DatumGetFloat4(binval)); + break; + case FLOAT8OID: + return static_cast(DatumGetFloat8(binval)); + break; + case NUMERICOID: + /* Note: out-of-range values will be clamped to +-HUGE_VAL */ + return static_cast(DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, binval))); + break; + default: + throw std::string("Unexpected type in column type of ") + info.name + ". Expected ANY-NUMERICAL"; + } + return 0.0; +} + +/** + * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html; + * [SPI_getbinval](https://www.postgresql.org/docs/8.1/static/spi-spi-getbinval.html) + * [Datum](https://doxygen.postgresql.org/datum_8h.html) + * [DatumGetInt16](https://doxygen.postgresql.org/postgres_8h.html#aec991e04209850f29a8a63df0c78ba2d) + * + * @param[in] tuple input row to be examined. + * @param[in] tupdesc tuple descriptor + * @param[in] info contain column information. + * @param[in] default_value returned when column contain NULL value. + * @throw ERROR Unexpected type in column. Expected column type is CHAR. + * @throw ERROR When value of column is NULL. + * @return Char type of column value is returned. + */ +char getChar( + const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info, char default_value) { + Datum binval; + bool isNull; + char value = default_value; + + binval = SPI_getbinval(tuple, tupdesc, info.colNumber, &isNull); + if (!(info.type == BPCHAROID)) { + throw std::string("Unexpected type in column type of ") + info.name + ". Expected CHAR"; + } + + if (!isNull) { + value = reinterpret_cast(binval)[1]; + } else { + if (info.strict) { + throw std::string("Unexpected Null value in column ") + info.name; + } + value = default_value; + } + return value; +} + + +/** @brief get the array contents from postgres + * + * @details This function generates the array inputs according to their type + * received through @a ArrayType *v parameter and store them in @a c_array. It + * can be empty also if received @a allow_empty true. The cases of failure are:- + * 1. When @a ndim is not equal to one dimension. + * 2. When no element is found i.e. nitems is zero or negative. + * 3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type + * 4. When size of @a c_array is out of range or memory. + * 5. When null value is found in the array. + * + * All these failures are represented as error through @a elog. + * @param[in] v Pointer to the postgres C array + * + * @pre the array has to be one dimension + * @pre Must have elements (when allow_empty is false) + * + * @returns set of elements on the PostgreSQL array + */ +std::unordered_set +pgarray_to_unordered_set(ArrayType *v) { + std::unordered_set results; + + if (!v) return results; + + auto element_type = ARR_ELEMTYPE(v); + auto dim = ARR_DIMS(v); + auto ndim = ARR_NDIM(v); + auto nitems = ArrayGetNItems(ndim, dim); + Datum *elements = nullptr; + bool *nulls = nullptr; + int16 typlen; + bool typbyval; + char typalign; + + + if (ndim == 0 || nitems <= 0) { + return results; + } + + if (ndim != 1) { + throw std::string("One dimension expected"); + } + + get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); + + switch (element_type) { + case INT2OID: + case INT4OID: + case INT8OID: + break; + default: + throw std::string("Expected array of ANY-INTEGER"); + } + + deconstruct_array(v, element_type, typlen, typbyval, + typalign, &elements, &nulls, + &nitems); + + int64_t data(0); + + for (int i = 0; i < nitems; i++) { + if (nulls[i]) { + throw std::string("NULL value found in Array!"); + } else { + switch (element_type) { + case INT2OID: + data = static_cast(DatumGetInt16(elements[i])); + break; + case INT4OID: + data = static_cast(DatumGetInt32(elements[i])); + break; + case INT8OID: + data = DatumGetInt64(elements[i]); + break; + } + } + /* + * Before saving, check if its a uint32_t + */ + + if (data < 0 || data > std::numeric_limits::max()) { + throw std::string("Illegal value found on array"); + } + results.insert(static_cast(data)); + } + + pfree(elements); + pfree(nulls); + return results; +} + +/** @brief get the array contents from postgres + * + * @details This function generates the array inputs according to their type + * received through @a ArrayType *v parameter and store them in @a c_array. It + * can be empty also if received @a allow_empty true. The cases of failure are:- + * 1. When @a ndim is not equal to one dimension. + * 2. When no element is found i.e. nitems is zero or negative. + * 3. If the element type doesn't lie in switch cases, give the error of expected array of any integer type + * 4. When size of @a c_array is out of range or memory. + * 5. When null value is found in the array. + * + * All these failures are represented as error through @a elog. + * @param[in] v Pointer to the postgres C array + * @param[in] allow_empty flag to allow empty arrays + * + * @pre the array has to be one dimension + * @pre Must have elements (when allow_empty is false) + * + * @returns Vector of elements of the PostgreSQL array + */ +std::vector +get_pgarray(ArrayType *v, bool allow_empty) { + std::vector results; + if (!v) return results; + + auto element_type = ARR_ELEMTYPE(v); + auto dim = ARR_DIMS(v); + auto ndim = ARR_NDIM(v); + auto nitems = ArrayGetNItems(ndim, dim); + Datum *elements = nullptr; + bool *nulls = nullptr; + int16 typlen; + bool typbyval; + char typalign; + + + if (allow_empty && (ndim == 0 || nitems <= 0)) { + return results; + } + + if (ndim != 1) { + throw std::string("One dimension expected"); + } + + if (nitems <= 0) { + throw std::string("No elements found"); + } + + get_typlenbyvalalign(element_type, &typlen, &typbyval, &typalign); + + /* validate input data type */ + switch (element_type) { + case INT2OID: + case INT4OID: + case INT8OID: + break; + default: + throw std::string("Expected array of ANY-INTEGER"); + } + + deconstruct_array(v, element_type, typlen, typbyval, + typalign, &elements, &nulls, + &nitems); + + int64_t data(0); + + results.reserve(static_cast(nitems)); + + for (int i = 0; i < nitems; i++) { + if (nulls[i]) { + throw std::string("NULL value found in Array!"); + } else { + switch (element_type) { + case INT2OID: + data = static_cast(DatumGetInt16(elements[i])); + break; + case INT4OID: + data = static_cast(DatumGetInt32(elements[i])); + break; + case INT8OID: + data = DatumGetInt64(elements[i]); + break; + } + } + results.push_back(data); + } + + pfree(elements); + pfree(nulls); + return results; +} + +std::vector +get_BigIntArr_wEmpty( + const HeapTuple tuple, const TupleDesc &tupdesc, + const vrprouting::Info &info) { + bool is_null = false; + + Datum raw_array = SPI_getbinval(tuple, tupdesc, info.colNumber, &is_null); + /* + * [DatumGetArrayTypeP](https://doxygen.postgresql.org/array_8h.html#aa1b8e77c103863862e06a7b7c07ec532) + * [pgr_get_bigIntArray](http://docs.vrprouting.org/doxy/2.2/arrays__input_8c_source.html) + */ + if (!raw_array) return std::vector(); + + ArrayType *pg_array = DatumGetArrayTypeP(raw_array); + + return get_pgarray(pg_array, true); +} + + +} // namespace + +namespace vrprouting { + +namespace detail { +std::vector +get_any_positive_array(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) { + if (!column_found(info)) return std::vector(); + auto data = get_BigIntArr_wEmpty(tuple, tupdesc, info); + for (const auto &e : data) { + if (e < 0) throw std::string("Unexpected negative value in array '") + info.name + "'"; + } + return data; +} + +std::vector +get_uint_array(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) { + bool is_null = false; + + Datum raw_array = SPI_getbinval(tuple, tupdesc, info.colNumber, &is_null); + if (!raw_array) return std::vector(); + + ArrayType *pg_array = DatumGetArrayTypeP(raw_array); + + auto data = get_pgarray(pg_array, true); + std::vector results(data.begin(), data.end()); + return results; +} + +/** + * @param [in] tuple + * @param [in] tupdesc + * @param [in] info about the column been fetched + * @param [in] opt_value default value when the column does not exist + * + * @returns The value found + * @returns opt_value when the column does not exist + * + * exceptions when the value is negative + * @pre for positive values only + */ +TInterval +get_interval( + const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, TInterval opt_value) { + TInterval value = column_found(info)? getInterval(tuple, tupdesc, info) : opt_value; + if (value < 0) throw std::string("Unexpected negative value in column '") + info.name + "'"; + return (TInterval) value; +} + +/** + * @param [in] tuple + * @param [in] tupdesc + * @param [in] info about the column been fetched + * @param [in] opt_value default value when the column does not exist + * + * @returns The value found + * @returns opt_value when the column does not exist + */ +TTimestamp +get_timestamp( + const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, TTimestamp opt_value) { + return column_found(info)? getTimeStamp(tuple, tupdesc, info) : opt_value; +} + +/** + * @param [in] tuple from postgres + * @param [in] tupdesc from postgres + * @param [in] info about the column been fetched + * @param [in] opt_value default value when the column does not exist + * + * @returns The value found + * @returns opt_value when the column does not exist + * + * Used with vrprouting::ANY_INTEGER + */ +int64_t +get_anyinteger(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, int64_t opt_value) { + return column_found(info)? getBigInt(tuple, tupdesc, info) : opt_value; +} +} // namespace detail + +/** + * @param[in] info column information + * @return @b TRUE when colNumber exist. + * @b FALSE when colNumber was not found. + * + * [SPI_ERROR_NOATTRIBUTE](https://doxygen.postgresql.org/spi_8h.html#ac1512d8aaa23c2d57bb0d1eb8f453ee2) + */ +bool column_found(const Info &info) { + return !(info.colNumber == SPI_ERROR_NOATTRIBUTE); +} + + +/** + * @param[in] tupdesc tuple descriptor + * @param[in] info contain one or more column information. + * + * @throw ERROR Unknown type of column. + */ +void fetch_column_info( + const TupleDesc &tupdesc, + std::vector &info) { + for (auto &coldata : info) { + if (get_column_info(tupdesc, coldata)) { + switch (coldata.eType) { + case ANY_INTEGER: + case TINTERVAL: + case ANY_UINT: + check_any_integer_type(coldata); + break; + case ANY_NUMERICAL: + check_any_numerical_type(coldata); + break; + case TEXT: + check_text_type(coldata); + break; + case CHAR1: + check_char_type(coldata); + break; + case ANY_INTEGER_ARRAY: + case ANY_POSITIVE_ARRAY: + check_any_integerarray_type(coldata); + break; + case POSITIVE_INTEGER: + case INTEGER: + check_integer_type(coldata); + break; + case JSONB: + check_jsonb_type(coldata); + break; + case INTEGER_ARRAY: + case ANY_UINT_ARRAY: + check_integerarray_type(coldata); + break; + case TIMESTAMP: + check_timestamp_type(coldata); + break; + case INTERVAL: + check_interval_type(coldata); + break; + default: + throw std::string("Case not found in column '") + coldata.name + "' Please inform the developers"; + } + } + } +} + + +/** + * @param [in] tuple from postgres + * @param [in] tupdesc from postgres + * @param [in] info about the column been fetched + * @param [in] opt_value default value when the column does not exist + * + * @returns The value found + * @returns opt_value when the column does not exist + * + * Used with vrprouting::ANY_NUMERICAL + */ +double +get_anynumerical(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, double opt_value) { + return column_found(info)? getFloat8(tuple, tupdesc, info) : opt_value; +} + + +/** + * @param [in] tuple + * @param [in] tupdesc + * @param [in] info about the column been fetched + * @param [in] opt_value default value when the column does not exist + * + * @returns The value found + * @returns opt_value when the column does not exist + */ +char +get_char(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info, char opt_value) { + return getChar(tuple, tupdesc, info, opt_value); +} + + +/** + * @param [in] tuple + * @param [in] tupdesc + * @param [in] info about the column been fetched + * + * @returns "{}" (empty jsonb) when when the column does not exist + */ +std::string get_jsonb(const HeapTuple tuple, const TupleDesc &tupdesc, const vrprouting::Info &info) { + return column_found(info)? DatumGetCString(SPI_getvalue(tuple, tupdesc, info.colNumber)) : "{}"; +} + +std::unordered_set +get_uint_unordered_set(const HeapTuple tuple, const TupleDesc &tupdesc, const Info &info) { + bool is_null = false; + Datum raw_array = SPI_getbinval(tuple, tupdesc, info.colNumber, &is_null); + if (!raw_array) return std::unordered_set(); + + ArrayType *pg_array = DatumGetArrayTypeP(raw_array); + + return pgarray_to_unordered_set(pg_array); +} + +/** + * Steps: + * 1) Similar to: https://doxygen.postgresql.org/backend_2utils_2adt_2timestamp_8c.html#a52973f03ed8296b632d4028121f7e077 + * 2) Using time.h to convert + * + * from time.h + * struct tm + * timezone + */ +TTimestamp +get_timestamp_without_timezone(TTimestamp timestamp) { + /* + * step 1 + */ + Timestamp date; + Timestamp time = timestamp; + TMODULO(time, date, USECS_PER_DAY); + if (time < INT64CONST(0)) { + time += USECS_PER_DAY; + date -= 1; + } + date += POSTGRES_EPOCH_JDATE; + /* Julian day routine does not work for negative Julian days */ + if (date < 0 || date > (Timestamp) INT_MAX) { + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("Julian day routine does not work for negative Julian days"))); + } + + /* + * using structure from time.h to store values + */ + struct tm info; + fsec_t fsec; + + /* + * calling postgres functions + */ + j2date(static_cast(date), &info.tm_year, &info.tm_mon, &info.tm_mday); + dt2time(time, &info.tm_hour, &info.tm_min, &info.tm_sec, &fsec); + + /* + * adjust values before calling mktime + */ + info.tm_isdst = -1; + info.tm_year = info.tm_year - 1900; + info.tm_mon = info.tm_mon - 1; + + /* + * mktime & timezone are defined in time.h + */ + return mktime(&info) - timezone; +} + + +} // namespace vrprouting From 14ae65d1e34dc836160dc92e83d8283b417074c0 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:18:36 -0600 Subject: [PATCH 06/20] Structures for data retreival: struct -> class in namespace vrprouting --- include/cpp_common/info.hpp | 55 +++++++++++++-------- include/cpp_common/matrix_cell_t.hpp | 13 +++-- include/cpp_common/orders_t.hpp | 51 ++++++++++---------- include/cpp_common/time_multipliers_t.hpp | 15 ++++-- include/cpp_common/vehicle_t.hpp | 43 +++++++++-------- include/cpp_common/vroom_break_t.hpp | 16 +++++-- include/cpp_common/vroom_job_t.hpp | 36 +++++++------- include/cpp_common/vroom_matrix_t.hpp | 16 +++++-- include/cpp_common/vroom_shipment_t.hpp | 56 ++++++++++++---------- include/cpp_common/vroom_time_window_t.hpp | 19 +++++--- include/cpp_common/vroom_vehicle_t.hpp | 43 ++++++++--------- 11 files changed, 206 insertions(+), 157 deletions(-) diff --git a/include/cpp_common/info.hpp b/include/cpp_common/info.hpp index 37275bf60..2a6e8d469 100644 --- a/include/cpp_common/info.hpp +++ b/include/cpp_common/info.hpp @@ -27,15 +27,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_INFO_HPP_ #pragma once -/* for int64_t */ -#ifndef __cplusplus -# include -# include -#endif - -// used for getting the data -typedef -enum { +#include +#include + +namespace vrprouting { + +enum expectType { INTEGER, ANY_INTEGER, ANY_NUMERICAL, @@ -45,18 +42,38 @@ enum { INTEGER_ARRAY, ANY_INTEGER_ARRAY, TIMESTAMP, - INTERVAL -} expectType; + INTERVAL, + + POSITIVE_INTEGER, + ANY_POSITIVE_INTEGER, + ANY_UINT, + ANY_POSITIVE_ARRAY, + UINT_ARRAY, + ANY_UINT_ARRAY, + /* similar types */ + STEP_TYPE = INTEGER, + AMOUNT = ANY_INTEGER, + ID = ANY_INTEGER, + TTIMESTAMP = ANY_INTEGER, + COORDINATE = ANY_NUMERICAL, + SPEED = ANY_NUMERICAL, + MULTIPLIER = ANY_NUMERICAL, + TINTERVAL = ANY_POSITIVE_INTEGER, + MATRIX_INDEX = ANY_POSITIVE_INTEGER, + IDX = ANY_UINT, + PAMOUNT = ANY_UINT +}; -typedef -struct { - int colNumber; - uint64_t type; - bool strict; - char *name; - expectType eType; -} Column_info_t; +class Info { + public: + int colNumber; + uint64_t type; + bool strict; + std::string name; + expectType eType; +}; +} // namespace vrprouting #endif // INCLUDE_CPP_COMMON_INFO_HPP_ diff --git a/include/cpp_common/matrix_cell_t.hpp b/include/cpp_common/matrix_cell_t.hpp index fe925bd2d..20aa49191 100644 --- a/include/cpp_common/matrix_cell_t.hpp +++ b/include/cpp_common/matrix_cell_t.hpp @@ -29,6 +29,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief traveling costs @note C/C++/postgreSQL connecting structure for input @@ -38,10 +40,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. to_vid | Arrival node's identifier cost | Travel cost from departure to arrival */ -struct Matrix_cell_t { - Id from_vid; /** @b departure node's identifier */ - Id to_vid; /** @b arrival node's identifier */ - TInterval cost; /** Travel Interval from departure to arrival */ +class Matrix_cell_t { + public: + Id from_vid; /** @b departure node's identifier */ + Id to_vid; /** @b arrival node's identifier */ + TInterval cost; /** Travel Interval from departure to arrival */ }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_MATRIX_CELL_T_HPP_ diff --git a/include/cpp_common/orders_t.hpp b/include/cpp_common/orders_t.hpp index 9aa08c3f0..3496767e4 100644 --- a/include/cpp_common/orders_t.hpp +++ b/include/cpp_common/orders_t.hpp @@ -28,6 +28,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief order's attributes @note C/C++/postgreSQL connecting structure for input @@ -44,33 +46,28 @@ deliver_open_t | Deliver open time deliver_close_t | Deliver close time deliver_service_t | Deliver service duration */ - - -/************************************************************************** - * pickDelivery types - * ***********************************************************************/ -/* - * its with either (foo_x, foo_y) pairs (for euclidean or with foo_node_id (for matrix) - */ -struct Orders_t{ - Id id; /** Order's identifier */ - PAmount demand; /** Number of demand */ - - Coordinate pick_x; /** Pick x coordinate: used in stand alone program for benchmarks */ - Coordinate pick_y; /** Pick y coordinate: used in stand alone program for benchmarks */ - Id pick_node_id; /** Pickup node identifier */ - - TTimestamp pick_open_t; /** Pickup open time*/ - TTimestamp pick_close_t; /** Pickup close time*/ - TInterval pick_service_t; /** Pickup service duration */ - - Coordinate deliver_x; /** Deliver x coordinate: used in stand alone program for benchmarks */ - Coordinate deliver_y; /** Deliver y coordinate: used in stand alone program for benchmarks */ - Id deliver_node_id; /** Deliver node identifier */ - - TTimestamp deliver_open_t; /** Deliver open time */ - TTimestamp deliver_close_t; /** Deliver close time */ - TInterval deliver_service_t; /** Deliver service duration */ +class Orders_t{ + public: + Id id; /** Order's identifier */ + PAmount demand; /** Number of demand */ + + Coordinate pick_x; /** Pick x coordinate */ + Coordinate pick_y; /** Pick y coordinate */ + Id pick_node_id; /** Pickup node identifier */ + + TTimestamp pick_open_t; /** Pickup open time*/ + TTimestamp pick_close_t; /** Pickup close time*/ + TInterval pick_service_t; /** Pickup service duration */ + + Coordinate deliver_x; /** Deliver x coordinate */ + Coordinate deliver_y; /** Deliver y coordinate */ + Id deliver_node_id; /** Deliver node identifier */ + + TTimestamp deliver_open_t; /** Deliver open time */ + TTimestamp deliver_close_t; /** Deliver close time */ + TInterval deliver_service_t; /** Deliver service duration */ }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_ORDERS_T_HPP_ diff --git a/include/cpp_common/time_multipliers_t.hpp b/include/cpp_common/time_multipliers_t.hpp index 28525d7fe..f0ea2886a 100644 --- a/include/cpp_common/time_multipliers_t.hpp +++ b/include/cpp_common/time_multipliers_t.hpp @@ -30,6 +30,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief Time Dependant Multipliers @note C/C++/postgreSQL connecting structure for input @@ -43,11 +45,14 @@ multiplier | takes effect starting from the @b start_time - Evrything between after the largest start time will have the multiplier of the largest start_time */ -struct Time_multipliers_t { - /** Time of day where the multiplier starts to be valid */ - TTimestamp start_time; - /** multiplier at hour */ - Multiplier multiplier; +class Time_multipliers_t { + public: + /** Time of day where the multiplier starts to be valid */ + TTimestamp start_time; + /** multiplier at hour */ + Multiplier multiplier; }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_TIME_MULTIPLIERS_T_HPP_ diff --git a/include/cpp_common/vehicle_t.hpp b/include/cpp_common/vehicle_t.hpp index 060f5f46b..e1cd62f63 100644 --- a/include/cpp_common/vehicle_t.hpp +++ b/include/cpp_common/vehicle_t.hpp @@ -27,8 +27,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_VEHICLE_T_HPP_ #pragma once +#include #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief vehicles's attributes @note C/C++/postgreSQL connecting structure for input @@ -45,30 +48,30 @@ end_open_t | End open time end_close_t | End close time end_service_t | End service time stops | Vehicle's stops -stops_size | Stops size */ -struct Vehicle_t { - Id id; /** Vehicle's identifier */ - PAmount capacity; /** Vehicle's capacity */ - Speed speed; - PAmount cant_v; /** Number of vehicles with same description **/ - Id *stops; /** Stops */ - size_t stops_size; /** Stops size */ +class Vehicle_t { + public: + Id id; /** Vehicle's identifier */ + PAmount capacity; /** Vehicle's capacity */ + Speed speed; + PAmount cant_v; /** Number of vehicles with same description **/ + std::vector stops; /** Stops */ - Id start_node_id; /** Start node's identifier */ - TTimestamp start_open_t; /** Start open time */ - TTimestamp start_close_t; /** Start close time */ - TInterval start_service_t; /** Start service duration */ - Coordinate start_x; - Coordinate start_y; + Id start_node_id; /** Start node's identifier */ + TTimestamp start_open_t; /** Start open time */ + TTimestamp start_close_t; /** Start close time */ + TInterval start_service_t; /** Start service duration */ + Coordinate start_x; + Coordinate start_y; - Id end_node_id; /** End node's identifier */ - TTimestamp end_open_t; /** End open time */ - TTimestamp end_close_t; /** End close time */ - TInterval end_service_t; /** End service time */ - Coordinate end_x; - Coordinate end_y; + Id end_node_id; /** End node's identifier */ + TTimestamp end_open_t; /** End open time */ + TTimestamp end_close_t; /** End close time */ + TInterval end_service_t; /** End service time */ + Coordinate end_x; + Coordinate end_y; }; +} // namespace vrprouting #endif // INCLUDE_CPP_COMMON_VEHICLE_T_HPP_ diff --git a/include/cpp_common/vroom_break_t.hpp b/include/cpp_common/vroom_break_t.hpp index 0a1de1aa8..f86b16aab 100644 --- a/include/cpp_common/vroom_break_t.hpp +++ b/include/cpp_common/vroom_break_t.hpp @@ -31,8 +31,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_VROOM_BREAK_T_HPP_ #pragma once +#include + #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief Vehicle's break attributes @note C/C++/postgreSQL connecting structure for input @@ -43,12 +47,14 @@ vehicle_id | Identifier of vehicle service | Duration of break data | Metadata information of break */ -struct Vroom_break_t { - Idx id; /** Identifier of break */ - Idx vehicle_id; /** Identifier of vehicle */ - Duration service; /** Duration of break */ - char *data; /** Metadata information of break */ +class Vroom_break_t { + public: + Idx id; /** Identifier of break */ + Idx vehicle_id; /** Identifier of vehicle */ + Duration service; /** Duration of break */ + std::string data; /** Metadata information of break */ }; +} // namespace vrprouting #endif // INCLUDE_CPP_COMMON_VROOM_BREAK_T_HPP_ diff --git a/include/cpp_common/vroom_job_t.hpp b/include/cpp_common/vroom_job_t.hpp index dc0f4400a..2c2a0bd92 100644 --- a/include/cpp_common/vroom_job_t.hpp +++ b/include/cpp_common/vroom_job_t.hpp @@ -31,8 +31,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_VROOM_JOB_T_HPP_ #pragma once +#include + +#include +#include #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief Job's attributes @note C/C++/postgreSQL connecting structure for input @@ -43,33 +49,29 @@ location_id | Location index of job in matrix setup | Job setup duration service | Job service duration delivery | Quantities for delivery -delivery_size | Number of delivery quantities pickup | Quantities for pickup -pickup_size | Number of pickup quantities skills | Mandatory skills -skills_size | Number of mandatory skills priority | Priority level of job data | Metadata information of job */ -struct Vroom_job_t { - Idx id; /** The job's identifier */ - MatrixIndex location_id; /** Location index of job in matrix */ +class Vroom_job_t { + public: + Idx id; /** The job's identifier */ + MatrixIndex location_id; /** Location index of job in matrix */ - Duration setup; /** Job setup duration */ - Duration service; /** Job service duration */ + ::vroom::Duration setup; /** Job setup duration */ + ::vroom::Duration service; /** Job service duration */ - Amount *delivery; /** Quantities for delivery */ - size_t delivery_size; /** Number of delivery quantities */ + ::vroom::Amount pickup; /** Quantities for pickup */ + ::vroom::Amount delivery; /** Quantities for delivery */ - Amount *pickup; /** Quantities for pickup */ - size_t pickup_size; /** Number of pickup quantities */ + ::vroom::Skills skills; /** Mandatory skills */ - Skill *skills; /** Mandatory skills */ - size_t skills_size; /** Number of mandatory skills */ + Priority priority; /** Priority level of job */ - Priority priority; /** Priority level of job */ - - char *data; /** Metadata information of job */ + std::string data; /** Metadata information of job */ }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_VROOM_JOB_T_HPP_ diff --git a/include/cpp_common/vroom_matrix_t.hpp b/include/cpp_common/vroom_matrix_t.hpp index 12d511ed6..16c2075e9 100644 --- a/include/cpp_common/vroom_matrix_t.hpp +++ b/include/cpp_common/vroom_matrix_t.hpp @@ -33,6 +33,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief Matrix's attributes @note C/C++/postgreSQL connecting structure for input @@ -43,12 +45,16 @@ end_id | End node identifier duration | Duration to travel from start to end cost | Cost to travel from start to end */ -struct Vroom_matrix_t { - MatrixIndex start_id; /** Start node identifier */ - MatrixIndex end_id; /** End node identifier */ +class Vroom_matrix_t { + public: + MatrixIndex start_id; /** Start node identifier */ + MatrixIndex end_id; /** End node identifier */ - Duration duration; /** Duration to travel from start to end */ - TravelCost cost; /** Cost to travel from start to end */ + Duration duration; /** Duration to travel from start to end */ + TravelCost cost; /** Cost to travel from start to end */ }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_VROOM_MATRIX_T_HPP_ + diff --git a/include/cpp_common/vroom_shipment_t.hpp b/include/cpp_common/vroom_shipment_t.hpp index e6f1b745e..d61ddf705 100644 --- a/include/cpp_common/vroom_shipment_t.hpp +++ b/include/cpp_common/vroom_shipment_t.hpp @@ -31,9 +31,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_VROOM_SHIPMENT_T_HPP_ #pragma once +#include + +#include + #include "c_types/typedefs.h" -/** @brief Vehicles's attributes +namespace vrprouting { + + +/** @brief Vroom's shipment's attributes @note C/C++/postgreSQL connecting structure for input name | description @@ -46,36 +53,33 @@ d_location_id | Delivery location index in matrix d_setup | Delivery setup time d_service | Delivery service time amount | Quantities for shipment -amount_size | Number of quantities skills | Mandatory skills -skills_size | Number of skills priority | Priority level of shipment p_data | Metadata information of pickup shipment d_data | Metadata information of delivery shipment */ -struct Vroom_shipment_t { - Idx id; /** Shipment identifier */ - - /** pickup shipment */ - MatrixIndex p_location_id; /** Pickup location index in matrix */ - Duration p_setup; /** Pickup setup time */ - Duration p_service; /** Pickup service time */ - - /** delivery shipment */ - MatrixIndex d_location_id; /** Delivery location index in matrix */ - Duration d_setup; /** Delivery setup time */ - Duration d_service; /** Delivery service time */ - - Amount *amount; /** Quantities for shipment */ - size_t amount_size; /** Number of quantities */ - - Skill *skills; /** Mandatory skills */ - size_t skills_size; /** Number of skills */ - - Priority priority; /** Priority level of shipment */ - - char *p_data; /** Metadata information of pickup shipment */ - char *d_data; /** Metadata information of delivery shipment */ +class Vroom_shipment_t { + public: + Idx id; /** Shipment identifier */ + + /** pickup shipment */ + MatrixIndex p_location_id; /** Pickup location index in matrix */ + Duration p_setup; /** Pickup setup time */ + Duration p_service; /** Pickup service time */ + + /** delivery shipment */ + MatrixIndex d_location_id; /** Delivery location index in matrix */ + Duration d_setup; /** Delivery setup time */ + Duration d_service; /** Delivery service time */ + + ::vroom::Amount amount; /** Quantities for shipment */ + ::vroom::Skills skills; /** Mandatory skills */ + Priority priority; /** Priority level of shipment */ + + std::string p_data; /** Metadata information of pickup shipment */ + std::string d_data; /** Metadata information of delivery shipment */ }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_VROOM_SHIPMENT_T_HPP_ diff --git a/include/cpp_common/vroom_time_window_t.hpp b/include/cpp_common/vroom_time_window_t.hpp index e9321414f..5c24b9406 100644 --- a/include/cpp_common/vroom_time_window_t.hpp +++ b/include/cpp_common/vroom_time_window_t.hpp @@ -31,8 +31,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_VROOM_TIME_WINDOW_T_HPP_ #pragma once +#include + #include "c_types/typedefs.h" +namespace vrprouting { + /** @brief Time window's attributes @note C/C++/postgreSQL connecting structure for input @@ -40,14 +44,15 @@ name | description :----- | :------- id | Identifier of the job/shipment/break kind | Whether the shipment is a pickup ('p') or a delivery ('d') -tw_open | Time window opening time -tw_close | Time window closing time +tw | The time windows */ -struct Vroom_time_window_t { - Idx id; - char kind; - Duration tw_open; - Duration tw_close; +class Vroom_time_window_t { + public: + Idx id; + char kind; + ::vroom::TimeWindow tw; }; +} // namespace vrprouting + #endif // INCLUDE_CPP_COMMON_VROOM_TIME_WINDOW_T_HPP_ diff --git a/include/cpp_common/vroom_vehicle_t.hpp b/include/cpp_common/vroom_vehicle_t.hpp index c23ca4d90..02932707a 100644 --- a/include/cpp_common/vroom_vehicle_t.hpp +++ b/include/cpp_common/vroom_vehicle_t.hpp @@ -31,46 +31,45 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_CPP_COMMON_VROOM_VEHICLE_T_HPP_ #pragma once +#include + +#include + #include "c_types/typedefs.h" -/** @brief Vehicles's attributes +namespace vrprouting { + +/** @brief Vroom's Vehicles's attributes -@note C/C++/postgreSQL connecting structure for input + @note C/C++/postgreSQL connecting structure for input name | description :----- | :------- id | The vehicle's identifier start_id | Start location index in matrix end_id | End location index in matrix capacity | Vehicle's capacity array -capacity_size | Vehicle's capacity array size skills | Vehicle's skills -skills_size | Number of vehicle's skills -tw_open | Time window start time -tw_close | Time window end time +tw | The time windows speed_factor | Vehicle travel time multiplier max_tasks | Max number of tasks in a route for the vehicle data | Metadata information of vehicle */ -struct Vroom_vehicle_t { - Idx id; /** The vehicle's identifier */ - MatrixIndex start_id; /** Start location index in matrix */ - MatrixIndex end_id; /** End location index in matrix */ - - Amount *capacity; /** Vehicle's capacity array */ - size_t capacity_size; /** Vehicle's capacity array size */ - - Skill *skills; /** Vehicle's skills */ - size_t skills_size; /** Number of vehicle's skills */ - - Duration tw_open; /** Time window start time */ - Duration tw_close; /** Time window end time */ +class Vroom_vehicle_t { + public: + Idx id; /** The vehicle's identifier */ + MatrixIndex start_id; /** Start location index in matrix */ + MatrixIndex end_id; /** End location index in matrix */ - double speed_factor; /** Vehicle travel time multiplier */ + ::vroom::Amount capacity; /** Vehicle's capacity array */ + ::vroom::Skills skills; /** Mandatory skills */ - int32_t max_tasks; /** Max number of tasks in a route for the vehicle */ + ::vroom::TimeWindow tw; /** The Vroom time windows */ - char *data; /** Metadata information of vehicle */ + Multiplier speed_factor; /** Vehicle travel time multiplier */ + int32_t max_tasks; /** Max number of tasks in a route for the vehicle */ + std::string data; /** Metadata information of vehicle */ }; +} // namespace vrprouting #endif // INCLUDE_CPP_COMMON_VROOM_VEHICLE_T_HPP_ From 95ceeb4c1ed054d0f44fdd8256ddf8a25148ae61 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:19:58 -0600 Subject: [PATCH 07/20] (compatibleVehicles) moving code from C file to C++ file --- include/drivers/compatibleVehicles_driver.h | 24 ++- src/compatibleVehicles/compatibleVehicles.c | 148 +----------------- .../compatibleVehicles_driver.cpp | 63 ++++++-- 3 files changed, 64 insertions(+), 171 deletions(-) diff --git a/include/drivers/compatibleVehicles_driver.h b/include/drivers/compatibleVehicles_driver.h index 1d426633c..b0dc0c10d 100644 --- a/include/drivers/compatibleVehicles_driver.h +++ b/include/drivers/compatibleVehicles_driver.h @@ -32,18 +32,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #define INCLUDE_DRIVERS_COMPATIBLEVEHICLES_DRIVER_H_ #pragma once -/* for size-t */ #ifdef __cplusplus -# include +#include +#include +using CompatibleVehicles_rt = struct CompatibleVehicles_rt; #else -# include -#endif - -typedef struct Orders_t Orders_t; -typedef struct Time_multipliers_t Time_multipliers_t; -typedef struct Vehicle_t Vehicle_t; -typedef struct Matrix_cell_t Matrix_cell_t; +#include +#include +#include typedef struct CompatibleVehicles_rt CompatibleVehicles_rt; +#endif #ifdef __cplusplus extern "C" { @@ -51,12 +49,8 @@ extern "C" { /** @brief Driver for processing a "compatible vehicles" problem */ void vrp_do_compatibleVehicles( - Orders_t customers_arr[], size_t total_customers, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, - Time_multipliers_t *multipliers_arr, size_t total_multipliers, - - double, + char*, char*, char*, char*, + double, bool, bool, bool, CompatibleVehicles_rt **, size_t*, char**, char**, char**); diff --git a/src/compatibleVehicles/compatibleVehicles.c b/src/compatibleVehicles/compatibleVehicles.c index 9d9a2feb0..51c73e20b 100644 --- a/src/compatibleVehicles/compatibleVehicles.c +++ b/src/compatibleVehicles/compatibleVehicles.c @@ -25,16 +25,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include #include "c_common/postgres_connection.h" -#include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/orders_input.h" -#include "c_common/vehicles_input.h" -#include "c_common/matrixRows_input.h" -#include "c_common/time_multipliers_input.h" -#include "cpp_common/orders_t.hpp" #include "c_types/compatibleVehicles_rt.h" #include "drivers/compatibleVehicles_driver.h" @@ -61,130 +54,19 @@ process( vrp_SPI_connect(); - if (factor <= 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: factor"), - errhint("Value found: %f <= 0", factor))); - } - - Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - - if (use_timestamps) { - get_shipments(pd_orders_sql, &pd_orders_arr, &total_pd_orders); - } else { - get_shipments_raw(pd_orders_sql, &pd_orders_arr, &total_pd_orders); - } - - if (total_pd_orders == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - if (use_timestamps) { - get_vehicles(vehicles_sql, &vehicles_arr, &total_vehicles, false); - } else { - get_vehicles_raw(vehicles_sql, &vehicles_arr, &total_vehicles, false); - } - - if (total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Time_multipliers_t *multipliers_arr = NULL; - size_t total_multipliers_arr = 0; - if (use_timestamps) { - get_timeMultipliers(multipliers_sql, &multipliers_arr, &total_multipliers_arr); - } else { - get_timeMultipliers_raw(multipliers_sql, &multipliers_arr, &total_multipliers_arr); - } - - if (total_multipliers_arr == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Matrix_cell_t *matrix_cells_arr = NULL; - size_t total_cells = 0; - if (use_timestamps) { - get_matrixRows(matrix_sql, &matrix_cells_arr, &total_cells); - } else { - get_matrixRows_plain(matrix_sql, &matrix_cells_arr, &total_cells); - } - - if (total_cells == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - PGR_DBG("Total %ld orders in query:", total_pd_orders); - PGR_DBG("Total %ld vehicles in query:", total_vehicles); - PGR_DBG("Total %ld matrix cells in query:", total_cells); - PGR_DBG("Total %ld multipliers in query:", total_cells); - clock_t start_t = clock(); vrp_do_compatibleVehicles( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, - matrix_cells_arr, total_cells, - multipliers_arr, total_multipliers_arr, + pd_orders_sql, + vehicles_sql, + matrix_sql, + multipliers_sql, factor, + use_timestamps, + false, // is_euclidean + false, // with_stops + result_tuples, result_count, @@ -201,20 +83,6 @@ process( vrp_global_report(&log_msg, ¬ice_msg, &err_msg); - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - vrp_SPI_finish(); } diff --git a/src/compatibleVehicles/compatibleVehicles_driver.cpp b/src/compatibleVehicles/compatibleVehicles_driver.cpp index 2875da45c..326d5f0c3 100644 --- a/src/compatibleVehicles/compatibleVehicles_driver.cpp +++ b/src/compatibleVehicles/compatibleVehicles_driver.cpp @@ -37,9 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" - -#include "cpp_common/matrix_cell_t.hpp" -#include "cpp_common/time_multipliers_t.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/orders_t.hpp" #include "cpp_common/vehicle_t.hpp" #include "problem/pickDeliver.hpp" @@ -47,13 +45,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void vrp_do_compatibleVehicles( - Orders_t orders_arr[], size_t total_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, - Time_multipliers_t *multipliers_arr, size_t total_multipliers, + char *orders_sql, + char *vehicles_sql, + char *matrix_sql, + char *multipliers_sql, double factor, + bool use_timestamps, + bool is_euclidean, + bool with_stops, + CompatibleVehicles_rt **return_tuples, size_t *return_count, @@ -71,6 +73,10 @@ vrp_do_compatibleVehicles( std::ostringstream err; try { using Matrix = vrprouting::problem::Matrix; + using vrprouting::pgget::pickdeliver::get_matrix; + using vrprouting::pgget::pickdeliver::get_orders; + using vrprouting::pgget::pickdeliver::get_vehicles; + using vrprouting::pgget::pickdeliver::get_timeMultipliers; /* * verify preconditions @@ -78,21 +84,46 @@ vrp_do_compatibleVehicles( pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); - pgassert(total_orders); - pgassert(total_vehicles); - pgassert(total_cells); pgassert(*return_count == 0); pgassert(!(*return_tuples)); - /* Data input starts */ + if (factor <= 0) { + *err_msg = to_pg_msg("Illegal value in parameter: factor"); + *log_msg = to_pg_msg("Expected value: factor > 0"); + return; + } /* - * transform to C++ containers + * Data input starts */ - std::vector vehicles(vehicles_arr, vehicles_arr + total_vehicles); - std::vector orders(orders_arr, orders_arr + total_orders); - std::vector costs(matrix_cells_arr, matrix_cells_arr + total_cells); - std::vector multipliers(multipliers_arr, multipliers_arr + total_multipliers); + hint = orders_sql; + auto orders = get_orders(std::string(orders_sql), is_euclidean, use_timestamps); + if (orders.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'orders' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = vehicles_sql; + auto vehicles = get_vehicles(std::string(vehicles_sql), is_euclidean, use_timestamps, with_stops); + if (vehicles.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'vehicles' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = matrix_sql; + auto costs = get_matrix(std::string(matrix_sql), use_timestamps); + + if (costs.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'matrix' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = multipliers_sql; + auto multipliers = get_timeMultipliers(std::string(multipliers_sql), use_timestamps); + hint = nullptr; /* Data input ends */ From 5aa7f4fd873058fb56848a3d25947934da6738aa Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:20:43 -0600 Subject: [PATCH 08/20] (pgr_pickDeliver) moving code from C file to C++ file --- include/drivers/pgr_pickDeliver_driver.h | 10 +- .../pgr_pickDeliver_driver.cpp | 70 +++++-- src/pgr_pickDeliver/pickDeliver.c | 188 +----------------- 3 files changed, 58 insertions(+), 210 deletions(-) diff --git a/include/drivers/pgr_pickDeliver_driver.h b/include/drivers/pgr_pickDeliver_driver.h index ea5bd81dc..20145d92a 100644 --- a/include/drivers/pgr_pickDeliver_driver.h +++ b/include/drivers/pgr_pickDeliver_driver.h @@ -35,17 +35,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus #include #include -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; -using Matrix_cell_t = struct Matrix_cell_t; using Solution_rt = struct Solution_rt; #else #include #include #include -typedef struct Orders_t Orders_t; -typedef struct Vehicle_t Vehicle_t; -typedef struct Matrix_cell_t Matrix_cell_t; typedef struct Solution_rt Solution_rt; #endif @@ -54,9 +48,7 @@ extern "C" { #endif void vrp_do_pgr_pickDeliver( - Orders_t *pd_orders_arr, size_t total_pd_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, + char*, char*, char*, double, int, int, Solution_rt**, size_t*, diff --git a/src/pgr_pickDeliver/pgr_pickDeliver_driver.cpp b/src/pgr_pickDeliver/pgr_pickDeliver_driver.cpp index 912ce0e22..691188ec0 100644 --- a/src/pgr_pickDeliver/pgr_pickDeliver_driver.cpp +++ b/src/pgr_pickDeliver/pgr_pickDeliver_driver.cpp @@ -27,18 +27,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "drivers/pgr_pickDeliver_driver.h" -#include #include #include -#include #include #include "c_types/solution_rt.h" #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" - -#include "cpp_common/matrix_cell_t.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/orders_t.hpp" #include "cpp_common/vehicle_t.hpp" #include "initialsol/initials_code.hpp" @@ -76,9 +73,9 @@ get_initial_solution(vrprouting::problem::PickDeliver &problem_ptr, int m_initia void vrp_do_pgr_pickDeliver( - struct Orders_t orders_arr[], size_t total_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, + char *orders_sql, + char *vehicles_sql, + char *matrix_sql, double factor, int max_cycles, @@ -101,6 +98,9 @@ vrp_do_pgr_pickDeliver( std::ostringstream err; try { using Matrix = vrprouting::problem::Matrix; + using vrprouting::pgget::pickdeliver::get_matrix; + using vrprouting::pgget::pickdeliver::get_orders; + using vrprouting::pgget::pickdeliver::get_vehicles; /* * verify preconditions @@ -108,20 +108,58 @@ vrp_do_pgr_pickDeliver( pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); - pgassert(total_orders); - pgassert(total_vehicles); - pgassert(total_vehicles); pgassert(*return_count == 0); pgassert(!(*return_tuples)); + if (initial_solution_id < 0 || initial_solution_id > 7) { + *err_msg = to_pg_msg("Illegal value in parameter: initial_sol"); + *log_msg = to_pg_msg("Expected value: 0 <= initial_sol < 7"); + return; + } + + if (max_cycles < 0) { + *err_msg = to_pg_msg("Illegal value in parameter: max_cycles"); + *log_msg = to_pg_msg("Expected value: max_cycles >= 0"); + return; + } + + if (factor <= 0) { + *err_msg = to_pg_msg("Illegal value in parameter: factor"); + *log_msg = to_pg_msg("Expected value: factor > 0"); + return; + } + /* Data input starts */ - /* - * transform to C++ containers - */ - std::vector vehicles(vehicles_arr, vehicles_arr + total_vehicles); - std::vector orders(orders_arr, orders_arr + total_orders); - std::vector costs(matrix_cells_arr, matrix_cells_arr + total_cells); + bool use_timestamps = false; + bool is_euclidean = false; + bool with_stops = false; + + hint = orders_sql; + auto orders = get_orders(std::string(orders_sql), is_euclidean, use_timestamps); + if (orders.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'orders' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = vehicles_sql; + auto vehicles = get_vehicles(std::string(vehicles_sql), is_euclidean, use_timestamps, with_stops); + if (vehicles.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'vehicles' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = matrix_sql; + auto costs = get_matrix(std::string(matrix_sql), use_timestamps); + + if (costs.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'matrix' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + hint = nullptr; /* Data input ends */ diff --git a/src/pgr_pickDeliver/pickDeliver.c b/src/pgr_pickDeliver/pickDeliver.c index 3428547bc..3d85a18cc 100644 --- a/src/pgr_pickDeliver/pickDeliver.c +++ b/src/pgr_pickDeliver/pickDeliver.c @@ -26,13 +26,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include "c_common/postgres_connection.h" -#include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/orders_input.h" -#include "c_common/vehicles_input.h" -#include "c_common/matrixRows_input.h" -#include "cpp_common/orders_t.hpp" #include "c_types/solution_rt.h" #include "drivers/pgr_pickDeliver_driver.h" @@ -59,177 +54,11 @@ process( vrp_SPI_connect(); - if (factor <= 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: factor"), - errhint("Value found: %f <= 0", factor))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - if (max_cycles < 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: max_cycles"), - errhint("Value found: %d <= 0", max_cycles))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - if (initial_solution_id < 0 || initial_solution_id > 7) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: initial"), - errhint("Value found: %d <= 0", initial_solution_id))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - - Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - get_shipments_raw(pd_orders_sql, - &pd_orders_arr, &total_pd_orders); - - if (total_pd_orders == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - - vrp_SPI_finish(); - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No orders found"))); - return; - } - - - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - get_vehicles_raw(vehicles_sql, - &vehicles_arr, &total_vehicles, - false); - - if (total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - - vrp_SPI_finish(); - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No vehicles found"))); - return; - } - -#if 0 - PGR_DBG("total orders %ld", total_pd_orders); - for (size_t i = 0; i < total_pd_orders; i++) { - PGR_DBG("%ld %f pick %ld - %ld %ld %ld" - "deliver %ld - %ld %ld %ld ", - pd_orders_arr[i].id, - pd_orders_arr[i].demand, - - pd_orders_arr[i].pick_node_id, - - pd_orders_arr[i].pick_open_t, - pd_orders_arr[i].pick_close_t, - pd_orders_arr[i].pick_service_t, - - pd_orders_arr[i].deliver_node_id, - - pd_orders_arr[i].deliver_open_t, - pd_orders_arr[i].deliver_close_t, - pd_orders_arr[i].deliver_service_t); - } - - - - PGR_DBG("total vehicles %ld", total_vehicles); - for (size_t i = 0; i < total_vehicles; i++) { - PGR_DBG("%ld %f %f , start %ld %ld %ld %ld " - "end %ld %ld %ld %ld number %ld ", - vehicles_arr[i].id, - vehicles_arr[i].capacity, - vehicles_arr[i].speed, - - vehicles_arr[i].start_node_id, - vehicles_arr[i].start_open_t, - vehicles_arr[i].start_close_t, - vehicles_arr[i].start_service_t, - - vehicles_arr[i].end_node_id, - vehicles_arr[i].end_open_t, - vehicles_arr[i].end_close_t, - vehicles_arr[i].end_service_t, - - vehicles_arr[i].cant_v); - } -#endif - - Matrix_cell_t *matrix_cells_arr = NULL; - size_t total_cells = 0; - get_matrixRows_plain(matrix_sql, &matrix_cells_arr, &total_cells); - - PGR_DBG("total matrix rows %ld", total_cells); -#if 0 - for (size_t i = 0; i < total_cells; i++) { - PGR_DBG("%ld %ld %f", - matrix_cells_arr[i].from_vid, - matrix_cells_arr[i].to_vid, - matrix_cells_arr[i].cost); - } -#endif - - if (total_cells == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No matrix found"))); - vrp_SPI_finish(); - return; - } - - - PGR_DBG("Total %ld orders in query:", total_pd_orders); - PGR_DBG("Total %ld vehicles in query:", total_vehicles); - PGR_DBG("Total %ld matrix cells in query:", total_cells); - - - PGR_DBG("Starting processing"); clock_t start_t = clock(); vrp_do_pgr_pickDeliver( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, - matrix_cells_arr, total_cells, + pd_orders_sql, + vehicles_sql, + matrix_sql, factor, max_cycles, @@ -250,17 +79,6 @@ process( } vrp_global_report(&log_msg, ¬ice_msg, &err_msg); - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - vrp_SPI_finish(); } From 307f36874ac1369927a84ae0623340e0364d2967 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:21:06 -0600 Subject: [PATCH 09/20] (pgr_pickDeliverEuclidean) moving code from C file to C++ file --- .../drivers/pgr_pickDeliverEuclidean_driver.h | 7 +- .../pgr_pickDeliverEuclidean_driver.cpp | 99 ++++++---------- src/pgr_pickDeliver/pickDeliverEuclidean.c | 111 +----------------- 3 files changed, 42 insertions(+), 175 deletions(-) diff --git a/include/drivers/pgr_pickDeliverEuclidean_driver.h b/include/drivers/pgr_pickDeliverEuclidean_driver.h index 934f38acf..48edfdc96 100644 --- a/include/drivers/pgr_pickDeliverEuclidean_driver.h +++ b/include/drivers/pgr_pickDeliverEuclidean_driver.h @@ -33,15 +33,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus #include #include -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; using Solution_rt = struct Solution_rt; #else #include #include #include -typedef struct Orders_t Orders_t; -typedef struct Vehicle_t Vehicle_t; typedef struct Solution_rt Solution_rt; #endif @@ -51,8 +47,7 @@ extern "C" { #endif void vrp_do_pgr_pickDeliverEuclidean( - Orders_t *pd_orders_arr, size_t total_pd_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, + char*, char*, double, int, int, Solution_rt**, size_t*, diff --git a/src/pgr_pickDeliver/pgr_pickDeliverEuclidean_driver.cpp b/src/pgr_pickDeliver/pgr_pickDeliverEuclidean_driver.cpp index 513178d51..e2b1560c4 100644 --- a/src/pgr_pickDeliver/pgr_pickDeliverEuclidean_driver.cpp +++ b/src/pgr_pickDeliver/pgr_pickDeliverEuclidean_driver.cpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" - +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/orders_t.hpp" #include "cpp_common/vehicle_t.hpp" #include "initialsol/simple.hpp" @@ -70,58 +70,12 @@ get_initial_solution(vrprouting::problem::PickDeliver &problem_ptr, int m_initia return m_solutions; } -bool -are_shipments_ok( - const std::vector &orders, - std::string *err_string, - std::string *hint_string) { - /** - * - demand > 0 (the type is unsigned so no need to check for negative values, only for it to be non 0 - * - pick_service_t >=0 - * - drop_service_t >=0 - * - p_open <= p_close - * - d_open <= d_close - */ - for (const auto &o : orders) { - if (o.demand == 0) { - *err_string = "Unexpected zero value found on column 'demand' of shipments"; - *hint_string = "Check shipment id #:" + std::to_string(o.id); - return false; - } - - if (o.pick_service_t < 0) { - *err_string = "Unexpected negative value found on column 'p_service_t' of shipments"; - *hint_string = "Check shipment id #:" + std::to_string(o.id); - return false; - } - - if (o.deliver_service_t < 0) { - *err_string = "Unexpected negative value found on column 'd_service_t' of shipments"; - *hint_string = "Check shipment id #:" + std::to_string(o.id); - return false; - } - - if (o.pick_open_t > o.pick_close_t) { - *err_string = "Unexpected pickup time windows found on shipments"; - *hint_string = "Check shipment id #:" + std::to_string(o.id); - return false; - } - - if (o.deliver_open_t > o.deliver_close_t) { - *err_string = "Unexpected delivery time windows found on shipments"; - *hint_string = "Check shipment id #:" + std::to_string(o.id); - return false; - } - } - return true; -} - } // namespace void vrp_do_pgr_pickDeliverEuclidean( - Orders_t *orders_arr, size_t total_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, + char *orders_sql, + char *vehicles_sql, double factor, int max_cycles, @@ -146,6 +100,8 @@ vrp_do_pgr_pickDeliverEuclidean( using Matrix = vrprouting::problem::Matrix; using Optimize = vrprouting::optimizers::simple::Optimize; using Initials_code = vrprouting::initialsol::simple::Initials_code; + using vrprouting::pgget::pickdeliver::get_orders; + using vrprouting::pgget::pickdeliver::get_vehicles; /* * verify preconditions @@ -156,24 +112,47 @@ vrp_do_pgr_pickDeliverEuclidean( pgassert(*return_count == 0); pgassert(!(*return_tuples)); - std::string err_string; - std::string hint_string; + if (initial_solution_id < 0 || initial_solution_id > 7) { + *err_msg = to_pg_msg("Illegal value in parameter: initial_sol"); + *log_msg = to_pg_msg("Expected value: 0 <= initial_sol <= 7"); + return; + } - /* - * Data input starts - */ + if (max_cycles < 0) { + *err_msg = to_pg_msg("Illegal value in parameter: max_cycles"); + *log_msg = to_pg_msg("Expected value: max_cycles >= 0"); + return; + } + + if (factor <= 0) { + *err_msg = to_pg_msg("Illegal value in parameter: factor"); + *log_msg = to_pg_msg("Expected value: factor > 0"); + return; + } /* - * transform to C++ containers + * Data input starts */ - std::vector orders(orders_arr, orders_arr + total_orders); - std::vector vehicles(vehicles_arr, vehicles_arr + total_vehicles); + bool use_timestamps = false; + bool is_euclidean = true; + bool with_stops = false; + + hint = orders_sql; + auto orders = get_orders(std::string(orders_sql), is_euclidean, use_timestamps); + if (orders.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'orders' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } - if (!are_shipments_ok(orders, &err_string, &hint_string)) { - *err_msg = to_pg_msg(err_string); - *log_msg = to_pg_msg(hint_string); + hint = vehicles_sql; + auto vehicles = get_vehicles(std::string(vehicles_sql), is_euclidean, use_timestamps, with_stops); + if (vehicles.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'vehicles' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; return; } + hint = nullptr; /* Data input ends */ diff --git a/src/pgr_pickDeliver/pickDeliverEuclidean.c b/src/pgr_pickDeliver/pickDeliverEuclidean.c index 1445a04d5..3f4ed4970 100644 --- a/src/pgr_pickDeliver/pickDeliverEuclidean.c +++ b/src/pgr_pickDeliver/pickDeliverEuclidean.c @@ -26,14 +26,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include "c_common/postgres_connection.h" -#include "utils/array.h" -#include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/orders_input.h" -#include "c_common/vehicles_input.h" -#include "cpp_common/orders_t.hpp" #include "c_types/solution_rt.h" #include "drivers/pgr_pickDeliverEuclidean_driver.h" @@ -58,109 +53,10 @@ process( vrp_SPI_connect(); - if (factor <= 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: factor"), - errhint("Value found: %f <= 0", factor))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - if (max_cycles < 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: max_cycles"), - errhint("Negative value found: max_cycles: %d ", max_cycles))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - if (initial_solution_id <= 0 || initial_solution_id > 6) { - elog(ERROR, "Illegal value in parameter: initial_sol"); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - PGR_DBG("Load orders"); - struct Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - get_shipments_euclidean(pd_orders_sql, - &pd_orders_arr, &total_pd_orders); - - PGR_DBG("Load vehicles"); - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - get_vehicles_euclidean(vehicles_sql, - &vehicles_arr, &total_vehicles, - false); - PGR_DBG("total vehicles %ld", total_vehicles); - -#if 0 - for (size_t i = 0; i < total_pd_orders; i++) { - PGR_DBG("%ld %f pick %f %f %ld - " - "%ld %ld %ld deliver %f %f %ld - %ld %ld %ld ", - pd_orders_arr[i].id, - pd_orders_arr[i].demand, - - pd_orders_arr[i].pick_x, - pd_orders_arr[i].pick_y, - pd_orders_arr[i].pick_node_id, - - pd_orders_arr[i].pick_open_t, - pd_orders_arr[i].pick_close_t, - pd_orders_arr[i].pick_service_t, - - pd_orders_arr[i].deliver_x, - pd_orders_arr[i].deliver_y, - pd_orders_arr[i].deliver_node_id, - - pd_orders_arr[i].deliver_open_t, - pd_orders_arr[i].deliver_close_t, - pd_orders_arr[i].deliver_service_t); - } - - - - for (size_t i = 0; i < total_vehicles; i++) { - PGR_DBG("%ld %f %f , start %f %f %ld %ld %ld " - "end %f %f %ld %ld %ld number %ld ", - vehicles_arr[i].id, - vehicles_arr[i].capacity, - vehicles_arr[i].speed, - - vehicles_arr[i].start_x, - vehicles_arr[i].start_y, - vehicles_arr[i].start_open_t, - vehicles_arr[i].start_close_t, - vehicles_arr[i].start_service_t, - - vehicles_arr[i].end_x, - vehicles_arr[i].end_y, - vehicles_arr[i].end_open_t, - vehicles_arr[i].end_close_t, - vehicles_arr[i].end_service_t, - - vehicles_arr[i].cant_v); - } -#endif - - if (total_pd_orders == 0 || total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - vrp_SPI_finish(); - return; - } - PGR_DBG("Total %ld orders in query:", total_pd_orders); - - PGR_DBG("Starting processing"); clock_t start_t = clock(); vrp_do_pgr_pickDeliverEuclidean( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, + pd_orders_sql, + vehicles_sql, factor, max_cycles, @@ -182,9 +78,6 @@ process( vrp_global_report(&log_msg, ¬ice_msg, &err_msg); - if (pd_orders_arr) pfree(pd_orders_arr); - if (vehicles_arr) pfree(vehicles_arr); - vrp_SPI_finish(); } From 6d01e1d11f0e416da89a628ab2e4ddb9ccc0e28a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:21:56 -0600 Subject: [PATCH 10/20] (pickDeliver) moving code from C file to C++ file --- include/drivers/pickDeliver_driver.h | 16 +-- src/pickDeliver/pickDeliver.c | 181 ++----------------------- src/pickDeliver/pickDeliver_driver.cpp | 87 +++++++++--- 3 files changed, 77 insertions(+), 207 deletions(-) diff --git a/include/drivers/pickDeliver_driver.h b/include/drivers/pickDeliver_driver.h index 3ccb54a48..10371247e 100644 --- a/include/drivers/pickDeliver_driver.h +++ b/include/drivers/pickDeliver_driver.h @@ -35,19 +35,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus #include #include -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; -using Matrix_cell_t = struct Matrix_cell_t; -using Time_multipliers_t = struct Time_multipliers_t; using Solution_rt = struct Solution_rt; #else #include #include #include -typedef struct Orders_t Orders_t; -typedef struct Vehicle_t Vehicle_t; -typedef struct Matrix_cell_t Matrix_cell_t; -typedef struct Time_multipliers_t Time_multipliers_t; typedef struct Solution_rt Solution_rt; #endif @@ -55,12 +47,10 @@ typedef struct Solution_rt Solution_rt; extern "C" { #endif + /** @brief Driver for processing a pickupDeliver problem */ void vrp_do_pickDeliver( - Orders_t customers_arr[], size_t total_customers, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, - Time_multipliers_t *multipliers_arr, size_t total_multipliers, - bool, double, int, bool, int64_t, + char*, char*, char*, char*, + double, int, int64_t, bool, bool, bool, bool, bool, Solution_rt**, size_t*, char**, char**, char**); diff --git a/src/pickDeliver/pickDeliver.c b/src/pickDeliver/pickDeliver.c index 719205d66..b7a8d67aa 100644 --- a/src/pickDeliver/pickDeliver.c +++ b/src/pickDeliver/pickDeliver.c @@ -26,19 +26,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include #include "c_common/postgres_connection.h" #include // NOLINT [build/include_order] -#include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/orders_input.h" -#include "cpp_common/get_check_data.hpp" -#include "c_common/vehicles_input.h" -#include "c_common/matrixRows_input.h" -#include "c_common/time_multipliers_input.h" #include "c_types/solution_rt.h" -#include "cpp_common/orders_t.hpp" #include "drivers/pickDeliver_driver.h" PGDLLEXPORT Datum _vrp_pickdeliver(PG_FUNCTION_ARGS); @@ -71,163 +63,22 @@ process( vrp_SPI_connect(); - /* - * Adjusting timestamp data to timezone UTC - */ - if (use_timestamps) { - execution_date = timestamp_without_timezone(execution_date); - } - - PGR_DBG("execution_date: %ld ", execution_date); - - //! [Factor must be postive] - if (factor <= 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: factor"), - errhint("Value found: %f <= 0", factor))); - } - //! [Factor must be postive] - - if (max_cycles < 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: max_cycles"), - errhint("Value found: %d <= 0", max_cycles))); - } - - Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - if (use_timestamps) { - get_shipments(pd_orders_sql, &pd_orders_arr, &total_pd_orders); - } else { - get_shipments_raw(pd_orders_sql, &pd_orders_arr, &total_pd_orders); - } - - if (total_pd_orders == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - if (use_timestamps) { - get_vehicles(vehicles_sql, &vehicles_arr, &total_vehicles, true); - } else { - get_vehicles_raw(vehicles_sql, &vehicles_arr, &total_vehicles, true); - } - - if (total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No vehicles found"))); - - vrp_SPI_finish(); - return; - } - - Time_multipliers_t *multipliers_arr = NULL; - size_t total_multipliers_arr = 0; - if (use_timestamps) { - PGR_DBG("get_timeMultipliers"); - get_timeMultipliers(multipliers_sql, &multipliers_arr, &total_multipliers_arr); - } else { - PGR_DBG("get_timeMultipliers_raw"); - get_timeMultipliers_raw(multipliers_sql, &multipliers_arr, &total_multipliers_arr); - } - - if (total_multipliers_arr == 0) { - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No matrix found"))); - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Matrix_cell_t *matrix_cells_arr = NULL; - size_t total_cells = 0; - if (use_timestamps) { - get_matrixRows(matrix_sql, &matrix_cells_arr, &total_cells); - } else { - get_matrixRows_plain(matrix_sql, &matrix_cells_arr, &total_cells); - } - - if (total_cells == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No matrix found"))); - vrp_SPI_finish(); - return; - } - - PGR_DBG("Total %ld orders in query:", total_pd_orders); - PGR_DBG("Total %ld vehicles in query:", total_vehicles); - PGR_DBG("Total %ld matrix cells in query:", total_cells); - PGR_DBG("Total %ld time dependant multipliers:", total_multipliers_arr); - clock_t start_t = clock(); vrp_do_pickDeliver( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, - matrix_cells_arr, total_cells, - multipliers_arr, total_multipliers_arr, + pd_orders_sql, + vehicles_sql, + matrix_sql, + multipliers_sql, - optimize, factor, max_cycles, + execution_date, + optimize, stop_on_all_served, - execution_date, + use_timestamps, + false, // is_euclidean + false, // with_stops result_tuples, result_count, @@ -245,20 +96,6 @@ process( vrp_global_report(&log_msg, ¬ice_msg, &err_msg); - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - vrp_SPI_finish(); } diff --git a/src/pickDeliver/pickDeliver_driver.cpp b/src/pickDeliver/pickDeliver_driver.cpp index e13ccc237..27907b8db 100644 --- a/src/pickDeliver/pickDeliver_driver.cpp +++ b/src/pickDeliver/pickDeliver_driver.cpp @@ -39,9 +39,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" - -#include "cpp_common/matrix_cell_t.hpp" -#include "cpp_common/time_multipliers_t.hpp" +#include "cpp_common/pgdata_getters.hpp" +#include "cpp_common/check_get_data.hpp" #include "cpp_common/orders_t.hpp" #include "cpp_common/vehicle_t.hpp" #include "initialsol/tabu.hpp" @@ -51,18 +50,22 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void vrp_do_pickDeliver( - Orders_t *orders_arr, size_t total_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, - Time_multipliers_t *multipliers_arr, size_t total_multipliers, - + char *orders_sql, + char *vehicles_sql, + char *matrix_sql, + char *multipliers_sql, - bool optimize, double factor, int max_cycles, - bool stop_on_all_served, int64_t execution_date, + bool optimize, + bool stop_on_all_served, + + bool use_timestamps, + bool is_euclidean, + bool with_stops, + Solution_rt **return_tuples, size_t *return_count, @@ -80,6 +83,10 @@ vrp_do_pickDeliver( std::ostringstream err; try { using Matrix = vrprouting::problem::Matrix; + using vrprouting::pgget::pickdeliver::get_matrix; + using vrprouting::pgget::pickdeliver::get_orders; + using vrprouting::pgget::pickdeliver::get_vehicles; + using vrprouting::pgget::pickdeliver::get_timeMultipliers; /* * verify preconditions @@ -87,21 +94,58 @@ vrp_do_pickDeliver( pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); - pgassert(total_orders); - pgassert(total_vehicles); - pgassert(total_cells); pgassert(*return_count == 0); pgassert(!(*return_tuples)); - /* Data input starts */ - /* - * transform to C++ containers + * Adjusting timestamp data to timezone UTC */ - std::vector vehicles(vehicles_arr, vehicles_arr + total_vehicles); - std::vector orders(orders_arr, orders_arr + total_orders); - std::vector costs(matrix_cells_arr, matrix_cells_arr + total_cells); - std::vector multipliers(multipliers_arr, multipliers_arr + total_multipliers); + if (use_timestamps) { + execution_date = vrprouting::get_timestamp_without_timezone(execution_date); + } + + if (max_cycles < 0) { + *err_msg = to_pg_msg("Illegal value in parameter: max_cycles"); + *log_msg = to_pg_msg("Expected value: max_cycles >= 0"); + return; + } + + if (factor <= 0) { + *err_msg = to_pg_msg("Illegal value in parameter: factor"); + *log_msg = to_pg_msg("Expected value: factor > 0"); + return; + } + + /* Data input starts */ + + hint = orders_sql; + auto orders = get_orders(std::string(orders_sql), is_euclidean, use_timestamps); + if (orders.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'orders' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = vehicles_sql; + auto vehicles = get_vehicles(std::string(vehicles_sql), is_euclidean, use_timestamps, with_stops); + if (vehicles.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'vehicles' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = matrix_sql; + auto costs = get_matrix(std::string(matrix_sql), use_timestamps); + + if (costs.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'matrix' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = multipliers_sql; + auto multipliers = get_timeMultipliers(std::string(multipliers_sql), use_timestamps); + hint = nullptr; /* Data input ends */ @@ -123,8 +167,7 @@ vrp_do_pickDeliver( for (const auto &v : vehicles) { node_ids += v.start_node_id; node_ids += v.end_node_id; - for (size_t j = 0; j < v.stops_size; ++j) { - auto s = v.stops[j]; + for (const auto &s : v.stops) { if (!order_ids.has(s)) { if (!missing) err << "Order in 'stops' information missing"; missing = true; From 083b75bf02830493b59bcc7ce47791db11b9204a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:22:28 -0600 Subject: [PATCH 11/20] (optimize) moving code from C file to C++ file --- include/drivers/optimize_driver.h | 15 +-- src/optimize/optimize.c | 176 ++---------------------------- src/optimize/optimize_driver.cpp | 98 ++++++++++++----- 3 files changed, 81 insertions(+), 208 deletions(-) diff --git a/include/drivers/optimize_driver.h b/include/drivers/optimize_driver.h index a2431f15b..2cc88612a 100644 --- a/include/drivers/optimize_driver.h +++ b/include/drivers/optimize_driver.h @@ -35,19 +35,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus #include #include -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; -using Matrix_cell_t = struct Matrix_cell_t; -using Time_multipliers_t = struct Time_multipliers_t; using Short_vehicle_rt = struct Short_vehicle_rt; #else #include #include #include -typedef struct Orders_t Orders_t; -typedef struct Vehicle_t Vehicle_t; -typedef struct Matrix_cell_t Matrix_cell_t; -typedef struct Time_multipliers_t Time_multipliers_t; typedef struct Short_vehicle_rt Short_vehicle_rt; #endif @@ -56,11 +48,8 @@ extern "C" { #endif void vrp_do_optimize( - Orders_t customers_arr[], size_t, - Vehicle_t *vehicles_arr, size_t, - Matrix_cell_t *, size_t, - Time_multipliers_t *, size_t, - double, int, int64_t, bool, bool, bool, + char*, char*, char*, char*, + double, int, int64_t, bool, int, bool, bool, bool, Short_vehicle_rt**, size_t*, char**, char**, char**); diff --git a/src/optimize/optimize.c b/src/optimize/optimize.c index 1b3821fa7..c9d5fc22a 100644 --- a/src/optimize/optimize.c +++ b/src/optimize/optimize.c @@ -27,15 +27,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include "c_common/postgres_connection.h" -#include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" -#include "c_common/orders_input.h" -#include "c_common/vehicles_input.h" -#include "c_common/matrixRows_input.h" -#include "c_common/time_multipliers_input.h" #include "c_types/short_vehicle_rt.h" -#include "cpp_common/orders_t.hpp" #include "drivers/optimize_driver.h" PGDLLEXPORT Datum @@ -67,161 +61,23 @@ process( vrp_SPI_connect(); - //! [Factor must be postive] - if (factor <= 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: factor"), - errhint("Value found: %f <= 0", factor))); - } - - //! [max_cycles must be postive] - if (max_cycles < 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: max_cycles"), - errhint("Value found: %d <= 0", max_cycles))); - } - - //! [subdivision_kind can be: 0, 1, or 2] - if (subdivision_kind < 0 || subdivision_kind > 2) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: subdivision_kind"), - errhint("Value found: %d", max_cycles))); - } - - Orders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - if (use_timestamps) { - get_shipments(pd_orders_sql, &pd_orders_arr, &total_pd_orders); - } else { - get_shipments_raw(pd_orders_sql, &pd_orders_arr, &total_pd_orders); - } - - if (total_pd_orders == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - if (use_timestamps) { - get_vehicles(vehicles_sql, &vehicles_arr, &total_vehicles, true); - } else { - get_vehicles_raw(vehicles_sql, &vehicles_arr, &total_vehicles, true); - } - - if (total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No vehicles found"))); - - vrp_SPI_finish(); - return; - } - - Time_multipliers_t *multipliers_arr = NULL; - size_t total_multipliers_arr = 0; - if (use_timestamps) { - get_timeMultipliers(multipliers_sql, &multipliers_arr, &total_multipliers_arr); - } else { - get_timeMultipliers_raw(multipliers_sql, &multipliers_arr, &total_multipliers_arr); - } - - if (total_multipliers_arr == 0) { - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No matrix found"))); - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - - vrp_SPI_finish(); - return; - } - - Matrix_cell_t *matrix_cells_arr = NULL; - size_t total_cells = 0; - if (use_timestamps) { - get_matrixRows(matrix_sql, &matrix_cells_arr, &total_cells); - } else { - get_matrixRows_plain(matrix_sql, &matrix_cells_arr, &total_cells); - } - - if (total_cells == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No matrix found"))); - vrp_SPI_finish(); - return; - } - - PGR_DBG("Total %ld orders in query:", total_pd_orders); - PGR_DBG("Total %ld vehicles in query:", total_vehicles); - PGR_DBG("Total %ld matrix cells in query:", total_cells); - PGR_DBG("Total %ld time dependant multipliers:", total_multipliers_arr); - clock_t start_t = clock(); vrp_do_optimize( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, - matrix_cells_arr, total_cells, - multipliers_arr, total_multipliers_arr, + pd_orders_sql, + vehicles_sql, + matrix_sql, + multipliers_sql, factor, max_cycles, execution_date, check_triangle_inequality, - subdivision_kind != 0, - subdivision_kind == 1, + subdivision_kind, + + use_timestamps, + false, // is_euclidean + false, // with_stops result_tuples, result_count, @@ -238,20 +94,6 @@ process( } vrp_global_report(&log_msg, ¬ice_msg, &err_msg); - /* freeing memory before return */ - if (pd_orders_arr) { - pfree(pd_orders_arr); pd_orders_arr = NULL; - } - if (vehicles_arr) { - pfree(vehicles_arr); vehicles_arr = NULL; - } - if (multipliers_arr) { - pfree(multipliers_arr); multipliers_arr = NULL; - } - if (matrix_cells_arr) { - pfree(matrix_cells_arr); matrix_cells_arr = NULL; - } - vrp_SPI_finish(); } diff --git a/src/optimize/optimize_driver.cpp b/src/optimize/optimize_driver.cpp index 0708d6cc9..e51f24ea8 100644 --- a/src/optimize/optimize_driver.cpp +++ b/src/optimize/optimize_driver.cpp @@ -39,11 +39,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/interruption.hpp" #include "cpp_common/messages.hpp" - -#include "cpp_common/matrix_cell_t.hpp" -#include "cpp_common/time_multipliers_t.hpp" #include "cpp_common/orders_t.hpp" #include "cpp_common/short_vehicle.hpp" #include "cpp_common/vehicle_t.hpp" @@ -55,8 +53,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace { -using Vehicle_t = struct Vehicle_t; -using Orders_t = struct Orders_t; +using Vehicle_t = vrprouting::Vehicle_t; +using Orders_t = vrprouting::Orders_t; using Short_vehicle = vrprouting::Short_vehicle; /** @brief Executes an optimization with the input data @@ -160,11 +158,7 @@ std::vector get_initial_stops(const std::vector &vehicles) { std::vector the_stops; for (const auto &v : vehicles) { - std::vector stops; - for (size_t j = 0; j < v.stops_size; ++j) { - stops.push_back(v.stops[j]); - } - the_stops.push_back({v.id, stops}); + the_stops.push_back({v.id, v.stops}); } return the_stops; } @@ -290,19 +284,21 @@ subdivide_processing( void vrp_do_optimize( - Orders_t *orders_arr, size_t total_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, - Time_multipliers_t *multipliers_arr, size_t total_multipliers, - + char *orders_sql, + char *vehicles_sql, + char *matrix_sql, + char *multipliers_sql, double factor, int max_cycles, int64_t execution_date, bool check_triangle_inequality, - bool subdivide, - bool subdivide_by_vehicle, + int subdivision_kind, + + bool use_timestamps, + bool is_euclidean, + bool with_stops, Short_vehicle_rt **return_tuples, size_t *return_count, @@ -320,7 +316,13 @@ vrp_do_optimize( std::ostringstream notice; std::ostringstream err; try { + using Vehicle_t = vrprouting::Vehicle_t; + using Orders_t = vrprouting::Orders_t; using Matrix = vrprouting::problem::Matrix; + using vrprouting::pgget::pickdeliver::get_matrix; + using vrprouting::pgget::pickdeliver::get_orders; + using vrprouting::pgget::pickdeliver::get_vehicles; + using vrprouting::pgget::pickdeliver::get_timeMultipliers; /* * verify preconditions @@ -328,26 +330,66 @@ vrp_do_optimize( pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); - pgassert(total_orders); - pgassert(total_vehicles); - pgassert(total_cells); pgassert(*return_count == 0); pgassert(!(*return_tuples)); - /* Data input starts */ + if (subdivision_kind < 0 || subdivision_kind > 2) { + *notice_msg = to_pg_msg("Illegal value in parameter: subdivision_kind"); + *log_msg = to_pg_msg("Expected value: 0 <= subdivision_kind < 2"); + return; + } + + if (max_cycles < 0) { + *err_msg = to_pg_msg("Illegal value in parameter: max_cycles"); + *log_msg = to_pg_msg("Expected value: max_cycles >= 0"); + return; + } + + if (factor <= 0) { + *err_msg = to_pg_msg("Illegal value in parameter: factor"); + *log_msg = to_pg_msg("Expected value: factor > 0"); + return; + } /* - * transform to C++ containers + * Data input starts */ - std::vector vehicles(vehicles_arr, vehicles_arr + total_vehicles); - std::vector orders(orders_arr, orders_arr + total_orders); - std::vector costs(matrix_cells_arr, matrix_cells_arr + total_cells); - std::vector multipliers(multipliers_arr, multipliers_arr + total_multipliers); + hint = orders_sql; + auto orders = get_orders(std::string(orders_sql), is_euclidean, use_timestamps); + if (orders.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'orders' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = vehicles_sql; + auto vehicles = get_vehicles(std::string(vehicles_sql), is_euclidean, use_timestamps, with_stops); + if (vehicles.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'vehicles' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = matrix_sql; + auto costs = get_matrix(std::string(matrix_sql), use_timestamps); + + if (costs.size() == 0) { + *notice_msg = to_pg_msg("Insufficient data found on 'matrix' inner query"); + *log_msg = hint? to_pg_msg(hint) : nullptr; + return; + } + + hint = multipliers_sql; + auto multipliers = get_timeMultipliers(std::string(multipliers_sql), use_timestamps); + hint = nullptr; /* Data input ends */ /* Processing starts */ + bool subdivide = (subdivision_kind != 0); + bool subdivide_by_vehicle = (subdivision_kind == 1); + Identifiers node_ids; Identifiers order_ids; Identifiers orders_found; @@ -378,8 +420,8 @@ vrp_do_optimize( for (const auto &v : vehicles) { node_ids += v.start_node_id; node_ids += v.end_node_id; - for (size_t j = 0; j < v.stops_size; ++j) { - order_ids += v.stops[j]; + for (const auto &s : v.stops) { + order_ids += s; } } From b88fd49f0efebbbf3c96231269a10a74815ae570 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:32:04 -0600 Subject: [PATCH 12/20] (vroom) moving code from C file to C++ file --- include/drivers/vroom_driver.h | 23 +---- src/vroom/vroom.c | 149 +++----------------------------- src/vroom/vroom_driver.cpp | 153 ++++++++++++++++++++++++++------- 3 files changed, 136 insertions(+), 189 deletions(-) diff --git a/include/drivers/vroom_driver.h b/include/drivers/vroom_driver.h index 70f151f0b..d5ebfe40d 100644 --- a/include/drivers/vroom_driver.h +++ b/include/drivers/vroom_driver.h @@ -33,23 +33,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #ifdef __cplusplus #include #include -using Vroom_job_t = struct Vroom_job_t; -using Vroom_time_window_t = struct Vroom_time_window_t; -using Vroom_shipment_t = struct Vroom_shipment_t; -using Vroom_vehicle_t = struct Vroom_vehicle_t; -using Vroom_break_t = struct Vroom_break_t; -using Vroom_matrix_t = struct Vroom_matrix_t; using Vroom_rt = struct Vroom_rt; #else #include #include #include -typedef struct Vroom_job_t Vroom_job_t; -typedef struct Vroom_time_window_t Vroom_time_window_t; -typedef struct Vroom_shipment_t Vroom_shipment_t; -typedef struct Vroom_vehicle_t Vroom_vehicle_t; -typedef struct Vroom_break_t Vroom_break_t; -typedef struct Vroom_matrix_t Vroom_matrix_t; typedef struct Vroom_rt Vroom_rt; #endif @@ -58,15 +46,8 @@ extern "C" { #endif void vrp_do_vroom( - Vroom_job_t *jobs, size_t total_jobs, - Vroom_time_window_t *jobs_tws, size_t total_jobs_tws, - Vroom_shipment_t *shipments, size_t total_shipments, - Vroom_time_window_t *shipments_tws, size_t total_shipments_tws, - Vroom_vehicle_t *vehicles, size_t total_vehicles, - Vroom_break_t *breaks, size_t total_breaks, - Vroom_time_window_t *breaks_tws, size_t total_breaks_tws, - Vroom_matrix_t *matrix_cells_arr, size_t total_cells, - int32_t, int32_t, int32_t, + char*, char*, char*, char*, char*, char*, char*, char*, + int32_t, int32_t, int16_t, bool, Vroom_rt**, size_t*, char**, char**, char**); diff --git a/src/vroom/vroom.c b/src/vroom/vroom.c index 302d8ee7d..a556dc260 100644 --- a/src/vroom/vroom.c +++ b/src/vroom/vroom.c @@ -48,17 +48,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" #include "c_types/vroom_rt.h" -#include "cpp_common/vroom_job_t.hpp" -#include "cpp_common/vroom_shipment_t.hpp" -#include "cpp_common/vroom_vehicle_t.hpp" -#include "cpp_common/vroom_matrix_t.hpp" - -#include "c_common/vroom/jobs_input.h" -#include "c_common/vroom/breaks_input.h" -#include "c_common/vroom/time_windows_input.h" -#include "c_common/vroom/shipments_input.h" -#include "c_common/vroom/vehicles_input.h" -#include "c_common/vroom/matrix_input.h" #include "drivers/vroom_driver.h" @@ -80,118 +69,31 @@ process( int32_t exploration_level, int32_t timeout, int16_t fn, - bool is_plain, + bool use_timestamps, Vroom_rt **result_tuples, size_t *result_count) { - clock_t start_loading = clock(); - vrp_SPI_connect(); - - (*result_tuples) = NULL; - (*result_count) = 0; - - Vroom_job_t *jobs = NULL; - size_t total_jobs = 0; - if (jobs_sql) { - get_vroom_jobs(jobs_sql, &jobs, &total_jobs, is_plain); - } - - Vroom_shipment_t *shipments = NULL; - size_t total_shipments = 0; - if (shipments_sql) { - get_vroom_shipments(shipments_sql, &shipments, &total_shipments, is_plain); - } - - if (total_jobs == 0 && total_shipments == 0) { - if (fn == 0) { - ereport(WARNING, (errmsg("Insufficient data found on Jobs SQL and Shipments SQL query."), - errhint("%s, %s", jobs_sql, shipments_sql))); - } else if (fn == 1) { - ereport(WARNING, (errmsg("Insufficient data found on Jobs SQL query."), - errhint("%s", jobs_sql))); - } else if (fn == 2) { - ereport(WARNING, (errmsg("Insufficient data found on Shipments SQL query."), - errhint("%s", shipments_sql))); - } - (*result_count) = 0; - (*result_tuples) = NULL; - vrp_SPI_finish(); - return; - } - - Vroom_time_window_t *jobs_tws = NULL; - size_t total_jobs_tws = 0; - if (jobs_tws_sql) { - get_vroom_time_windows(jobs_tws_sql, &jobs_tws, &total_jobs_tws, - is_plain); - } - - Vroom_time_window_t *shipments_tws = NULL; - size_t total_shipments_tws = 0; - if (shipments_tws_sql) { - get_vroom_shipments_time_windows(shipments_tws_sql, &shipments_tws, - &total_shipments_tws, is_plain); - } - - Vroom_vehicle_t *vehicles = NULL; - size_t total_vehicles = 0; - get_vroom_vehicles(vehicles_sql, &vehicles, &total_vehicles, is_plain); - - if (total_vehicles == 0) { - ereport(WARNING, (errmsg("Insufficient data found on Vehicles SQL query."), - errhint("%s", vehicles_sql))); - (*result_count) = 0; - (*result_tuples) = NULL; - vrp_SPI_finish(); - return; - } - - Vroom_break_t *breaks = NULL; - size_t total_breaks = 0; - if (breaks_sql) { - get_vroom_breaks(breaks_sql, &breaks, &total_breaks, is_plain); - } - - Vroom_time_window_t *breaks_tws = NULL; - size_t total_breaks_tws = 0; - if (breaks_tws_sql) { - get_vroom_time_windows(breaks_tws_sql, &breaks_tws, &total_breaks_tws, - is_plain); - } - - Vroom_matrix_t *matrix_rows = NULL; - size_t total_matrix_rows = 0; - get_vroom_matrix(matrix_sql, &matrix_rows, &total_matrix_rows, is_plain); - - if (total_matrix_rows == 0) { - ereport(WARNING, (errmsg("Insufficient data found on Matrix SQL query."), - errhint("%s", matrix_sql))); - (*result_count) = 0; - (*result_tuples) = NULL; - vrp_SPI_finish(); - return; - } - - clock_t start_t = clock(); char *log_msg = NULL; char *notice_msg = NULL; char *err_msg = NULL; - int32_t loading_time = (int)((clock() - start_loading) / CLOCKS_PER_SEC) * 1000; + vrp_SPI_connect(); + clock_t start_t = clock(); vrp_do_vroom( - jobs, total_jobs, - jobs_tws, total_jobs_tws, - shipments, total_shipments, - shipments_tws, total_shipments_tws, - vehicles, total_vehicles, - breaks, total_breaks, - breaks_tws, total_breaks_tws, - matrix_rows, total_matrix_rows, + jobs_sql, + jobs_tws_sql, + shipments_sql, + shipments_tws_sql, + vehicles_sql, + breaks_sql, + breaks_tws_sql, + matrix_sql, exploration_level, timeout, - loading_time, + fn, + use_timestamps, result_tuples, result_count, @@ -210,11 +112,6 @@ process( vrp_global_report(&log_msg, ¬ice_msg, &err_msg); - if (jobs) pfree(jobs); - if (shipments) pfree(shipments); - if (vehicles) pfree(vehicles); - if (matrix_rows) pfree(matrix_rows); - vrp_SPI_finish(); } @@ -245,24 +142,6 @@ PGDLLEXPORT Datum _vrp_vroom(PG_FUNCTION_ARGS) { int16_t fn = PG_GETARG_INT16(10); bool is_plain = PG_GETARG_BOOL(11); - // Verify that both jobs_sql and shipments_sql are not NULL - if (args[0] == NULL && args[2] == NULL) { - if (fn == 0) { - elog(ERROR, "Both Jobs SQL and Shipments NULL must not be NULL"); - } else if (fn == 1) { - elog(ERROR, "Jobs SQL must not be NULL"); - } else if (fn == 2) { - elog(ERROR, "Shipments SQL must not be NULL"); - } - } - - if (args[4] == NULL) { - elog(ERROR, "Vehicles SQL must not be NULL"); - } - - if (args[7] == NULL) { - elog(ERROR, "Matrix SQL must not be NULL"); - } process( args[0], @@ -276,7 +155,7 @@ PGDLLEXPORT Datum _vrp_vroom(PG_FUNCTION_ARGS) { exploration_level, timeout, fn, - is_plain, + !is_plain, &result_tuples, &result_count); diff --git a/src/vroom/vroom_driver.cpp b/src/vroom/vroom_driver.cpp index 0d80322d6..3b850301d 100644 --- a/src/vroom/vroom_driver.cpp +++ b/src/vroom/vroom_driver.cpp @@ -38,6 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/alloc.hpp" #include "cpp_common/assert.hpp" +#include "cpp_common/pgdata_getters.hpp" #include "cpp_common/identifiers.hpp" #include "cpp_common/vroom_job_t.hpp" @@ -51,18 +52,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. void vrp_do_vroom( - Vroom_job_t *jobs_arr, size_t total_jobs, - Vroom_time_window_t *jobs_tws_arr, size_t total_jobs_tws, - Vroom_shipment_t *shipments_arr, size_t total_shipments, - Vroom_time_window_t *shipments_tws_arr, size_t total_shipments_tws, - Vroom_vehicle_t *vehicles_arr, size_t total_vehicles, - Vroom_break_t *breaks_arr, size_t total_breaks, - Vroom_time_window_t *breaks_tws_arr, size_t total_breaks_tws, - Vroom_matrix_t *matrix_arr, size_t total_matrix, + char *jobs_sql, + char *jobs_tws_sql, + char *shipments_sql, + char *shipments_tws_sql, + char *vehicles_sql, + char *breaks_sql, + char *breaks_tws_sql, + char *matrix_sql, int32_t exploration_level, int32_t timeout, - int32_t loading_time, + + int16_t fn_used, + bool use_timestamps, Vroom_rt **return_tuples, size_t *return_count, @@ -81,6 +84,12 @@ vrp_do_vroom( std::ostringstream notice; try { using Matrix = vrprouting::vroom::Matrix; + using vrprouting::pgget::vroom::get_matrix; + using vrprouting::pgget::vroom::get_breaks; + using vrprouting::pgget::vroom::get_timewindows; + using vrprouting::pgget::vroom::get_jobs; + using vrprouting::pgget::vroom::get_shipments; + using vrprouting::pgget::vroom::get_vehicles; /* * verify preconditions @@ -90,33 +99,113 @@ vrp_do_vroom( pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(!(*return_count)); - pgassert(jobs_arr || shipments_arr); - pgassert(vehicles_arr); - pgassert(matrix_arr); - pgassert(total_jobs || total_shipments); - pgassert(total_vehicles); - pgassert(total_matrix); + /* + * Verify that both jobs_sql and shipments_sql are not NULL + */ + if (!jobs_sql && !shipments_sql) { + if (fn_used == 0) { + *err_msg = to_pg_msg("Both Jobs SQL and Shipments NULL must not be NULL"); + return; + } else if (fn_used == 1) { + *err_msg = to_pg_msg("Jobs SQL must not be NULL"); + return; + } else if (fn_used == 2) { + *err_msg = to_pg_msg("Shipments SQL must not be NULL"); + return; + } + } + + if (!vehicles_sql) { + *err_msg = to_pg_msg("Vehicles SQL must not be NULL"); + return; + } + + if (!matrix_sql) { + *err_msg = to_pg_msg("Matrix SQL must not be NULL"); + return; + } auto start_time = std::chrono::high_resolution_clock::now(); /* Data input starts */ - /* - * transform to C++ containers - */ - std::vector jobs(jobs_arr, jobs_arr + total_jobs); - std::vector jobs_tw(jobs_tws_arr, jobs_tws_arr + total_jobs_tws); - std::vector shipments(shipments_arr, shipments_arr + total_shipments); - std::vector shipments_tw(shipments_tws_arr, shipments_tws_arr + total_shipments_tws); - std::vector vehicles(vehicles_arr, vehicles_arr + total_vehicles); - std::vector breaks(breaks_arr, breaks_arr + total_breaks); - std::vector breaks_tw(breaks_tws_arr, breaks_tws_arr + total_breaks_tws); - std::vector costs(matrix_arr, matrix_arr + total_matrix); - - /* Data input ends */ - - /* Processing starts */ + hint = jobs_sql; + auto jobs = get_jobs( + jobs_sql? std::string(jobs_sql) : std::string(), + use_timestamps); + + hint = shipments_sql; + auto shipments = get_shipments( + shipments_sql? std::string(shipments_sql) : std::string(), + use_timestamps); + + if (jobs.empty() && shipments.empty()) { + if (fn_used == 0) { + if (!shipments_sql || !jobs_sql) { + *notice_msg = to_pg_msg("Jobs SQL and/or Shipments SQL query are NULL"); + return; + } + *notice_msg = to_pg_msg("Insufficient data found on Jobs SQL and/or Shipments SQL query."); + auto s1 = shipments_sql? std::string(shipments_sql) : ""; + auto s2 = jobs_sql? std::string(jobs_sql) : ""; + s1 = s1 + " " + s2; + *log_msg = to_pg_msg(s1); + } else if (fn_used == 1) { + *notice_msg = jobs_sql? + to_pg_msg("Insufficient data found on Jobs SQL query.") + : to_pg_msg("Jobs SQL query not found"); + *log_msg = jobs_sql? to_pg_msg(std::string(jobs_sql)) : nullptr; + } else if (fn_used == 2) { + *notice_msg = shipments_sql? + to_pg_msg("Insufficient data found on Shipments SQL query.") + : to_pg_msg("Jobs SQL query not found"); + *log_msg = shipments_sql? to_pg_msg(std::string(shipments_sql)) : nullptr; + } + return; + } + + hint = jobs_tws_sql; + auto jobs_tw = get_timewindows( + jobs_tws_sql? std::string(jobs_tws_sql) : std::string(), + use_timestamps, false); + + hint = shipments_tws_sql; + auto shipments_tw = get_timewindows( + shipments_tws_sql? std::string(shipments_tws_sql) : std::string(), + use_timestamps, true); + + hint = vehicles_sql; + auto vehicles = get_vehicles(vehicles_sql, use_timestamps); + + if (vehicles.size() == 0) { + *notice_msg = vehicles_sql? + to_pg_msg("Insufficient data found on Vehicles SQL query.") + : to_pg_msg("Vehicles SQL query not found"); + *log_msg = vehicles_sql? to_pg_msg(std::string(vehicles_sql)) : nullptr; + return; + } + + hint = breaks_sql; + auto breaks = get_breaks( + breaks_sql? std::string(breaks_sql) : std::string(), + use_timestamps); + + hint = breaks_tws_sql; + auto breaks_tw = get_timewindows( + breaks_tws_sql? std::string(breaks_tws_sql) : std::string(), + use_timestamps, false); + + hint = matrix_sql; + auto costs = get_matrix(matrix_sql, use_timestamps); + + if (costs.size() == 0) { + *notice_msg = matrix_sql? to_pg_msg("Insufficient data found on Matrix SQL query.") + : to_pg_msg("Matrix SQL query not found"); + *log_msg = matrix_sql? to_pg_msg(std::string(matrix_sql)) : nullptr; + return; + } + Identifiers location_ids; for (const auto &j : jobs) { @@ -185,9 +274,7 @@ vrp_do_vroom( problem.add_shipments(shipments, shipments_tw); auto end_time = std::chrono::high_resolution_clock::now(); - loading_time += static_cast( - std::chrono::duration_cast(end_time - start_time) - .count()); + auto loading_time = std::chrono::duration_cast(end_time - start_time).count(); std::vector results = problem.solve(exploration_level, timeout, loading_time); From 12c8d9ba00b5c705073d3f420b7d64d42c589669 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 30 Aug 2024 11:22:53 -0600 Subject: [PATCH 13/20] (CPP) adjusting code to changes from struct to class --- include/cpp_common/base_matrix.hpp | 5 +++-- include/cpp_common/vroom_matrix.hpp | 5 ++--- include/problem/matrix.hpp | 5 +++-- include/problem/orders.hpp | 4 ++-- include/problem/pickDeliver.hpp | 4 ++-- include/problem/tw_node.hpp | 7 ++++--- src/problem/fleet.cpp | 5 ++--- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/cpp_common/base_matrix.hpp b/include/cpp_common/base_matrix.hpp index 11e772620..12053f398 100644 --- a/include/cpp_common/base_matrix.hpp +++ b/include/cpp_common/base_matrix.hpp @@ -34,9 +34,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/typedefs.h" #include "cpp_common/identifiers.hpp" -using Matrix_cell_t = struct Matrix_cell_t; - namespace vrprouting { + +class Matrix_cell_t; + namespace base { diff --git a/include/cpp_common/vroom_matrix.hpp b/include/cpp_common/vroom_matrix.hpp index fed3ac8c7..c72bde06b 100644 --- a/include/cpp_common/vroom_matrix.hpp +++ b/include/cpp_common/vroom_matrix.hpp @@ -40,11 +40,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_types/typedefs.h" #include "cpp_common/identifiers.hpp" +namespace vrprouting { -using Vroom_matrix_t = struct Vroom_matrix_t; -using Matrix_cell_t = struct Matrix_cell_t; +class Vroom_matrix_t; -namespace vrprouting { namespace vroom { /** @brief N x N matrix diff --git a/include/problem/matrix.hpp b/include/problem/matrix.hpp index 497592e62..d0789521f 100644 --- a/include/problem/matrix.hpp +++ b/include/problem/matrix.hpp @@ -36,9 +36,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/base_matrix.hpp" #include "cpp_common/identifiers.hpp" -using Time_multipliers_t = struct Time_multipliers_t; - namespace vrprouting { + +class Time_multipliers_t; + namespace problem { class Matrix : public base::Base_Matrix { diff --git a/include/problem/orders.hpp b/include/problem/orders.hpp index a959f3d4e..5e1f2d74e 100644 --- a/include/problem/orders.hpp +++ b/include/problem/orders.hpp @@ -36,10 +36,10 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/identifiers.hpp" #include "problem/vehicle_node.hpp" +namespace vrprouting { -using Orders_t = struct Orders_t; +class Orders_t; -namespace vrprouting { namespace problem { class Vehicle_node; diff --git a/include/problem/pickDeliver.hpp b/include/problem/pickDeliver.hpp index 4ddd8aba8..6851c0396 100644 --- a/include/problem/pickDeliver.hpp +++ b/include/problem/pickDeliver.hpp @@ -37,11 +37,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "problem/fleet.hpp" using CompatibleVehicles_rt = struct CompatibleVehicles_rt; -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; namespace vrprouting { +class Orders_t; +class Vehicle_t; class Short_vehicle; namespace problem { diff --git a/include/problem/tw_node.hpp b/include/problem/tw_node.hpp index 35ef880d0..09cabf35d 100644 --- a/include/problem/tw_node.hpp +++ b/include/problem/tw_node.hpp @@ -34,10 +34,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/identifier.hpp" #include "problem/node_types.hpp" -using Orders_t = struct Orders_t; -using Vehicle_t = struct Vehicle_t; - namespace vrprouting { + +class Orders_t; +class Vehicle_t; + namespace problem { class Matrix; diff --git a/src/problem/fleet.cpp b/src/problem/fleet.cpp index 39ef641a4..825d81678 100644 --- a/src/problem/fleet.cpp +++ b/src/problem/fleet.cpp @@ -268,7 +268,7 @@ Fleet::add_vehicle( * stops can only be assigned when there is only one vehicle */ vehicle.cant_v == 1 ? - std::vector(vehicle.stops, vehicle.stops + vehicle.stops_size) : + std::vector(vehicle.stops.begin(), vehicle.stops.end()) : std::vector(), vehicle.capacity, @@ -403,8 +403,7 @@ void Fleet::build_fleet( (std::numeric_limits::max)(), vehicles[0].speed, 1, - nullptr, - 0, + std::vector(), /* * Start values From 734937781269e511d9b5c69415379bef53aa166a Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 30 Aug 2024 11:25:35 -0600 Subject: [PATCH 14/20] (vroom) cleaning up code --- include/vroom/vroom.hpp | 32 ++---- src/problem/vroom.cpp | 235 ++++++++++++---------------------------- 2 files changed, 80 insertions(+), 187 deletions(-) diff --git a/include/vroom/vroom.hpp b/include/vroom/vroom.hpp index 7320f5517..d9db415c9 100644 --- a/include/vroom/vroom.hpp +++ b/include/vroom/vroom.hpp @@ -43,18 +43,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "cpp_common/vroom_matrix.hpp" #include "cpp_common/messages.hpp" -using Vroom_break_t = struct Vroom_break_t; -using Vroom_job_t = struct Vroom_job_t; -using Vroom_shipment_t = struct Vroom_shipment_t; -using Vroom_time_window_t = struct Vroom_time_window_t; -using Vroom_vehicle_t = struct Vroom_vehicle_t; using Vroom_rt = struct Vroom_rt; namespace vrprouting { + +class Vroom_shipment_t; +class Vroom_break_t; +class Vroom_vehicle_t; +class Vroom_time_window_t; +class Vroom_job_t; + namespace problem { class Vroom : public vrprouting::Messages { - using MapTW = std::vector; + using MapTW = std::map, std::vector<::vroom::TimeWindow>>; public: /** @brief sets m_jobs by adding the Vroom_job_t */ @@ -80,24 +82,6 @@ class Vroom : public vrprouting::Messages { std::vector solve(int32_t, int32_t, int64_t); private: - std::vector<::vroom::TimeWindow> get_vroom_time_windows(const std::vector&) const; - ::vroom::Amount get_vroom_amounts(const std::vector&) const; - ::vroom::Amount get_vroom_amounts(const Amount *amounts, size_t count) const; - ::vroom::Skills get_vroom_skills(const Skill*, size_t) const; - ::vroom::Job get_vroom_job( - const Vroom_job_t&, - const std::vector&) const; - std::pair<::vroom::Job, ::vroom::Job> get_vroom_shipment( - const Vroom_shipment_t&, - const std::vector&, - const std::vector&) const; - std::vector<::vroom::Break> get_vroom_breaks( - const std::vector&, - const std::vector&) const; - ::vroom::Vehicle get_vroom_vehicle( - const Vroom_vehicle_t&, - const std::vector&, - const std::vector&) const; void get_amount(const ::vroom::Amount&, Amount**); StepType get_job_step_type(const ::vroom::JOB_TYPE&); StepType get_step_type(const ::vroom::Step&); diff --git a/src/problem/vroom.cpp b/src/problem/vroom.cpp index b4f9225a1..cd0cc97ea 100644 --- a/src/problem/vroom.cpp +++ b/src/problem/vroom.cpp @@ -53,58 +53,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. namespace vrprouting { namespace problem { -std::vector<::vroom::TimeWindow> -Vroom::get_vroom_time_windows(const std::vector &time_windows) const { - std::vector<::vroom::TimeWindow> tws; - for (auto time_window : time_windows) { - tws.push_back(::vroom::TimeWindow(time_window.tw_open, time_window.tw_close)); - } - return !tws.empty() ? - tws - : std::vector<::vroom::TimeWindow>(1, ::vroom::TimeWindow()); -} - -::vroom::Amount -Vroom::get_vroom_amounts(const std::vector &amounts) const { - ::vroom::Amount amt; - if (!amounts.empty()) { - for (auto amount : amounts) { - amt.push_back(amount); - } - } else { - const unsigned int amount_size = m_vehicles.size() ? - static_cast(m_vehicles[0].capacity.size()) : 0; - // Default to zero amount with provided size. - amt = ::vroom::Amount(amount_size); - } - return amt; -} - -::vroom::Amount -Vroom::get_vroom_amounts(const Amount *amounts, size_t count) const { - return get_vroom_amounts(std::vector(amounts, amounts + count)); -} - -::vroom::Skills -Vroom::get_vroom_skills(const Skill *skills, size_t count) const { - return std::unordered_set (skills, skills + count); -} - -::vroom::Job -Vroom::get_vroom_job( - const Vroom_job_t &job, - const std::vector &job_tws) const { - auto delivery = get_vroom_amounts(job.delivery, job.delivery_size); - auto pickup = get_vroom_amounts(job.pickup, job.pickup_size); - auto skills = get_vroom_skills(job.skills, job.skills_size); - auto time_windows = get_vroom_time_windows(job_tws); - auto location_id = static_cast<::vroom::Index>(m_matrix.get_index(job.location_id)); - return ::vroom::Job( - job.id, location_id, job.setup, job.service, - delivery, pickup, skills, job.priority, - time_windows, job.data); -} - /* * param[in] jobs The vector container of Vroom_job_t * param[in] jobs_tws The vector container of Vroom_time_window_t @@ -112,40 +60,26 @@ Vroom::get_vroom_job( void Vroom::add_jobs( const std::vector &jobs, - const std::vector &jobs_tws) { - std::map> job_tws_map; - for (auto job_tw : jobs_tws) { - Idx id = job_tw.id; - if (job_tws_map.find(id) == job_tws_map.end()) { - job_tws_map[id] = std::vector(); - } - job_tws_map[id].push_back(job_tw); - } + const std::map, std::vector<::vroom::TimeWindow>> &job_tws) { + std::vector<::vroom::TimeWindow> default_tw(1, ::vroom::TimeWindow()); + auto default_amount = ::vroom::Amount(m_vehicles.size() ? m_vehicles[0].capacity.size() : 0); + for (auto job : jobs) { - m_jobs.push_back(get_vroom_job(job, job_tws_map[job.id])); - } -} + auto job_tw = job_tws.find(std::make_pair(job.id, ' ')) == job_tws.end() ? + default_tw + : job_tws.at(std::make_pair(job.id, ' ')); + + auto pickup = job.pickup.empty() ? default_amount : job.pickup; + auto delivery = job.delivery.empty() ? default_amount : job.delivery; -std::pair<::vroom::Job, ::vroom::Job> -Vroom::get_vroom_shipment( - const Vroom_shipment_t &shipment, - const std::vector &pickup_tws, - const std::vector &delivery_tws) const { - auto amount = get_vroom_amounts(shipment.amount, shipment.amount_size); - auto skills = get_vroom_skills(shipment.skills, shipment.skills_size); - auto p_time_windows = get_vroom_time_windows(pickup_tws); - auto d_time_windows = get_vroom_time_windows(delivery_tws); - auto p_location_id = static_cast<::vroom::Index>(m_matrix.get_index(shipment.p_location_id)); - auto d_location_id = static_cast<::vroom::Index>(m_matrix.get_index(shipment.d_location_id)); - auto pickup = ::vroom::Job( - shipment.id, ::vroom::JOB_TYPE::PICKUP, p_location_id, - shipment.p_setup, shipment.p_service, amount, - skills, shipment.priority, p_time_windows, shipment.p_data); - auto delivery = ::vroom::Job( - shipment.id, ::vroom::JOB_TYPE::DELIVERY, d_location_id, - shipment.d_setup, shipment.d_service, amount, - skills, shipment.priority, d_time_windows, shipment.d_data); - return std::make_pair(pickup, delivery); + auto location_id = static_cast<::vroom::Index>(m_matrix.get_index(job.location_id)); + + auto vjob = ::vroom::Job( + job.id, location_id, job.setup, job.service, + delivery, pickup, job.skills, job.priority, + job_tw, job.data); + m_jobs.push_back(vjob); + } } /* @@ -155,72 +89,29 @@ Vroom::get_vroom_shipment( void Vroom::add_shipments( const std::vector &shipments, - const std::vector &shipments_tws) { - std::map> pickup_tws_map; - std::map> delivery_tws_map; - for (auto shipment_tw : shipments_tws) { - Idx id = shipment_tw.id; - if (shipment_tw.kind == 'p') { - if (pickup_tws_map.find(id) == pickup_tws_map.end()) { - pickup_tws_map[id] = std::vector(); - } - pickup_tws_map[id].push_back(shipment_tw); - } else if (shipment_tw.kind == 'd') { - if (delivery_tws_map.find(id) == delivery_tws_map.end()) { - delivery_tws_map[id] = std::vector(); - } - delivery_tws_map[id].push_back(shipment_tw); - } - } + const std::map, std::vector<::vroom::TimeWindow>> &shipments_tws) { + std::vector<::vroom::TimeWindow> default_tw(1, ::vroom::TimeWindow()); for (auto shipment : shipments) { - m_shipments.push_back(get_vroom_shipment(shipment, pickup_tws_map[shipment.id], delivery_tws_map[shipment.id])); - } -} - -std::vector<::vroom::Break> -Vroom::get_vroom_breaks( - const std::vector &breaks, - const std::vector &breaks_tws) const { - std::map> breaks_tws_map; - for (const auto &break_tw : breaks_tws) { - Idx id = break_tw.id; - if (breaks_tws_map.find(id) == breaks_tws_map.end()) { - breaks_tws_map[id] = std::vector(); - } - breaks_tws_map[id].push_back(break_tw); - } - std::vector<::vroom::Break> v_breaks; - for (const auto &v_break : breaks) { - v_breaks.push_back( - ::vroom::Break( - v_break.id, get_vroom_time_windows(breaks_tws_map[v_break.id]), v_break.service, v_break.data)); - } - return v_breaks; -} - -::vroom::Vehicle -Vroom::get_vroom_vehicle( - const Vroom_vehicle_t &vehicle, - const std::vector &breaks, - const std::vector &breaks_tws) const { - auto capacity = get_vroom_amounts(vehicle.capacity, vehicle.capacity_size); - auto skills = get_vroom_skills(vehicle.skills, vehicle.skills_size); - auto time_window = ::vroom::TimeWindow(vehicle.tw_open, vehicle.tw_close); - auto v_breaks = get_vroom_breaks(breaks, breaks_tws); - - std::optional<::vroom::Location> start_id; - std::optional<::vroom::Location> end_id; - // Set the value of start or end index only if they are present - if (vehicle.start_id != -1) { - start_id = static_cast<::vroom::Index>(m_matrix.get_index(vehicle.start_id)); - } - if (vehicle.end_id != -1) { - end_id = static_cast<::vroom::Index>(m_matrix.get_index(vehicle.end_id)); + auto pick_tw = shipments_tws.find(std::make_pair(shipment.id, 'p')) == shipments_tws.end() ? + default_tw + : shipments_tws.at(std::make_pair(shipment.id, 'p')); + auto drop_tw = shipments_tws.find(std::make_pair(shipment.id, 'd')) == shipments_tws.end() ? + default_tw + : shipments_tws.at(std::make_pair(shipment.id, 'd')); + + auto p_location_id = static_cast<::vroom::Index>(m_matrix.get_index(shipment.p_location_id)); + auto d_location_id = static_cast<::vroom::Index>(m_matrix.get_index(shipment.d_location_id)); + auto pickup = ::vroom::Job( + shipment.id, ::vroom::JOB_TYPE::PICKUP, p_location_id, + shipment.p_setup, shipment.p_service, shipment.amount, + shipment.skills, shipment.priority, pick_tw, shipment.p_data); + auto delivery = ::vroom::Job( + shipment.id, ::vroom::JOB_TYPE::DELIVERY, d_location_id, + shipment.d_setup, shipment.d_service, shipment.amount, + shipment.skills, shipment.priority, drop_tw, shipment.d_data); + + m_shipments.push_back(std::make_pair(pickup, delivery)); } - return ::vroom::Vehicle(vehicle.id, start_id, end_id, - ::vroom::DEFAULT_PROFILE, capacity, skills, time_window, - v_breaks, vehicle.data, vehicle.speed_factor, - static_cast(vehicle.max_tasks)); } /* @@ -232,15 +123,8 @@ void Vroom::add_vehicles( const std::vector &vehicles, const std::vector &breaks, - const std::vector &breaks_tws) { - std::map> breaks_tws_map; - for (auto break_tw : breaks_tws) { - Idx id = break_tw.id; - if (breaks_tws_map.find(id) == breaks_tws_map.end()) { - breaks_tws_map[id] = std::vector(); - } - breaks_tws_map[id].push_back(break_tw); - } + const std::map, std::vector<::vroom::TimeWindow>> &breaks_tws) { + std::vector<::vroom::TimeWindow> default_tw(1, ::vroom::TimeWindow()); std::map> v_breaks_map; for (auto v_break : breaks) { @@ -251,14 +135,39 @@ Vroom::add_vehicles( v_breaks_map[v_id].push_back(v_break); } + auto default_capacity = ::vroom::Amount(m_vehicles.size() ? m_vehicles[0].capacity.size() : 0); + for (auto vehicle : vehicles) { - std::vector v_breaks = v_breaks_map[vehicle.id]; - std::vector v_breaks_tws; - for (auto v_break : v_breaks) { - std::vector tws = breaks_tws_map[v_break.id]; - v_breaks_tws.insert(v_breaks_tws.end(), tws.begin(), tws.end()); + std::vector vehicle_breaks = v_breaks_map[vehicle.id]; + /* + * Builds the vroom::Break collection + */ + std::vector<::vroom::Break> vroom_breaks; + for (const auto &vb : vehicle_breaks) { + auto tws = breaks_tws.find(std::make_pair(vb.id, ' ')) == breaks_tws.end() ? + default_tw + : breaks_tws.at(std::make_pair(vb.id, ' ')); + vroom_breaks.push_back(::vroom::Break(vb.id, tws, vb.service, vb.data)); } - m_vehicles.push_back(get_vroom_vehicle(vehicle, v_breaks, v_breaks_tws)); + + auto capacity = vehicle.capacity.empty() ? default_capacity : vehicle.capacity; + + std::optional<::vroom::Location> start_id; + std::optional<::vroom::Location> end_id; + // Set the value of start or end index only if they are present + if (vehicle.start_id != -1) { + start_id = static_cast<::vroom::Index>(m_matrix.get_index(vehicle.start_id)); + } + if (vehicle.end_id != -1) { + end_id = static_cast<::vroom::Index>(m_matrix.get_index(vehicle.end_id)); + } + + auto vroom_vehicle = ::vroom::Vehicle( + vehicle.id, start_id, end_id, + ::vroom::DEFAULT_PROFILE, capacity, vehicle.skills, vehicle.tw, + vroom_breaks, vehicle.data, vehicle.speed_factor, + static_cast(vehicle.max_tasks)); + m_vehicles.push_back(vroom_vehicle); } } From 7bdb945cf8c4b8f9bfd00adcf26b1ec95e27d79b Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 30 Aug 2024 11:27:06 -0600 Subject: [PATCH 15/20] (build) adjusting the build to the files --- src/common/CMakeLists.txt | 14 -------------- src/cpp_common/CMakeLists.txt | 4 +++- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d5ebb8cba..3f7d1ab8c 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,19 +1,5 @@ ADD_LIBRARY(common OBJECT postgres_connection.c - - matrixRows_input.c - arrays_input.c - orders_input.c - vehicles_input.c - time_multipliers_input.c - time_msg.c e_report.c - - vroom/time_windows_input.c - vroom/breaks_input.c - vroom/jobs_input.c - vroom/shipments_input.c - vroom/vehicles_input.c - vroom/matrix_input.c ) diff --git a/src/cpp_common/CMakeLists.txt b/src/cpp_common/CMakeLists.txt index cac9cc623..8e2befc4f 100644 --- a/src/cpp_common/CMakeLists.txt +++ b/src/cpp_common/CMakeLists.txt @@ -1,9 +1,11 @@ ADD_LIBRARY(cpp_common OBJECT base_matrix.cpp messages.cpp + pgdata_fetchers.cpp + pgdata_getters.cpp + check_get_data.cpp identifier.cpp assert.cpp alloc.cpp - get_check_data.c vroom_matrix.cpp ) From e29347f85b9433da676f02540e3f7e08afe71f43 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 30 Aug 2024 11:38:51 -0600 Subject: [PATCH 16/20] (pgtap) some messages & tests change --- .../pgr_pickDeliver/pickDeliver/innerQuery.pg | 18 ++--- .../wrong_data_pickDeliverEuclidean.pg | 48 +++++--------- pgtap/vroom/edge_cases.pg | 26 ++++---- pgtap/vroom/inner_query.pg | 66 +++++++++++-------- 4 files changed, 80 insertions(+), 78 deletions(-) diff --git a/pgtap/pgr_pickDeliver/pickDeliver/innerQuery.pg b/pgtap/pgr_pickDeliver/pickDeliver/innerQuery.pg index 3ce8b1d85..91edbf002 100644 --- a/pgtap/pgr_pickDeliver/pickDeliver/innerQuery.pg +++ b/pgtap/pgr_pickDeliver/pickDeliver/innerQuery.pg @@ -54,13 +54,13 @@ BEGIN RETURN query SELECT lives_ok(query,'BIGINT on ' ||parameter||' is OK'); query := start_sql || parameter || '::REAL ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'.$$,'Expected Exception REAL with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception REAL with '||parameter); query := start_sql || parameter || '::FLOAT8 ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'.$$,'Expected Exception FLOAT with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception FLOAT with '||parameter); query := start_sql || parameter || '::NUMERIC ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'.$$,'Expected Exception NUMERIC with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception NUMERIC with '||parameter); END; $BODY$ LANGUAGE plpgsql; @@ -135,13 +135,13 @@ BEGIN RETURN query SELECT lives_ok(query,'BIGINT on ' ||parameter||' is OK'); query := start_sql || parameter || '::REAL ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected Column '$$||parameter||$$' type. Expected ANY-INTEGER$$,'Expected Exception REAL with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception REAL with '||parameter); query := start_sql || parameter || '::FLOAT8 ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected Column '$$||parameter||$$' type. Expected ANY-INTEGER$$,'Expected Exception FLOAT8 with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception FLOAT8 with '||parameter); query := start_sql || parameter || '::NUMERIC ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected Column '$$||parameter||$$' type. Expected ANY-INTEGER$$,'Expected Exception NUMERIC with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception NUMERIC with '||parameter); END; $BODY$ LANGUAGE plpgsql; @@ -217,13 +217,13 @@ BEGIN RETURN query SELECT lives_ok(query,'matrix BIGINT on ' ||parameter||' is OK'); query := start_sql || parameter || '::REAL ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'.$$,'Expected Exception REAL with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception REAL with '||parameter); query := start_sql || parameter || '::FLOAT8 ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'.$$,'Expected Exception FLOAT8 with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception FLOAT8 with '||parameter); query := start_sql || parameter || '::NUMERIC ' || end_sql; - RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'.$$,'Expected Exception NUMERIC with '||parameter); + RETURN query SELECT throws_ok(query,'XX000',$$Unexpected type in column '$$||parameter||$$'. Expected ANY-INTEGER$$,'Expected Exception NUMERIC with '||parameter); END; $BODY$ LANGUAGE plpgsql; diff --git a/pgtap/pgr_pickDeliver/wrong_data_pickDeliverEuclidean.pg b/pgtap/pgr_pickDeliver/wrong_data_pickDeliverEuclidean.pg index 011f79ef6..e395cc883 100644 --- a/pgtap/pgr_pickDeliver/wrong_data_pickDeliverEuclidean.pg +++ b/pgtap/pgr_pickDeliver/wrong_data_pickDeliverEuclidean.pg @@ -50,7 +50,7 @@ PREPARE initsol2 AS SELECT * FROM _vrp_pgr_pickDeliverEuclidean( $$SELECT * FROM orders_1$$, $$SELECT * FROM vehicles_1$$, - initial_sol := 7); + initial_sol := 8); PREPARE initsol3 AS SELECT * FROM _vrp_pgr_pickDeliverEuclidean( @@ -68,12 +68,10 @@ SELECT throws_ok('initsol1', SELECT throws_ok('initsol2', 'XX000', 'Illegal value in parameter: initial_sol', - 'Should throw: initial_sol > 6'); + 'Should throw: initial_sol > 7'); -SELECT throws_ok('initsol3', - 'XX000', - 'Illegal value in parameter: initial_sol', - 'Should throw: initial_sol = 0'); +SELECT lives_ok('initsol3', + 'Should live: initial_sol = 0'); -------------------------------------- -- testing wrong data on factor @@ -182,25 +180,19 @@ SELECT * FROM _vrp_pgr_pickDeliverEuclidean( $$SELECT * FROM orders_1$$, $$SELECT *, 10 AS e_open FROM vehicles_1$$); -SELECT throws_ok('vehiles5', - 'XX000', - $$Column 'e_open' not Found$$, - 'vehiles5, Should throw: e_close found, but not e_open'); +SELECT lives_ok('vehiles5', + 'vehiles5, Should live: e_close found, but not e_open'); -SELECT throws_ok('vehiles6', - 'XX000', - $$Column 'e_close' not Found$$, - 'vehiles6, Should throw: e_open found, but not e_close'); +SELECT lives_ok('vehiles6', + 'vehiles6, Should live: e_open found, but not e_close'); SELECT throws_ok('vehiles7', 'XX000', - $$Column 'e_y' not Found$$, + $$Column found: 'e_x', missing column: 'e_y'$$, 'vehiles7, Should throw: e_x found, but not e_y'); -SELECT throws_ok('vehiles8', - 'XX000', - $$Column 'e_close' not Found$$, - 'vehiles8, Should throw: e_y found, but not e_x'); +SELECT lives_ok('vehiles8', + 'vehiles8, Should live: e_open found, but not e_close'); -- s_open and s_close should exist together or not at all @@ -214,15 +206,11 @@ SELECT * FROM _vrp_pgr_pickDeliverEuclidean( $$SELECT * FROM orders_1$$, $$SELECT id, s_x, s_y, s_open, capacity FROM vehicles_1$$); -SELECT throws_ok('no_s_open', - 'XX000', - $$Column 's_open' not Found$$, - 'no_s_open, Should throw: s_close found, but not s_open'); +SELECT lives_ok('no_s_open', + 'no_s_open, Should live: s_close found, but not s_open'); -SELECT throws_ok('no_s_close', - 'XX000', - $$Column 's_close' not Found$$, - 'no_s_close, Should throw: s_open found, but not s_close'); +SELECT lives_ok('no_s_close', + 'no_s_close, Should live: s_open found, but not s_close'); --------------------- -- s_open > s_close --------------------- @@ -270,7 +258,7 @@ UPDATE orders_1 SET amount= -20 WHERE id =1; SELECT throws_ok('orders1', 'XX000', - 'Unexpected Negative value in column amount', + $$Unexpected negative value in column 'amount'$$, 'Should throw: pickup.demand < 0'); UPDATE orders_1 SET amount= 10 WHERE id =1; @@ -282,7 +270,7 @@ UPDATE orders_1 SET d_close = 5 WHERE id = 1; SELECT throws_ok('orders1', 'XX000', - 'Unexpected delivery time windows found on shipments', + $$Invalid time window found: 'd_close' < 'd_open'$$, 'orders1, Should throw: d_open > d_close'); UPDATE orders_1 SET d_close = 15 WHERE id = 1; @@ -294,7 +282,7 @@ UPDATE orders_1 SET p_close = 1 WHERE id = 1; SELECT throws_ok('orders1', 'XX000', - 'Unexpected pickup time windows found on shipments', + $$Invalid time window found: 'p_close' < 'p_open'$$, 'orders1, Should throw: p_open > p_close'); UPDATE orders_1 SET p_close = 10 WHERE id = 1; diff --git a/pgtap/vroom/edge_cases.pg b/pgtap/vroom/edge_cases.pg index 54702c4d7..b59677bb8 100644 --- a/pgtap/vroom/edge_cases.pg +++ b/pgtap/vroom/edge_cases.pg @@ -172,7 +172,7 @@ BEGIN SELECT throws_ok( 'jobs_neg_priority', 'XX000', - 'Unexpected Negative value in column priority', + $$Unexpected negative value in column 'priority'$$, 'Problem with negative priority jobs' ); @@ -194,7 +194,7 @@ BEGIN SELECT throws_ok( 'jobs_101_priority', 'XX000', - 'Priority exceeds the max priority 100', + $$Invalid value in column 'priority'. Maximum value allowed 100$$, 'Problem with > 100 priority jobs' ); @@ -254,7 +254,7 @@ BEGIN SELECT throws_ok( 'shipments_neg_priority', 'XX000', - 'Unexpected Negative value in column priority', + $$Unexpected negative value in column 'priority'$$, 'Problem with negative priority shipments' ); @@ -277,7 +277,7 @@ BEGIN SELECT throws_ok( 'shipments_101_priority', 'XX000', - 'Priority exceeds the max priority 100', + $$Invalid value in column 'priority'. Maximum value allowed 100$$, 'Problem with > 100 priority shipments' ); @@ -417,7 +417,7 @@ BEGIN SELECT throws_ok( 'jobs_invalid_tw_problem', 'XX000', - 'Invalid time window (3001, 3000)', + $$Invalid time window found: 'tw_close' < 'tw_open'$$, 'Problem with jobs having invalid time windows' ); @@ -462,7 +462,7 @@ BEGIN SELECT throws_ok( 'shipments_invalid_tw_problem', 'XX000', - 'Invalid time window (3001, 3000)', + $$Invalid time window found: 'tw_close' < 'tw_open'$$, 'Problem with shipments having invalid time windows' ); @@ -507,7 +507,7 @@ BEGIN SELECT throws_ok( 'breaks_invalid_tw_problem', 'XX000', - 'Invalid time window (3001, 3000)', + $$Invalid time window found: 'tw_close' < 'tw_open'$$, 'Problem with breaks having invalid time windows' ); @@ -554,7 +554,7 @@ BEGIN SELECT throws_ok( 'vehicles_invalid_tw_problem', 'XX000', - 'Invalid time window (3001, 3000)', + $$Invalid time window found: 'tw_close' < 'tw_open'$$, 'Problem with vehicles having invalid time windows' ); @@ -652,7 +652,7 @@ BEGIN SELECT throws_ok( 'vehicles_no_index_problem', 'XX000', - 'At least one out of start_id or end_id must be present', + $$Missing column(s): 'start_id' and/or 'end_id' must exist$$, 'Problem with vehicles having neither start_id nor end_id' ); @@ -1169,7 +1169,7 @@ BEGIN SELECT throws_ok( 'problem_negative_tasks', 'XX000', - 'Invalid max_tasks value -1', + $$Unexpected negative value in column 'max_tasks'$$, 'Problem with max_tasks as -1' ); @@ -1319,7 +1319,7 @@ BEGIN SELECT throws_ok( 'problem_negative_speed_factor', 'XX000', - 'Invalid speed_factor -1.000000', + $$Invalid negative or zero value in column 'speed_factor'$$, 'Problem with speed_factor as -1' ); @@ -1341,9 +1341,9 @@ BEGIN SELECT throws_ok( 'problem_negative_speed_factor', 'XX000', - 'Invalid speed_factor -1.000000', + $$Invalid negative or zero value in column 'speed_factor'$$, 'Problem with speed_factor as 0' - ); +); UPDATE vehicles SET speed_factor = 5.01 WHERE id = 4; PREPARE problem_more_than_5times_speed_factor AS diff --git a/pgtap/vroom/inner_query.pg b/pgtap/vroom/inner_query.pg index 9edce9829..6e86bf23b 100644 --- a/pgtap/vroom/inner_query.pg +++ b/pgtap/vroom/inner_query.pg @@ -16,7 +16,8 @@ SELECT * FROM vrp_vroomPlain( ); */ -CREATE OR REPLACE FUNCTION test_value(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT, accept TEXT[], reject TEXT[]) +CREATE OR REPLACE FUNCTION test_value(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], + parameter TEXT, accept TEXT[], reject TEXT[], expected TEXT DEFAULT 'ANY-INTEGER') RETURNS SETOF TEXT AS $BODY$ DECLARE @@ -24,6 +25,9 @@ DECLARE query TEXT; p TEXT; type_name TEXT; + msg1 TEXT; + msg2 TEXT; + msg3 TEXT; BEGIN start_sql = 'SELECT * FROM ' || fn || '(' || start_sql || '$$ SELECT '; FOREACH p IN ARRAY params @@ -37,13 +41,25 @@ BEGIN FOREACH type_name IN ARRAY accept LOOP query := start_sql || parameter || '::' || type_name || end_sql; - RETURN query SELECT lives_ok(query); + RETURN query SELECT lives_ok(query, 'Lives: ' || type_name || ',' || parameter); END LOOP; + IF (accept[1] = ('TIMESTAMP')) THEN msg1 = lower(accept[1]) || ' without time zone'; END IF; FOREACH type_name IN ARRAY reject LOOP query := start_sql || parameter || '::' || type_name || end_sql; - RETURN query SELECT throws_ok(query); + msg2 = lower(type_name); + IF (type_name = ('TIMESTAMP')) THEN msg2 = lower(type_name) || ' without time zone'; END IF; + msg3 = 'Throws: ' || accept[1] || ' to ' || type_name || ', ' || parameter; + IF (accept[1] IN ('INTERVAL') AND type_name NOT IN ('TIME')) THEN + RETURN query SELECT throws_ok(query, '42846', $$cannot cast type $$ || msg1 || $$ to $$ || msg2, msg3); + + ELSIF (accept[1] IN ('TIMESTAMP') AND type_name IN ('INTERVAL', 'INTEGER')) THEN + RETURN query SELECT throws_ok(query, '42846', $$cannot cast type $$ || msg1 || $$ to $$ || msg2, msg3); + + ELSE + RETURN query SELECT throws_ok(query, 'XX000', $$Unexpected type in column '$$ || parameter || $$'. Expected $$ || expected, msg3); + END IF; END LOOP; END; $BODY$ LANGUAGE plpgsql; @@ -68,7 +84,7 @@ DECLARE accept TEXT[] := ARRAY['SMALLINT', 'INTEGER']; reject TEXT[] := ARRAY['BIGINT', 'REAL', 'FLOAT8', 'NUMERIC']; BEGIN - RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject, 'SMALLINT or INTEGER'); END; $BODY$ LANGUAGE plpgsql; @@ -80,19 +96,19 @@ DECLARE accept TEXT[] := ARRAY['SMALLINT[]', 'INTEGER[]', 'BIGINT[]']; reject TEXT[] := ARRAY['REAL[]', 'FLOAT8[]', 'NUMERIC[]']; BEGIN - RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject, 'ANY-INTEGER-ARRAY'); END; $BODY$ LANGUAGE plpgsql; -CREATE OR REPLACE FUNCTION test_arrayInteger(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT) +CREATE OR REPLACE FUNCTION test_integerArray(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT) RETURNS SETOF TEXT AS $BODY$ DECLARE accept TEXT[] := ARRAY['SMALLINT[]', 'INTEGER[]']; reject TEXT[] := ARRAY['BIGINT[]', 'REAL[]', 'FLOAT8[]', 'NUMERIC[]']; BEGIN - RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject, 'INTEGER-ARRAY'); END; $BODY$ LANGUAGE plpgsql; @@ -116,7 +132,7 @@ DECLARE accept TEXT[] := ARRAY['CHAR']; reject TEXT[] := ARRAY['VARCHAR', 'TEXT']; BEGIN - RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject, 'TEXT'); END; $BODY$ LANGUAGE plpgsql; @@ -128,7 +144,7 @@ DECLARE accept TEXT[] := ARRAY['INTERVAL']; reject TEXT[] := ARRAY['TIMESTAMP', 'DATE', 'TIME', 'INTEGER']; BEGIN - RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject, 'INTERVAL'); END; $BODY$ LANGUAGE plpgsql; @@ -140,7 +156,7 @@ DECLARE accept TEXT[] := ARRAY['TIMESTAMP']; reject TEXT[] := ARRAY['INTERVAL', 'DATE', 'TIME', 'INTEGER']; BEGIN - RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject, 'TIMESTAMP'); END; $BODY$ LANGUAGE plpgsql; @@ -157,10 +173,10 @@ BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'location_id'); RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'delivery'); RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'pickup'); - RETURN QUERY SELECT test_arrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'skills'); + RETURN QUERY SELECT test_integerArray(fn, inner_query_table, start_sql, rest_sql, params, 'skills'); RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'priority'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'service'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'service'); ELSE RETURN QUERY SELECT test_Interval(fn, inner_query_table, start_sql, rest_sql, params, 'service'); END IF; @@ -180,11 +196,11 @@ BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'p_location_id'); RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'd_location_id'); RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'amount'); - RETURN QUERY SELECT test_arrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'skills'); + RETURN QUERY SELECT test_integerArray(fn, inner_query_table, start_sql, rest_sql, params, 'skills'); RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'priority'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'p_service'); - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'd_service'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'p_service'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'd_service'); ELSE RETURN QUERY SELECT test_Interval(fn, inner_query_table, start_sql, rest_sql, params, 'p_service'); RETURN QUERY SELECT test_Interval(fn, inner_query_table, start_sql, rest_sql, params, 'd_service'); @@ -205,11 +221,11 @@ BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'start_id'); RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'end_id'); RETURN QUERY SELECT test_anyArrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'capacity'); - RETURN QUERY SELECT test_arrayInteger(fn, inner_query_table, start_sql, rest_sql, params, 'skills'); + RETURN QUERY SELECT test_integerArray(fn, inner_query_table, start_sql, rest_sql, params, 'skills'); RETURN QUERY SELECT test_anyNumerical(fn, inner_query_table, start_sql, rest_sql, params, 'speed_factor'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); ELSE RETURN QUERY SELECT test_Timestamp(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); RETURN QUERY SELECT test_Timestamp(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); @@ -230,7 +246,7 @@ BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'start_id'); RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'end_id'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'duration'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'duration'); ELSE RETURN QUERY SELECT test_Interval(fn, inner_query_table, start_sql, rest_sql, params, 'duration'); END IF; @@ -250,7 +266,7 @@ BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'id'); RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'vehicle_id'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'service'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'service'); ELSE RETURN QUERY SELECT test_Interval(fn, inner_query_table, start_sql, rest_sql, params, 'service'); END IF; @@ -267,8 +283,8 @@ DECLARE BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'id'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); ELSE RETURN QUERY SELECT test_Timestamp(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); RETURN QUERY SELECT test_Timestamp(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); @@ -287,8 +303,8 @@ BEGIN RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'id'); RETURN QUERY SELECT test_Char(fn, inner_query_table, start_sql, rest_sql, params, 'kind'); IF is_plain = TRUE THEN - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); - RETURN QUERY SELECT test_Integer(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); ELSE RETURN QUERY SELECT test_Timestamp(fn, inner_query_table, start_sql, rest_sql, params, 'tw_open'); RETURN QUERY SELECT test_Timestamp(fn, inner_query_table, start_sql, rest_sql, params, 'tw_close'); @@ -448,7 +464,6 @@ BEGIN '$$SELECT * FROM vehicles$$, $$SELECT * FROM breaks$$, $$SELECT * FROM breaks_time_windows$$, '; rest_sql := additional; RETURN QUERY SELECT inner_query_matrix(fn, start_sql, rest_sql, is_plain); - END; $BODY$ LANGUAGE plpgsql; @@ -477,6 +492,5 @@ SELECT inner_query(is_plain => FALSE, additional => ', exploration_level => 5)') SELECT inner_query(is_plain => FALSE, additional => ', timeout => $$-00:00:01$$::INTERVAL)'); SELECT inner_query(is_plain => FALSE, additional => ', exploration_level => 5, timeout => $$-00:00:01$$::INTERVAL)'); - SELECT * FROM finish(); ROLLBACK; From bc25275561375e3ac0513d0f44de6eb60fb8e7f3 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 30 Aug 2024 12:00:52 -0600 Subject: [PATCH 17/20] (docqueries) some messages & results change --- docqueries/viewRoute/viewRoute/sample-with-service.result | 8 ++++++++ docqueries/viewRoute/viewRoute/sample_output.result | 8 ++++++++ .../viewRoute/viewRouteRaw/sample-with-service.result | 8 ++++++++ docqueries/viewRoute/viewRouteRaw/sample_output.result | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/docqueries/viewRoute/viewRoute/sample-with-service.result b/docqueries/viewRoute/viewRoute/sample-with-service.result index f67459b07..f3c9f2041 100644 --- a/docqueries/viewRoute/viewRoute/sample-with-service.result +++ b/docqueries/viewRoute/viewRoute/sample-with-service.result @@ -73,6 +73,14 @@ FROM vrp_viewRoute( $$SELECT start_vid, end_vid, travel_time FROM timeMatrix$$, $$SELECT * FROM tdm('2019-12-09'::TIMESTAMP, '2019-12-13'::TIMESTAMP)$$, 699::BIGINT, factor => 1.0::float); +NOTICE: Insufficient data found on 'orders' inner query +HINT: + WITH + __shipments AS (SELECT * FROM shipments), + __vehicles AS (SELECT * FROM vehicles) + SELECT * FROM __shipments + WHERE id IN (SELECT distinct(unnest(stops)) FROM __vehicles v WHERE id = 699) + seq | vehicle_seq | vehicle_id | stop_seq | stop_type | stop_id | shipment_id | load | travel | arrival | waiting_time | schedule | service | departure | cvtot | twvtot | location -----+-------------+------------+----------+-----------+---------+-------------+------+--------+---------+--------------+----------+---------+-----------+-------+--------+---------- (0 rows) diff --git a/docqueries/viewRoute/viewRoute/sample_output.result b/docqueries/viewRoute/viewRoute/sample_output.result index 70882ab6e..6b270b9b3 100644 --- a/docqueries/viewRoute/viewRoute/sample_output.result +++ b/docqueries/viewRoute/viewRoute/sample_output.result @@ -69,6 +69,14 @@ FROM vrp_viewRoute( $$SELECT start_vid, end_vid, travel_time FROM timeMatrix$$, $$SELECT * FROM tdm('2019-12-09'::TIMESTAMP, '2019-12-13'::TIMESTAMP)$$, 699::BIGINT, factor => 1.0::float); +NOTICE: Insufficient data found on 'orders' inner query +HINT: + WITH + __shipments AS (SELECT * FROM shipments), + __vehicles AS (SELECT * FROM vehicles) + SELECT * FROM __shipments + WHERE id IN (SELECT distinct(unnest(stops)) FROM __vehicles v WHERE id = 699) + seq | vehicle_seq | vehicle_id | stop_seq | stop_type | stop_id | shipment_id | load | travel | arrival | waiting_time | schedule | service | departure | cvtot | twvtot | location -----+-------------+------------+----------+-----------+---------+-------------+------+--------+---------+--------------+----------+---------+-----------+-------+--------+---------- (0 rows) diff --git a/docqueries/viewRoute/viewRouteRaw/sample-with-service.result b/docqueries/viewRoute/viewRouteRaw/sample-with-service.result index 3996b3cc7..baca01743 100644 --- a/docqueries/viewRoute/viewRouteRaw/sample-with-service.result +++ b/docqueries/viewRoute/viewRouteRaw/sample-with-service.result @@ -73,6 +73,14 @@ FROM vrp_viewRouteRaw( $$SELECT start_vid, end_vid, agg_cost FROM timeMatrix$$, $$SELECT * FROM tdm_raw('2019-12-09'::TIMESTAMP, '2019-12-13'::TIMESTAMP)$$, 699::BIGINT, factor => 1.0::float); +NOTICE: Insufficient data found on 'orders' inner query +HINT: + WITH + __shipments AS (SELECT * FROM shipments), + __vehicles AS (SELECT * FROM vehicles) + SELECT * FROM __shipments + WHERE id IN (SELECT distinct(unnest(stops)) FROM __vehicles v WHERE id = 699) + seq | vehicle_seq | vehicle_id | stop_seq | stop_type | stop_id | order_id | cargo | travel_fd | arrival_ft | wait_fd | schedule_ft | service_fd | departure_ft | cvtot | twvtot | location -----+-------------+------------+----------+-----------+---------+----------+-------+-----------+------------+---------+-------------+------------+--------------+-------+--------+---------- (0 rows) diff --git a/docqueries/viewRoute/viewRouteRaw/sample_output.result b/docqueries/viewRoute/viewRouteRaw/sample_output.result index 60dac4a08..09f433b54 100644 --- a/docqueries/viewRoute/viewRouteRaw/sample_output.result +++ b/docqueries/viewRoute/viewRouteRaw/sample_output.result @@ -69,6 +69,14 @@ FROM vrp_viewRouteRaw( $$SELECT start_vid, end_vid, agg_cost FROM timeMatrix$$, $$SELECT * FROM tdm_raw('2019-12-09'::TIMESTAMP, '2019-12-13'::TIMESTAMP)$$, 699::BIGINT, factor => 1.0::float); +NOTICE: Insufficient data found on 'orders' inner query +HINT: + WITH + __shipments AS (SELECT * FROM shipments), + __vehicles AS (SELECT * FROM vehicles) + SELECT * FROM __shipments + WHERE id IN (SELECT distinct(unnest(stops)) FROM __vehicles v WHERE id = 699) + seq | vehicle_seq | vehicle_id | stop_seq | stop_type | stop_id | order_id | cargo | travel_fd | arrival_ft | wait_fd | schedule_ft | service_fd | departure_ft | cvtot | twvtot | location -----+-------------+------------+----------+-----------+---------+----------+-------+-----------+------------+---------+-------------+------------+--------------+-------+--------+---------- (0 rows) From c0634a34daf841c89fbd326450c8b8bc6c0b0f19 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Sun, 25 Aug 2024 15:30:45 -0600 Subject: [PATCH 18/20] Removing unused files --- include/c_common/arrays_input.h | 43 - include/c_common/matrixRows_input.h | 45 - include/c_common/orders_input.h | 53 -- include/c_common/time_multipliers_input.h | 49 - include/c_common/vehicles_input.h | 58 -- include/c_common/vroom/breaks_input.h | 46 - include/c_common/vroom/jobs_input.h | 46 - include/c_common/vroom/matrix_input.h | 46 - include/c_common/vroom/shipments_input.h | 46 - include/c_common/vroom/time_windows_input.h | 54 -- include/c_common/vroom/vehicles_input.h | 46 - include/cpp_common/get_check_data.hpp | 166 ---- include/vrp/initial_solution.h | 72 -- include/vrp/initials_code.h | 50 -- src/common/arrays_input.c | 230 ----- src/common/matrixRows_input.c | 201 ----- src/common/orders_input.c | 363 -------- src/common/time_multipliers_input.c | 194 ---- src/common/vehicles_input.c | 464 ---------- src/common/vroom/breaks_input.c | 167 ---- src/common/vroom/jobs_input.c | 204 ----- src/common/vroom/matrix_input.c | 166 ---- src/common/vroom/shipments_input.c | 219 ----- src/common/vroom/time_windows_input.c | 225 ----- src/common/vroom/vehicles_input.c | 225 ----- src/cpp_common/get_check_data.c | 946 -------------------- 26 files changed, 4424 deletions(-) delete mode 100644 include/c_common/arrays_input.h delete mode 100644 include/c_common/matrixRows_input.h delete mode 100644 include/c_common/orders_input.h delete mode 100644 include/c_common/time_multipliers_input.h delete mode 100644 include/c_common/vehicles_input.h delete mode 100644 include/c_common/vroom/breaks_input.h delete mode 100644 include/c_common/vroom/jobs_input.h delete mode 100644 include/c_common/vroom/matrix_input.h delete mode 100644 include/c_common/vroom/shipments_input.h delete mode 100644 include/c_common/vroom/time_windows_input.h delete mode 100644 include/c_common/vroom/vehicles_input.h delete mode 100644 include/cpp_common/get_check_data.hpp delete mode 100644 include/vrp/initial_solution.h delete mode 100644 include/vrp/initials_code.h delete mode 100644 src/common/arrays_input.c delete mode 100644 src/common/matrixRows_input.c delete mode 100644 src/common/orders_input.c delete mode 100644 src/common/time_multipliers_input.c delete mode 100644 src/common/vehicles_input.c delete mode 100644 src/common/vroom/breaks_input.c delete mode 100644 src/common/vroom/jobs_input.c delete mode 100644 src/common/vroom/matrix_input.c delete mode 100644 src/common/vroom/shipments_input.c delete mode 100644 src/common/vroom/time_windows_input.c delete mode 100644 src/common/vroom/vehicles_input.c delete mode 100644 src/cpp_common/get_check_data.c diff --git a/include/c_common/arrays_input.h b/include/c_common/arrays_input.h deleted file mode 100644 index 33db7eadc..000000000 --- a/include/c_common/arrays_input.h +++ /dev/null @@ -1,43 +0,0 @@ -/*PGR-GNU***************************************************************** -File: arrays_input.h - -Copyright (c) 2015 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_ARRAYS_INPUT_H_ -#define INCLUDE_C_COMMON_ARRAYS_INPUT_H_ -#pragma once - - -#include -#include -#include - -/** @brief enforces the input array to be @b NOT empty */ -int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input); - -/** @brief Allows the input array to be empty */ -int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input); - -/** @brief Allows the input array, with non-negative elements to be empty */ -uint32_t* pgr_get_positiveIntArray_allowEmpty(size_t *arrlen, ArrayType *input); - -#endif // INCLUDE_C_COMMON_ARRAYS_INPUT_H_ diff --git a/include/c_common/matrixRows_input.h b/include/c_common/matrixRows_input.h deleted file mode 100644 index 5e3169981..000000000 --- a/include/c_common/matrixRows_input.h +++ /dev/null @@ -1,45 +0,0 @@ -/*PGR-GNU***************************************************************** -File: matrixRows_input.h - -Copyright (c) 2015 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_MATRIXROWS_INPUT_H_ -#define INCLUDE_C_COMMON_MATRIXROWS_INPUT_H_ -#pragma once - -#include - -typedef struct Matrix_cell_t Matrix_cell_t; - -/** @brief Get the travel time matrix */ -void get_matrixRows( - char *sql, - Matrix_cell_t **rows, - size_t *total_rows); - -/** @brief Get the travel time matrix with numerical types*/ -void get_matrixRows_plain( - char *sql, - Matrix_cell_t **rows, - size_t *total_rows); - -#endif // INCLUDE_C_COMMON_MATRIXROWS_INPUT_H_ diff --git a/include/c_common/orders_input.h b/include/c_common/orders_input.h deleted file mode 100644 index 6b2aa8261..000000000 --- a/include/c_common/orders_input.h +++ /dev/null @@ -1,53 +0,0 @@ -/*PGR-GNU***************************************************************** -File: orders_input.h - -Copyright (c) 2016 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_ORDERS_INPUT_H_ -#define INCLUDE_C_COMMON_ORDERS_INPUT_H_ -#pragma once - -#include -typedef struct Orders_t Orders_t; - -/** @brief Reads the pick-Deliver shipments for timestams and intervals*/ -void -get_shipments( - char *, - Orders_t **, - size_t *); - -/** @brief Reads the pick-Deliver shipments for raw data*/ -void -get_shipments_raw( - char *, - Orders_t **, - size_t *); - -/** @brief Reads the pick-Deliver shipments for euclidean information*/ -void -get_shipments_euclidean( - char *, - Orders_t **, - size_t *); - -#endif // INCLUDE_C_COMMON_ORDERS_INPUT_H_ diff --git a/include/c_common/time_multipliers_input.h b/include/c_common/time_multipliers_input.h deleted file mode 100644 index 229ea092f..000000000 --- a/include/c_common/time_multipliers_input.h +++ /dev/null @@ -1,49 +0,0 @@ -/*PGR-GNU***************************************************************** -File: time_multipliers_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2021 Celia Virginia Vergara Castillo -Copyright (c) 2021 Joseph Emile Honour Percival - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_TIME_MULTIPLIERS_INPUT_H_ -#define INCLUDE_C_COMMON_TIME_MULTIPLIERS_INPUT_H_ -#pragma once - -#include - -typedef struct Time_multipliers_t Time_multipliers_t; - -/** @brief Get the time multipliers using interval*/ -void get_timeMultipliers( - char *sql, - Time_multipliers_t **row, - size_t *total_rows); - -/** @brief Get the time multipliers using bigint*/ -void get_timeMultipliers_raw( - char *sql, - Time_multipliers_t **row, - size_t *total_rows); - -#endif // INCLUDE_C_COMMON_TIME_MULTIPLIERS_INPUT_H_ diff --git a/include/c_common/vehicles_input.h b/include/c_common/vehicles_input.h deleted file mode 100644 index ef36e0c90..000000000 --- a/include/c_common/vehicles_input.h +++ /dev/null @@ -1,58 +0,0 @@ -/*PGR-GNU***************************************************************** -File: vehicles_input.h - -Copyright (c) 2016 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VEHICLES_INPUT_H_ -#define INCLUDE_C_COMMON_VEHICLES_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vehicle_t Vehicle_t; - -/** @brief Reads the vehicles information */ -void -get_vehicles( - char *, - Vehicle_t **, - size_t *, - bool); - -/** @brief Reads the vehicles information */ -void -get_vehicles_raw( - char *, - Vehicle_t **, - size_t *, - bool); - -/** @brief Reads the vehicles information */ -void -get_vehicles_euclidean( - char *, - Vehicle_t **, - size_t *, - bool); - -#endif // INCLUDE_C_COMMON_VEHICLES_INPUT_H_ diff --git a/include/c_common/vroom/breaks_input.h b/include/c_common/vroom/breaks_input.h deleted file mode 100644 index a5d2f85b4..000000000 --- a/include/c_common/vroom/breaks_input.h +++ /dev/null @@ -1,46 +0,0 @@ -/*PGR-GNU***************************************************************** -File: breaks_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VROOM_BREAKS_INPUT_H_ -#define INCLUDE_C_COMMON_VROOM_BREAKS_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vroom_break_t Vroom_break_t; - -/** @brief Reads the VROOM breaks */ -void -get_vroom_breaks( - char *breaks_sql, - Vroom_break_t **breaks, - size_t *total_breaks, - bool is_plain); - -#endif // INCLUDE_C_COMMON_VROOM_BREAKS_INPUT_H_ diff --git a/include/c_common/vroom/jobs_input.h b/include/c_common/vroom/jobs_input.h deleted file mode 100644 index 7ec4c0cb5..000000000 --- a/include/c_common/vroom/jobs_input.h +++ /dev/null @@ -1,46 +0,0 @@ -/*PGR-GNU***************************************************************** -File: jobs_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VROOM_JOBS_INPUT_H_ -#define INCLUDE_C_COMMON_VROOM_JOBS_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vroom_job_t Vroom_job_t; - -/** @brief Reads the VROOM jobs */ -void -get_vroom_jobs( - char *jobs_sql, - Vroom_job_t **jobs, - size_t *total_jobs, - bool is_plain); - -#endif // INCLUDE_C_COMMON_VROOM_JOBS_INPUT_H_ diff --git a/include/c_common/vroom/matrix_input.h b/include/c_common/vroom/matrix_input.h deleted file mode 100644 index 67c10756e..000000000 --- a/include/c_common/vroom/matrix_input.h +++ /dev/null @@ -1,46 +0,0 @@ -/*PGR-GNU***************************************************************** -File: matrix_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VROOM_MATRIX_INPUT_H_ -#define INCLUDE_C_COMMON_VROOM_MATRIX_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vroom_matrix_t Vroom_matrix_t; - -/** @brief Reads the VROOM matrix */ -void -get_vroom_matrix( - char *matrix_sql, - Vroom_matrix_t **matrix, - size_t *total_matrix_rows, - bool is_plain); - -#endif // INCLUDE_C_COMMON_VROOM_MATRIX_INPUT_H_ diff --git a/include/c_common/vroom/shipments_input.h b/include/c_common/vroom/shipments_input.h deleted file mode 100644 index 22008b461..000000000 --- a/include/c_common/vroom/shipments_input.h +++ /dev/null @@ -1,46 +0,0 @@ -/*PGR-GNU***************************************************************** -File: shipments_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VROOM_SHIPMENTS_INPUT_H_ -#define INCLUDE_C_COMMON_VROOM_SHIPMENTS_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vroom_shipment_t Vroom_shipment_t; - -/** @brief Reads the VROOM shipments */ -void -get_vroom_shipments( - char *shipments_sql, - Vroom_shipment_t **shipments, - size_t *total_shipments, - bool is_plain); - -#endif // INCLUDE_C_COMMON_VROOM_SHIPMENTS_INPUT_H_ diff --git a/include/c_common/vroom/time_windows_input.h b/include/c_common/vroom/time_windows_input.h deleted file mode 100644 index f139bd592..000000000 --- a/include/c_common/vroom/time_windows_input.h +++ /dev/null @@ -1,54 +0,0 @@ -/*PGR-GNU***************************************************************** -File: time_windows_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VROOM_TIME_WINDOWS_INPUT_H_ -#define INCLUDE_C_COMMON_VROOM_TIME_WINDOWS_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vroom_time_window_t Vroom_time_window_t; - -/** @brief Reads the VROOM time windows */ -void -get_vroom_time_windows( - char *time_windows_sql, - Vroom_time_window_t **time_windows, - size_t *total_time_windows, - bool is_plain); - -/** @brief Reads the VROOM shipments time windows */ -void -get_vroom_shipments_time_windows( - char *time_windows_sql, - Vroom_time_window_t **time_windows, - size_t *total_time_windows, - bool is_plain); - -#endif // INCLUDE_C_COMMON_VROOM_TIME_WINDOWS_INPUT_H_ diff --git a/include/c_common/vroom/vehicles_input.h b/include/c_common/vroom/vehicles_input.h deleted file mode 100644 index 3e27e22e3..000000000 --- a/include/c_common/vroom/vehicles_input.h +++ /dev/null @@ -1,46 +0,0 @@ -/*PGR-GNU***************************************************************** -File: vehicles_input.h - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_C_COMMON_VROOM_VEHICLES_INPUT_H_ -#define INCLUDE_C_COMMON_VROOM_VEHICLES_INPUT_H_ -#pragma once - -#include -#include - -typedef struct Vroom_vehicle_t Vroom_vehicle_t; - -/** @brief Reads the VROOM vehicles */ -void -get_vroom_vehicles( - char *vehicles_sql, - Vroom_vehicle_t **vehicles, - size_t *total_vehicles, - bool is_plain); - -#endif // INCLUDE_C_COMMON_VROOM_VEHICLES_INPUT_H_ diff --git a/include/cpp_common/get_check_data.hpp b/include/cpp_common/get_check_data.hpp deleted file mode 100644 index fe28ee169..000000000 --- a/include/cpp_common/get_check_data.hpp +++ /dev/null @@ -1,166 +0,0 @@ -/*PGR-GNU***************************************************************** -FILE: get_check_data.hpp - -Copyright (c) 2015 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#ifndef INCLUDE_CPP_COMMON_GET_CHECK_DATA_HPP_ -#define INCLUDE_CPP_COMMON_GET_CHECK_DATA_HPP_ -#pragma once - -#include "c_common/postgres_connection.h" -#include "cpp_common/info.hpp" -#include "c_types/typedefs.h" - -/** @brief Check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUTE). */ -bool column_found(int colNumber); - -/** @brief Function tells expected type of each column and then check the correspondence type of each column. */ -void pgr_fetch_column_info( - Column_info_t info[], - int info_size); - -/*! @brief get value of specified column in char type. */ -char -spi_getChar( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - char default_value); - -/** @brief Function returns the values of specified columns in array. */ -int64_t* -spi_getBigIntArr( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size); - -/** @brief Function returns the values of specified columns in array. */ -int64_t* -spi_getBigIntArr_allowEmpty( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size); - -/** @brief Function returns the values of specified columns in array. */ -int64_t* -spi_getPositiveBigIntArr_allowEmpty( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size); - -/** @brief Function returns the values of specified columns in array. */ -uint32_t* -spi_getPositiveIntArr_allowEmpty( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size); - -/** @brief gets value of specified column in double type. */ -double -spi_getFloat8( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info); - -/** @brief gets string representation of the value of specified column. */ -char* -spi_getText( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info); - -/** @brief gets the vehicle max tasks value */ -int32_t -spi_getMaxTasks( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info); - -/** @brief Converts timestamp to timestamp without timezone */ -TTimestamp timestamp_without_timezone(TTimestamp timestamp); - -/** @brief gets a timestamp value from postgres type TIMESTAMP */ -TTimestamp get_TTimestamp(HeapTuple*, TupleDesc*, Column_info_t, TTimestamp); - -/** @brief gets a timestamp value from postgres type TIMESTAMP >= 1970-01-01 00:00:00*/ -TTimestamp get_PositiveTTimestamp(HeapTuple*, TupleDesc*, Column_info_t, TTimestamp); - -/** @brief gets a timestamp value from ANY-INTEGER */ -TTimestamp get_TTimestamp_plain(HeapTuple*, TupleDesc*, Column_info_t, TTimestamp); - -/** @brief gets a timestamp value from ANY-INTEGER > 0 */ -TTimestamp get_PositiveTTimestamp_plain(HeapTuple*, TupleDesc*, Column_info_t, TTimestamp); - -/** @brief gets an interval value from postgres type INTERVAL */ -TInterval get_TInterval(HeapTuple*, TupleDesc*, Column_info_t, TInterval); - -/** @brief gets an interval value from postgres type INTERVAL > 0 */ -TInterval get_PositiveTInterval(HeapTuple*, TupleDesc*, Column_info_t, TInterval); - -/** @brief gets an interval value from ANY-INTEGER */ -TInterval get_TInterval_plain(HeapTuple*, TupleDesc*, Column_info_t, TInterval); - -/** @brief gets an interval value from ANY-INTEGER > 0 */ -TInterval get_PositiveTInterval_plain(HeapTuple*, TupleDesc*, Column_info_t, TInterval); - -/** get Id from data */ -Id get_Id(HeapTuple*, TupleDesc*, Column_info_t, Id); - -/** get Idx from data */ -Idx get_Idx(HeapTuple*, TupleDesc*, Column_info_t, Idx); - -/** get StepType from data */ -StepType get_StepType(HeapTuple *, TupleDesc *, Column_info_t, StepType); - -/** get MatrixIndex from data */ -MatrixIndex get_MatrixIndex(HeapTuple*, TupleDesc*, Column_info_t, MatrixIndex); - -/** get Duration from data */ -Duration get_Duration(HeapTuple*, TupleDesc*, Column_info_t, Duration); - -/** get TravelCost from data */ -TravelCost get_Cost(HeapTuple*, TupleDesc*, Column_info_t, TravelCost); - -/** get Kind from data */ -char get_Kind(HeapTuple*, TupleDesc*, Column_info_t, char); - -/** get Priority from data */ -Priority get_Priority(HeapTuple*, TupleDesc*, Column_info_t, Priority); - -/** get Distance from data */ -Distance get_Distance(HeapTuple*, TupleDesc*, Column_info_t, Distance); - -/** get Amount from data */ -Amount get_Amount(HeapTuple*, TupleDesc*, Column_info_t, Amount); - -/** get positive Amount from data */ -PAmount get_PositiveAmount(HeapTuple*, TupleDesc*, Column_info_t, PAmount); - -/** get a coordinate value */ -Coordinate spi_getCoordinate(HeapTuple*, TupleDesc*, Column_info_t, Coordinate); - - -#endif // INCLUDE_CPP_COMMON_GET_CHECK_DATA_HPP_ diff --git a/include/vrp/initial_solution.h b/include/vrp/initial_solution.h deleted file mode 100644 index 2feea0d08..000000000 --- a/include/vrp/initial_solution.h +++ /dev/null @@ -1,72 +0,0 @@ -/*PGR-GNU***************************************************************** - -FILE: initial_solution.h - -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -/*! @file */ - -#ifndef INCLUDE_VRP_INITIAL_SOLUTION_H_ -#define INCLUDE_VRP_INITIAL_SOLUTION_H_ -#pragma once - -#include -#include -#include "vrp/pd_orders.h" -#include "vrp/solution.h" -#include "vrp/initials_code.hpp" -#include "cpp_common/identifiers.hpp" - -namespace vrprouting { -namespace vrp { - - -class Pgr_pickDeliver; - - -class Initial_solution : public Solution { - public: - Initial_solution( - Initials_code kind, - size_t); - - void invariant() const; - - private: - /* - * one truck per order - */ - void one_truck_all_orders(); - - void do_while_foo(int kind); - - - private: - Identifiers all_orders; - Identifiers unassigned; - Identifiers assigned; -}; - -} // namespace vrp -} // namespace vrprouting - -#endif // INCLUDE_VRP_INITIAL_SOLUTION_H_ diff --git a/include/vrp/initials_code.h b/include/vrp/initials_code.h deleted file mode 100644 index 49989f54d..000000000 --- a/include/vrp/initials_code.h +++ /dev/null @@ -1,50 +0,0 @@ -/*PGR-GNU***************************************************************** - -FILE: initials_code.hpp - -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -/*! @file */ - -#ifndef INCLUDE_VRP_INITIALS_CODE_H_ -#define INCLUDE_VRP_INITIALS_CODE_H_ -#pragma once - -namespace vrprouting { -namespace vrp { - -/*! Different kinds to insert an order into the vehicle */ -enum Initials_code { - OneTruck, /*! All orders in one truck */ - OnePerTruck, /*! One Order per truck */ - FrontTruck, /*! Insetion at the front of the truck */ - BackTruck, /*! Insetion at the back of the truck */ - BestInsert, /*! Best place to insert Order */ - BestBack, /*! Push back order that allows more orders to be inserted at the back */ - BestFront, /*! Push front order that allows more orders to be inserted at the front */ - OneDepot /*! Pick at front, drop at back, OneDepot for all vehicles */ -}; - -} // namespace vrp -} // namespace vrprouting - -#endif // INCLUDE_VRP_INITIALS_CODE_H_ diff --git a/src/common/arrays_input.c b/src/common/arrays_input.c deleted file mode 100644 index 24cbd7aea..000000000 --- a/src/common/arrays_input.c +++ /dev/null @@ -1,230 +0,0 @@ -/*PGR-GNU***************************************************************** -File: arrays_input.c - -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2015 Celia Virginia Vergara Castillo - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ -/* @file */ - -#include "c_common/arrays_input.h" - -#include -#include -#include - - -#ifdef PROFILE -#include "c_common/debug_macro.h" -#endif - -static -int64_t* -pgr_get_bigIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) { -#ifdef PROFILE - clock_t start_t = clock(); -#endif - - int64_t *c_array = NULL; - - Oid element_type = ARR_ELEMTYPE(v); - int *dim = ARR_DIMS(v); - int ndim = ARR_NDIM(v); - int nitems = ArrayGetNItems(ndim, dim); - Datum *elements; - bool *nulls; - int16 typlen; - bool typbyval; - char typalign; - - assert((*arrlen) == 0); - - - if (allow_empty && (ndim == 0 || nitems <= 0)) { - return (int64_t*) NULL; - } - /* the array is not empty*/ - - if (ndim != 1) { - elog(ERROR, "One dimension expected"); - } - - if (nitems <= 0) { - elog(ERROR, "No elements found"); - } - - get_typlenbyvalalign(element_type, - &typlen, &typbyval, &typalign); - - /* validate input data type */ - switch (element_type) { - case INT2OID: - case INT4OID: - case INT8OID: - break; - default: - elog(ERROR, "Expected array of ANY-INTEGER"); - } - - deconstruct_array(v, element_type, typlen, typbyval, - typalign, &elements, &nulls, - &nitems); - - c_array = (int64_t *) palloc(sizeof(int64_t) * (size_t)nitems); - if (!c_array) { - elog(ERROR, "Out of memory!"); - } - - - int i; - for (i = 0; i < nitems; i++) { - if (nulls[i]) { - pfree(c_array); - elog(ERROR, "NULL value found in Array!"); - } else { - switch (element_type) { - case INT2OID: - c_array[i] = (int64_t) DatumGetInt16(elements[i]); - break; - case INT4OID: - c_array[i] = (int64_t) DatumGetInt32(elements[i]); - break; - case INT8OID: - c_array[i] = DatumGetInt64(elements[i]); - break; - } - } - } - (*arrlen) = (size_t)nitems; - - pfree(elements); - pfree(nulls); -#ifdef PROFILE - time_msg("reading Array", start_t, clock()); -#endif - return c_array; -} - - -static -uint32_t* -pgr_get_positiveIntArr(ArrayType *v, size_t *arrlen, bool allow_empty) { -#ifdef PROFILE - clock_t start_t = clock(); -#endif - - uint32_t *c_array = NULL; - - Oid element_type = ARR_ELEMTYPE(v); - int *dim = ARR_DIMS(v); - int ndim = ARR_NDIM(v); - int nitems = ArrayGetNItems(ndim, dim); - Datum *elements; - bool *nulls; - int16 typlen; - bool typbyval; - char typalign; - - assert((*arrlen) == 0); - - - if (allow_empty && (ndim == 0 || nitems <= 0)) { - return (uint32_t*) NULL; - } - /* the array is not empty*/ - - if (ndim != 1) { - elog(ERROR, "One dimension expected"); - } - - if (nitems <= 0) { - elog(ERROR, "No elements found"); - } - - get_typlenbyvalalign(element_type, - &typlen, &typbyval, &typalign); - - /* validate input data type */ - switch (element_type) { - case INT2OID: - case INT4OID: - break; - default: - elog(ERROR, "Expected array of INTEGER"); - } - - deconstruct_array(v, element_type, typlen, typbyval, - typalign, &elements, &nulls, - &nitems); - - c_array = (uint32_t *) palloc(sizeof(uint32_t) * (size_t)nitems); - if (!c_array) { - elog(ERROR, "Out of memory!"); - } - - - int i; - for (i = 0; i < nitems; i++) { - if (nulls[i]) { - pfree(c_array); - elog(ERROR, "NULL value found in Array!"); - } else { - int32_t element; - switch (element_type) { - case INT2OID: - element = (int32_t) DatumGetInt16(elements[i]); - break; - case INT4OID: - element = (int32_t) DatumGetInt32(elements[i]); - break; - } - if (element < 0) { - elog(ERROR, "Unexpected Negative value %d in array", element); - } - c_array[i] = (uint32_t) element; - } - } - (*arrlen) = (size_t)nitems; - - pfree(elements); - pfree(nulls); -#ifdef PROFILE - time_msg("reading Array", start_t, clock()); -#endif - return c_array; -} - - -int64_t* pgr_get_bigIntArray(size_t *arrlen, ArrayType *input) { - return pgr_get_bigIntArr(input, arrlen, false); -} - - - -int64_t* pgr_get_bigIntArray_allowEmpty(size_t *arrlen, ArrayType *input) { - return pgr_get_bigIntArr(input, arrlen, true); -} - - -uint32_t* pgr_get_positiveIntArray_allowEmpty(size_t *arrlen, ArrayType *input) { - return pgr_get_positiveIntArr(input, arrlen, true); -} diff --git a/src/common/matrixRows_input.c b/src/common/matrixRows_input.c deleted file mode 100644 index 006cdd891..000000000 --- a/src/common/matrixRows_input.c +++ /dev/null @@ -1,201 +0,0 @@ -/*PGR-GNU***************************************************************** -File: matrixRows_input.c - -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2015 Celia Virginia Vergara Castillo - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/matrixRows_input.h" - -#include "cpp_common/info.hpp" -#include "cpp_common/matrix_cell_t.hpp" - -#include "cpp_common/get_check_data.hpp" - -#ifdef PROFILE -#include "c_common/time_msg.h" -#include "c_common/debug_macro.h" -#endif - -static -void fetch_plain( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info[3], - Matrix_cell_t *row) { - row->from_vid = get_Id(tuple, tupdesc, info[0], -1); - row->to_vid = get_Id(tuple, tupdesc, info[1], -1); - row->cost = get_PositiveTInterval_plain(tuple, tupdesc, info[2], 0); -} - -static -void fetch_timestamps( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info[3], - Matrix_cell_t *row) { - row->from_vid = get_Id(tuple, tupdesc, info[0], -1); - row->to_vid = get_Id(tuple, tupdesc, info[1], -1); - row->cost = get_PositiveTInterval(tuple, tupdesc, info[2], 0); -} - -/*! - * bigint start_vid, - * bigint end_vid, - * float agg_cost, - */ -static -void -get_matrixRows_general( - char *sql, - Column_info_t *info, - const int kind, - Matrix_cell_t **rows, - size_t *total_rows) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", sql); -#endif - - const int tuple_limit = 1000000; - size_t total_tuples = 0; - const int column_count = 3; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(sql); - - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - - bool moredata = true; - (*total_rows) = total_tuples; - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) - pgr_fetch_column_info(info, column_count); - - size_t ntuples = SPI_processed; - total_tuples += ntuples; - - if (ntuples > 0) { - if ((*rows) == NULL) - (*rows) = (Matrix_cell_t *)palloc0( - total_tuples * sizeof(Matrix_cell_t)); - else - (*rows) = (Matrix_cell_t *)repalloc( - (*rows), total_tuples * sizeof(Matrix_cell_t)); - - if ((*rows) == NULL) { - elog(ERROR, "Out of memory"); - } - - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - - size_t t; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - switch (kind) { - case 0 : fetch_timestamps(&tuple, &tupdesc, info, - &(*rows)[total_tuples - ntuples + t]); - break; - case 1 : fetch_plain(&tuple, &tupdesc, info, - &(*rows)[total_tuples - ntuples + t]); - break; - } - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - - if (total_tuples == 0) { - (*total_rows) = 0; - return; - } - - (*total_rows) = total_tuples; -#ifdef PROFILE - time_msg(" reading time matrix", start_t, clock()); -#endif -} - -/** - * @param [in] sql SQL query that has the following columns: start_vid, end_vid, agg_cost - * @param [out] rows C Container that holds all the matrix rows - * @param [out] total_rows Total rows recieved - */ -void -get_matrixRows( - char *sql, - Matrix_cell_t **rows, - size_t *total_rows) { - Column_info_t info[3]; - - int i; - for (i = 0; i < 3; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - info[0].name = "start_vid"; - info[1].name = "end_vid"; - info[2].name = "travel_time"; - - info[2].eType = INTERVAL; - get_matrixRows_general(sql, info, 0, rows, total_rows); -} - -/** - * @param [in] sql SQL query that has the following columns: start_vid, end_vid, agg_cost - * @param [out] rows C Container that holds all the matrix rows - * @param [out] total_rows Total rows recieved - */ -void -get_matrixRows_plain( - char *sql, - Matrix_cell_t **rows, - size_t *total_rows) { - Column_info_t info[3]; - - int i; - for (i = 0; i < 3; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - info[0].name = "start_vid"; - info[1].name = "end_vid"; - info[2].name = "agg_cost"; - - info[2].eType = ANY_NUMERICAL; - get_matrixRows_general(sql, info, 1, rows, total_rows); -} diff --git a/src/common/orders_input.c b/src/common/orders_input.c deleted file mode 100644 index afdedbdad..000000000 --- a/src/common/orders_input.c +++ /dev/null @@ -1,363 +0,0 @@ -/*PGR-GNU***************************************************************** -File: pd_orders_input.c - -Copyright (c) 2016 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2016 Celia Virginia Vergara Castillo - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/orders_input.h" - -#include "cpp_common/orders_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" -#ifdef PROFILE -#include "c_common/time_msg.h" -#endif - - -static -void fetch_euclidean( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Orders_t *pd_order) { - pd_order->id = get_Id(tuple, tupdesc, info[0], -1); - pd_order->demand = get_PositiveAmount(tuple, tupdesc, info[1], 0); - - /* - * the pickups - */ - pd_order->pick_open_t = get_TTimestamp_plain(tuple, tupdesc, info[2], -1); - pd_order->pick_close_t = get_TTimestamp_plain(tuple, tupdesc, info[3], -1); - pd_order->pick_service_t = get_TInterval_plain(tuple, tupdesc, info[4], 0); - - /* - * the deliveries - */ - pd_order->deliver_open_t = get_TTimestamp_plain(tuple, tupdesc, info[5], -1); - pd_order->deliver_close_t = get_TTimestamp_plain(tuple, tupdesc, info[6], -1); - pd_order->deliver_service_t = get_TInterval_plain(tuple, tupdesc, info[7], 0); - - pd_order->pick_x = spi_getCoordinate(tuple, tupdesc, info[8], 0); - pd_order->pick_y = spi_getCoordinate(tuple, tupdesc, info[9], 0); - pd_order->deliver_x = spi_getCoordinate(tuple, tupdesc, info[10], 0); - pd_order->deliver_y = spi_getCoordinate(tuple, tupdesc, info[11], 0); - - /* - * ignored information - */ - pd_order->pick_node_id = 0; - pd_order->deliver_node_id = 0; -} - - -static -void fetch_raw( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Orders_t *pd_order) { - pd_order->id = get_Id(tuple, tupdesc, info[0], -1); - pd_order->demand = get_PositiveAmount(tuple, tupdesc, info[1], 0); - - /* - * the pickups - */ - pd_order->pick_node_id = get_Id(tuple, tupdesc, info[8], -1); - pd_order->pick_open_t = get_TTimestamp_plain(tuple, tupdesc, info[2], -1); - pd_order->pick_close_t = get_TTimestamp_plain(tuple, tupdesc, info[3], -1); - pd_order->pick_service_t = get_TInterval_plain(tuple, tupdesc, info[4], 0); - - /* - * the deliveries - */ - pd_order->deliver_node_id = get_Id(tuple, tupdesc, info[9], -1); - pd_order->deliver_open_t = get_TTimestamp_plain(tuple, tupdesc, info[5], -1); - pd_order->deliver_close_t = get_TTimestamp_plain(tuple, tupdesc, info[6], -1); - pd_order->deliver_service_t = get_TInterval_plain(tuple, tupdesc, info[7], 0); - - - /* - * Ignored information - */ - pd_order->pick_x = 0; - pd_order->pick_y = 0; - pd_order->deliver_x = 0; - pd_order->deliver_y = 0; -} - -static -void fetch_timestamps( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Orders_t *pd_order) { - pd_order->id = get_Id(tuple, tupdesc, info[0], -1); - pd_order->demand = get_PositiveAmount(tuple, tupdesc, info[1], 0); - - /* - * the pickups - */ - pd_order->pick_node_id = get_Id(tuple, tupdesc, info[2], -1); - pd_order->pick_open_t = get_TTimestamp(tuple, tupdesc, info[3], -1); - pd_order->pick_close_t = get_TTimestamp(tuple, tupdesc, info[4], -1); - pd_order->pick_service_t = get_TInterval(tuple, tupdesc, info[5], 0); - - /* - * the deliveries - */ - pd_order->deliver_node_id = get_Id(tuple, tupdesc, info[6], -1); - pd_order->deliver_open_t = get_TTimestamp(tuple, tupdesc, info[7], -1); - pd_order->deliver_close_t = get_TTimestamp(tuple, tupdesc, info[8], -1); - pd_order->deliver_service_t = get_TInterval(tuple, tupdesc, info[9], 0); - - PGR_DBG("pick_service_t %ld deliver_service_t %ld", pd_order->pick_service_t, pd_order->deliver_service_t); - - /* - * Ignored information - */ - pd_order->pick_x = 0; - pd_order->pick_y = 0; - pd_order->deliver_x = 0; - pd_order->deliver_y = 0; -} - - - -static -void -pgr_get_pd_orders_general( - char *pd_orders_sql, - Orders_t **pd_orders, - size_t *total_pd_orders, - - Column_info_t *info, - const int column_count, - - int kind) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", pd_orders_sql); -#endif - - const int tuple_limit = 1000000; - - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(pd_orders_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_pd_orders) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*pd_orders) == NULL) - (*pd_orders) = (Orders_t *)palloc0( - total_tuples * sizeof(Orders_t)); - else - (*pd_orders) = (Orders_t *)repalloc( - (*pd_orders), - total_tuples * sizeof(Orders_t)); - - if ((*pd_orders) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - switch (kind) { - case 0 : fetch_timestamps(&tuple, &tupdesc, info, - &(*pd_orders)[total_tuples - ntuples + t]); - break; - case 1 : fetch_raw(&tuple, &tupdesc, info, - &(*pd_orders)[total_tuples - ntuples + t]); - break; - case 2 : fetch_euclidean(&tuple, &tupdesc, info, - &(*pd_orders)[total_tuples - ntuples + t]); - break; - } - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_pd_orders) = 0; - return; - } - - (*total_pd_orders) = total_tuples; -#ifdef PROFILE - time_msg("reading shipments", start_t, clock()); -#endif -} - -/** - * @param[in] sql SQL query to execute - * @param[out] rows C Container that holds the data - * @param[out] total_rows Total rows recieved - */ -void -get_shipments( - char *sql, - Orders_t **rows, - size_t *total_rows) { - const int column_count = 10; - Column_info_t info[10]; - - for (int i = 0; i < column_count; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "amount"; - info[2].name = "p_id"; - info[3].name = "p_tw_open"; - info[4].name = "p_tw_close"; - info[5].name = "p_t_service"; - info[6].name = "d_id"; - info[7].name = "d_tw_open"; - info[8].name = "d_tw_close"; - info[9].name = "d_t_service"; - - info[3].eType = TIMESTAMP; - info[4].eType = TIMESTAMP; - info[7].eType = TIMESTAMP; - info[8].eType = TIMESTAMP; - info[5].eType = INTERVAL; - info[9].eType = INTERVAL; - - /* service is optional*/ - info[5].strict = false; - info[9].strict = false; - - pgr_get_pd_orders_general(sql, rows, total_rows, info, column_count, 0); -} - -/** - * @param[in] sql SQL query to execute - * @param[out] rows C Container that holds the data - * @param[out] total_rows Total rows recieved - */ -void -get_shipments_raw( - char *sql, - Orders_t **rows, - size_t *total_rows) { - const int column_count = 10; - Column_info_t info[10]; - - for (int i = 0; i < column_count; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "amount"; - info[2].name = "p_open"; - info[3].name = "p_close"; - info[4].name = "p_service"; - info[5].name = "d_open"; - info[6].name = "d_close"; - info[7].name = "d_service"; - info[8].name = "p_id"; - info[9].name = "d_id"; - - /* service is optional*/ - info[4].strict = false; - info[7].strict = false; - - pgr_get_pd_orders_general(sql, rows, total_rows, info, column_count, 1); -} - -/** - * @param[in] sql SQL query to execute - * @param[out] rows C Container that holds the data - * @param[out] total_rows Total rows recieved - */ -void -get_shipments_euclidean( - char *sql, - Orders_t **rows, - size_t *total_rows) { - const int column_count = 12; - Column_info_t info[12]; - - for (int i = 0; i < column_count; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "amount"; - info[2].name = "p_open"; - info[3].name = "p_close"; - info[4].name = "p_service"; - info[5].name = "d_open"; - info[6].name = "d_close"; - info[7].name = "d_service"; - info[8].name = "p_x"; - info[9].name = "p_y"; - info[10].name = "d_x"; - info[11].name = "d_y"; - - /* service is optional*/ - info[4].strict = false; - info[7].strict = false; - - /* (x,y) are ignored*/ - info[8].eType = ANY_NUMERICAL; - info[9].eType = ANY_NUMERICAL; - info[10].eType = ANY_NUMERICAL; - info[11].eType = ANY_NUMERICAL; - - pgr_get_pd_orders_general(sql, rows, total_rows, info, column_count, 2); -} diff --git a/src/common/time_multipliers_input.c b/src/common/time_multipliers_input.c deleted file mode 100644 index 4c31ef907..000000000 --- a/src/common/time_multipliers_input.c +++ /dev/null @@ -1,194 +0,0 @@ -/*PGR-GNU***************************************************************** -File: time_multipliers_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2021 Copyright (c) 2021 Joseph Emile Honour Percival - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/time_multipliers_input.h" - -#include "cpp_common/info.hpp" -#include "cpp_common/time_multipliers_t.hpp" -#include "cpp_common/get_check_data.hpp" - -#ifdef PROFILE -#include "c_common/time_msg.h" -#include "c_common/debug_macro.h" -#endif - - -static -void fetch_raw( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info[2], - Time_multipliers_t *row) { - row->start_time = get_TTimestamp_plain(tuple, tupdesc, info[0], 0); - row->multiplier = spi_getFloat8(tuple, tupdesc, info[1]); -} - -static -void fetch_timestamps( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info[2], - Time_multipliers_t *row) { - row->start_time = get_TTimestamp(tuple, tupdesc, info[0], 0); - row->multiplier = spi_getFloat8(tuple, tupdesc, info[1]); -} - -/** - * param [in] sql multipliers SQL - * param [in,out] rows catptured information - * param [in,out] total_rows total information captured - */ -static -void get_timeMultipliersGeneral( - char *sql, - Column_info_t *info, - const int kind, - Time_multipliers_t **rows, - size_t *total_rows) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", sql); -#endif - const int tuple_limit = 1000000; - size_t total_tuples = 0; - const int column_count = 2; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(sql); - - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - - bool moredata = true; - (*total_rows) = total_tuples; - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) - pgr_fetch_column_info(info, column_count); - - size_t ntuples = SPI_processed; - total_tuples += ntuples; - - if (ntuples > 0) { - if ((*rows) == NULL) - (*rows) = (Time_multipliers_t *)palloc0( - total_tuples * sizeof(Time_multipliers_t)); - else - (*rows) = (Time_multipliers_t *)repalloc( - (*rows), total_tuples * sizeof(Time_multipliers_t)); - - if ((*rows) == NULL) { - elog(ERROR, "Out of memory"); - } - - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - - for (size_t t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - switch (kind) { - case 0 : fetch_timestamps(&tuple, &tupdesc, info, - &(*rows)[total_tuples - ntuples + t]); - break; - case 1 : fetch_raw(&tuple, &tupdesc, info, - &(*rows)[total_tuples - ntuples + t]); - break; - } - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - - if (total_tuples == 0) { - (*total_rows) = 0; - return; - } - - (*total_rows) = total_tuples; -#ifdef PROFILE - time_msg("reading time dependant multipliers", start_t, clock()); -#endif -} - -/** - @param [in] sql query that has the following columns: start_time, multiplier - @param [out] rows C Container that holds all the multipliers rows - @param [out] total_rows Total rows recieved - */ -void get_timeMultipliers( - char *sql, - Time_multipliers_t **rows, - size_t *total_rows) { - Column_info_t info[2]; - - int i; - for (i = 0; i < 2; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_NUMERICAL; - } - info[0].name = "start_time"; - info[1].name = "multiplier"; - - info[0].eType = TIMESTAMP; - - get_timeMultipliersGeneral(sql, info, 0, rows, total_rows); -} - -/** - @param [in] sql query that has the following columns: start_time, multiplier - @param [out] rows C Container that holds all the multipliers - @param [out] total_rows Total rows recieved - */ -void get_timeMultipliers_raw( - char *sql, - Time_multipliers_t **rows, - size_t *total_rows) { - Column_info_t info[2]; - - int i; - for (i = 0; i < 2; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_NUMERICAL; - } - info[0].name = "start_value"; - info[1].name = "multiplier"; - - info[0].eType = ANY_INTEGER; - - get_timeMultipliersGeneral(sql, info, 1, rows, total_rows); -} diff --git a/src/common/vehicles_input.c b/src/common/vehicles_input.c deleted file mode 100644 index 8ae5dd2be..000000000 --- a/src/common/vehicles_input.c +++ /dev/null @@ -1,464 +0,0 @@ -/*PGR-GNU***************************************************************** -File: vehicles_input.c - -Copyright (c) 2016 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2016 Celia Virginia Vergara Castillo - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vehicles_input.h" - -#include -#include - - -#include "cpp_common/info.hpp" -#include "cpp_common/vehicle_t.hpp" - -#include "cpp_common/get_check_data.hpp" - -#ifdef PROFILE -#include "c_common/time_msg.h" -#include "c_common/debug_macro.h" -#endif - -static -void check_pairs(Column_info_t lhs, Column_info_t rhs) { - if (!(column_found(lhs.colNumber)) && column_found(rhs.colNumber)) { - ereport(ERROR, - (errmsg("Column \'%s\' not Found", lhs.name), - errhint("%s was found, also column is expected %s ", - rhs.name, lhs.name))); - } -} - -static -void fetch_euclidean( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vehicle_t *vehicle, - bool with_stops) { - bool with_id = false; - /* - * s_tw_open, s_tw_close must exist or non at all - */ - check_pairs(info[4], info[5]); - check_pairs(info[5], info[4]); - /* - * e_tw_open, e_tw_close must exist or non at all - */ - check_pairs(info[6], info[7]); - check_pairs(info[7], info[6]); - - /* - * e_x, e_y must exist or non at all - */ - check_pairs(info[13], info[14]); - check_pairs(info[14], info[13]); - - vehicle->id = get_Id(tuple, tupdesc, info[0], -1); - vehicle->capacity = get_PositiveAmount(tuple, tupdesc, info[1], 0); - vehicle->cant_v = get_PositiveAmount(tuple, tupdesc, info[2], 1); - vehicle->speed = column_found(info[3].colNumber) ? spi_getFloat8(tuple, tupdesc, info[3]) : 1; - - /* - * start values - */ - vehicle->start_open_t = get_TTimestamp_plain(tuple, tupdesc, info[4], 0); - vehicle->start_close_t = get_TTimestamp_plain(tuple, tupdesc, info[5], INT64_MAX); - - /* - * end values - */ - vehicle->end_open_t = get_TTimestamp_plain(tuple, tupdesc, info[6], vehicle->start_open_t); - vehicle->end_close_t = get_TTimestamp_plain(tuple, tupdesc, info[7], vehicle->start_close_t); - - /* - * service time values - */ - vehicle->start_service_t = get_PositiveTInterval_plain(tuple, tupdesc, info[9], 0); - vehicle->end_service_t = get_PositiveTInterval_plain(tuple, tupdesc, info[10], 0); - - /* - * stops - */ - vehicle->stops = NULL; - vehicle->stops_size = 0; - if (with_stops && column_found(info[8].colNumber)) { - vehicle->stops = spi_getBigIntArr_allowEmpty(tuple, tupdesc, info[8], &vehicle->stops_size); - } - - /* - * Values for eucledian - */ - vehicle->start_x = with_id ? 0 : spi_getCoordinate(tuple, tupdesc, info[11], 0); - vehicle->start_y = with_id ? 0 : spi_getCoordinate(tuple, tupdesc, info[12], 0); - vehicle->end_x = with_id ? 0 : spi_getCoordinate(tuple, tupdesc, info[13], vehicle->start_x); - vehicle->end_y = with_id ? 0 : spi_getCoordinate(tuple, tupdesc, info[14], vehicle->start_y); -} - -static -void fetch_raw( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vehicle_t *vehicle, - bool with_stops) { - /* - * s_tw_open, s_tw_close must exist or non at all - */ - check_pairs(info[6], info[7]); - check_pairs(info[7], info[6]); - /* - * e_tw_open, e_tw_close must exist or non at all - */ - check_pairs(info[10], info[11]); - check_pairs(info[10], info[11]); - - vehicle->id = get_Id(tuple, tupdesc, info[0], -1); - vehicle->capacity = get_PositiveAmount(tuple, tupdesc, info[1], 0); - vehicle->cant_v = get_PositiveAmount(tuple, tupdesc, info[2], 1); - vehicle->speed = column_found(info[3].colNumber) ? spi_getFloat8(tuple, tupdesc, info[3]) : 1; - vehicle->stops = NULL; - vehicle->stops_size = 0; - if (with_stops && column_found(info[4].colNumber)) { - vehicle->stops = spi_getBigIntArr_allowEmpty(tuple, tupdesc, info[4], &vehicle->stops_size); - } - - /* - * start values - */ - vehicle->start_node_id = get_Id(tuple, tupdesc, info[5], -1); - vehicle->start_open_t = get_TTimestamp_plain(tuple, tupdesc, info[6], 0); - vehicle->start_close_t = get_TTimestamp_plain(tuple, tupdesc, info[7], INT64_MAX); - vehicle->start_service_t = get_PositiveTInterval_plain(tuple, tupdesc, info[8], 0); - - /* - * end values - */ - vehicle->end_node_id = get_Id(tuple, tupdesc, info[9], vehicle->start_node_id); - vehicle->end_open_t = get_TTimestamp_plain(tuple, tupdesc, info[10], vehicle->start_open_t); - vehicle->end_close_t = get_TTimestamp_plain(tuple, tupdesc, info[11], vehicle->start_close_t); - vehicle->end_service_t = get_PositiveTInterval_plain(tuple, tupdesc, info[12], 0); - - /* - * Ignored values - */ - vehicle->start_x = 0; - vehicle->start_y = 0; - vehicle->end_x = 0; - vehicle->end_y = 0; -} - -static -void fetch_timestamps( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vehicle_t *vehicle, - bool with_stops) { - /* - * s_tw_open, s_tw_close must exist or non at all - */ - check_pairs(info[6], info[7]); - check_pairs(info[7], info[6]); - /* - * e_tw_open, e_tw_close must exist or non at all - */ - check_pairs(info[10], info[11]); - check_pairs(info[10], info[11]); - - vehicle->id = get_Id(tuple, tupdesc, info[0], -1); - vehicle->capacity = get_PositiveAmount(tuple, tupdesc, info[1], 0); - vehicle->cant_v = get_PositiveAmount(tuple, tupdesc, info[2], 1); - vehicle->speed = column_found(info[3].colNumber) ? spi_getFloat8(tuple, tupdesc, info[3]) : 1; - vehicle->stops = NULL; - vehicle->stops_size = 0; - if (with_stops && column_found(info[4].colNumber)) { - vehicle->stops = spi_getBigIntArr_allowEmpty(tuple, tupdesc, info[4], &vehicle->stops_size); - } - - /* - * start values - */ - vehicle->start_node_id = get_Id(tuple, tupdesc, info[5], -1); - vehicle->start_open_t = get_TTimestamp(tuple, tupdesc, info[6], 0); - vehicle->start_close_t = get_TTimestamp(tuple, tupdesc, info[7], INT64_MAX); - vehicle->start_service_t = get_PositiveTInterval(tuple, tupdesc, info[8], 0); - - /* - * end values - */ - vehicle->end_node_id = get_Id(tuple, tupdesc, info[9], vehicle->start_node_id); - vehicle->end_open_t = get_TTimestamp(tuple, tupdesc, info[10], vehicle->start_open_t); - vehicle->end_close_t = get_TTimestamp(tuple, tupdesc, info[11], vehicle->start_close_t); - vehicle->end_service_t = get_PositiveTInterval(tuple, tupdesc, info[12], 0); - - /* - * Ignored values - */ - vehicle->start_x = 0; - vehicle->start_y = 0; - vehicle->end_x = 0; - vehicle->end_y = 0; -} - - -static -void db_get_vehicles( - char *vehicles_sql, - Vehicle_t **vehicles, - size_t *total_vehicles, - - Column_info_t *info, - const int column_count, - - int kind, - bool with_stops) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", vehicles_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(vehicles_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_vehicles) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*vehicles) == NULL) - (*vehicles) = (Vehicle_t *)palloc0( - total_tuples * sizeof(Vehicle_t)); - else - (*vehicles) = (Vehicle_t *)repalloc( - (*vehicles), - total_tuples * sizeof(Vehicle_t)); - - if ((*vehicles) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - switch (kind) { - case 0 : fetch_timestamps(&tuple, &tupdesc, info, - &(*vehicles)[total_tuples - ntuples + t], with_stops); - break; - case 1 : fetch_raw(&tuple, &tupdesc, info, - &(*vehicles)[total_tuples - ntuples + t], with_stops); - break; - case 2 : fetch_euclidean(&tuple, &tupdesc, info, - &(*vehicles)[total_tuples - ntuples + t], with_stops); - break; - } - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_vehicles) = 0; - return; - } - - (*total_vehicles) = total_tuples; -#ifdef PROFILE - time_msg("reading vehicles", start_t, clock()); -#endif -} - -/** - * @param[in] sql SQL query to execute - * @param[in] with_stops do not ignore stops column - * @param[out] rows C Container that holds the data - * @param[out] total_rows Total rows recieved - */ -void -get_vehicles( - char *sql, - Vehicle_t **rows, - size_t *total_rows, - bool with_stops) { - const int column_count = 13; - Column_info_t info[13]; - - for (int i = 0; i < column_count; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = false; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "capacity"; - info[2].name = "number"; - info[3].name = "speed"; - info[4].name = "stops"; - info[5].name = "s_id"; - info[6].name = "s_tw_open"; - info[7].name = "s_tw_close"; - info[8].name = "s_t_service"; - info[9].name = "e_id"; - info[10].name = "e_tw_open"; - info[11].name = "e_tw_close"; - info[12].name = "e_t_service"; - - info[6].eType = TIMESTAMP; - info[7].eType = TIMESTAMP; - info[10].eType = TIMESTAMP; - info[11].eType = TIMESTAMP; - info[8].eType = INTERVAL; - info[12].eType = INTERVAL; - - info[4].eType = ANY_INTEGER_ARRAY; // stops - info[3].eType = ANY_NUMERICAL; // speed - - info[0].strict = true; - info[1].strict = true; - info[5].strict = true; - - db_get_vehicles(sql, rows, total_rows, info, column_count, 0, with_stops); -} - -/** - * @param[in] sql SQL query to execute - * @param[in] with_stops do not ignore stops column - * @param[out] rows C Container that holds the data - * @param[out] total_rows Total rows recieved - */ -void -get_vehicles_raw( - char *sql, - Vehicle_t **rows, - size_t *total_rows, - bool with_stops) { - const int column_count = 13; - Column_info_t info[13]; - - for (int i = 0; i < column_count; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = false; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "capacity"; - info[2].name = "number"; - info[3].name = "speed"; - info[4].name = "stops"; - info[5].name = "s_id"; - info[6].name = "s_open"; - info[7].name = "s_close"; - info[8].name = "s_service"; - info[9].name = "e_id"; - info[10].name = "e_open"; - info[11].name = "e_close"; - info[12].name = "e_service"; - - - info[4].eType = ANY_INTEGER_ARRAY; // stops - info[3].eType = ANY_NUMERICAL; // speed - - info[0].strict = true; - info[1].strict = true; - info[5].strict = true; - - db_get_vehicles(sql, rows, total_rows, info, column_count, 1, with_stops); -} - -/** - * @param[in] sql SQL query to execute - * @param[in] with_stops do not ignore stops column - * @param[out] rows C Container that holds the data - * @param[out] total_rows Total rows recieved - */ -void -get_vehicles_euclidean( - char *sql, - Vehicle_t **rows, - size_t *total_rows, - bool with_stops) { - const int column_count = 15; - Column_info_t info[15]; - - for (int i = 0; i < column_count; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = false; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "capacity"; - info[2].name = "number"; - info[3].name = "speed"; - info[4].name = "s_open"; - info[5].name = "s_close"; - info[6].name = "e_open"; - info[7].name = "e_close"; - info[8].name = "stops"; - info[9].name = "s_service"; - info[10].name = "e_service"; - info[11].name = "s_x"; - info[12].name = "s_y"; - info[13].name = "e_x"; - info[14].name = "e_y"; - - - info[8].eType = ANY_INTEGER_ARRAY; // stops - info[11].eType = ANY_NUMERICAL; // s_x - info[12].eType = ANY_NUMERICAL; // s_y - info[13].eType = ANY_NUMERICAL; // e_x - info[14].eType = ANY_NUMERICAL; // e_y - - info[0].strict = true; - info[1].strict = true; - info[11].strict = true; - info[12].strict = true; - - db_get_vehicles(sql, rows, total_rows, info, column_count, 2, with_stops); -} diff --git a/src/common/vroom/breaks_input.c b/src/common/vroom/breaks_input.c deleted file mode 100644 index 5e0a62619..000000000 --- a/src/common/vroom/breaks_input.c +++ /dev/null @@ -1,167 +0,0 @@ -/*PGR-GNU***************************************************************** -File: breaks_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vroom/breaks_input.h" - -#include "cpp_common/vroom_break_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" - -static -void fetch_breaks( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vroom_break_t *vroom_break, - bool is_plain) { - vroom_break->id = get_Idx(tuple, tupdesc, info[0], 0); - vroom_break->vehicle_id = get_Idx(tuple, tupdesc, info[1], 0); - if (is_plain) { - vroom_break->service = get_Duration(tuple, tupdesc, info[2], 0); - } else { - vroom_break->service = - (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0); - } - vroom_break->data = column_found(info[3].colNumber) - ? spi_getText(tuple, tupdesc, info[3]) - : strdup("{}"); -} - - -static -void db_get_breaks( - char *breaks_sql, - Vroom_break_t **breaks, - size_t *total_breaks, - - Column_info_t *info, - const int column_count, - bool is_plain) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", breaks_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(breaks_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_breaks) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*breaks) == NULL) - (*breaks) = (Vroom_break_t *)palloc0( - total_tuples * sizeof(Vroom_break_t)); - else - (*breaks) = (Vroom_break_t *)repalloc( - (*breaks), - total_tuples * sizeof(Vroom_break_t)); - - if ((*breaks) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - fetch_breaks(&tuple, &tupdesc, info, - &(*breaks)[total_tuples - ntuples + t], is_plain); - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_breaks) = 0; - return; - } - - (*total_breaks) = total_tuples; -#ifdef PROFILE - time_msg("reading breaks", start_t, clock()); -#endif -} - - -void -get_vroom_breaks( - char *sql, - Vroom_break_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 4; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "vehicle_id"; - info[2].name = "service"; - info[3].name = "data"; - - info[2].eType = INTEGER; // service - info[3].eType = JSONB; // data - - if (!is_plain) { - info[2].eType = INTERVAL; // service - } - - /* service and data are not mandatory */ - info[2].strict = false; - info[3].strict = false; - - db_get_breaks(sql, rows, total_rows, info, kColumnCount, is_plain); -} diff --git a/src/common/vroom/jobs_input.c b/src/common/vroom/jobs_input.c deleted file mode 100644 index f23b64f92..000000000 --- a/src/common/vroom/jobs_input.c +++ /dev/null @@ -1,204 +0,0 @@ -/*PGR-GNU***************************************************************** -File: jobs_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vroom/jobs_input.h" - -#include "cpp_common/vroom_job_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" - -static -void fetch_jobs( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vroom_job_t *job, - bool is_plain) { - job->id = get_Idx(tuple, tupdesc, info[0], 0); - job->location_id = get_MatrixIndex(tuple, tupdesc, info[1], 0); - - if (is_plain) { - job->setup = get_Duration(tuple, tupdesc, info[2], 0); - job->service = get_Duration(tuple, tupdesc, info[3], 0); - } else { - job->setup = (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0); - job->service = (Duration)get_PositiveTInterval(tuple, tupdesc, info[3], 0); - } - - /* - * The deliveries - */ - job->delivery_size = 0; - job->delivery = column_found(info[4].colNumber) ? - spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[4], &job->delivery_size) - : NULL; - - /* - * The pickups - */ - job->pickup_size = 0; - job->pickup = column_found(info[5].colNumber) ? - spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[5], &job->pickup_size) - : NULL; - - job->skills_size = 0; - job->skills = column_found(info[6].colNumber) ? - spi_getPositiveIntArr_allowEmpty(tuple, tupdesc, info[6], &job->skills_size) - : NULL; - - job->priority = get_Priority(tuple, tupdesc, info[7], 0); - - job->data = column_found(info[8].colNumber) - ? spi_getText(tuple, tupdesc, info[8]) - : strdup("{}"); -} - - -static -void db_get_jobs( - char *jobs_sql, - Vroom_job_t **jobs, - size_t *total_jobs, - - Column_info_t *info, - const int column_count, - bool is_plain) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", jobs_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(jobs_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_jobs) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*jobs) == NULL) - (*jobs) = (Vroom_job_t *)palloc0( - total_tuples * sizeof(Vroom_job_t)); - else - (*jobs) = (Vroom_job_t *)repalloc( - (*jobs), - total_tuples * sizeof(Vroom_job_t)); - - if ((*jobs) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - fetch_jobs(&tuple, &tupdesc, info, - &(*jobs)[total_tuples - ntuples + t], is_plain); - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_jobs) = 0; - return; - } - - (*total_jobs) = total_tuples; -#ifdef PROFILE - time_msg("reading jobs", start_t, clock()); -#endif -} - - -void -get_vroom_jobs( - char *sql, - Vroom_job_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 9; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = false; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "location_id"; - info[2].name = "setup"; - info[3].name = "service"; - info[4].name = "delivery"; - info[5].name = "pickup"; - info[6].name = "skills"; - info[7].name = "priority"; - info[8].name = "data"; - - info[2].eType = INTEGER; // setup - info[3].eType = INTEGER; // service - info[4].eType = ANY_INTEGER_ARRAY; // delivery - info[5].eType = ANY_INTEGER_ARRAY; // pickup - info[6].eType = INTEGER_ARRAY; // skills - info[7].eType = INTEGER; // priority - info[8].eType = JSONB; // data - - if (!is_plain) { - info[2].eType = INTERVAL; // setup - info[3].eType = INTERVAL; // service - } - - /* Only id and location_id are mandatory */ - info[0].strict = true; - info[1].strict = true; - - db_get_jobs(sql, rows, total_rows, info, kColumnCount, is_plain); -} diff --git a/src/common/vroom/matrix_input.c b/src/common/vroom/matrix_input.c deleted file mode 100644 index 4452ee5f3..000000000 --- a/src/common/vroom/matrix_input.c +++ /dev/null @@ -1,166 +0,0 @@ -/*PGR-GNU***************************************************************** -File: matrix_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vroom/matrix_input.h" - -#include "cpp_common/vroom_matrix_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" - -void fetch_matrix( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vroom_matrix_t *matrix, - bool is_plain) { - matrix->start_id = get_MatrixIndex(tuple, tupdesc, info[0], -1); - matrix->end_id = get_MatrixIndex(tuple, tupdesc, info[1], -1); - - if (is_plain) { - matrix->duration = get_Duration(tuple, tupdesc, info[2], 0); - } else { - matrix->duration = (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0); - } - - // If unspecified, cost is same as the duration - matrix->cost = get_Cost(tuple, tupdesc, info[3], matrix->duration); -} - - -static -void db_get_matrix( - char *matrix_sql, - Vroom_matrix_t **matrix, - size_t *total_matrix_rows, - - Column_info_t *info, - const int column_count, - bool is_plain) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", matrix_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(matrix_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_matrix_rows) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*matrix) == NULL) - (*matrix) = (Vroom_matrix_t *)palloc0( - total_tuples * sizeof(Vroom_matrix_t)); - else - (*matrix) = (Vroom_matrix_t *)repalloc( - (*matrix), - total_tuples * sizeof(Vroom_matrix_t)); - - if ((*matrix) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - fetch_matrix(&tuple, &tupdesc, info, - &(*matrix)[total_tuples - ntuples + t], is_plain); - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_matrix_rows) = 0; - return; - } - - (*total_matrix_rows) = total_tuples; -#ifdef PROFILE - time_msg("reading matrix", start_t, clock()); -#endif -} - - -void -get_vroom_matrix( - char *sql, - Vroom_matrix_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 4; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = ANY_INTEGER; - } - info[0].name = "start_id"; - info[1].name = "end_id"; - info[2].name = "duration"; - info[3].name = "cost"; - - info[2].eType = INTEGER; // duration - info[3].eType = INTEGER; // cost - - if (!is_plain) { - info[2].eType = INTERVAL; // duration - } - - /** - * cost is not mandatory - */ - info[3].strict = false; - - db_get_matrix(sql, rows, total_rows, info, kColumnCount, is_plain); -} diff --git a/src/common/vroom/shipments_input.c b/src/common/vroom/shipments_input.c deleted file mode 100644 index 88e9d90db..000000000 --- a/src/common/vroom/shipments_input.c +++ /dev/null @@ -1,219 +0,0 @@ -/*PGR-GNU***************************************************************** -File: shipments_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vroom/shipments_input.h" - -#include "cpp_common/vroom_shipment_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" - -static -void fetch_shipments( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vroom_shipment_t *shipment, - bool is_plain) { - shipment->id = get_Idx(tuple, tupdesc, info[0], 0); - - shipment->p_location_id = get_MatrixIndex(tuple, tupdesc, info[1], 0); - shipment->d_location_id = get_MatrixIndex(tuple, tupdesc, info[4], 0); - - if (is_plain) { - shipment->p_setup = get_Duration(tuple, tupdesc, info[2], 0); - shipment->p_service = get_Duration(tuple, tupdesc, info[3], 0); - shipment->d_setup = get_Duration(tuple, tupdesc, info[5], 0); - shipment->d_service = get_Duration(tuple, tupdesc, info[6], 0); - } else { - shipment->p_setup = - (Duration)get_PositiveTInterval(tuple, tupdesc, info[2], 0); - shipment->p_service = - (Duration)get_PositiveTInterval(tuple, tupdesc, info[3], 0); - shipment->d_setup = - (Duration)get_PositiveTInterval(tuple, tupdesc, info[5], 0); - shipment->d_service = - (Duration)get_PositiveTInterval(tuple, tupdesc, info[6], 0); - } - - shipment->amount_size = 0; - shipment->amount = column_found(info[7].colNumber) ? - spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[7], &shipment->amount_size) - : NULL; - - shipment->skills_size = 0; - shipment->skills = column_found(info[8].colNumber) ? - spi_getPositiveIntArr_allowEmpty(tuple, tupdesc, info[8], &shipment->skills_size) - : NULL; - - shipment->priority = get_Priority(tuple, tupdesc, info[9], 0); - - shipment->p_data = column_found(info[10].colNumber) - ? spi_getText(tuple, tupdesc, info[10]) - : strdup("{}"); - shipment->d_data = column_found(info[11].colNumber) - ? spi_getText(tuple, tupdesc, info[11]) - : strdup("{}"); -} - - -static -void db_get_shipments( - char *shipments_sql, - Vroom_shipment_t **shipments, - size_t *total_shipments, - - Column_info_t *info, - const int column_count, - bool is_plain) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", shipments_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(shipments_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_shipments) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*shipments) == NULL) - (*shipments) = (Vroom_shipment_t *)palloc0( - total_tuples * sizeof(Vroom_shipment_t)); - else - (*shipments) = (Vroom_shipment_t *)repalloc( - (*shipments), - total_tuples * sizeof(Vroom_shipment_t)); - - if ((*shipments) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - fetch_shipments(&tuple, &tupdesc, info, - &(*shipments)[total_tuples - ntuples + t], is_plain); - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_shipments) = 0; - return; - } - - (*total_shipments) = total_tuples; -#ifdef PROFILE - time_msg("reading shipments", start_t, clock()); -#endif -} - - -void -get_vroom_shipments( - char *sql, - Vroom_shipment_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 12; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = false; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - - /* pickup shipments */ - info[1].name = "p_location_id"; - info[2].name = "p_setup"; - info[3].name = "p_service"; - - /* delivery shipments */ - info[4].name = "d_location_id"; - info[5].name = "d_setup"; - info[6].name = "d_service"; - - info[7].name = "amount"; - info[8].name = "skills"; - info[9].name = "priority"; - info[10].name = "p_data"; - info[11].name = "d_data"; - - info[2].eType = INTEGER; // p_setup - info[3].eType = INTEGER; // p_service - info[5].eType = INTEGER; // d_setup - info[6].eType = INTEGER; // d_service - info[7].eType = ANY_INTEGER_ARRAY; // amount - info[8].eType = INTEGER_ARRAY; // skills - info[9].eType = INTEGER; // priority - info[10].eType = JSONB; // p_data - info[11].eType = JSONB; // d_data - - if (!is_plain) { - info[2].eType = INTERVAL; // p_setup - info[3].eType = INTERVAL; // p_service - info[5].eType = INTERVAL; // d_setup - info[6].eType = INTERVAL; // d_service - } - - /* id and location_id of pickup and delivery are mandatory */ - info[0].strict = true; - info[1].strict = true; - info[4].strict = true; - - db_get_shipments(sql, rows, total_rows, info, kColumnCount, is_plain); -} diff --git a/src/common/vroom/time_windows_input.c b/src/common/vroom/time_windows_input.c deleted file mode 100644 index 3d6ece02a..000000000 --- a/src/common/vroom/time_windows_input.c +++ /dev/null @@ -1,225 +0,0 @@ -/*PGR-GNU***************************************************************** -File: time_windows_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vroom/time_windows_input.h" - -#include "cpp_common/vroom_time_window_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" - -static -void fetch_time_windows( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vroom_time_window_t *time_window, - bool is_shipment, - bool is_plain) { - - time_window->id = get_Idx(tuple, tupdesc, info[0], 0); - - if (is_shipment) { - char kind = get_Kind(tuple, tupdesc, info[1], ' '); - if (kind != 'p' && kind != 'd') { - ereport(ERROR, (errmsg("Invalid kind %c", kind), - errhint("Kind must be either 'p' or 'd'"))); - } - time_window->kind = kind; - if (is_plain) { - time_window->tw_open = get_Duration(tuple, tupdesc, info[2], 0); - time_window->tw_close = get_Duration(tuple, tupdesc, info[3], 0); - } else { - time_window->tw_open = - (Duration)get_PositiveTTimestamp(tuple, tupdesc, info[2], 0); - time_window->tw_close = - (Duration)get_PositiveTTimestamp(tuple, tupdesc, info[3], 0); - } - } else { - if (is_plain) { - time_window->tw_open = get_Duration(tuple, tupdesc, info[1], 0); - time_window->tw_close = get_Duration(tuple, tupdesc, info[2], 0); - } else { - time_window->tw_open = - (Duration)get_PositiveTTimestamp(tuple, tupdesc, info[1], 0); - time_window->tw_close = - (Duration)get_PositiveTTimestamp(tuple, tupdesc, info[2], 0); - } - } - - if (time_window->tw_open > time_window->tw_close) { - ereport(ERROR, - (errmsg("Invalid time window (%d, %d)", - time_window->tw_open, time_window->tw_close), - errhint("Time window start time %d must be " - "less than or equal to time window end time %d", - time_window->tw_open, time_window->tw_close))); - } -} - - -static -void db_get_time_windows( - char *time_windows_sql, - Vroom_time_window_t **time_windows, - size_t *total_time_windows, - - Column_info_t *info, - const int column_count, - bool is_shipment, - bool is_plain) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", time_windows_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(time_windows_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_time_windows) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*time_windows) == NULL) - (*time_windows) = (Vroom_time_window_t *)palloc0( - total_tuples * sizeof(Vroom_time_window_t)); - else - (*time_windows) = (Vroom_time_window_t *)repalloc( - (*time_windows), - total_tuples * sizeof(Vroom_time_window_t)); - - if ((*time_windows) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - fetch_time_windows(&tuple, &tupdesc, info, - &(*time_windows)[total_tuples - ntuples + t], - is_shipment, is_plain); - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_time_windows) = 0; - return; - } - - (*total_time_windows) = total_tuples; -#ifdef PROFILE - time_msg("reading time windows", start_t, clock()); -#endif -} - - -void -get_vroom_time_windows( - char *sql, - Vroom_time_window_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 3; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = INTEGER; - } - - info[0].name = "id"; - info[1].name = "tw_open"; - info[2].name = "tw_close"; - - info[0].eType = ANY_INTEGER; // id - - if (!is_plain) { - info[1].eType = TIMESTAMP; // tw_open - info[2].eType = TIMESTAMP; // tw_close - } - - db_get_time_windows(sql, rows, total_rows, info, kColumnCount, 0, is_plain); -} - -void -get_vroom_shipments_time_windows( - char *sql, - Vroom_time_window_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 4; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = true; - info[i].eType = INTEGER; - } - - info[0].name = "id"; - info[1].name = "kind"; - info[2].name = "tw_open"; - info[3].name = "tw_close"; - - info[0].eType = ANY_INTEGER; // id - info[1].eType = CHAR1; // kind - - if (!is_plain) { - info[2].eType = TIMESTAMP; // tw_open - info[3].eType = TIMESTAMP; // tw_close - } - - db_get_time_windows(sql, rows, total_rows, info, kColumnCount, 1, is_plain); -} diff --git a/src/common/vroom/vehicles_input.c b/src/common/vroom/vehicles_input.c deleted file mode 100644 index d406fda49..000000000 --- a/src/common/vroom/vehicles_input.c +++ /dev/null @@ -1,225 +0,0 @@ -/*PGR-GNU***************************************************************** -File: vehicles_input.c - -Copyright (c) 2021 pgRouting developers -Mail: project@pgrouting.org - -Function's developer: -Copyright (c) 2021 Ashish Kumar -Mail: ashishkr23438@gmail.com - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include "c_common/vroom/vehicles_input.h" - -#include "cpp_common/vroom_vehicle_t.hpp" -#include "cpp_common/info.hpp" - -#include "c_common/debug_macro.h" -#include "cpp_common/get_check_data.hpp" - -static -void fetch_vehicles( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t *info, - Vroom_vehicle_t *vehicle, - bool is_plain) { - vehicle->id = get_Idx(tuple, tupdesc, info[0], 0); - vehicle->start_id = get_MatrixIndex(tuple, tupdesc, info[1], -1); - vehicle->end_id = get_MatrixIndex(tuple, tupdesc, info[2], -1); - - vehicle->capacity_size = 0; - vehicle->capacity = column_found(info[3].colNumber) ? - spi_getPositiveBigIntArr_allowEmpty(tuple, tupdesc, info[3], &vehicle->capacity_size) - : NULL; - - vehicle->skills_size = 0; - vehicle->skills = column_found(info[4].colNumber) ? - spi_getPositiveIntArr_allowEmpty(tuple, tupdesc, info[4], &vehicle->skills_size) - : NULL; - - if (is_plain) { - vehicle->tw_open = get_Duration(tuple, tupdesc, info[5], 0); - vehicle->tw_close = get_Duration(tuple, tupdesc, info[6], UINT_MAX); - } else { - vehicle->tw_open = - (Duration)get_PositiveTTimestamp(tuple, tupdesc, info[5], 0); - vehicle->tw_close = - (Duration)get_PositiveTTimestamp(tuple, tupdesc, info[6], UINT_MAX); - } - - if (vehicle->tw_open > vehicle->tw_close) { - ereport(ERROR, - (errmsg("Invalid time window (%d, %d)", - vehicle->tw_open, vehicle->tw_close), - errhint("Time window start time %d must be " - "less than or equal to time window end time %d", - vehicle->tw_open, vehicle->tw_close))); - } - - vehicle->speed_factor = column_found(info[7].colNumber) ? - spi_getFloat8(tuple, tupdesc, info[7]) - : 1.0; - - if (vehicle->speed_factor <= 0.0) { - ereport(ERROR, (errmsg("Invalid speed_factor %lf", vehicle->speed_factor), - errhint("Speed factor must be greater than 0"))); - } - - vehicle->max_tasks = column_found(info[8].colNumber) - ? spi_getMaxTasks(tuple, tupdesc, info[8]) - : INT_MAX; // 2147483647 - - vehicle->data = column_found(info[9].colNumber) - ? spi_getText(tuple, tupdesc, info[9]) - : strdup("{}"); -} - - -static -void db_get_vehicles( - char *vehicles_sql, - Vroom_vehicle_t **vehicles, - size_t *total_vehicles, - - Column_info_t *info, - const int column_count, - bool is_plain) { -#ifdef PROFILE - clock_t start_t = clock(); - PGR_DBG("%s", vehicles_sql); -#endif - - const int tuple_limit = 1000000; - - size_t total_tuples; - - void *SPIplan; - SPIplan = vrp_SPI_prepare(vehicles_sql); - Portal SPIportal; - SPIportal = vrp_SPI_cursor_open(SPIplan); - - bool moredata = true; - (*total_vehicles) = total_tuples = 0; - - /* on the first tuple get the column numbers */ - - while (moredata == true) { - SPI_cursor_fetch(SPIportal, true, tuple_limit); - if (total_tuples == 0) { - /* Atleast one out of start_id or end_id must be present */ - info[1].colNumber = SPI_fnumber(SPI_tuptable->tupdesc, "start_id"); - info[2].colNumber = SPI_fnumber(SPI_tuptable->tupdesc, "end_id"); - if (!column_found(info[1].colNumber) && !column_found(info[2].colNumber)) { - elog(ERROR, "At least one out of start_id or end_id must be present"); - } - pgr_fetch_column_info(info, column_count); - } - size_t ntuples = SPI_processed; - total_tuples += ntuples; - if (ntuples > 0) { - if ((*vehicles) == NULL) - (*vehicles) = (Vroom_vehicle_t *)palloc0( - total_tuples * sizeof(Vroom_vehicle_t)); - else - (*vehicles) = (Vroom_vehicle_t *)repalloc( - (*vehicles), - total_tuples * sizeof(Vroom_vehicle_t)); - - if ((*vehicles) == NULL) { - elog(ERROR, "Out of memory"); - } - - size_t t; - SPITupleTable *tuptable = SPI_tuptable; - TupleDesc tupdesc = SPI_tuptable->tupdesc; - for (t = 0; t < ntuples; t++) { - HeapTuple tuple = tuptable->vals[t]; - fetch_vehicles(&tuple, &tupdesc, info, - &(*vehicles)[total_tuples - ntuples + t], is_plain); - } - SPI_freetuptable(tuptable); - } else { - moredata = false; - } - } - - SPI_cursor_close(SPIportal); - - if (total_tuples == 0) { - (*total_vehicles) = 0; - return; - } - - (*total_vehicles) = total_tuples; -#ifdef PROFILE - time_msg("reading vehicles", start_t, clock()); -#endif -} - - -void -get_vroom_vehicles( - char *sql, - Vroom_vehicle_t **rows, - size_t *total_rows, - bool is_plain) { - int kColumnCount = 10; - Column_info_t info[kColumnCount]; - - for (int i = 0; i < kColumnCount; ++i) { - info[i].colNumber = -1; - info[i].type = 0; - info[i].strict = false; - info[i].eType = ANY_INTEGER; - } - - info[0].name = "id"; - info[1].name = "start_id"; - info[2].name = "end_id"; - info[3].name = "capacity"; - info[4].name = "skills"; - info[5].name = "tw_open"; - info[6].name = "tw_close"; - info[7].name = "speed_factor"; - info[8].name = "max_tasks"; - info[9].name = "data"; - - info[3].eType = ANY_INTEGER_ARRAY; // capacity - info[4].eType = INTEGER_ARRAY; // skills - info[5].eType = INTEGER; // tw_open - info[6].eType = INTEGER; // tw_close - info[7].eType = ANY_NUMERICAL; // speed_factor - info[8].eType = INTEGER; // max_tasks - info[9].eType = JSONB; // data - - if (!is_plain) { - info[5].eType = TIMESTAMP; // tw_open - info[6].eType = TIMESTAMP; // tw_close - } - - /** - * id is mandatory. - * At least one out of start_id or end_id must be present, but that is checked later. - */ - info[0].strict = true; - - db_get_vehicles(sql, rows, total_rows, info, kColumnCount, is_plain); -} diff --git a/src/cpp_common/get_check_data.c b/src/cpp_common/get_check_data.c deleted file mode 100644 index 2596048f5..000000000 --- a/src/cpp_common/get_check_data.c +++ /dev/null @@ -1,946 +0,0 @@ -/*PGR-GNU***************************************************************** -File: get_check_data.c - -Copyright (c) 2015 pgRouting developers -Mail: project@pgrouting.org - -Developer: -Copyright (c) 2015 Celia Virginia Vergara Castillo - ------- - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - ********************************************************************PGR-GNU*/ - -#include -#include -#include "c_common/postgres_connection.h" -#include // NOLINT [build/include_order] -#include // NOLINT [build/include_order] - -#include "cpp_common/get_check_data.hpp" -#include "c_common/arrays_input.h" - -#include "catalog/pg_type.h" - -#include "c_common/debug_macro.h" -#include "c_types/typedefs.h" - -static -void -check_interval_type(Column_info_t info) { - if (!(info.type == 1186)) { - elog(ERROR, - "Unexpected Column '%s' type. Expected INTERVAL", - info.name); - } -} - -/* - * [BPCHAROID](https://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html#afa7749dbe36d31874205189d9d6b21d7) - * [INT2ARRAYOID](https://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html#ac265fe7b0bb75fead13b16bf072722e9) - */ -static -void -check_char_type(Column_info_t info) { - if (!(info.type == BPCHAROID)) { - elog(ERROR, "Unexpected Column '%s' type. Expected CHAR", info.name); - } -} - -static -void -check_text_type(Column_info_t info) { - if (!(info.type == TEXTOID)) { - elog(ERROR, "Unexpected Column '%s' type. Expected TEXT", info.name); - } -} - -static -void -check_jsonb_type(Column_info_t info) { - if (!(info.type == JSONBOID)) { - elog(ERROR, "Unexpected Column '%s' type. Expected JSONB %ld", info.name, info.type); - } -} - -static -void -check_integer_type(Column_info_t info) { - if (!(info.type == INT2OID || info.type == INT4OID)) { - ereport(ERROR, - (errmsg_internal("Unexpected type in column '%s'.", info.name), - errhint("Expected SMALLINT or INTEGER"))); - } -} - -static -void -check_any_integer_type(Column_info_t info) { - if (!(info.type == INT2OID - || info.type == INT4OID - || info.type == INT8OID)) { - ereport(ERROR, - (errmsg_internal("Unexpected type in column '%s'.", info.name), - errhint("Expected ANY-INTEGER"))); - } -} - -static -void -check_integerarray_type(Column_info_t info) { - if (!(info.type == INT2ARRAYOID - || info.type == INT4ARRAYOID)) { - elog(ERROR, - "Unexpected Column '%s' type. Expected SMALLINT-ARRAY or INTEGER-ARRAY", - info.name); - } -} - -static -void -check_any_integerarray_type(Column_info_t info) { - if (!(info.type == INT2ARRAYOID - || info.type == INT4ARRAYOID - || info.type == 1016)) { - elog(ERROR, - "Unexpected Column '%s' type. Expected ANY-INTEGER-ARRAY", - info.name); - } -} - -static -void -check_any_numerical_type(Column_info_t info) { - if (!(info.type == INT2OID - || info.type == INT4OID - || info.type == INT8OID - || info.type == FLOAT4OID - || info.type == FLOAT8OID - || info.type == NUMERICOID)) { - ereport(ERROR, - (errmsg_internal("Unexpected type in column '%s'.", info.name), - errhint("Found: %lu\nExpected ANY-NUMERICAL", info.type))); - } -} - -static -void -check_timestamp_type(Column_info_t info) { - if (!(info.type == 1114)) { - elog(ERROR, - "Unexpected Column '%s' type. Expected TIMESTAMP", - info.name); - } -} - -static -bool -fetch_column_info( - Column_info_t *info) { - /* - * [SPI_fnumber](https://www.postgresql.org/docs/12/spi-spi-fnumber.html) - */ - info->colNumber = SPI_fnumber(SPI_tuptable->tupdesc, info->name); - if (info->strict && !column_found(info->colNumber)) { - elog(ERROR, "Column '%s' not Found", info->name); - } - - if (column_found(info->colNumber)) { - /* - * [SPI_gettypeid](https://www.postgresql.org/docs/12/spi-spi-gettypeid.html) - */ - (info->type) = SPI_gettypeid(SPI_tuptable->tupdesc, (info->colNumber)); - PGR_DBG("%s %ld", info->name, info->type); - if (SPI_result == SPI_ERROR_NOATTRIBUTE) { - elog(ERROR, "Type of column '%s' not Found", info->name); - } - return true; - } - return false; -} - -static -int32_t -spi_getInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) { - Datum binval; - bool isnull; - int32_t value = 0; - binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull); - - if (isnull) elog(ERROR, "Unexpected Null value in column %s", info.name); - - switch (info.type) { - case INT2OID: - value = (int32_t) DatumGetInt16(binval); - break; - case INT4OID: - value = (int32_t) DatumGetInt32(binval); - break; - default: - ereport(ERROR, - (errmsg_internal("Unexpected type in column '%s'.", info.name), - errhint("Found: %lu\nExpected INTEGER", info.type))); - } - return value; -} - -static -int64_t -spi_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) { - Datum binval; - bool isnull; - int64_t value = 0; - binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull); - - if (isnull) elog(ERROR, "Unexpected Null value in column %s", info.name); - - switch (info.type) { - case INT2OID: - value = (int64_t) DatumGetInt16(binval); - break; - case INT4OID: - value = (int64_t) DatumGetInt32(binval); - break; - case INT8OID: - value = DatumGetInt64(binval); - break; - default: - ereport(ERROR, - (errmsg_internal("Unexpected type in column '%s'.", info.name), - errhint("Found: %lu\nExpected ANY-INTEGER", info.type))); - } - return value; -} - -static -TTimestamp -spi_getTimeStamp(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) { - Datum binval; - bool isnull; - TTimestamp value = 0; - binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull); - - if (isnull) elog(ERROR, "Unexpected Null value in column %s", info.name); - - switch (info.type) { - case 1114: - value = timestamp_without_timezone((TTimestamp) Int64GetDatum(binval)); - break; - default: - elog(ERROR, - "[SPI_getTimeStamp] Unexpected type in column %s. found %ld expected 1114", - info.name, info.type); - } - return value; -} - -static -TInterval -spi_getInterval(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) { - Datum binval; - bool isnull; - Interval* interval; - - binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull); - - if (isnull) elog(ERROR, "Unexpected Null value in column %s", info.name); - - switch (info.type) { - case INTERVALOID: - interval = DatumGetIntervalP(binval); - break; - default: - elog(ERROR, - "[spi_getInterval] Unexpected type in column %s. found %ld expected %d", - info.name, info.type, INTERVALOID); - } - PGR_DBG("time %ld secs %ld", interval->time, - interval->time / 1000000 - + interval->day * SECS_PER_DAY - + (int64_t)(interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * SECS_PER_DAY))); - - return interval->time / 1000000 - + interval->day * SECS_PER_DAY - + (int64_t)(interval->month * ((DAYS_PER_YEAR / (double) MONTHS_PER_YEAR) * SECS_PER_DAY)); -} - -/* - @param[in] tuple input row to be examined. - @param[in] tupdesc input row description. - @param[in] info contain column information. - @param[in] default_value returned when column contain NULL value. - - @throw ERROR Unexpected Column type. Expected column type is CHAR. - @throw ERROR When value of column is NULL. - - @return Char type of column value is returned. - - * http://doxygen.postgresql.org/include_2catalog_2pg__type_8h.html; - * [SPI_getbinval](https://www.postgresql.org/docs/8.1/static/spi-spi-getbinval.html) - * [Datum](https://doxygen.postgresql.org/datum_8h.html) - * [DatumGetInt16](https://doxygen.postgresql.org/postgres_8h.html#aec991e04209850f29a8a63df0c78ba2d) - */ -char -spi_getChar( - HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, char default_value) { - Datum binval; - bool isNull; - char value = default_value; - - binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isNull); - if (!(info.type == BPCHAROID)) { - elog(ERROR, "Unexpected Column type of %s. Expected CHAR", info.name); - } - if (!isNull) { - value = ((char*)binval)[1]; - } else { - if (info.strict) { - elog(ERROR, "Unexpected Null value in column %s", info.name); - } - value = default_value; - } - return value; -} - -int32_t -spi_getMaxTasks( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info) { - int32_t value = spi_getInt(tuple, tupdesc, info); - if (value < 0) { - ereport( - ERROR, - (errmsg("Invalid max_tasks value %d", value), - errhint( - "Maximum number of tasks must be greater than or equal to 0"))); - } - return value; -} - -int64_t* -spi_getBigIntArr( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size) { - bool is_null = false; - - Datum raw_array = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &is_null); - /* - * [DatumGetArrayTypeP](https://doxygen.postgresql.org/array_8h.html#aa1b8e77c103863862e06a7b7c07ec532) - * [pgr_get_bigIntArray](http://docs.pgrouting.org/doxy/2.2/arrays__input_8c_source.html) - */ - ArrayType *pg_array = DatumGetArrayTypeP(raw_array); - - return pgr_get_bigIntArray((size_t*)the_size, pg_array); -} - -int64_t* -spi_getBigIntArr_allowEmpty( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size) { - bool is_null = false; - - Datum raw_array = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &is_null); - /* - * [DatumGetArrayTypeP](https://doxygen.postgresql.org/array_8h.html#aa1b8e77c103863862e06a7b7c07ec532) - * [pgr_get_bigIntArray](http://docs.pgrouting.org/doxy/2.2/arrays__input_8c_source.html) - */ - if (!raw_array) { - *the_size = 0; - return NULL; - } - - ArrayType *pg_array = DatumGetArrayTypeP(raw_array); - - return pgr_get_bigIntArray_allowEmpty(the_size, pg_array); -} - -int64_t* -spi_getPositiveBigIntArr_allowEmpty( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size) { - int64_t *array = spi_getBigIntArr_allowEmpty(tuple, tupdesc, info, the_size); - for (size_t i = 0; i < *the_size; i++) { - if (array[i] < 0) { - elog(ERROR, "Unexpected Negative value %ld in array", array[i]); - } - } - return array; -} - - -uint32_t* -spi_getPositiveIntArr_allowEmpty( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info, - size_t *the_size) { - bool is_null = false; - - Datum raw_array = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &is_null); - if (!raw_array) { - *the_size = 0; - return NULL; - } - - ArrayType *pg_array = DatumGetArrayTypeP(raw_array); - - return pgr_get_positiveIntArray_allowEmpty(the_size, pg_array); -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -TTimestamp -get_TTimestamp_plain(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TTimestamp opt_value) { - return column_found(info.colNumber)? - (TTimestamp)spi_getBigInt(tuple, tupdesc, info) - : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for positive values only - */ -TTimestamp -get_PositiveTTimestamp_plain(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TTimestamp opt_value) { - TTimestamp value = get_TTimestamp_plain(tuple, tupdesc, info, opt_value); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -TTimestamp -get_TTimestamp(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TTimestamp opt_value) { - return column_found(info.colNumber)? - spi_getTimeStamp(tuple, tupdesc, info) - : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for positive values only - */ -TTimestamp -get_PositiveTTimestamp(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TTimestamp opt_value) { - TTimestamp value = get_TTimestamp(tuple, tupdesc, info, opt_value); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -TInterval -get_TInterval(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TInterval opt_value) { - return column_found(info.colNumber)? - (TInterval)spi_getInterval(tuple, tupdesc, info) - : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for positive values only - */ -TInterval -get_PositiveTInterval(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TInterval opt_value) { - TInterval value = get_TInterval(tuple, tupdesc, info, opt_value); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return (TInterval) value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -TInterval -get_TInterval_plain(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TInterval opt_value) { - return column_found(info.colNumber)? - (TInterval)spi_getBigInt(tuple, tupdesc, info) - : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for positive values only - */ -TInterval -get_PositiveTInterval_plain(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TInterval opt_value) { - TInterval value = get_TInterval_plain(tuple, tupdesc, info, opt_value); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return (TInterval) value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -Id -get_Id(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Id opt_value) { - return column_found(info.colNumber)? - (Id)spi_getBigInt(tuple, tupdesc, info) - : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is not positive - * @pre for positive values only - */ -Idx -get_Idx(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Idx opt_value) { - Id value = get_Id(tuple, tupdesc, info, 0); - if (value <= 0) elog(ERROR, "Unexpected Negative value or Zero in column %s", info.name); - return column_found(info.colNumber)? (Idx) value : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -StepType get_StepType(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, - StepType opt_value) { - StepType step_type = column_found(info.colNumber) - ? spi_getInt(tuple, tupdesc, info) - : opt_value; - StepType min_value = 1; - StepType max_value = 6; - if (step_type < min_value || step_type > max_value) { - elog(ERROR, "Step value should lie between %d and %d", min_value, max_value); - } - return step_type; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - */ -Amount -get_Amount(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Amount opt_value) { - return (Amount) column_found(info.colNumber)? - spi_getBigInt(tuple, tupdesc, info) - : opt_value; -} - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for non-negative values only - */ -PAmount -get_PositiveAmount(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, PAmount opt_value) { - Amount value = get_Amount(tuple, tupdesc, info, 0); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return column_found(info.colNumber)? (PAmount) value : opt_value; -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is not positive - * @pre for positive values only - */ -MatrixIndex -get_MatrixIndex(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, MatrixIndex opt_value) { - if (column_found(info.colNumber)) { - int64_t value = spi_getBigInt(tuple, tupdesc, info); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return (MatrixIndex) value; - } - return opt_value; -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for non-negative values only - */ -Duration -get_Duration(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Duration opt_value) { - if (column_found(info.colNumber)) { - int32_t value = spi_getInt(tuple, tupdesc, info); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return (Duration) value; - } - return opt_value; -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for non-negative values only - */ -TravelCost -get_Cost(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, TravelCost opt_value) { - if (column_found(info.colNumber)) { - int32_t value = spi_getInt(tuple, tupdesc, info); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return (TravelCost)value; - } - return opt_value; -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - */ -char -get_Kind(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, char opt_value) { - if (column_found(info.colNumber)) { - char value = spi_getChar(tuple, tupdesc, info, opt_value); - return value; - } - return opt_value; -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for non-negative values only - */ -Priority -get_Priority(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Priority opt_value) { - if (column_found(info.colNumber)) { - int32_t value = spi_getInt(tuple, tupdesc, info); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - if (value > 100) elog(ERROR, "Priority exceeds the max priority 100"); - return (Priority) value; - } - return opt_value; -} - - -/** - * @param[in] tuple - * @param[in] tupdesc - * @param[in] info about the column been fetched - * @param[in] opt_value default value when the column does not exist - * - * @returns The value found - * @returns opt_value when the column does not exist - * - * exceptions when the value is negative - * @pre for non-negative values only - */ -Distance -get_Distance(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Distance opt_value) { - if (column_found(info.colNumber)) { - int32_t value = spi_getInt(tuple, tupdesc, info); - if (value < 0) elog(ERROR, "Unexpected Negative value in column %s", info.name); - return (Distance) value; - } - return opt_value; -} - - -double -spi_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) { - Datum binval; - bool isnull = false; - double value = 0.0; - binval = SPI_getbinval(*tuple, *tupdesc, info.colNumber, &isnull); - if (isnull) - elog(ERROR, "Unexpected Null value in column %s", info.name); - - switch (info.type) { - case INT2OID: - value = (double) DatumGetInt16(binval); - break; - case INT4OID: - value = (double) DatumGetInt32(binval); - break; - case INT8OID: - value = (double) DatumGetInt64(binval); - break; - case FLOAT4OID: - value = (double) DatumGetFloat4(binval); - break; - case FLOAT8OID: - value = DatumGetFloat8(binval); - break; - case NUMERICOID: - /* Note: out-of-range values will be clamped to +-HUGE_VAL */ - value = (double) DatumGetFloat8(DirectFunctionCall1(numeric_float8_no_overflow, binval)); - break; - default: - ereport(ERROR, - (errmsg_internal("Unexpected type in column '%s'.", info.name), - errhint("Found: %lu\nExpected ANY-NUMERICAL", info.type))); - } - return value; -} - -Coordinate -spi_getCoordinate(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, Coordinate opt_value) { - return column_found(info.colNumber)? - (Coordinate) spi_getFloat8(tuple, tupdesc, info) - : opt_value; -} - -/** - * under development - */ -/* - * [DatumGetCString](https://doxygen.postgresql.org/postgres_8h.html#ae401c8476d1a12b420e3061823a206a7) - */ -char* -spi_getText(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info) { - char *val = DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info.colNumber)); - return val; -} - -/** - * Steps: - * 1) Similar to: https://doxygen.postgresql.org/backend_2utils_2adt_2timestamp_8c.html#a52973f03ed8296b632d4028121f7e077 - * 2) Using time.h to convert - * - * from time.h - * struct tm - * timezone - */ -TTimestamp -timestamp_without_timezone(TTimestamp timestamp) { - /* - * step 1 - */ - Timestamp date; - Timestamp time = timestamp; - TMODULO(time, date, USECS_PER_DAY); - if (time < INT64CONST(0)) { - time += USECS_PER_DAY; - date -= 1; - } - date += POSTGRES_EPOCH_JDATE; - /* Julian day routine does not work for negative Julian days */ - if (date < 0 || date > (Timestamp) INT_MAX) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Julian day routine does not work for negative Julian days"))); - } - - /* - * using structure from time.h to store values - */ - struct tm info; - fsec_t fsec; - - /* - * calling postgres functions - */ - j2date((int) date, &info.tm_year, &info.tm_mon, &info.tm_mday); - dt2time(time, &info.tm_hour, &info.tm_min, &info.tm_sec, &fsec); - - /* - * adjust values before calling mktime - */ - info.tm_isdst = -1; - info.tm_year = info.tm_year - 1900; - info.tm_mon = info.tm_mon - 1; - - /* - * mktime & timezone are defined in time.h - */ - return mktime(&info) - timezone; -} - - -/* - * @param[in] colNumber Column number (count starts at 1). - - * [SPI_ERROR_NOATTRIBUTE](https://doxygen.postgresql.org/spi_8h.html#ac1512d8aaa23c2d57bb0d1eb8f453ee2) - * @return @b TRUE when colNumber exist. - * @b FALSE when colNumber was not found. -*/ -bool -column_found(int colNumber) { - return !(colNumber == SPI_ERROR_NOATTRIBUTE); -} - - -void pgr_fetch_column_info( - Column_info_t info[], - int info_size) { - for (int i = 0; i < info_size; ++i) { - if (fetch_column_info(&info[i])) { - switch (info[i].eType) { - case INTEGER: - check_integer_type(info[i]); - break; - case ANY_INTEGER: - check_any_integer_type(info[i]); - break; - case ANY_NUMERICAL: - check_any_numerical_type(info[i]); - break; - case TEXT: - check_text_type(info[i]); - break; - case JSONB: - check_jsonb_type(info[i]); - break; - case CHAR1: - check_char_type(info[i]); - break; - case INTEGER_ARRAY: - check_integerarray_type(info[i]); - break; - case ANY_INTEGER_ARRAY: - check_any_integerarray_type(info[i]); - break; - case TIMESTAMP: - check_timestamp_type(info[i]); - break; - case INTERVAL: - check_interval_type(info[i]); - break; - default: - elog(ERROR, "Unknown type of column %s", info[i].name); - } - } - } -} From 65d1be200f44e588c9b16086ba3e229722a5e060 Mon Sep 17 00:00:00 2001 From: cvvergara Date: Fri, 13 Sep 2024 10:17:12 -0600 Subject: [PATCH 19/20] updating release notes --- NEWS.md | 2 +- doc/general/release_notes.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 19c64c608..833a8f668 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,7 +16,7 @@ on Github. * Moved sphinx doc from .c files .rst files * Removing prefix `pgr_` & addding `namespace vrprouting` * Separating implementation from header vroom.hpp - +* Queries are read on the cpp code. **Documentation queries** diff --git a/doc/general/release_notes.rst b/doc/general/release_notes.rst index 2baa4efa7..9fbcd304a 100644 --- a/doc/general/release_notes.rst +++ b/doc/general/release_notes.rst @@ -51,7 +51,7 @@ on Github. * Moved sphinx doc from .c files .rst files * Removing prefix `pgr_` & addding `namespace vrprouting` * Separating implementation from header vroom.hpp - +* Queries are read on the cpp code. .. rubric:: Documentation queries From 1096d2c519648b08bf54ebeef89032fc319c0124 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:19:51 +0000 Subject: [PATCH 20/20] Update locale: commit for hash 65d1be20 --- locale/en/LC_MESSAGES/release_notes.po | 6 +++++- locale/pot/release_notes.pot | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/locale/en/LC_MESSAGES/release_notes.po b/locale/en/LC_MESSAGES/release_notes.po index 52006f43f..03e0391df 100644 --- a/locale/en/LC_MESSAGES/release_notes.po +++ b/locale/en/LC_MESSAGES/release_notes.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: vrpRouting v0.0.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-10 18:38+0000\n" +"POT-Creation-Date: 2024-09-13 16:19+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -93,6 +93,10 @@ msgstr "" msgid "Separating implementation from header vroom.hpp" msgstr "" +#: ../../build/doc/release_notes.rst:54 +msgid "Queries are read on the cpp code." +msgstr "" + #: ../../build/doc/release_notes.rst:57 msgid "Documentation queries" msgstr "" diff --git a/locale/pot/release_notes.pot b/locale/pot/release_notes.pot index 3f311f15d..d5220c515 100644 --- a/locale/pot/release_notes.pot +++ b/locale/pot/release_notes.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: vrpRouting v0.4.2\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-08-10 18:38+0000\n" +"POT-Creation-Date: 2024-09-13 16:19+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -80,6 +80,10 @@ msgstr "" msgid "Separating implementation from header vroom.hpp" msgstr "" +#: ../../build/doc/release_notes.rst:54 +msgid "Queries are read on the cpp code." +msgstr "" + #: ../../build/doc/release_notes.rst:57 msgid "Documentation queries" msgstr ""