31 #include "D4CEScanner.h" 32 #include "D4ConstraintEvaluator.h" 33 #include "d4_ce_parser.tab.hh" 37 #include "D4Dimensions.h" 41 #include "Constructor.h" 42 #include "D4Sequence.h" 45 #include "D4FilterClause.h" 52 #define PREVENT_XXS_VIA_CE 1 53 #if NDEBUG && !PREVENT_XXS_VIA_CE 54 #error("Never release libdap with PREVENT_XXS_VIA_CE turned off") 59 bool D4ConstraintEvaluator::parse(
const std::string &expr)
63 std::istringstream iss(expr);
64 D4CEScanner scanner(iss);
65 D4CEParser parser(scanner, *
this );
67 if (trace_parsing()) {
68 parser.set_debug_level(1);
69 parser.set_debug_stream(std::cerr);
72 return parser.parse() == 0;
81 void D4ConstraintEvaluator::throw_not_found(
const string &,
const string &)
83 #if PREVENT_XXS_VIA_CE 84 throw Error(no_such_variable,
string(
"The constraint expression referenced a variable that was not found in the dataset."));
86 throw Error(no_such_variable, d_expr +
": The variable " +
id +
" was not found in the dataset (" + ident +
").");
90 void D4ConstraintEvaluator::throw_not_array(
const string &,
const string &)
92 #if PREVENT_XXS_VIA_CE 93 throw Error(no_such_variable,
string(
"The constraint expression referenced an Array that was not found in the dataset."));
95 throw Error(no_such_variable, d_expr +
": The variable '" +
id +
"' is not an Array variable (" + ident +
").");
99 void D4ConstraintEvaluator::search_for_and_mark_arrays(BaseType *btp)
101 DBG(cerr <<
"Entering D4ConstraintEvaluator::search_for_and_mark_arrays...(" << btp->name() <<
")" << endl);
103 assert(btp->is_constructor_type());
105 Constructor *ctor =
static_cast<Constructor*
>(btp);
106 for (Constructor::Vars_iter i = ctor->var_begin(), e = ctor->var_end(); i != e; ++i) {
107 switch ((*i)->type()) {
109 DBG(cerr <<
"Found an array: " << (*i)->name() << endl);
110 mark_array_variable(*i);
112 case dods_structure_c:
113 case dods_sequence_c:
114 DBG(cerr <<
"Found a ctor: " << (*i)->name() << endl);
115 search_for_and_mark_arrays(*i);
133 D4ConstraintEvaluator::mark_variable(BaseType *btp)
137 DBG(cerr <<
"In D4ConstraintEvaluator::mark_variable... (" << btp->name() <<
"; " << btp->type_name() <<
")" << endl);
139 btp->set_send_p(
true);
141 if (btp->type() == dods_array_c) {
142 mark_array_variable(btp);
146 if (btp->is_constructor_type()) {
147 search_for_and_mark_arrays(btp);
149 else if (btp->type() == dods_array_c && btp->var() && btp->var()->is_constructor_type()) {
150 search_for_and_mark_arrays(btp->var());
154 BaseType *parent = btp->get_parent();
156 parent->BaseType::set_send_p(
true);
157 parent = parent->get_parent();
163 static bool array_uses_shared_dimension(Array *map, D4Dimension *source_dim)
165 for (
Array::Dim_iter d = map->dim_begin(), e = map->dim_end(); d != e; ++d) {
166 if (source_dim->name() == (*d).name)
return true;
188 D4ConstraintEvaluator::mark_array_variable(BaseType *btp)
190 assert(btp->type() == dods_array_c);
192 Array *a =
static_cast<Array*
>(btp);
197 if (d_indexes.empty()) {
198 for (
Array::Dim_iter d = a->dim_begin(), de = a->dim_end(); d != de; ++d) {
199 D4Dimension *dim = a->dimension_D4dim(d);
201 a->add_constraint(d, dim);
207 if (d_indexes.size() != a->dimensions())
208 throw Error(malformed_expr,
"The index constraint for '" + btp->name() +
"' does not match its rank.");
211 for (vector<index>::iterator i = d_indexes.begin(), e = d_indexes.end(); i != e; ++i) {
212 if ((*i).stride > (
unsigned long long) (a->dimension_stop(d,
false) - a->dimension_start(d,
false)) + 1)
213 throw Error(malformed_expr,
214 "For '" + btp->name()
215 +
"', the index stride value is greater than the number of elements in the Array");
217 && ((*i).stop) > (
unsigned long long) (a->dimension_stop(d,
false) - a->dimension_start(d,
false)) + 1)
218 throw Error(malformed_expr,
219 "For '" + btp->name()
220 +
"', the index stop value is greater than the number of elements in the Array");
222 D4Dimension *dim = a->dimension_D4dim(d);
229 if (dim && (*i).empty) {
233 a->add_constraint(d, dim);
243 a->add_constraint(d, (*i).start, (*i).stride, (*i).rest ? -1 : (*i).stop);
254 if (!a->maps()->empty()) {
255 for (D4Maps::D4MapsIter m = a->maps()->map_begin(), e = a->maps()->map_end(); m != e; ++m) {
256 if ((*m)->array() == 0)
257 throw Error(malformed_expr,
258 "An array with Maps was found, but one of the Maps was not defined correctly.");
260 Array *map =
const_cast<Array*
>((*m)->array());
263 if (dim && array_uses_shared_dimension(map, dim)) {
264 D4Map *map_to_be_removed = *m;
265 a->maps()->remove_map(map_to_be_removed);
266 delete map_to_be_removed;
291 D4ConstraintEvaluator::slice_dimension(
const std::string &
id,
const index &i)
295 if (i.stride > dim->size())
296 throw Error(malformed_expr,
297 "For '" +
id +
"', the index stride value is greater than the size of the dimension");
298 if (!i.rest && (i.stop > dim->size() - 1))
299 throw Error(malformed_expr,
"For '" +
id +
"', the index stop value is greater than the size of the dimension");
301 dim->set_constraint(i.start, i.stride, i.rest ? dim->size() - 1 : i.stop);
306 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i)
308 unsigned long long v = get_int64(i.c_str());
309 return index(v, 1, v,
false,
false ,
"");
312 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
const std::string &s,
313 const std::string &e)
315 return index(get_int64(i.c_str()), get_int64(s.c_str()), get_int64(e.c_str()),
false,
false ,
"");
318 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
unsigned long long s,
319 const std::string &e)
321 return index(get_int64(i.c_str()), s, get_int64(e.c_str()),
false,
false ,
"");
324 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
const std::string &s)
326 return index(get_int64(i.c_str()), get_int64(s.c_str()), 0,
true,
false ,
"");
329 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
unsigned long long s)
331 return index(get_uint64(i.c_str()), s, 0,
true,
false ,
"");
334 static string expr_msg(
const std::string &op,
const std::string &arg1,
const std::string &arg2)
336 return "(" + arg1 +
" " + op +
" " + arg2 +
").";
356 static D4FilterClause::ops get_op_code(
const std::string &op)
358 DBGN(cerr <<
"Entering " << __PRETTY_FUNCTION__ << endl <<
"op: " << op << endl);
361 return D4FilterClause::less;
363 return D4FilterClause::greater;
365 return D4FilterClause::less_equal;
367 return D4FilterClause::greater_equal;
369 return D4FilterClause::equal;
371 return D4FilterClause::not_equal;
373 return D4FilterClause::match;
375 throw Error(malformed_expr,
"The opertator '" + op +
"' is not supported.");
399 void D4ConstraintEvaluator::add_filter_clause(
const std::string &op,
const std::string &arg1,
const std::string &arg2)
401 DBG(cerr <<
"Entering: " << __PRETTY_FUNCTION__ << endl);
404 D4Sequence *s =
dynamic_cast<D4Sequence*
>(top_basetype());
406 throw Error(malformed_expr,
407 "When a filter expression is used, it must be bound to a Sequence variable: " + expr_msg(op, arg1, arg2));
409 DBG(cerr <<
"s->name(): " << s->name() << endl);
412 BaseType *a1 = s->var(arg1);
413 BaseType *a2 = s->var(arg2);
414 DBG(cerr <<
"a1: " << a1 <<
", a2: " << a2 << endl);
417 throw Error(malformed_expr,
418 "One of the arguments in a filter expression must be a constant: " + expr_msg(op, arg1, arg2));
420 throw Error(malformed_expr,
421 "One of the arguments in a filter expression must be a variable in a Sequence: " 422 + expr_msg(op, arg1, arg2));
426 s->clauses().add_clause(
new D4FilterClause(get_op_code(op),
new D4RValue(a1),
D4RValueFactory(arg2)));
429 s->clauses().add_clause(
new D4FilterClause(get_op_code(op),
D4RValueFactory(arg1),
new D4RValue(a2)));
440 D4ConstraintEvaluator::remove_quotes(
string &s)
442 if (*s.begin() ==
'\"' && *(s.end() - 1) ==
'\"') {
444 s.erase(s.end() - 1);
456 void D4ConstraintEvaluator::error(
const libdap::location &,
const std::string &m)
459 #if PREVENT_XXS_VIA_CE 460 oss <<
"Constraint expression parse error: " << m << ends;
462 oss << l <<
": " << m << ends;
464 throw Error(malformed_expr, oss.str());
D4Dimension * find_dim(const string &path)
Find the dimension using a path. Using the DAP4 name syntax, lookup a dimension. The dimension must b...
top level DAP object to house generic methods
std::vector< dimension >::iterator Dim_iter
D4RValue * D4RValueFactory(std::string cpps)
Build an appropriate RValue.