libdap  Updated for version 3.19.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4ConstraintEvaluator.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2002,2003 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include <string>
26 #include <sstream>
27 #include <iterator>
28 
29 //#define DODS_DEBUG
30 
31 #include "D4CEScanner.h"
32 #include "D4ConstraintEvaluator.h"
33 #include "d4_ce_parser.tab.hh"
34 
35 #include "DMR.h"
36 #include "D4Group.h"
37 #include "D4Dimensions.h"
38 #include "D4Maps.h"
39 #include "BaseType.h"
40 #include "Array.h"
41 #include "Constructor.h"
42 #include "D4Sequence.h"
43 
44 #include "D4RValue.h"
45 #include "D4FilterClause.h"
46 
47 #include "escaping.h"
48 #include "parser.h" // for get_ull()
49 #include "debug.h"
50 
51 namespace libdap {
52 
53 bool D4ConstraintEvaluator::parse(const std::string &expr)
54 {
55  d_expr = expr; // set for error messages. See the %initial-action section of .yy
56 
57  std::istringstream iss(expr);
58  D4CEScanner scanner(iss);
59  D4CEParser parser(scanner, *this /* driver */);
60 
61  if (trace_parsing()) {
62  parser.set_debug_level(1);
63  parser.set_debug_stream(std::cerr);
64  }
65 
66  return parser.parse() == 0;
67 }
68 
69 void
70 D4ConstraintEvaluator::throw_not_found(const string &id, const string &ident)
71 {
72  throw Error(no_such_variable, d_expr + ": The variable " + id + " was not found in the dataset (" + ident + ").");
73 }
74 
75 void
76 D4ConstraintEvaluator::throw_not_array(const string &id, const string &ident)
77 {
78  throw Error(no_such_variable, d_expr + ": The variable '" + id + "' is not an Array variable (" + ident + ").");
79 }
80 
81 void
82 D4ConstraintEvaluator::search_for_and_mark_arrays(BaseType *btp)
83 {
84  DBG(cerr << "Entering D4ConstraintEvaluator::search_for_and_mark_arrays...(" << btp->name() << ")" << endl);
85 
86  assert(btp->is_constructor_type());
87 
88  Constructor *ctor = static_cast<Constructor*>(btp);
89  for (Constructor::Vars_iter i = ctor->var_begin(), e = ctor->var_end(); i != e; ++i) {
90  switch ((*i)->type()) {
91  case dods_array_c:
92  DBG(cerr << "Found an array: " << (*i)->name() << endl);
93  mark_array_variable(*i);
94  break;
95  case dods_structure_c:
96  case dods_sequence_c:
97  DBG(cerr << "Found a ctor: " << (*i)->name() << endl);
98  search_for_and_mark_arrays(*i);
99  break;
100  default:
101  break;
102  }
103  }
104 }
105 
115 BaseType *
116 D4ConstraintEvaluator::mark_variable(BaseType *btp)
117 {
118  assert(btp);
119 
120  DBG(cerr << "In D4ConstraintEvaluator::mark_variable... (" << btp->name() << "; " << btp->type_name() << ")" << endl);
121 
122  btp->set_send_p(true);
123 
124  if (btp->type() == dods_array_c ) {
125  mark_array_variable(btp);
126  }
127 
128  // Test for Constructors and marks arrays they contain
129  if (btp->is_constructor_type()) {
130  search_for_and_mark_arrays(btp);
131  }
132  else if (btp->type() == dods_array_c && btp->var() && btp->var()->is_constructor_type()) {
133  search_for_and_mark_arrays(btp->var());
134  }
135 
136  // Now set the parent variables
137  BaseType *parent = btp->get_parent();
138  while (parent) {
139  parent->BaseType::set_send_p(true); // Just set the parent using BaseType's impl.
140  parent = parent->get_parent();
141  }
142 
143  return btp;
144 }
145 
146 static bool
147 array_uses_shared_dimension(Array *map, D4Dimension *source_dim)
148 {
149  for (Array::Dim_iter d = map->dim_begin(), e = map->dim_end(); d != e; ++d) {
150  if (source_dim->name() == (*d).name)
151  return true;
152  }
153 
154  return false;
155 }
156 
170 // Note: If a Map is not part of the current projection, do not include mention of it
171 // in the response DMR (CDMR)
172 BaseType *
173 D4ConstraintEvaluator::mark_array_variable(BaseType *btp)
174 {
175  assert(btp->type() == dods_array_c);
176 
177  Array *a = static_cast<Array*>(btp);
178 
179  // If an array appears in a CE without the slicing operators ([]) we still have to
180  // call add_constraint(...) for all of it's sdims for them to appear in
181  // the Constrained DMR.
182  if (d_indexes.empty()) {
183  for (Array::Dim_iter d = a->dim_begin(), de = a->dim_end(); d != de; ++d) {
184  D4Dimension *dim = a->dimension_D4dim(d);
185  if (dim) {
186  a->add_constraint(d, dim);
187  }
188  }
189  }
190  else {
191  // Test that the indexes and dimensions match in number
192  if (d_indexes.size() != a->dimensions())
193  throw Error(malformed_expr, "The index constraint for '" + btp->name() + "' does not match its rank.");
194 
195  Array::Dim_iter d = a->dim_begin();
196  for (vector<index>::iterator i = d_indexes.begin(), e = d_indexes.end(); i != e; ++i) {
197  if ((*i).stride > (unsigned long long) (a->dimension_stop(d, false) - a->dimension_start(d, false)) + 1)
198  throw Error(malformed_expr, "For '" + btp->name() + "', the index stride value is greater than the number of elements in the Array");
199  if (!(*i).rest && ((*i).stop) > (unsigned long long) (a->dimension_stop(d, false) - a->dimension_start(d, false)) + 1)
200  throw Error(malformed_expr, "For '" + btp->name() + "', the index stop value is greater than the number of elements in the Array");
201 
202  D4Dimension *dim = a->dimension_D4dim(d);
203 
204  // In a DAP4 CE, specifying '[]' as an array dimension slice has two meanings.
205  // It can mean 'all the elements' of the dimension or 'apply the slicing inherited
206  // from the shared dimension'. The latter might be provide 'all the elements'
207  // but regardless, the Array object must record the CE correctly.
208 
209  if (dim && (*i).empty) {
210  // This case corresponds to a CE that uses the '[]' notation for a
211  // particular dimension - meaning, use the Shared Dimension size for
212  // this dimension's 'slice'.
213  a->add_constraint(d, dim); // calls set_used_by_projected_var(true) + more
214  }
215  else {
216  // This case corresponds to a 'local dimension slice' (See sections 8.6.2 and
217  // 8.7 of the spec as of 4/12/16). When a local dimension slice is used, drop
218  // the Map(s) that include that dimension. This enables people to constrain
219  // an Array when some of the Array's dimensions don't use Shared Dimensions
220  // but others do.
221 
222  // First apply the constraint to the Array's dimension
223  a->add_constraint(d, (*i).start, (*i).stride, (*i).rest ? -1 : (*i).stop);
224 
225  // Then, if the Array has Maps, scan those Maps for any that use dimensions
226  // that match the name of this particular dimension. If any such Maps are found
227  // remove them. This ensure that the Array can be constrained using the 'local
228  // dimension slice' without the constrained DMR containing references to Maps
229  // that don't exist (or are otherwise nonsensical).
230  //
231  // This code came about as a fix for problems discovered during testing of
232  // local dimension slices. See https://opendap.atlassian.net/browse/HYRAX-98
233  // jhrg 4/12/16
234  if (!a->maps()->empty()) {
235  for(D4Maps::D4MapsIter m = a->maps()->map_begin(), e = a->maps()->map_end(); m != e; ++m) {
236  if ((*m)->array() == 0)
237  throw Error(malformed_expr, "An array with Maps was found, but one of the Maps was not defined correctly.");
238 
239  Array *map = const_cast<Array*>((*m)->array()); // Array lacks const iterator support
240  // Added a test to ensure 'dim' is not null. This could be the case if
241  // execution gets here and the index *i was not empty. jhrg 4/18/17
242  if (dim && array_uses_shared_dimension(map, dim)) {
243  D4Map *map_to_be_removed = *m;
244  a->maps()->remove_map(map_to_be_removed); // Invalidates the iterator
245  delete map_to_be_removed; // removed from container; delete
246  break; // must leave the for loop because 'm' is now invalid
247  }
248  }
249  }
250  }
251 
252  ++d;
253  }
254  }
255 
256  d_indexes.clear(); // Clear the info so the next slice expression can be parsed.
257 
258  return btp;
259 }
260 
269 D4Dimension *
270 D4ConstraintEvaluator::slice_dimension(const std::string &id, const index &i)
271 {
272  D4Dimension *dim = dmr()->root()->find_dim(id);
273 
274  if (i.stride > dim->size())
275  throw Error(malformed_expr, "For '" + id + "', the index stride value is greater than the size of the dimension");
276  if (!i.rest && (i.stop > dim->size() - 1))
277  throw Error(malformed_expr, "For '" + id + "', the index stop value is greater than the size of the dimension");
278 
279  dim->set_constraint(i.start, i.stride, i.rest ? dim->size() - 1: i.stop);
280 
281  return dim;
282 }
283 
284 D4ConstraintEvaluator::index
285 D4ConstraintEvaluator::make_index(const std::string &i)
286 {
287  unsigned long long v = get_int64(i.c_str());
288  return index(v, 1, v, false, false /*empty*/, "");
289 }
290 
291 D4ConstraintEvaluator::index
292 D4ConstraintEvaluator::make_index(const std::string &i, const std::string &s, const std::string &e)
293 {
294  return index(get_int64(i.c_str()), get_int64(s.c_str()), get_int64(e.c_str()), false, false /*empty*/, "");
295 }
296 
297 D4ConstraintEvaluator::index
298 D4ConstraintEvaluator::make_index(const std::string &i, unsigned long long s, const std::string &e)
299 {
300  return index(get_int64(i.c_str()), s, get_int64(e.c_str()), false, false /*empty*/, "");
301 }
302 
303 D4ConstraintEvaluator::index
304 D4ConstraintEvaluator::make_index(const std::string &i, const std::string &s)
305 {
306  return index(get_int64(i.c_str()), get_int64(s.c_str()), 0, true, false /*empty*/, "");
307 }
308 
309 D4ConstraintEvaluator::index
310 D4ConstraintEvaluator::make_index(const std::string &i, unsigned long long s)
311 {
312  return index(get_uint64(i.c_str()), s, 0, true, false /*empty*/, "");
313 }
314 
315 static string
316 expr_msg(const std::string &op, const std::string &arg1, const std::string &arg2)
317 {
318  return "(" + arg1 + " " + op + " " + arg2 + ").";
319 }
320 
338 static D4FilterClause::ops
339 get_op_code(const std::string &op)
340 {
341  DBGN(cerr << "Entering " << __PRETTY_FUNCTION__ << endl << "op: " << op << endl);
342 
343  if (op == "<")
344  return D4FilterClause::less;
345  else if (op == ">")
346  return D4FilterClause::greater;
347  else if (op == "<=")
348  return D4FilterClause::less_equal;
349  else if (op == ">=")
350  return D4FilterClause::greater_equal;
351  else if (op == "==")
352  return D4FilterClause::equal;
353  else if (op == "!=")
354  return D4FilterClause::not_equal;
355  else if (op == "~=")
356  return D4FilterClause::match;
357  else
358  throw Error(malformed_expr, "The opertator '" + op + "' is not supported.");
359 }
360 
382 void
383 D4ConstraintEvaluator::add_filter_clause(const std::string &op, const std::string &arg1, const std::string &arg2)
384 {
385  DBG(cerr << "Entering: " << __PRETTY_FUNCTION__ << endl);
386 
387  // Check that there really is a D4Sequence associated with this filter clause.
388  D4Sequence *s = dynamic_cast<D4Sequence*>(top_basetype());
389  if (!s)
390  throw Error(malformed_expr,
391  "When a filter expression is used, it must be bound to a Sequence variable: " + expr_msg(op, arg1, arg2));
392 
393  DBG(cerr << "s->name(): " << s->name() << endl);
394 
395  // Check that arg1 and 2 are valid
396  BaseType *a1 = s->var(arg1);
397  BaseType *a2 = s->var(arg2);
398  DBG(cerr << "a1: " << a1 << ", a2: " << a2 << endl);
399 
400  if (a1 && a2)
401  throw Error(malformed_expr,
402  "One of the arguments in a filter expression must be a constant: " + expr_msg(op, arg1, arg2));
403  if (!(a1 || a2))
404  throw Error(malformed_expr,
405  "One of the arguments in a filter expression must be a variable in a Sequence: " + expr_msg(op, arg1, arg2));
406 
407  // Now we know a1 XOR a2 is true
408  if (a1) {
409  s->clauses().add_clause(new D4FilterClause(get_op_code(op), new D4RValue(a1), D4RValueFactory(arg2)));
410  }
411  else {
412  s->clauses().add_clause(new D4FilterClause(get_op_code(op), D4RValueFactory(arg1), new D4RValue(a2)));
413  }
414 }
415 
422 string &
423 D4ConstraintEvaluator::remove_quotes(string &s)
424 {
425  if (*s.begin() == '\"' && *(s.end() - 1) == '\"') {
426  s.erase(s.begin());
427  s.erase(s.end() - 1);
428  }
429 
430  return s;
431 }
432 
433 // This method is called from the parser (see d4_ce_parser.yy, down in the code
434 // section). This will be called during the call to D4CEParser::parse(), that
435 // is inside D4ConstraintEvaluator::parse(...)
436 void
437 D4ConstraintEvaluator::error(const libdap::location &l, const std::string &m)
438 {
439  ostringstream oss;
440  oss << l << ": " << m << ends;
441  throw Error(malformed_expr, oss.str());
442 }
443 
444 } /* namespace libdap */
libdap::DMR::root
D4Group * root()
Definition: DMR.cc:360
libdap::D4Group::find_dim
D4Dimension * find_dim(const string &path)
Find the dimension using a path. Using the DAP4 name syntax, lookup a dimension. The dimension must b...
Definition: D4Group.cc:275
libdap::Array::Dim_iter
std::vector< dimension >::iterator Dim_iter
Definition: Array.h:206
libdap
Definition: AlarmHandler.h:35
libdap::D4RValueFactory
D4RValue * D4RValueFactory(std::string cpps)
Build an appropriate RValue.
Definition: D4RValue.cc:218