Constraint_Interpreter.cpp

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

Generated on Tue Feb 2 17:49:26 2010 for TAO_CosTrader by  doxygen 1.4.7