pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pickDeliver.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pickDeliver.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail:
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
30 #include "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 #include "fmgr.h"
39 
40 // #define DEBUG
41 
42 #include "./../../common/src/debug_macro.h"
43 #include "./../../common/src/pgr_types.h"
44 #include "./../../common/src/postgres_connection.h"
45 #include "./customers_input.h"
46 
47 #include "./pickDeliver_driver.h"
48 
49 PGDLLEXPORT Datum
50 pickDeliver(PG_FUNCTION_ARGS);
51 
52 
53 /*********************************************************************/
54 /* MODIFY AS NEEDED */
55 static
56 void
57 process(char* customers_sql,
58  int max_vehicles,
59  double capacity,
60  double speed,
61  int max_cycles,
62  General_vehicle_orders_t **result_tuples,
63  size_t *result_count) {
64 
65  if (max_vehicles <= 0) {
66  elog(ERROR, "Illegal value in parameter: max_vehicles");
67  (*result_count) = 0;
68  (*result_tuples) = NULL;
69  return;
70  }
71  if (capacity <= 0) {
72  elog(ERROR, "Illegal value in parameter: capacity");
73  (*result_count) = 0;
74  (*result_tuples) = NULL;
75  return;
76  }
77  if (speed <= 0) {
78  elog(ERROR, "Illegal value in parameter: speed");
79  (*result_count) = 0;
80  (*result_tuples) = NULL;
81  return;
82  }
83  if (max_cycles <= 0) {
84  elog(ERROR, "Illegal value in parameter: max_cycles");
85  (*result_count) = 0;
86  (*result_tuples) = NULL;
87  return;
88  }
89 
91 
92  PGR_DBG("Load data");
93  Customer_t *customers_arr = NULL;
94  size_t total_customers = 0;
95  pgr_get_customers_data(customers_sql, &customers_arr, &total_customers);
96 
97  if (total_customers == 0) {
98  PGR_DBG("No customers found");
99  (*result_count) = 0;
100  (*result_tuples) = NULL;
101  pgr_SPI_finish();
102  return;
103  }
104  PGR_DBG("Total %ld customers in query:", total_customers);
105 
106  PGR_DBG("Starting processing");
107  char *log_msg = NULL;
108  char *err_msg = NULL;
110  customers_arr, total_customers,
111  max_vehicles,
112  capacity,
113  speed,
114  max_cycles,
115  result_tuples,
116  result_count,
117  &log_msg,
118  &err_msg);
119  PGR_DBG("Returning %ld tuples\n", *result_count);
120  PGR_DBG("Returned log = %s\n", log_msg);
121  if (log_msg) {
122  elog(DEBUG1, "%s", log_msg);
123  free(log_msg);
124  }
125  if (err_msg) {
126  elog(ERROR, "%s", err_msg);
127  free(err_msg);
128  }
129 
130 
131  pfree(customers_arr);
132  pgr_SPI_finish();
133 }
134 /* */
135 /******************************************************************************/
136 
138 PGDLLEXPORT Datum
139 pickDeliver(PG_FUNCTION_ARGS) {
140  FuncCallContext *funcctx;
141  uint32_t call_cntr;
142  uint32_t max_calls;
143  TupleDesc tuple_desc;
144 
145  /**************************************************************************/
146  /* MODIFY AS NEEDED */
147  /* */
148  General_vehicle_orders_t *result_tuples = 0;
149  size_t result_count = 0;
150  /* */
151  /**************************************************************************/
152 
153  if (SRF_IS_FIRSTCALL()) {
154  MemoryContext oldcontext;
155  funcctx = SRF_FIRSTCALL_INIT();
156  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
157 
158  /**********************************************************************/
159  /* MODIFY AS NEEDED */
160  /*
161  orders_sql TEXT,
162  max_vehicles INTEGER,
163  capacity FLOAT,
164  speed FLOAT,
165  max_cycles INTEGER,
166  **********************************************************************/
167 
168  PGR_DBG("Calling process");
169  process(
170  pgr_text2char(PG_GETARG_TEXT_P(0)),
171  PG_GETARG_INT32(1),
172  PG_GETARG_FLOAT8(2),
173  PG_GETARG_FLOAT8(3),
174  PG_GETARG_INT32(4),
175  &result_tuples,
176  &result_count);
177 
178  /* */
179  /*********************************************************************/
180 
181  funcctx->max_calls = (uint32_t)result_count;
182  funcctx->user_fctx = result_tuples;
183  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
184  != TYPEFUNC_COMPOSITE) {
185  ereport(ERROR,
186  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
187  errmsg("function returning record called in context "
188  "that cannot accept type record")));
189  }
190 
191  funcctx->tuple_desc = tuple_desc;
192  MemoryContextSwitchTo(oldcontext);
193  }
194 
195  funcctx = SRF_PERCALL_SETUP();
196  call_cntr = funcctx->call_cntr;
197  max_calls = funcctx->max_calls;
198  tuple_desc = funcctx->tuple_desc;
199  result_tuples = (General_vehicle_orders_t*) funcctx->user_fctx;
200 
201  if (call_cntr < max_calls) {
202  HeapTuple tuple;
203  Datum result;
204  Datum *values;
205  bool* nulls;
206 
207  /*********************************************************************/
208  /* MODIFY!!!!! */
209  /* This has to match you output otherwise the server crashes */
210  /*
211  OUT seq INTEGER,
212  OUT vehicle_id INTEGER,
213  OUT vehicle_seq INTEGER,
214  OUT order_id BIGINT,
215  OUT cost FLOAT,
216  OUT agg_cost FLOAT
217  *********************************************************************/
218 
219 
220  values = palloc(9 * sizeof(Datum));
221  nulls = palloc(9 * sizeof(bool));
222 
223  size_t i;
224  for (i = 0; i < 9; ++i) {
225  nulls[i] = false;
226  }
227 
228 
229  // postgres starts counting from 1
230  values[0] = Int32GetDatum(call_cntr + 1);
231  values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_id);
232  values[2] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq);
233  values[3] = Int64GetDatum(result_tuples[call_cntr].order_id);
234  values[4] = Float8GetDatum(result_tuples[call_cntr].travelTime);
235  values[5] = Float8GetDatum(result_tuples[call_cntr].arrivalTime);
236  values[6] = Float8GetDatum(result_tuples[call_cntr].waitTime);
237  values[7] = Float8GetDatum(result_tuples[call_cntr].serviceTime);
238  values[8] = Float8GetDatum(result_tuples[call_cntr].departureTime);
239 
240  /*********************************************************************/
241 
242  tuple = heap_form_tuple(tuple_desc, values, nulls);
243  result = HeapTupleGetDatum(tuple);
244  SRF_RETURN_NEXT(funcctx, result);
245  } else {
246  // cleanup
247  if (result_tuples) free(result_tuples);
248 
249  SRF_RETURN_DONE(funcctx);
250  }
251 }
void pgr_get_customers_data(char *customers_sql, Customer_t **customers, size_t *total_customers)
#define PGR_DBG(...)
Definition: debug_macro.h:33
static void process(char *customers_sql, int max_vehicles, double capacity, double speed, int max_cycles, General_vehicle_orders_t **result_tuples, size_t *result_count)
Definition: pickDeliver.c:57
void pgr_SPI_finish(void)
void pgr_SPI_connect(void)
PGDLLEXPORT Datum pickDeliver(PG_FUNCTION_ARGS)
Definition: pickDeliver.c:139
char * err_msg
Definition: BDATester.cpp:50
PG_FUNCTION_INFO_V1(pickDeliver)
char * pgr_text2char(text *in)
void do_pgr_pickDeliver(Customer_t *customers_arr, size_t total_customers, int max_vehicles, double capacity, double speed, int max_cycles, General_vehicle_orders_t **result_tuples, size_t *total_count, char **log_msg, char **err_msg)