PGROUTING  2.6-dev
bdAstar.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: bdAstar.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 
31 #include "utils/array.h"
32 
33 #include "c_common/debug_macro.h"
34 #include "c_common/e_report.h"
35 #include "c_common/time_msg.h"
36 
37 #include "c_common/edges_input.h"
38 #include "c_common/arrays_input.h"
40 
41 
42 
45 
46 PGDLLEXPORT Datum bd_astar(PG_FUNCTION_ARGS);
48 
49 
50 static
51 void
52 process(char* edges_sql,
53  ArrayType *starts,
54  ArrayType *ends,
55  bool directed,
56  int heuristic,
57  double factor,
58  double epsilon,
59  bool only_cost,
60  General_path_element_t **result_tuples,
61  size_t *result_count) {
62  check_parameters(heuristic, factor, epsilon);
63 
65 
66  int64_t* start_vidsArr = NULL;
67  size_t size_start_vidsArr = 0;
68  start_vidsArr = (int64_t*)
69  pgr_get_bigIntArray(&size_start_vidsArr, starts);
70 
71  int64_t* end_vidsArr = NULL;
72  size_t size_end_vidsArr = 0;
73  end_vidsArr = (int64_t*)
74  pgr_get_bigIntArray(&size_end_vidsArr, ends);
75 
76  PGR_DBG("Load data");
77  Pgr_edge_xy_t *edges = NULL;
78  size_t total_edges = 0;
79 
80  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
81  PGR_DBG("Total %ld edges in query:", total_edges);
82 
83  if (total_edges == 0) {
84  PGR_DBG("No edges found");
85  (*result_count) = 0;
86  (*result_tuples) = NULL;
88  return;
89  }
90 
91  PGR_DBG("Starting processing");
92  char* log_msg = NULL;
93  char* notice_msg = NULL;
94  char* err_msg = NULL;
95  clock_t start_t = clock();
97  edges, total_edges,
98  start_vidsArr, size_start_vidsArr,
99  end_vidsArr, size_end_vidsArr,
100 
101  directed,
102  heuristic,
103  factor,
104  epsilon,
105  only_cost,
106 
107  result_tuples,
108  result_count,
109  &log_msg,
110  &notice_msg,
111  &err_msg);
112 
113  if (only_cost) {
114  time_msg("pgr_bdAstarCost()", start_t, clock());
115  } else {
116  time_msg("pgr_bdAstar()", start_t, clock());
117  }
118 
119  if (err_msg && (*result_tuples)) {
120  pfree(*result_tuples);
121  (*result_count) = 0;
122  (*result_tuples) = NULL;
123  }
124 
125  pgr_global_report(log_msg, notice_msg, err_msg);
126 
127  if (log_msg) pfree(log_msg);
128  if (notice_msg) pfree(notice_msg);
129  if (err_msg) pfree(err_msg);
130  if (edges) pfree(edges);
131 
132  pgr_SPI_finish();
133 }
134 
135 PGDLLEXPORT Datum
136 bd_astar(PG_FUNCTION_ARGS) {
137  FuncCallContext *funcctx;
138  TupleDesc tuple_desc;
139 
140  General_path_element_t *result_tuples = 0;
141  size_t result_count = 0;
142 
143  if (SRF_IS_FIRSTCALL()) {
144  MemoryContext oldcontext;
145  funcctx = SRF_FIRSTCALL_INIT();
146  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
147 
148 
149  /**********************************************************************
150  edges_sql TEXT,
151  start_vid BIGINT,
152  end_vid BIGINT,
153  directed BOOLEAN DEFAULT true,
154  heuristic INTEGER DEFAULT 0,
155  factor FLOAT DEFAULT 1.0,
156  epsilon FLOAT DEFAULT 1.0,
157 
158  **********************************************************************/
159 
160  PGR_DBG("Calling process");
161  process(
162  text_to_cstring(PG_GETARG_TEXT_P(0)),
163  PG_GETARG_ARRAYTYPE_P(1),
164  PG_GETARG_ARRAYTYPE_P(2),
165 
166  PG_GETARG_BOOL(3),
167  PG_GETARG_INT32(4),
168  PG_GETARG_FLOAT8(5),
169  PG_GETARG_FLOAT8(6),
170  PG_GETARG_BOOL(7),
171  &result_tuples,
172  &result_count);
173 
174 
175 #if PGSQL_VERSION > 95
176  funcctx->max_calls = result_count;
177 #else
178  funcctx->max_calls = (uint32_t)result_count;
179 #endif
180  funcctx->user_fctx = result_tuples;
181  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
182  != TYPEFUNC_COMPOSITE)
183  ereport(ERROR,
184  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
185  errmsg("function returning record called in context "
186  "that cannot accept type record")));
187 
188  funcctx->tuple_desc = tuple_desc;
189  MemoryContextSwitchTo(oldcontext);
190  }
191 
192  funcctx = SRF_PERCALL_SETUP();
193  tuple_desc = funcctx->tuple_desc;
194  result_tuples = (General_path_element_t*) funcctx->user_fctx;
195 
196  if (funcctx->call_cntr < funcctx->max_calls) {
197  HeapTuple tuple;
198  Datum result;
199  Datum *values;
200  bool* nulls;
201  size_t call_cntr = funcctx->call_cntr;
202 
203 
204  /**********************************************************************
205  OUT seq INTEGER,
206  OUT path_seq INTEGER,
207  OUT node BIGINT,
208  OUT edge BIGINT,
209  OUT cost FLOAT,
210  OUT agg_cost FLOAT
211  *********************************************************************/
212 
213  size_t numb = 8;
214  values = palloc(numb * sizeof(Datum));
215  nulls = palloc(numb * sizeof(bool));
216 
217 
218  size_t i;
219  for (i = 0; i < numb; ++i) {
220  nulls[i] = false;
221  }
222 
223  values[0] = Int32GetDatum(call_cntr + 1);
224  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
225  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
226  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
227  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
228  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
229  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
230  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
231 
232 
233  tuple = heap_form_tuple(tuple_desc, values, nulls);
234  result = HeapTupleGetDatum(tuple);
235  SRF_RETURN_NEXT(funcctx, result);
236  } else {
237  SRF_RETURN_DONE(funcctx);
238  }
239 }
240 
PGDLLEXPORT Datum bd_astar(PG_FUNCTION_ARGS)
Definition: bdAstar.c:136
Definition: trsp.h:31
PG_FUNCTION_INFO_V1(bd_astar)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void do_pgr_bdAstar(Pgr_edge_xy_t *edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
void pgr_SPI_finish(void)
void pgr_get_edges_xy(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
Edges with x, y vertices values.
Definition: edges_input.c:578
void check_parameters(int heuristic, double factor, double epsilon)
if(DOXYGEN_FOUND) configure_file($
Definition: CMakeLists.txt:13
void pgr_SPI_connect(void)
static void process(char *edges_sql, ArrayType *starts, ArrayType *ends, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
Definition: bdAstar.c:52