summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Constraint_Validator.cpp
blob: cdf5c7262602274d5cb7891fd8e6eeb8c0922333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Constraint_Validator.cpp

// ========================================================================
// $Id$
// 
// = LIBRARY 
//    orbsvcs
//  
// = FILENAME
//    Constraint_Validator.cpp
// 
// = AUTHOR 
//    Seth Widoff <sbw1@cs.wustl.edu> 
//   
// ========================================================================

#include "Constraint_Validator.h"

TAO_Constraint_Validator::
TAO_Constraint_Validator
(CosTradingRepos::ServiceTypeRepository::TypeStruct* type_struct)
{
  CosTradingRepos::ServiceTypeRepository::PropStructSeq& prop_seq =
    type_struct->props;
  int length = prop_seq.length();
  
  // Create a map of the service type properties to their types.
  for (int i = 0; i < length; i++)
    {
      CORBA::TypeCode_var corba_type = prop_seq[i].value_type;
      TAO_String_Hash_Key prop_name_str = (const char*) prop_seq[i].name;
      this->type_map_.bind (prop_name_str, corba_type.ptr ());
    }
}

int
TAO_Constraint_Validator::validate (TAO_Constraint* root)
{
  return root->accept(this);
}

int
TAO_Constraint_Validator::visit_constraint(TAO_Unary_Constraint* constraint)
{
  int return_value = -1;
  TAO_Expression_Type type;
  TAO_Constraint* operand = constraint->operand();
  this->extract_type(operand, type);
  
  if (this->expr_returns_boolean (type))
    return_value =  operand->accept (this);

  return return_value;
}

int
TAO_Constraint_Validator::visit_first(TAO_Noop_Constraint* noop)
{
  return 0;
}

int
TAO_Constraint_Validator::visit_random(TAO_Noop_Constraint* noop)
{
  return 0;
}

int
TAO_Constraint_Validator::visit_with(TAO_Unary_Constraint* unary_with)
{
  return this->visit_constraint(unary_with);
}

int
TAO_Constraint_Validator::visit_min(TAO_Unary_Constraint* unary_min)
{
  return this->visit_unary_minus(unary_min);
}

int
TAO_Constraint_Validator::visit_max(TAO_Unary_Constraint* unary_max)
{
  return this->visit_unary_minus(unary_max);
}

int 
TAO_Constraint_Validator::
visit_and(TAO_Binary_Constraint* boolean_and)
{
  int return_value = -1;
  TAO_Constraint* left = boolean_and->left_operand(),
    *right = boolean_and->right_operand();
  TAO_Expression_Type left_type, right_type;
  this->extract_type(left, left_type);
  this->extract_type(right, right_type);
  
  // Can only "and" expressions that return a boolean value
  if (this->expr_returns_boolean(left_type) &&
      this->expr_returns_boolean(right_type))
    {
      if (left->accept(this) == 0 &&
	  right->accept(this) == 0)
	return_value = 0;
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_or(TAO_Binary_Constraint* boolean_or)
{
  // The types for or are the same as those for and.
  return this->visit_and(boolean_or);
}

int
TAO_Constraint_Validator::
visit_not(TAO_Unary_Constraint* unary_not)
{
  int return_value = -1;
  // Not can only negate an expression that returns a boolean.
  TAO_Expression_Type type;
  TAO_Constraint* operand = unary_not->operand();
  this->extract_type(operand, type);

  if (this->expr_returns_boolean(type))
    return_value = operand->accept(this);

  return return_value;
}

int
TAO_Constraint_Validator::
visit_exist(TAO_Unary_Constraint* unary_exist)
{
  // Exist simply requires that its operand be a property name
  // included in the service type.
  int return_value = -1;
  TAO_Constraint* operand = unary_exist->operand();
  TAO_Expression_Type type = operand->expr_type();
  
  if (type == TAO_IDENT)
    return_value = operand->accept(this);

  return return_value;
}

int
TAO_Constraint_Validator::
visit_unary_minus(TAO_Unary_Constraint* unary_minus)
{
  // Unary minus can only negate a numeric operand.
  int return_value = -1;
  TAO_Expression_Type type;
  TAO_Constraint* operand = unary_minus->operand();
  this->extract_type(operand, type);
    
  if (this->expr_returns_number(type))
    return_value = operand->accept(this);

  return return_value;
}

int
TAO_Constraint_Validator::
visit_add(TAO_Binary_Constraint* boolean_add)
{
  // All the mathematical operators require numeric operands.
  int return_value = -1;
  TAO_Constraint* left = boolean_add->left_operand(),
    *right = boolean_add->right_operand();
  TAO_Expression_Type left_type, right_type;
  this->extract_type(left, left_type);
  this->extract_type(right, right_type);

  if (this->expr_returns_number(left_type) &&
      this->expr_returns_number(right_type))
    {
      if (left->accept(this) == 0 &&
	  right->accept(this) == 0)
	return_value = 0;
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_sub(TAO_Binary_Constraint* boolean_sub)
{
  return this->visit_add(boolean_sub);
}

int
TAO_Constraint_Validator::
visit_mult(TAO_Binary_Constraint* boolean_mult)
{
  return this->visit_add(boolean_mult);
}

int
TAO_Constraint_Validator::
visit_div(TAO_Binary_Constraint* boolean_div)
{
  // Div not only requires that both of its operands be numeric, but
  // also the the demoninator not be zero. However, since the
  // denominator can be an expression who's value can only be
  // determined while evaulating the constraint expression, this
  // method detects only when the demoniator is a literal whose value
  // is zero.
  int return_value = -1;
  TAO_Constraint* left = boolean_div->left_operand(),
    *right = boolean_div->right_operand();
  TAO_Expression_Type left_type, right_type;
  this->extract_type(left, left_type);
  this->extract_type(right, right_type);

  if (this->expr_returns_number(left_type) &&
      this->expr_returns_number(right_type))
    {
      // Prevent division by zero, a no no.
      int right_isnt_zero = 1;
      switch(right->expr_type())
	{
	case TAO_UNSIGNED:
	  right_isnt_zero = (CORBA::ULong)(*((TAO_Literal_Constraint*)right));
	  break;
	case TAO_SIGNED:
	  right_isnt_zero = (CORBA::Long)(*((TAO_Literal_Constraint*)right));
	  break;
	case TAO_DOUBLE:
	  right_isnt_zero = (CORBA::Double)(*((TAO_Literal_Constraint*)right));
	  break;
	}

      if (right_isnt_zero)
	{
	  if (left->accept(this) == 0 &&
	      right->accept(this) == 0)
	    return_value = 0;
	}
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_twiddle(TAO_Binary_Constraint* binary_twiddle)
{
  // Twiddle requires that both of its operand be strings.
  int return_value = -1;
  TAO_Constraint* left = binary_twiddle->left_operand(),
    *right = binary_twiddle->right_operand();
  TAO_Expression_Type left_type, right_type;
  this->extract_type(left, left_type);
  this->extract_type(right, right_type);
  
  if (this->expr_returns_string(left_type) &&
      this->expr_returns_string(right_type))
    {
      if (left->accept(this) == 0 &&
	  right->accept(this) == 0)
	return_value = 0;
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_in (TAO_Binary_Constraint* binary_in)
{
  // In requires that the right operand be a sequence of a simple type
  // and that its left operand be an expression that evaluates to a
  // value of the same simple type.
  int return_value = -1;
  TAO_Constraint* left = binary_in->left_operand(),
    *right = binary_in->right_operand();
  TAO_Expression_Type left_type, right_type;
  CORBA::TypeCode* prop_type = this->extract_type(right, right_type);
  this->extract_type(left, left_type);

  if (right_type == TAO_SEQUENCE)
    {   
      int types_match = 0;
      CORBA::Environment env;
      
      if (this->expr_returns_number (left_type))
	{
	  types_match =
	    prop_type->equal (TAO_Sequences::_tc_ShortSeq, env) ||
	    prop_type->equal (TAO_Sequences::_tc_UShortSeq, env) ||
	    prop_type->equal (TAO_Sequences::_tc_LongSeq, env) ||
	    prop_type->equal (TAO_Sequences::_tc_ULongSeq, env) ||
	    prop_type->equal (TAO_Sequences::_tc_DoubleSeq, env) ||
	    prop_type->equal (TAO_Sequences::_tc_FloatSeq, env);
	}
      else if (this->expr_returns_boolean (left_type))
	types_match = prop_type->equal (TAO_Sequences::_tc_BooleanSeq, env);
      else if (this->expr_returns_string (left_type))
	types_match = prop_type->equal (TAO_Sequences::_tc_StringSeq, env);
      
      if (types_match)
	return_value = left->accept(this);
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_less_than(TAO_Binary_Constraint* boolean_lt)
{
  // Comparison operations require that both operands be of the same
  // simple type.
  int return_value = -1;
  TAO_Constraint* left = boolean_lt->left_operand(),
    *right = boolean_lt->right_operand();
  TAO_Expression_Type left_type, right_type;
  this->extract_type(left, left_type);
  this->extract_type(right, right_type);

  if ((this->expr_returns_number(left_type) &&
       this->expr_returns_number(right_type)) ||
      (this->expr_returns_string(left_type) &&
       this->expr_returns_string(right_type)))
    {
      if (left->accept(this) == 0 &&
	  right->accept(this) == 0)
	return_value = 0;
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_less_than_equal(TAO_Binary_Constraint* boolean_lte)
{
  return this->visit_less_than(boolean_lte);
}

int
TAO_Constraint_Validator::
visit_greater_than(TAO_Binary_Constraint* boolean_gt)
{
  return this->visit_less_than(boolean_gt);
}

int
TAO_Constraint_Validator::
visit_greater_than_equal(TAO_Binary_Constraint* boolean_gte)
{
  return this->visit_less_than(boolean_gte);
}

int
TAO_Constraint_Validator::
visit_equal(TAO_Binary_Constraint* boolean_eq)
{
  int return_value = -1;
  TAO_Constraint* left = boolean_eq->left_operand(),
    *right = boolean_eq->right_operand();
  TAO_Expression_Type left_type, right_type;
  this->extract_type(left, left_type);
  this->extract_type(right, right_type);

  if ((this->expr_returns_number(left_type) &&
       this->expr_returns_number(right_type)) ||
      (this->expr_returns_string(left_type) &&
       this->expr_returns_string(right_type)) ||
      (this->expr_returns_boolean(left_type) &&
       this->expr_returns_boolean(right_type)))
    {
      if (left->accept(this) == 0 &&
	  right->accept(this) == 0)
	return_value = 0;
    }

  return return_value;
}

int
TAO_Constraint_Validator::
visit_not_equal(TAO_Binary_Constraint* boolean_neq)
{
  return this->visit_equal(boolean_neq);
}

int
TAO_Constraint_Validator::
visit_literal(TAO_Literal_Constraint* literal)
{
  return 0;
}

int
TAO_Constraint_Validator::
visit_property(TAO_Property_Constraint* literal)
{
  return 0;
}

CORBA::TypeCode*
TAO_Constraint_Validator::extract_type (TAO_Constraint* expr,
					TAO_Expression_Type& type)
{
  CORBA::TypeCode* return_value = 0;

  type = expr->expr_type();
  if (type == TAO_IDENT)
    {
      TAO_Property_Constraint* prop = (TAO_Property_Constraint*) expr;
      TAO_String_Hash_Key prop_name (prop->name());

      if (this->type_map_.find (prop_name, return_value) == 0)
	type = TAO_Literal_Constraint::comparable_type (return_value);
    }

  return return_value;
}

int
TAO_Constraint_Validator::expr_returns_boolean(TAO_Expression_Type expr_type)
{
  // If the expression is a boolean operations, a boolean literal, or
  // a boolean property, return 1.
  int return_value = 0;
  
  if (expr_type <= TAO_BOOLEAN)
    return_value = 1;
  
  return return_value;
}


int
TAO_Constraint_Validator::expr_returns_number(TAO_Expression_Type expr_type)
{
  // If the expression is a number operation, a numeric literal, or a
  // numeric property, return 1.
  int return_value = 0;
  
  if ((expr_type >= TAO_PLUS && expr_type <= TAO_NUMBER) ||
      (expr_type >= TAO_UNSIGNED && expr_type <= TAO_DOUBLE))
    return_value = 1;

  return return_value;
}

int
TAO_Constraint_Validator::expr_returns_string(TAO_Expression_Type expr_type)
{
  // If the expression is an operation with a string return value, a
  // string literal, or a property whose type is string, return 1.
  int return_value = 0;

  if (expr_type == TAO_STRING)
    return_value = 1;

  return return_value;
}