PGROUTING  2.4
 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 "./../../common/src/postgres_connection.h"
31 #include "utils/array.h"
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/pgr_types.h"
35 #include "./customers_input.h"
36 
37 #include "./pickDeliver_driver.h"
38 
39 PGDLLEXPORT Datum
40 pickDeliver(PG_FUNCTION_ARGS);
41 
42 
43 /*********************************************************************/
44 /* MODIFY AS NEEDED */
45 static
46 void
47 process(char* customers_sql,
48  int max_vehicles,
49  double capacity,
50  double speed,
51  int max_cycles,
52  General_vehicle_orders_t **result_tuples,
53  size_t *result_count) {
54 
55  if (max_vehicles <= 0) {
56  elog(ERROR, "Illegal value in parameter: max_vehicles");
57  (*result_count) = 0;
58  (*result_tuples) = NULL;
59  return;
60  }
61  if (capacity <= 0) {
62  elog(ERROR, "Illegal value in parameter: capacity");
63  (*result_count) = 0;
64  (*result_tuples) = NULL;
65  return;
66  }
67  if (speed <= 0) {
68  elog(ERROR, "Illegal value in parameter: speed");
69  (*result_count) = 0;
70  (*result_tuples) = NULL;
71  return;
72  }
73  if (max_cycles <= 0) {
74  elog(ERROR, "Illegal value in parameter: max_cycles");
75  (*result_count) = 0;
76  (*result_tuples) = NULL;
77  return;
78  }
79 
81 
82  PGR_DBG("Load data");
83  Customer_t *customers_arr = NULL;
84  size_t total_customers = 0;
85  pgr_get_customers_data(customers_sql, &customers_arr, &total_customers);
86 
87  if (total_customers == 0) {
88  PGR_DBG("No customers found");
89  (*result_count) = 0;
90  (*result_tuples) = NULL;
92  return;
93  }
94  PGR_DBG("Total %ld customers in query:", total_customers);
95 
96  PGR_DBG("Starting processing");
97  char *log_msg = NULL;
98  char *err_msg = NULL;
100  customers_arr, total_customers,
101  max_vehicles,
102  capacity,
103  speed,
104  max_cycles,
105  result_tuples,
106  result_count,
107  &log_msg,
108  &err_msg);
109  PGR_DBG("Returning %ld tuples\n", *result_count);
110  PGR_DBG("Returned log = %s\n", log_msg);
111  if (log_msg) {
112  elog(DEBUG1, "%s", log_msg);
113  free(log_msg);
114  }
115  if (err_msg) {
116  elog(ERROR, "%s", err_msg);
117  free(err_msg);
118  }
119 
120 
121  pfree(customers_arr);
122  pgr_SPI_finish();
123 }
124 /* */
125 /******************************************************************************/
126 
128 PGDLLEXPORT Datum
129 pickDeliver(PG_FUNCTION_ARGS) {
130  FuncCallContext *funcctx;
131  TupleDesc tuple_desc;
132 
133  /**************************************************************************/
134  /* MODIFY AS NEEDED */
135  /* */
136  General_vehicle_orders_t *result_tuples = 0;
137  size_t result_count = 0;
138  /* */
139  /**************************************************************************/
140 
141  if (SRF_IS_FIRSTCALL()) {
142  MemoryContext oldcontext;
143  funcctx = SRF_FIRSTCALL_INIT();
144  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
145 
146  /**********************************************************************/
147  /* MODIFY AS NEEDED */
148  /*
149  orders_sql TEXT,
150  max_vehicles INTEGER,
151  capacity FLOAT,
152  speed FLOAT,
153  max_cycles INTEGER,
154  **********************************************************************/
155 
156  PGR_DBG("Calling process");
157  process(
158  text_to_cstring(PG_GETARG_TEXT_P(0)),
159  PG_GETARG_INT32(1),
160  PG_GETARG_FLOAT8(2),
161  PG_GETARG_FLOAT8(3),
162  PG_GETARG_INT32(4),
163  &result_tuples,
164  &result_count);
165 
166  /* */
167  /*********************************************************************/
168 
169 #if PGSQL_VERSION > 95
170  funcctx->max_calls = result_count;
171 #else
172  funcctx->max_calls = (uint32_t)result_count;
173 #endif
174  funcctx->user_fctx = result_tuples;
175  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
176  != TYPEFUNC_COMPOSITE) {
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("function returning record called in context "
180  "that cannot accept type record")));
181  }
182 
183  funcctx->tuple_desc = tuple_desc;
184  MemoryContextSwitchTo(oldcontext);
185  }
186 
187  funcctx = SRF_PERCALL_SETUP();
188  tuple_desc = funcctx->tuple_desc;
189  result_tuples = (General_vehicle_orders_t*) funcctx->user_fctx;
190 
191  if (funcctx->call_cntr < funcctx->max_calls) {
192  HeapTuple tuple;
193  Datum result;
194  Datum *values;
195  bool* nulls;
196 
197  /*********************************************************************/
198  /* MODIFY!!!!! */
199  /* This has to match you output otherwise the server crashes */
200  /*
201  OUT seq INTEGER,
202  OUT vehicle_id INTEGER,
203  OUT vehicle_seq INTEGER,
204  OUT order_id BIGINT,
205  OUT cost FLOAT,
206  OUT agg_cost FLOAT
207  *********************************************************************/
208 
209 
210  values = palloc(9 * sizeof(Datum));
211  nulls = palloc(9 * sizeof(bool));
212 
213  size_t i;
214  for (i = 0; i < 9; ++i) {
215  nulls[i] = false;
216  }
217 
218 
219  // postgres starts counting from 1
220  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
221  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].vehicle_id);
222  values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].vehicle_seq);
223  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].order_id);
224  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].travelTime);
225  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].arrivalTime);
226  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].waitTime);
227  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].serviceTime);
228  values[8] = Float8GetDatum(result_tuples[funcctx->call_cntr].departureTime);
229 
230  /*********************************************************************/
231 
232  tuple = heap_form_tuple(tuple_desc, values, nulls);
233  result = HeapTupleGetDatum(tuple);
234  SRF_RETURN_NEXT(funcctx, result);
235  } else {
236  SRF_RETURN_DONE(funcctx);
237  }
238 }
void pgr_get_customers_data(char *customers_sql, Customer_t **customers, size_t *total_customers)
#define PGR_DBG(...)
Definition: debug_macro.h:34
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:47
void pgr_SPI_finish(void)
void pgr_SPI_connect(void)
PGDLLEXPORT Datum pickDeliver(PG_FUNCTION_ARGS)
Definition: pickDeliver.c:129
char * err_msg
Definition: BDATester.cpp:50
PG_FUNCTION_INFO_V1(pickDeliver)
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)