libdap++  Updated for version 3.11.7
GSEClause.cc
Go to the documentation of this file.
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // (c) COPYRIGHT URI/MIT 1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // The Grid Selection Expression Clause class.
33 
34 
35 #include "config.h"
36 
37 static char id[] not_used =
38  {"$Id: GSEClause.cc 24370 2011-03-28 16:21:32Z jimg $"
39  };
40 
41 #include <iostream>
42 #include <sstream>
43 
44 #include "dods-datatypes.h"
45 #include "Error.h"
46 #include "InternalErr.h"
47 
48 #include "debug.h"
49 #include "GSEClause.h"
50 #include "parser.h"
51 #include "gse.tab.hh"
52 
53 using namespace std;
54 
55 int gse_parse(void *arg);
56 void gse_restart(FILE *in);
57 
58 // Glue routines declared in gse.lex
59 void gse_switch_to_buffer(void *new_buffer);
60 void gse_delete_buffer(void * buffer);
61 void *gse_string(const char *yy_str);
62 
63 namespace libdap {
64 
65 // Private methods
66 
67 GSEClause::GSEClause()
68 {
69  throw InternalErr(__FILE__, __LINE__, "default ctor called for GSEClause");
70 }
71 
72 GSEClause::GSEClause(const GSEClause &)
73 {
74  throw InternalErr(__FILE__, __LINE__, "copy ctor called for GSEClause");
75 }
76 
77 GSEClause &GSEClause::operator=(GSEClause &)
78 {
79  throw InternalErr(__FILE__, __LINE__, "assigment called for GSEClause");
80 }
81 
82 // For the comparisions here, we should use an epsilon to catch issues
83 // with floating point values. jhrg 01/12/06
84 template<class T>
85 static bool
86 compare(T elem, relop op, double value)
87 {
88  switch (op) {
89  case dods_greater_op:
90  return elem > value;
92  return elem >= value;
93  case dods_less_op:
94  return elem < value;
95  case dods_less_equal_op:
96  return elem <= value;
97  case dods_equal_op:
98  return elem == value;
99  case dods_not_equal_op:
100  return elem != value;
101  case dods_nop_op:
102  throw Error(malformed_expr, "Attempt to use NOP in Grid selection.");
103  default:
104  throw Error(malformed_expr, "Unknown relational operator in Grid selection.");
105  }
106 }
107 
108 // These values are used in error messages, hence the strings.
109 template<class T>
110 void
111 GSEClause::set_map_min_max_value(T min, T max)
112 {
113  DBG(cerr << "Inside set map min max value " << min << ", " << max << endl);
114  std::ostringstream oss1;
115  oss1 << min;
116  d_map_min_value = oss1.str();
117 
118  std::ostringstream oss2;
119  oss2 << max;
120  d_map_max_value = oss2.str();
121 }
122 
123 // Read the map array, scan, set start and stop.
124 template<class T>
125 void
126 GSEClause::set_start_stop()
127 {
128  T *vals = new T[d_map->length()];
129  d_map->value(vals);
130 
131  // Set the map's max and min values for use in error messages (it's a lot
132  // easier to do here, now, than later... 9/20/2001 jhrg)
133  set_map_min_max_value<T>(vals[d_start], vals[d_stop]);
134 
135  // Starting at the current start point in the map (initially index position
136  // zero), scan forward until the comparison is true. Set the new value
137  // of d_start to that location. Note that each clause applies to exactly
138  // one map. The 'i <= end' test keeps us from setting start _past_ the
139  // end ;-)
140  int i = d_start;
141  int end = d_stop;
142  while (i <= end && !compare<T>(vals[i], d_op1, d_value1))
143  i++;
144 
145  d_start = i;
146 
147  // Now scan backward from the end. We scan all the way to the actual start
148  // although it would probably work to stop at 'i >= d_start'.
149  i = end;
150  while (i >= 0 && !compare<T>(vals[i], d_op1, d_value1))
151  i--;
152  d_stop = i;
153 
154  // Every clause must have one operator but the second is optional since
155  // the more complex form of a clause is optional. That is, the above two
156  // loops took care of constraints like 'x < 7' but we need the following
157  // for ones like '3 < x < 7'.
158  if (d_op2 != dods_nop_op) {
159  int i = d_start;
160  int end = d_stop;
161  while (i <= end && !compare<T>(vals[i], d_op2, d_value2))
162  i++;
163 
164  d_start = i;
165 
166  i = end;
167  while (i >= 0 && !compare<T>(vals[i], d_op2, d_value2))
168  i--;
169 
170  d_stop = i;
171  }
172 
173  delete[] vals;
174 }
175 
176 void
177 GSEClause::compute_indices()
178 {
179  switch (d_map->var()->type()) {
180  case dods_byte_c:
181  set_start_stop<dods_byte>();
182  break;
183  case dods_int16_c:
184  set_start_stop<dods_int16>();
185  break;
186  case dods_uint16_c:
187  set_start_stop<dods_uint16>();
188  break;
189  case dods_int32_c:
190  set_start_stop<dods_int32>();
191  break;
192  case dods_uint32_c:
193  set_start_stop<dods_uint32>();
194  break;
195  case dods_float32_c:
196  set_start_stop<dods_float32>();
197  break;
198  case dods_float64_c:
199  set_start_stop<dods_float64>();
200  break;
201  default:
202  throw Error(malformed_expr,
203  "Grid selection using non-numeric map vectors is not supported");
204  }
205 
206 }
207 
208 // Public methods
209 
211 GSEClause::GSEClause(Grid *grid, const string &map, const double value,
212  const relop op)
213  : d_map(0),
214  d_value1(value), d_value2(0), d_op1(op), d_op2(dods_nop_op),
215  d_map_min_value(""), d_map_max_value("")
216 {
217  d_map = dynamic_cast<Array *>(grid->var(map));
218  if (!d_map)
219  throw Error(string("The map variable '") + map
220  + string("' does not exist in the grid '")
221  + grid->name() + string("'."));
222 
223  DBG(cerr << d_map->toString());
224 
225  // Initialize the start and stop indices.
226  Array::Dim_iter iter = d_map->dim_begin();
227  d_start = d_map->dimension_start(iter);
228  d_stop = d_map->dimension_stop(iter);
229 
230  compute_indices();
231 }
232 
234 GSEClause::GSEClause(Grid *grid, const string &map, const double value1,
235  const relop op1, const double value2, const relop op2)
236  : d_map(0),
237  d_value1(value1), d_value2(value2), d_op1(op1), d_op2(op2),
238  d_map_min_value(""), d_map_max_value("")
239 {
240  d_map = dynamic_cast<Array *>(grid->var(map));
241  if (!d_map)
242  throw Error(string("The map variable '") + map
243  + string("' does not exist in the grid '")
244  + grid->name() + string("'."));
245 
246  DBG(cerr << d_map->toString());
247 
248  // Initialize the start and stop indices.
249  Array::Dim_iter iter = d_map->dim_begin();
250  d_start = d_map->dimension_start(iter);
251  d_stop = d_map->dimension_stop(iter);
252 
253  compute_indices();
254 }
255 
258 bool
260 {
261  if (!d_map)
262  return false;
263 
264  // More ...
265 
266  return true;
267 }
268 
271 Array *
273 {
274  return d_map;
275 }
276 
281 void
283 {
284  d_map = map;
285 }
286 
289 string
291 {
292  return d_map->name();
293 }
294 
298 int
300 {
301  return d_start;
302 }
303 
306 void
308 {
309  d_start = start;
310 }
311 
315 int
317 {
318  DBG(cerr << "Returning stop index value of: " << d_stop << endl);
319  return d_stop;
320 }
321 
324 void
326 {
327  d_stop = stop;
328 }
329 
334 string
336 {
337  return d_map_min_value;
338 }
339 
344 string
346 {
347  return d_map_max_value;
348 }
349 
350 } // namespace libdap
351