Constraint_Interpreter.cpp

Go to the documentation of this file.
00001 // Constraint_Interpreter.cpp,v 1.28 2006/03/14 06:14:35 jtc Exp
00002 
00003 #include "orbsvcs/Trader/Constraint_Interpreter.h"
00004 #include "orbsvcs/Trader/Trader_Constraint_Visitors.h"
00005 
00006 ACE_RCSID (Trader,
00007            Constraint_Interpreter,
00008            "Constraint_Interpreter.cpp,v 1.28 2006/03/14 06:14:35 jtc Exp")
00009 
00010 
00011 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 TAO_Constraint_Interpreter::TAO_Constraint_Interpreter (
00014     const CosTradingRepos::ServiceTypeRepository::TypeStruct& ts,
00015     const char* constraints
00016     ACE_ENV_ARG_DECL
00017   )
00018   ACE_THROW_SPEC ((CosTrading::IllegalConstraint,
00019                    CORBA::NO_MEMORY))
00020   : TAO_Interpreter ()
00021 {
00022   // @@ Throwing Exception from constructor is very nasty situation to
00023   // deal with.
00024 
00025   TAO_Trader_Constraint_Validator type_checker (ts);
00026 
00027   if (TAO_Interpreter::is_empty_string (constraints))
00028     {
00029       ACE_NEW_THROW_EX (this->root_,
00030                         TAO_Literal_Constraint ((CORBA::Boolean) 1),
00031                         CORBA::NO_MEMORY ());
00032       ACE_CHECK;
00033     }
00034   else
00035     {
00036       if (this->build_tree (constraints) != 0)
00037         ACE_THROW (CosTrading::IllegalConstraint (constraints));
00038 
00039       if (type_checker.validate (this->root_) == -1)
00040         ACE_THROW (CosTrading::IllegalConstraint (constraints));
00041     }
00042 }
00043 
00044 TAO_Constraint_Interpreter::
00045 TAO_Constraint_Interpreter (TAO_Constraint_Validator& validator,
00046                            const char* constraints
00047                            ACE_ENV_ARG_DECL)
00048   ACE_THROW_SPEC ((CosTrading::IllegalConstraint,
00049                    CORBA::NO_MEMORY))
00050 {
00051   if (TAO_Interpreter::is_empty_string (constraints))
00052     {
00053       ACE_NEW_THROW_EX (this->root_,
00054                         TAO_Literal_Constraint ((CORBA::Boolean) 1),
00055                         CORBA::NO_MEMORY ());
00056       ACE_CHECK;
00057     }
00058   else
00059     {
00060       if (this->build_tree (constraints) != 0)
00061         ACE_THROW (CosTrading::IllegalConstraint (constraints));
00062 
00063       if (validator.validate (this->root_) == -1)
00064         ACE_THROW (CosTrading::IllegalConstraint (constraints));
00065     }
00066 }
00067 
00068 TAO_Constraint_Interpreter::~TAO_Constraint_Interpreter (void)
00069 {
00070 }
00071 
00072 CORBA::Boolean
00073 TAO_Constraint_Interpreter::evaluate (CosTrading::Offer* offer)
00074 {
00075   TAO_Trader_Constraint_Evaluator evaluator (offer);
00076   return evaluator.evaluate_constraint (this->root_);
00077 }
00078 
00079 CORBA::Boolean
00080 TAO_Constraint_Interpreter::evaluate (TAO_Constraint_Evaluator& evaluator)
00081 {
00082   return evaluator.evaluate_constraint (this->root_);
00083 }
00084 
00085 TAO_Preference_Interpreter::TAO_Preference_Interpreter (
00086     const CosTradingRepos::ServiceTypeRepository::TypeStruct& ts,
00087     const char* preference
00088     ACE_ENV_ARG_DECL
00089   )
00090   ACE_THROW_SPEC ((CosTrading::Lookup::IllegalPreference,
00091                    CORBA::NO_MEMORY))
00092     : TAO_Interpreter ()
00093 {
00094   TAO_Trader_Constraint_Validator type_checker (ts);
00095 
00096   if (TAO_Interpreter::is_empty_string (preference))
00097     {
00098       ACE_NEW_THROW_EX (this->root_,
00099                         TAO_Noop_Constraint (TAO_FIRST),
00100                         CORBA::NO_MEMORY ());
00101       ACE_CHECK;
00102     }
00103   else
00104     {
00105       if (this->build_tree (preference) != 0)
00106         ACE_THROW (CosTrading::Lookup::IllegalPreference (preference));
00107 
00108       if (type_checker.validate (this->root_) == -1)
00109         ACE_THROW (CosTrading::Lookup::IllegalPreference (preference));
00110     }
00111 }
00112 
00113 TAO_Preference_Interpreter::
00114 TAO_Preference_Interpreter (TAO_Constraint_Validator& validator,
00115                            const char* preference
00116                            ACE_ENV_ARG_DECL)
00117   ACE_THROW_SPEC ((CosTrading::Lookup::IllegalPreference,
00118                    CORBA::NO_MEMORY))
00119     : TAO_Interpreter ()
00120 {
00121   if (TAO_Interpreter::is_empty_string (preference))
00122     {
00123       ACE_NEW_THROW_EX (this->root_,
00124                         TAO_Noop_Constraint (TAO_FIRST),
00125                         CORBA::NO_MEMORY ());
00126       ACE_CHECK;
00127     }
00128   else
00129     {
00130       if (this->build_tree (preference) != 0)
00131         ACE_THROW (CosTrading::Lookup::IllegalPreference (preference));
00132 
00133       if (validator.validate (this->root_) == -1)
00134         ACE_THROW (CosTrading::Lookup::IllegalPreference (preference));
00135     }
00136 }
00137 
00138 TAO_Preference_Interpreter::~TAO_Preference_Interpreter ()
00139 {
00140 }
00141 
00142 void
00143 TAO_Preference_Interpreter::
00144 order_offer (CosTrading::Offer* offer,
00145              CosTrading::OfferId offer_id)
00146 {
00147   TAO_Trader_Constraint_Evaluator evaluator (offer);
00148   this->order_offer (evaluator, offer, offer_id);
00149 }
00150 
00151 void
00152 TAO_Preference_Interpreter::
00153 order_offer (TAO_Constraint_Evaluator& evaluator,
00154              CosTrading::Offer* offer,
00155              CosTrading::OfferId offer_id)
00156 {
00157   if (this->root_ != 0)
00158     {
00159       Preference_Info pref_info;
00160 
00161       pref_info.offer_ = offer;
00162       pref_info.offer_id_ = offer_id;
00163       pref_info.evaluated_ = 1;
00164 
00165       if (evaluator.evaluate_preference (this->root_, pref_info.value_) == 0)
00166         {
00167           // If the evaluation succeeds, insert the node into the
00168           // correct place in the queue.
00169           TAO_Expression_Type expr_type = this->root_->expr_type ();
00170 
00171           if (expr_type == TAO_FIRST
00172               || (expr_type == TAO_WITH
00173                   && ! static_cast<CORBA::Boolean> (pref_info.value_)))
00174             this->offers_.enqueue_tail (pref_info);
00175           else
00176             this->offers_.enqueue_head (pref_info);
00177 
00178           if (expr_type == TAO_MIN || expr_type == TAO_MAX)
00179             {
00180               Ordered_Offers::ITERATOR offer_iter (this->offers_);
00181 
00182               // Push the new item down the list until the min/max
00183               // criterion is satisfied. Observe the evaluation failed
00184               // / evaluation suceeded partion in the list.
00185               offer_iter.advance ();
00186 
00187               for (int i = 1;
00188                    offer_iter.done () == 0;
00189                    offer_iter.advance (), i++)
00190                 {
00191                   Preference_Info* current_offer;
00192                   offer_iter.next (current_offer);
00193 
00194                   // Maintain the sorted order in the first partition.
00195                   if (current_offer->evaluated_ == 1
00196                       && ((expr_type == TAO_MIN
00197                         && pref_info.value_ > current_offer->value_)
00198                        || (expr_type == TAO_MAX
00199                            && pref_info.value_ < current_offer->value_)))
00200                     {
00201                       // Swap the out of order pair
00202                       this->offers_.set (*current_offer,
00203                                          i - 1);
00204                       this->offers_.set (pref_info, i);
00205                     }
00206                   else
00207                     break;
00208                 }
00209             }
00210         }
00211       else
00212         {
00213           // If the evaluation fails, just tack the sucker onto the
00214           // end of the queue.
00215           pref_info.evaluated_ = 0;
00216           this->offers_.enqueue_tail (pref_info);
00217         }
00218     }
00219 }
00220 
00221 int
00222 TAO_Preference_Interpreter::
00223 remove_offer (CosTrading::Offer*& offer,
00224               CosTrading::OfferId& offer_id)
00225 {
00226   int return_value = -1;
00227   Preference_Info pref_info;
00228 
00229   return_value = this->offers_.dequeue_head (pref_info);
00230 
00231   if (return_value == 0)
00232     {
00233       offer = pref_info.offer_;
00234       offer_id = pref_info.offer_id_;
00235     }
00236 
00237   return return_value;
00238 }
00239 
00240 int
00241 TAO_Preference_Interpreter::
00242 remove_offer (CosTrading::Offer*& offer)
00243 {
00244   CosTrading::OfferId offer_id = 0;
00245   return this->remove_offer (offer, offer_id);
00246 }
00247 
00248 
00249 size_t
00250 TAO_Preference_Interpreter::num_offers (void)
00251 {
00252   return this->offers_.size ();
00253 }
00254 
00255 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 13:59:56 2006 for TAO_CosTrader by doxygen 1.3.6