summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Constraint_Evaluator.cpp
blob: f791413e8b1ae85b6777085eda39ddcafec4799f (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
// Constraint_Evaluator.cpp
// $Id$

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


#define get_left_operand() (this->queue_.rbegin()[1])
#define get_right_operand() this->queue_.back()
#define get_operand() this->queue_.back()

#include "Constraint_Evaluator.h"

TAO_Constraint_Evaluator::
TAO_Constraint_Evaluator(CosTrading::Offer* offer,
			 CORBA::Boolean supports_dp)
  : prop_eval_ (*offer, supports_dp)
{
  this->props_.clear();
  int length = offer->properties.length();

  // Create a map of property names to their values.
  for (int i = 0; i < length; i++)
    {
      string name = (const char*)offer->properties[i].name;
      this->props_[name] = i;
    }    
}


CORBA::Boolean
TAO_Constraint_Evaluator::evaluate_constraint(TAO_Constraint* root)
{
  CORBA::Boolean result = 0;
  while (! this->queue_.empty())
    this->queue_.pop_back();
   
  // Evaluate the offer according to the constraints in root_;
  if (root != 0)
    {
      if ((root->accept(this) == 0) &&
	  (! this->queue_.empty ()))
	{
	  result = (CORBA::Boolean) get_operand();
	  this->queue_.pop_back ();
	}
    }

  // If a property couldn't be evaluated we must return 0.
  return result;
}

int
TAO_Constraint_Evaluator::
evaluate_preference(TAO_Constraint* root,
		    TAO_Literal_Constraint& result)
{
  int return_value = -1;
  while (! this->queue_.empty())
    this->queue_.pop_back();

  // Evaluate the offer according to the constraints in root_;
  if (root != 0)
    {
      if ((root->accept(this) == 0) &&
	  (! this->queue_.empty ()))
	{
	  result = get_operand();
	  this->queue_.pop_back();
	  return_value = 0;
	}
    }
  
  return return_value;
}

int
TAO_Constraint_Evaluator::visit_constraint(TAO_Unary_Constraint* constraint)
{
  TAO_Constraint* operand = constraint->operand ();
  return operand->accept(this);
}

int
TAO_Constraint_Evaluator::visit_with(TAO_Unary_Constraint* unary_with)
{
  TAO_Constraint* operand = unary_with->operand ();
  return operand->accept(this);
}

int
TAO_Constraint_Evaluator::visit_min(TAO_Unary_Constraint* unary_min)
{
  TAO_Constraint* operand = unary_min->operand ();
  return operand->accept(this);
}

int
TAO_Constraint_Evaluator::visit_max(TAO_Unary_Constraint* unary_max)
{
  TAO_Constraint* operand = unary_max->operand ();
  return operand->accept(this);
}

int
TAO_Constraint_Evaluator::visit_random(TAO_Noop_Constraint* noop_random)
{
  TAO_Literal_Constraint random((CORBA::Long) (ACE_OS::rand ()));
  this->queue_.push_back(random);  
  return 0;
}

int
TAO_Constraint_Evaluator::visit_first(TAO_Noop_Constraint* noop_first)
{
  TAO_Literal_Constraint first((CORBA::Long) 0);
  this->queue_.push_back (first);  
  return 0;
}

int
TAO_Constraint_Evaluator::
visit_and(TAO_Binary_Constraint* boolean_and)
{
  int return_value = -1;
  CORBA::Boolean result = (CORBA::Boolean) 0;
  TAO_Constraint* left = boolean_and->left_operand(),
    *right = boolean_and->right_operand();

  // Short circuiting AND.
  
  if (left->accept(this) == 0)
    {
      result = (CORBA::Boolean) get_operand();
      this->queue_.pop_back ();
      
      if (result)
	{
	  if (right->accept(this) == 0)
	    {
	      result = (CORBA::Boolean) get_operand();
	      this->queue_.pop_back ();

	      return_value = 0;
	    }
	}
      else
	return_value = 0;
    }

  if (return_value != -1)
    this->queue_.push_back (TAO_Literal_Constraint (result));
  
  return return_value;
}

int
TAO_Constraint_Evaluator::
visit_or(TAO_Binary_Constraint* boolean_or)
{
  int return_value = -1;
  CORBA::Boolean result = (CORBA::Boolean) 0;
  TAO_Constraint* left = boolean_or->left_operand (),
    *right = boolean_or->right_operand ();

  // Short-circuiting OR.
  
  if (left->accept(this) == 0)
    {
      result = (CORBA::Boolean) get_operand();
      this->queue_.pop_back ();
      
      if (result == (CORBA::Boolean) 0)
	{
	  if (right->accept (this) == 0)
	    {
	      result = (CORBA::Boolean) get_operand();
	      this->queue_.pop_back();
	      return_value = 0;
	    }
	}
      else
	return_value = 0;
    }

  if (return_value != -1)
    this->queue_.push_back (TAO_Literal_Constraint (result));
  
  return return_value;
}

int
TAO_Constraint_Evaluator::
visit_not(TAO_Unary_Constraint* unary_not)
{
  int return_value = -1;
  TAO_Constraint* operand = unary_not->operand();

  // Logical NOT.
  
  if (operand->accept(this) == 0)
    {
      CORBA::Boolean result = ! (CORBA::Boolean)get_operand();
      this->queue_.pop_back ();
      this->queue_.push_back (TAO_Literal_Constraint (result));

      return_value = 0;
    }

  return return_value;
}

int
TAO_Constraint_Evaluator::
visit_exist(TAO_Unary_Constraint* unary_exist)
{
  TAO_Property_Constraint* operand =
    (TAO_Property_Constraint*) unary_exist->operand ();
  string property_name ((const char*) operand->name ());

  // Determine if a property is defined on this offer.
  
  CORBA::Boolean result = (CORBA::Boolean)
    (this->props_.find (property_name) != this->props_.end());

  this->queue_.push_back (TAO_Literal_Constraint (result));
  
  return 0;
}

int
TAO_Constraint_Evaluator::
visit_unary_minus(TAO_Unary_Constraint* unary_minus)
{
  int return_value = -1;
  TAO_Constraint* operand = unary_minus->operand();

  if (operand->accept(this) == 0)
    {
      TAO_Literal_Constraint& result = - get_operand();
      this->queue_.pop_back ();
      this->queue_.push_back (result);

      return_value = 0;
    }

  return return_value;
}

void
TAO_Constraint_Evaluator::do_the_op (int operation)
{
  TAO_Literal_Constraint& l_op = get_left_operand();
  TAO_Literal_Constraint& r_op = get_right_operand();

  // Perform the listed bindary operation on the first two elements on 
  // the stack.
  
  TAO_Literal_Constraint& result =
    (operation <= TAO_NE)
    ?
    TAO_Literal_Constraint
    ((CORBA::Boolean)
     ((operation == TAO_GT) ? l_op > r_op :
      (operation == TAO_GE) ? l_op >= r_op :
      (operation == TAO_LT) ? l_op < r_op :
      (operation == TAO_LE) ? l_op <= r_op :
      (operation == TAO_NE) ? l_op != r_op :
      (operation == TAO_EQ) ? l_op == r_op : 0))
    :    
    ((operation == TAO_PLUS) ? l_op + r_op :
     (operation == TAO_MINUS) ? l_op - r_op :
     (operation == TAO_MULT) ? l_op * r_op :
     (operation == TAO_DIV) ? l_op / r_op :
     TAO_Literal_Constraint ());
    
  this->queue_.pop_back ();
  this->queue_.pop_back ();
  this->queue_.push_back (result);
}

int
TAO_Constraint_Evaluator::visit_bin_op (TAO_Binary_Constraint* op,
					 int operation)
{
  int return_value = -1;
  TAO_Constraint* left = op->left_operand(),
    *right = op->right_operand(); 

  // Perform an operation on the results of evaluating the left and
  // right branches of this subtree.
  if (left->accept (this) == 0)
    {
      if (right->accept (this) == 0)
	{	  
	  this->do_the_op (operation);
	  return_value = 0;
	}
      else
	this->queue_.pop_back ();
    }

  return return_value;
}

int
TAO_Constraint_Evaluator::
visit_add(TAO_Binary_Constraint* boolean_add)
{
  return this->visit_bin_op (boolean_add, TAO_PLUS);  
}

int
TAO_Constraint_Evaluator::
visit_sub(TAO_Binary_Constraint* boolean_sub)
{
  return this->visit_bin_op (boolean_sub, TAO_MINUS);
}

int
TAO_Constraint_Evaluator::
visit_mult(TAO_Binary_Constraint* boolean_mult)
{
  return this->visit_bin_op (boolean_mult, TAO_MULT);  
}

int
TAO_Constraint_Evaluator::
visit_div(TAO_Binary_Constraint* boolean_div)
{
  return this->visit_bin_op (boolean_div, TAO_DIV);  
}

int
TAO_Constraint_Evaluator::
visit_twiddle(TAO_Binary_Constraint* binary_twiddle)
{
  int return_value = -1;
  TAO_Constraint* left = binary_twiddle->left_operand(),
    *right = binary_twiddle->right_operand();

  // Determine if the left operand is a substring of the right.
  
  if (left->accept (this) == 0)
    {
      if (right->accept(this) == 0)
	{
	  TAO_Literal_Constraint& left_operand = get_left_operand();
	  TAO_Literal_Constraint& right_operand = get_right_operand();
	  
	  CORBA::Boolean result = (CORBA::Boolean)
	    (ACE_OS::strstr ((const char*)left_operand,
			     (const char*)right_operand) != 0);
	  
	  this->queue_.pop_back();
	  this->queue_.pop_back();
	  this->queue_.push_back (TAO_Literal_Constraint (result));
	  return_value = 0;
	}
      else
	this->queue_.pop_back ();
    }

  return return_value;
}

int
TAO_Constraint_Evaluator::
visit_in(TAO_Binary_Constraint* binary_in)
{
  int return_value = -1;
  TAO_Constraint* left = binary_in->left_operand(),
    *right = binary_in->right_operand();

  // Determine if the left operand is contained in the right.
  
  if (left->accept(this) == 0)
    {
      if (this->visit_property ((TAO_Property_Constraint*) right) == 0)
	{
	  TAO_Literal_Constraint& left_value = get_left_operand(); 
	  const CORBA::Any* any = (const CORBA::Any*) get_right_operand();

	  if (any != 0)
	    {
	      CORBA::Boolean result =
		this->sequence_does_contain ((CORBA::Any*) any, left_value);
	      
	      this->queue_.pop_back ();
	      this->queue_.pop_back ();
	      this->queue_.push_back (TAO_Literal_Constraint (result));
	      return_value = 0;
	    }
	  else
	    this->queue_.pop_back ();	  
	}
    }

  return return_value;
}

int
TAO_Constraint_Evaluator::
visit_less_than(TAO_Binary_Constraint* boolean_lt)
{
  return this->visit_bin_op (boolean_lt, TAO_LT);
}

int
TAO_Constraint_Evaluator::
visit_less_than_equal(TAO_Binary_Constraint* boolean_lte)
{
  return this->visit_bin_op (boolean_lte, TAO_LE);
}

int
TAO_Constraint_Evaluator::
visit_greater_than(TAO_Binary_Constraint* boolean_gt)
{
  return this->visit_bin_op (boolean_gt, TAO_GT);
}

int
TAO_Constraint_Evaluator::
visit_greater_than_equal(TAO_Binary_Constraint* boolean_gte)
{
  return this->visit_bin_op (boolean_gte, TAO_GE); 
}

int
TAO_Constraint_Evaluator::
visit_equal(TAO_Binary_Constraint* boolean_eq)
{
  return this->visit_bin_op (boolean_eq, TAO_EQ); 
}

int
TAO_Constraint_Evaluator::
visit_not_equal(TAO_Binary_Constraint* boolean_neq)
{
  return this->visit_bin_op (boolean_neq, TAO_NE); 
}

int
TAO_Constraint_Evaluator::
visit_literal(TAO_Literal_Constraint* literal)
{
  this->queue_.push_back (*literal);
  return 0;
}

int
TAO_Constraint_Evaluator::
visit_property(TAO_Property_Constraint* literal)
{
  int return_value = -1;
  // Handle case where property is not, in fact, mapped to a value
  string prop_name((const char*) literal->name ());
  Property_Map_Iter prop_iter = this->props_.find (prop_name);

  if (prop_iter != this->props_.end())
    {
      CORBA::Environment env;
      // Retrieve the value of the property from the Property_Evaluator
      int prop_index = (*prop_iter).second;

      CORBA::Any* value =
	this->prop_eval_.property_value (prop_index, env);

      if (value != 0)
	{
	  this->queue_.push_back (TAO_Literal_Constraint (value));
	  return_value = 0;
	}
    }

  return return_value;
}


template <class SEQ, class OPERAND_TYPE> CORBA::Boolean
TAO_find (SEQ& sequence, const OPERAND_TYPE element)
{
  int length = sequence.length(),
    return_value = 0;

  for (int i = 0; i < length; i++)
    {
      if (sequence[i] == element)
	{	  
	  return_value = 1;
	  break;
	}
    }

  return (CORBA::Boolean) return_value;
}

#ifdef ACE_HAS_TEMPLATE_SPECIALIZATION
template<> CORBA::Boolean
TAO_find (StringSeq& sequence, const char* element)
#else
CORBA::Boolean
TAO_find_string (StringSeq& sequence, const char* element)
#endif /* ACE_HAS_TEMPLATE_SPECIALIZATION */
{
 int length = sequence.length(),
    return_value = 0;

  for (int i = 0; i < length; i++)
    {
      if (ACE_OS::strcmp(sequence[i], element) == 0)
	{	  
	  return_value = 1;
	  break;
	}
    }

  return (CORBA::Boolean) return_value;
}

CORBA::Boolean
TAO_Constraint_Evaluator::
sequence_does_contain(CORBA::Any* sequence,
		      TAO_Literal_Constraint& element)
{
  // Helper method to cast the void* value returned from the sequence
  // any into a sequence type locally compiled from idl. The sequence
  // wrapper uses the [] operator to locate the target element in the
  // sequence. 

  CORBA::Environment env;
  CORBA::Boolean return_value = CORBA::B_FALSE;
  CORBA::TypeCode_ptr type = sequence->type ();
  CORBA::TypeCode_ptr content = type->content_type (env);
  TAO_CHECK_ENV_RETURN (env, return_value);
  CORBA::TCKind sequence_type = content->kind (env);
  TAO_CHECK_ENV_RETURN (env, return_value);

  // What's up with this?
  if (sequence_type == CORBA::tk_sequence)
    {
      CORBA::TypeCode_ptr tmp = content->content_type (env);
      TAO_CHECK_ENV_RETURN (env, return_value);
      sequence_type = tmp->kind (env);
      TAO_CHECK_ENV_RETURN (env, return_value);
    }

  /*
  switch(sequence_type)
    {
    case CORBA::tk_short:
      {
	ShortSeq short_seq;
	if ((*sequence) >>= short_seq)
	  return_value = ::TAO_find (short_seq, (CORBA::Long)element);
      }
    break;
    case CORBA::tk_ushort:
      {
	UShortSeq ushort_seq;
	if ((*sequence) >>= ushort_seq)
	  return_value = ::TAO_find (ushort_seq, (CORBA::ULong)element);
      }
      break;
    case CORBA::tk_long:
      {
	LongSeq long_seq;
	if ((*sequence) >>= long_seq)
	  return_value = ::TAO_find (long_seq, (CORBA::Long)element);
      }
      break;
    case CORBA::tk_ulong:
      {
	ULongSeq ulong_seq;
	if ((*sequence) >>= ulong_seq)
	  return_value = ::TAO_find (ulong_seq, (CORBA::ULong)element);
      }
      break;
    case CORBA::tk_float:
      {
	FloatSeq float_seq;
	if ((*sequence) >>= float_seq)
	  return_value = ::TAO_find (float_seq, (CORBA::Double)element);
      }
      break;
    case CORBA::tk_double:
      {
	DoubleSeq double_seq;
	if ((*sequence) >>= double_seq)
	  return_value = ::TAO_find (double_seq, (CORBA::Double)element);
      }
      break;
    case CORBA::tk_boolean:
      {
	BooleanSeq bool_seq;
	if ((*sequence) >>= bool_seq)
	  return_value = ::TAO_find (bool_seq, (CORBA::ULong)element);
      }
      break;
    case CORBA::tk_string:
      {
	StringSeq string_seq;
	if ((*sequence) >>= string_seq)
	  {
#ifdef ACE_HAS_TEMPLATE_SPECIALIZATION
	    return_value = ::TAO_find (string_seq, (const char *)element);
#else
	    return_value = ::TAO_find_string (string_seq, (const char*) element);
#endif // ACE_HAS_TEMPLATE_SPECIALIZATION 
	  }
	break;
      }
      }
            */
return return_value;
}