vrpRouting  0.3
compatibleVehicles.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: compatibleVehicles.c
3 
4 Copyright (c) 2015 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2015 Celia Virginia Vergara Castillo
9 
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26  ********************************************************************PGR-GNU*/
27 
28 #include <stdbool.h>
30 #include "c_common/debug_macro.h"
31 #include "c_common/e_report.h"
32 #include "c_common/time_msg.h"
33 #include "c_common/orders_input.h"
40 
41 PGDLLEXPORT Datum
42 _vrp_compatiblevehicles(PG_FUNCTION_ARGS);
44 
45 
46 static
47 void
49  char* pd_orders_sql,
50  char* vehicles_sql,
51  char* matrix_sql,
52  char* multipliers_sql,
53  double factor,
54  bool use_timestamps,
55 
56  CompatibleVehicles_rt **result_tuples,
57  size_t *result_count) {
58  if (factor <= 0) {
59  ereport(ERROR,
60  (errcode(ERRCODE_INTERNAL_ERROR),
61  errmsg("Illegal value in parameter: factor"),
62  errhint("Value found: %f <= 0", factor)));
63  }
64 
66 
67  PickDeliveryOrders_t *pd_orders_arr = NULL;
68  size_t total_pd_orders = 0;
69 
70  if (use_timestamps) {
71  get_shipments(pd_orders_sql, &pd_orders_arr, &total_pd_orders);
72  } else {
73  get_shipments_raw(pd_orders_sql, &pd_orders_arr, &total_pd_orders);
74  }
75 
76  if (total_pd_orders == 0) {
77  (*result_count) = 0;
78  (*result_tuples) = NULL;
79 
80  /* freeing memory before return */
81  if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;}
82 
84  return;
85  }
86 
87  Vehicle_t *vehicles_arr = NULL;
88  size_t total_vehicles = 0;
89  if (use_timestamps) {
90  get_vehicles(vehicles_sql, &vehicles_arr, &total_vehicles, false);
91  } else {
92  get_vehicles_raw(vehicles_sql, &vehicles_arr, &total_vehicles, false);
93  }
94 
95  if (total_vehicles == 0) {
96  (*result_count) = 0;
97  (*result_tuples) = NULL;
98 
99  /* freeing memory before return */
100  if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;}
101  if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;}
102 
103  pgr_SPI_finish();
104  return;
105  }
106 
107  Time_multipliers_t *multipliers_arr = NULL;
108  size_t total_multipliers_arr = 0;
109  if (use_timestamps) {
110  get_timeMultipliers(multipliers_sql, &multipliers_arr, &total_multipliers_arr);
111  } else {
112  get_timeMultipliers_raw(multipliers_sql, &multipliers_arr, &total_multipliers_arr);
113  }
114 
115  if (total_multipliers_arr == 0) {
116  (*result_count) = 0;
117  (*result_tuples) = NULL;
118 
119  /* freeing memory before return */
120  if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;}
121  if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;}
122  if (multipliers_arr) {pfree(multipliers_arr); multipliers_arr = NULL;}
123 
124  pgr_SPI_finish();
125  return;
126  }
127 
128  Matrix_cell_t *matrix_cells_arr = NULL;
129  size_t total_cells = 0;
130  if (use_timestamps) {
131  get_matrixRows(matrix_sql, &matrix_cells_arr, &total_cells);
132  } else {
133  get_matrixRows_plain(matrix_sql, &matrix_cells_arr, &total_cells);
134  }
135 
136  if (total_cells == 0) {
137  (*result_count) = 0;
138  (*result_tuples) = NULL;
139 
140  /* freeing memory before return */
141  if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;}
142  if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;}
143  if (multipliers_arr) {pfree(multipliers_arr); multipliers_arr = NULL;}
144  if (matrix_cells_arr) {pfree(matrix_cells_arr); matrix_cells_arr = NULL;}
145 
146  pgr_SPI_finish();
147  return;
148  }
149 
150  PGR_DBG("Total %ld orders in query:", total_pd_orders);
151  PGR_DBG("Total %ld vehicles in query:", total_vehicles);
152  PGR_DBG("Total %ld matrix cells in query:", total_cells);
153  PGR_DBG("Total %ld multipliers in query:", total_cells);
154 
155 #ifdef PROFILE
156  clock_t start_t = clock();
157 #endif
158  char *log_msg = NULL;
159  char *notice_msg = NULL;
160  char *err_msg = NULL;
161 
163  pd_orders_arr, total_pd_orders,
164  vehicles_arr, total_vehicles,
165  matrix_cells_arr, total_cells,
166  multipliers_arr, total_multipliers_arr,
167 
168  factor,
169 
170  result_tuples,
171  result_count,
172 
173  &log_msg,
174  &notice_msg,
175  &err_msg);
176 
177 #ifdef PROFILE
178  time_msg("vrp_compatibleVehicles", start_t, clock());
179 #endif
180  if (err_msg && (*result_tuples)) {
181  pfree(*result_tuples);
182  (*result_count) = 0;
183  (*result_tuples) = NULL;
184  }
185 
186  pgr_global_report(log_msg, notice_msg, err_msg);
187 
188  /* freeing memory before return */
189  if (log_msg) {pfree(log_msg); log_msg = NULL;}
190  if (notice_msg) {pfree(notice_msg); notice_msg = NULL;}
191  if (err_msg) {pfree(err_msg); err_msg = NULL;}
192  if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;}
193  if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;}
194  if (multipliers_arr) {pfree(multipliers_arr); multipliers_arr = NULL;}
195  if (matrix_cells_arr) {pfree(matrix_cells_arr); matrix_cells_arr = NULL;}
196 
197  pgr_SPI_finish();
198 }
199 
200 
201 
202 /******************************************************************************/
203 
204 
205 PGDLLEXPORT Datum
206 _vrp_compatiblevehicles(PG_FUNCTION_ARGS) {
207  FuncCallContext *funcctx;
208  TupleDesc tuple_desc;
209 
210  /**************************************************************************/
211  CompatibleVehicles_rt *result_tuples = 0;
212  size_t result_count = 0;
213  /**************************************************************************/
214 
215  if (SRF_IS_FIRSTCALL()) {
216  MemoryContext oldcontext;
217  funcctx = SRF_FIRSTCALL_INIT();
218  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
219 
220  /**********************************************************************
221  orders_sql TEXT,
222  vehicles_sql TEXT,
223  matrix_cell_sql TEXT,
224  factor FLOAT DEFAULT 1,
225  **********************************************************************/
226 
227  process(
228  text_to_cstring(PG_GETARG_TEXT_P(0)),
229  text_to_cstring(PG_GETARG_TEXT_P(1)),
230  text_to_cstring(PG_GETARG_TEXT_P(2)),
231  text_to_cstring(PG_GETARG_TEXT_P(3)),
232  PG_GETARG_FLOAT8(4),
233  PG_GETARG_BOOL(5),
234  &result_tuples,
235  &result_count);
236 
237  /*********************************************************************/
238 
239  funcctx->max_calls = result_count;
240  funcctx->user_fctx = result_tuples;
241  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
242  != TYPEFUNC_COMPOSITE) {
243  ereport(ERROR,
244  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
245  errmsg("function returning record called in context "
246  "that cannot accept type record")));
247  }
248 
249  funcctx->tuple_desc = tuple_desc;
250  MemoryContextSwitchTo(oldcontext);
251  }
252 
253  funcctx = SRF_PERCALL_SETUP();
254  tuple_desc = funcctx->tuple_desc;
255  result_tuples = (CompatibleVehicles_rt*) funcctx->user_fctx;
256 
257  if (funcctx->call_cntr < funcctx->max_calls) {
258  HeapTuple tuple;
259  Datum result;
260  Datum *values;
261  bool* nulls;
262  size_t call_cntr = funcctx->call_cntr;
263 
264  size_t numb = 2;
265  values = palloc(numb * sizeof(Datum));
266  nulls = palloc(numb * sizeof(bool));
267 
268  size_t i;
269  for (i = 0; i < numb; ++i) {
270  nulls[i] = false;
271  }
272 
273  values[0] = Int64GetDatum(result_tuples[call_cntr].order_id);
274  values[1] = Int64GetDatum(result_tuples[call_cntr].vehicle_id);
275 
276  tuple = heap_form_tuple(tuple_desc, values, nulls);
277  result = HeapTupleGetDatum(tuple);
278 
279  tuple = heap_form_tuple(tuple_desc, values, nulls);
280  result = HeapTupleGetDatum(tuple);
281 
282 
283  pfree(values); values = NULL;
284  pfree(nulls); nulls = NULL;
285 
286  SRF_RETURN_NEXT(funcctx, result);
287  } else {
288  if (result_tuples) {pfree(result_tuples); result_tuples = NULL;}
289  funcctx->user_fctx = NULL;
290  SRF_RETURN_DONE(funcctx);
291  }
292 }
time_msg.h
do_compatibleVehicles
void do_compatibleVehicles(PickDeliveryOrders_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 factor, CompatibleVehicles_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Driver for processing a "compatible vehicles" problem.
Definition: compatibleVehicles_driver.cpp:98
pickDeliveryOrders_t.h
postgres_connection.h
get_matrixRows
void get_matrixRows(char *sql, Matrix_cell_t **rows, size_t *total_rows)
Get the travel time matrix.
Definition: matrixRows_input.c:176
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:79
get_matrixRows_plain
void get_matrixRows_plain(char *sql, Matrix_cell_t **rows, size_t *total_rows)
Get the travel time matrix with numerical types.
Definition: matrixRows_input.c:203
Time_multipliers_t
Time Dependant Multipliers.
Definition: time_multipliers_t.h:46
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
matrixRows_input.h
process
static void process(char *pd_orders_sql, char *vehicles_sql, char *matrix_sql, char *multipliers_sql, double factor, bool use_timestamps, CompatibleVehicles_rt **result_tuples, size_t *result_count)
Definition: compatibleVehicles.c:48
Vehicle_t
vehicles's attributes
Definition: vehicle_t.h:50
compatibleVehicles_rt.h
orders_input.h
vehicles_input.h
get_timeMultipliers
void get_timeMultipliers(char *sql, Time_multipliers_t **rows, size_t *total_rows)
Get the time multipliers using interval.
Definition: time_multipliers_input.c:149
debug_macro.h
_vrp_compatiblevehicles
PGDLLEXPORT Datum _vrp_compatiblevehicles(PG_FUNCTION_ARGS)
Definition: compatibleVehicles.c:206
get_vehicles_raw
void get_vehicles_raw(char *sql, Vehicle_t **rows, size_t *total_rows, bool with_stops)
Reads the vehicles information.
Definition: vehicles_input.c:522
get_shipments
void get_shipments(char *sql, PickDeliveryOrders_t **rows, size_t *total_rows)
Reads the pick-Deliver shipments for timestams and intervals.
Definition: orders_input.c:331
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
Matrix_cell_t
traveling costs
Definition: matrix_cell_t.h:41
get_vehicles
void get_vehicles(char *sql, Vehicle_t **rows, size_t *total_rows, bool with_stops)
Reads the vehicles information.
Definition: vehicles_input.c:469
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
CompatibleVehicles_rt
order-vehicle compatability relationship
Definition: compatibleVehicles_rt.h:50
time_multipliers_input.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_vrp_compatiblevehicles)
compatibleVehicles_driver.h
get_timeMultipliers_raw
void get_timeMultipliers_raw(char *sql, Time_multipliers_t **rows, size_t *total_rows)
Get the time multipliers using bigint.
Definition: time_multipliers_input.c:175
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:30
get_shipments_raw
void get_shipments_raw(char *sql, PickDeliveryOrders_t **rows, size_t *total_rows)
Reads the pick-Deliver shipments for raw data.
Definition: orders_input.c:376