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)
64 DBG(cerr <<
"Entering D4ConstraintEvaluator::parse: " << endl);
65 std::istringstream iss(expr);
66 D4CEScanner scanner(iss);
67 D4CEParser parser(scanner, *
this );
69 if (trace_parsing()) {
70 parser.set_debug_level(1);
71 parser.set_debug_stream(std::cerr);
77 return parser.parse() == 0;
86 void D4ConstraintEvaluator::throw_not_found(
const string &,
const string &)
88 #if PREVENT_XXS_VIA_CE
89 throw Error(no_such_variable,
string(
"The constraint expression referenced a variable that was not found in the dataset."));
91 throw Error(no_such_variable, d_expr +
": The variable " +
id +
" was not found in the dataset (" + ident +
").");
95 void D4ConstraintEvaluator::throw_not_array(
const string &,
const string &)
97 #if PREVENT_XXS_VIA_CE
98 throw Error(no_such_variable,
string(
"The constraint expression referenced an Array that was not found in the dataset."));
100 throw Error(no_such_variable, d_expr +
": The variable '" +
id +
"' is not an Array variable (" + ident +
").");
104 void D4ConstraintEvaluator::search_for_and_mark_arrays(BaseType *btp)
106 DBG(cerr <<
"Entering D4ConstraintEvaluator::search_for_and_mark_arrays...(" << btp->name() <<
")" << endl);
108 assert(btp->is_constructor_type());
110 Constructor *ctor =
static_cast<Constructor*
>(btp);
111 for (Constructor::Vars_iter i = ctor->var_begin(), e = ctor->var_end(); i != e; ++i) {
112 switch ((*i)->type()) {
114 DBG(cerr <<
"Found an array: " << (*i)->name() << endl);
115 mark_array_variable(*i);
117 case dods_structure_c:
118 case dods_sequence_c:
119 DBG(cerr <<
"Found a ctor: " << (*i)->name() << endl);
120 search_for_and_mark_arrays(*i);
138 D4ConstraintEvaluator::mark_variable(BaseType *btp)
142 DBG(cerr <<
"In D4ConstraintEvaluator::mark_variable... (" << btp->name() <<
"; " << btp->type_name() <<
")" << endl);
144 btp->set_send_p(
true);
146 if (btp->type() == dods_array_c) {
147 mark_array_variable(btp);
151 if (btp->is_constructor_type()) {
152 search_for_and_mark_arrays(btp);
154 else if (btp->type() == dods_array_c && btp->var() && btp->var()->is_constructor_type()) {
155 search_for_and_mark_arrays(btp->var());
159 BaseType *parent = btp->get_parent();
161 parent->BaseType::set_send_p(
true);
162 parent = parent->get_parent();
168 static bool array_uses_shared_dimension(Array *map, D4Dimension *source_dim)
170 for (
Array::Dim_iter d = map->dim_begin(), e = map->dim_end(); d != e; ++d) {
171 if (source_dim->name() == (*d).name)
return true;
193 D4ConstraintEvaluator::mark_array_variable(BaseType *btp)
195 assert(btp->type() == dods_array_c);
197 Array *a =
static_cast<Array*
>(btp);
202 if (d_indexes.empty()) {
203 for (
Array::Dim_iter d = a->dim_begin(), de = a->dim_end(); d != de; ++d) {
204 D4Dimension *dim = a->dimension_D4dim(d);
206 a->add_constraint(d, dim);
212 if (d_indexes.size() != a->dimensions())
213 throw Error(malformed_expr,
"The index constraint for '" + btp->name() +
"' does not match its rank.");
216 for (vector<index>::iterator i = d_indexes.begin(), e = d_indexes.end(); i != e; ++i) {
217 if ((*i).stride > (
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 stride value is greater than the number of elements in the Array");
222 && ((*i).stop) > (
unsigned long long) (a->dimension_stop(d,
false) - a->dimension_start(d,
false)) + 1)
223 throw Error(malformed_expr,
224 "For '" + btp->name()
225 +
"', the index stop value is greater than the number of elements in the Array");
227 D4Dimension *dim = a->dimension_D4dim(d);
234 if (dim && (*i).empty) {
238 a->add_constraint(d, dim);
248 a->add_constraint(d, (*i).start, (*i).stride, (*i).rest ? -1 : (*i).stop);
259 if (!a->maps()->empty()) {
260 for (D4Maps::D4MapsIter m = a->maps()->map_begin(), e = a->maps()->map_end(); m != e; ++m) {
261 if ((*m)->array() == 0)
262 throw Error(malformed_expr,
263 "An array with Maps was found, but one of the Maps was not defined correctly.");
265 Array *map =
const_cast<Array*
>((*m)->array());
268 if (dim && array_uses_shared_dimension(map, dim)) {
269 D4Map *map_to_be_removed = *m;
270 a->maps()->remove_map(map_to_be_removed);
271 delete map_to_be_removed;
296 D4ConstraintEvaluator::slice_dimension(
const std::string &
id,
const index &i)
300 if (i.stride > dim->size())
301 throw Error(malformed_expr,
302 "For '" +
id +
"', the index stride value is greater than the size of the dimension");
303 if (!i.rest && (i.stop > dim->size() - 1))
304 throw Error(malformed_expr,
"For '" +
id +
"', the index stop value is greater than the size of the dimension");
306 dim->set_constraint(i.start, i.stride, i.rest ? dim->size() - 1 : i.stop);
311 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i)
313 unsigned long long v = get_int64(i.c_str());
314 return index(v, 1, v,
false,
false ,
"");
317 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
const std::string &s,
318 const std::string &e)
320 return index(get_int64(i.c_str()), get_int64(s.c_str()), get_int64(e.c_str()),
false,
false ,
"");
323 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
unsigned long long s,
324 const std::string &e)
326 return index(get_int64(i.c_str()), s, get_int64(e.c_str()),
false,
false ,
"");
329 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
const std::string &s)
331 return index(get_int64(i.c_str()), get_int64(s.c_str()), 0,
true,
false ,
"");
334 D4ConstraintEvaluator::index D4ConstraintEvaluator::make_index(
const std::string &i,
unsigned long long s)
336 return index(get_uint64(i.c_str()), s, 0,
true,
false ,
"");
339 static string expr_msg(
const std::string &op,
const std::string &arg1,
const std::string &arg2)
341 return "(" + arg1 +
" " + op +
" " + arg2 +
").";
361 static D4FilterClause::ops get_op_code(
const std::string &op)
363 DBGN(cerr <<
"Entering " << __PRETTY_FUNCTION__ << endl <<
"op: " << op << endl);
366 return D4FilterClause::less;
368 return D4FilterClause::greater;
370 return D4FilterClause::less_equal;
372 return D4FilterClause::greater_equal;
374 return D4FilterClause::equal;
376 return D4FilterClause::not_equal;
378 return D4FilterClause::match;
380 throw Error(malformed_expr,
"The opertator '" + op +
"' is not supported.");
404 void D4ConstraintEvaluator::add_filter_clause(
const std::string &op,
const std::string &arg1,
const std::string &arg2)
406 DBG(cerr <<
"Entering: " << __PRETTY_FUNCTION__ << endl);
409 D4Sequence *s =
dynamic_cast<D4Sequence*
>(top_basetype());
411 throw Error(malformed_expr,
412 "When a filter expression is used, it must be bound to a Sequence variable: " + expr_msg(op, arg1, arg2));
414 DBG(cerr <<
"s->name(): " << s->name() << endl);
417 BaseType *a1 = s->var(arg1);
418 BaseType *a2 = s->var(arg2);
419 DBG(cerr <<
"a1: " << a1 <<
", a2: " << a2 << endl);
422 throw Error(malformed_expr,
423 "One of the arguments in a filter expression must be a constant: " + expr_msg(op, arg1, arg2));
425 throw Error(malformed_expr,
426 "One of the arguments in a filter expression must be a variable in a Sequence: "
427 + expr_msg(op, arg1, arg2));
431 s->clauses().add_clause(
new D4FilterClause(get_op_code(op),
new D4RValue(a1),
D4RValueFactory(arg2)));
434 s->clauses().add_clause(
new D4FilterClause(get_op_code(op),
D4RValueFactory(arg1),
new D4RValue(a2)));
445 D4ConstraintEvaluator::remove_quotes(
string &s)
447 if (*s.begin() ==
'\"' && *(s.end() - 1) ==
'\"') {
449 s.erase(s.end() - 1);
461 void D4ConstraintEvaluator::error(
const libdap::location &,
const std::string &m)
464 #if PREVENT_XXS_VIA_CE
465 oss <<
"Constraint expression parse error: " << m << ends;
467 oss << l <<
": " << m << ends;
469 throw Error(malformed_expr, oss.str());
std::vector< dimension >::iterator Dim_iter
D4Dimension * find_dim(const string &path)
Find the dimension using a path. Using the DAP4 name syntax, lookup a dimension. The dimension must b...
void set_ce_empty(bool ce_empty)
Set the flag that marks the expression constraint as empty.
top level DAP object to house generic methods
D4RValue * D4RValueFactory(std::string cpps)
Build an appropriate RValue.