summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Trader/Preference_Interpreter.cpp
blob: 55fd85864a8aea87721fca7b804965554558c419 (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
// Preference_Interpreter.cpp

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

#include "Preference_Interpreter.h"
#include <values.h>
#include <ctype.h>
#include <stdio.h>

TAO_Preference_Interpreter::
TAO_Preference_Interpreter(CosTradingRepos::ServiceTypeRepository::TypeStruct* ts,
			   const char* preference,
			   CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::Lookup::IllegalPreference))
  : TAO_Interpreter ()
{
  TAO_Constraint_Validator type_checker(ts);

  if (TAO_Interpreter::is_empty_string(preference))
    this->root_ = new TAO_Noop_Constraint(TAO_FIRST);
  else
    {
      if (this->build_tree (preference) != 0)
	TAO_THROW (CosTrading::Lookup::IllegalPreference (preference));	  

      if (type_checker.validate (this->root_) == -1)
	TAO_THROW (CosTrading::Lookup::IllegalPreference (preference));
    }
}

TAO_Preference_Interpreter::
TAO_Preference_Interpreter(TAO_Constraint_Validator& validator,
			   const char* preference,
			   CORBA::Environment& _env)
  TAO_THROW_SPEC ((CosTrading::Lookup::IllegalPreference))
    : TAO_Interpreter ()
{
  if (TAO_Interpreter::is_empty_string(preference))
    this->root_ = new TAO_Noop_Constraint(TAO_FIRST);
  else
    {
      if (this->build_tree (preference) != 0)
	TAO_THROW (CosTrading::Lookup::IllegalPreference (preference));	  
      
      if (validator.validate (this->root_) == -1)
	TAO_THROW (CosTrading::Lookup::IllegalPreference (preference));
    }
}

TAO_Preference_Interpreter::~TAO_Preference_Interpreter()
{
}

void 
TAO_Preference_Interpreter::
order_offer (CosTrading::OfferId offer_id,
	     CosTrading::Offer* offer)
{
  TAO_Constraint_Evaluator evaluator(offer);
  this->order_offer (offer_id, offer, evaluator);
}

void 
TAO_Preference_Interpreter::
order_offer (CosTrading::OfferId offer_id,
	     CosTrading::Offer* offer,
	     TAO_Constraint_Evaluator& evaluator)
{
  if (this->root_ != 0)
    {
      TAO_Literal_Constraint return_value;	  
      TAO_Expression_Type expr_type = this->root_->expr_type ();
      if (evaluator.evaluate_preference (this->root_, return_value) == 0)
	this->offers_.insert (return_value, make_pair (offer_id, offer));
      else
	{
	  TAO_Literal_Constraint end ((CORBA::Double) MAXDOUBLE);
	  this->offers_.insert (end, make_pair (offer_id, offer));      
	}
    }
}

int
TAO_Preference_Interpreter::
remove_offer (CosTrading::OfferId& offer_id,
	      CosTrading::Offer*& offer)
{
  int return_value = 0;
  TAO_Expression_Type expr_type = this->root_->expr_type ();
  ORDERED_OFFERS::iterator offer_beg = this->offers_.begin ();
  ORDERED_OFFERS::iterator offer_end = this->offers_.end ();

  if (offer_beg != offer_end)
    {
      if (expr_type == TAO_WITH || expr_type == TAO_MAX)
	{
	  offer_end--;
	  offer_id = (*offer_end).second.first;
	  offer = (*offer_end).second.second;
	  this->offers_.erase (offer_end);
	}
      else
	{
	  offer_id = (*offer_beg).second.first;
	  offer = (*offer_beg).second.second;
	  this->offers_.erase (offer_beg);
	}
      
      return_value = 1;
    }
  
  return return_value;
}

int 
TAO_Preference_Interpreter::num_offers(void)
{
  return this->offers_.size();
}