📄 constraint_visitors.cpp
字号:
// Constraint_Visitors.cpp,v 1.36 2003/10/28 18:34:24 bala Exp
#include "Constraint_Visitors.h"
#include "Constraint_Nodes.h"
#include "Interpreter_Utils_T.h"
#include "tao/DynamicAny/DynSequence_i.h"
ACE_RCSID(Trader, Constraint_Visitors, "Constraint_Visitors.cpp,v 1.36 2003/10/28 18:34:24 bala Exp")
TAO_Constraint_Evaluator::Operand_Queue::Operand_Queue (void)
{
}
TAO_Literal_Constraint&
TAO_Constraint_Evaluator::Operand_Queue::get_left_operand (void)
{
TAO_Literal_Constraint* left_operand = 0;
this->get (left_operand, 1);
return *left_operand;
}
TAO_Literal_Constraint&
TAO_Constraint_Evaluator::Operand_Queue::get_right_operand (void)
{
TAO_Literal_Constraint* right_operand = 0;
this->get (right_operand);
return *right_operand;
}
TAO_Literal_Constraint&
TAO_Constraint_Evaluator::Operand_Queue::get_operand (void)
{
TAO_Literal_Constraint* operand = 0;
this->get (operand);
return *operand;
}
void
TAO_Constraint_Evaluator::Operand_Queue::dequeue_operand (void)
{
TAO_Literal_Constraint operand;
this->dequeue_head (operand);
}
TAO_Constraint_Evaluator::TAO_Constraint_Evaluator (void)
{
// No-Op.
}
CORBA::Boolean
TAO_Constraint_Evaluator::evaluate_constraint (TAO_Constraint* root)
{
CORBA::Boolean result = 0;
this->queue_.reset ();
// Evaluate the offer according to the constraints in root_;
if (root != 0)
{
if ((root->accept (this) == 0) &&
(! this->queue_.is_empty ()))
{
result = (CORBA::Boolean) this->queue_.get_operand();
this->queue_.dequeue_operand ();
}
}
// 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_.is_empty ())
this->queue_.dequeue_operand ();
// Evaluate the offer according to the constraints in root_;
if (root != 0)
{
if ((root->accept (this) == 0) &&
(! this->queue_.is_empty ()))
{
result = this->queue_.get_operand ();
this->queue_.dequeue_operand ();
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 *)
{
TAO_Literal_Constraint random ((CORBA::Long) (ACE_OS::rand ()));
this->queue_.enqueue_head (random);
return 0;
}
int
TAO_Constraint_Evaluator::visit_first (TAO_Noop_Constraint *)
{
TAO_Literal_Constraint first ((CORBA::Long) 0);
this->queue_.enqueue_head (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) this->queue_.get_operand ();
this->queue_.dequeue_operand ();
if (result)
{
if (right->accept(this) == 0)
{
result = (CORBA::Boolean) this->queue_.get_operand();
this->queue_.dequeue_operand ();
return_value = 0;
}
}
else
return_value = 0;
}
if (return_value != -1)
this->queue_.enqueue_head (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) this->queue_.get_operand ();
this->queue_.dequeue_operand ();
if (result == (CORBA::Boolean) 0)
{
if (right->accept (this) == 0)
{
result = (CORBA::Boolean) this->queue_.get_operand ();
this->queue_.dequeue_operand ();
return_value = 0;
}
}
else
return_value = 0;
}
if (return_value != -1)
this->queue_.enqueue_head (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)this->queue_.get_operand ();
this->queue_.dequeue_operand ();
this->queue_.enqueue_head (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 ();
TAO_String_Hash_Key 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) == 0);
this->queue_.enqueue_head (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 = - this->queue_.get_operand ();
this->queue_.dequeue_operand ();
this->queue_.enqueue_head (result);
return_value = 0;
}
return return_value;
}
void
TAO_Constraint_Evaluator::do_the_op (int operation)
{
TAO_Literal_Constraint& l_op = this->queue_.get_left_operand ();
TAO_Literal_Constraint& r_op = this->queue_.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_.dequeue_operand ();
this->queue_.dequeue_operand ();
this->queue_.enqueue_head (result);
}
int
TAO_Constraint_Evaluator::visit_bin_op (TAO_Binary_Constraint* op,
int operation)
{
int return_value = -1;
TAO_Constraint* left = op->left_operand ();
TAO_Constraint* 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_.dequeue_operand ();
}
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 subTAO_String_Hash_Key of the right.
if (left->accept (this) == 0)
{
if (right->accept (this) == 0)
{
TAO_Literal_Constraint& left_operand = this->queue_.get_left_operand ();
TAO_Literal_Constraint& right_operand = this->queue_.get_right_operand ();
CORBA::Boolean result = (CORBA::Boolean)
(ACE_OS::strstr ((const char*)left_operand,
(const char*)right_operand) != 0);
this->queue_.dequeue_operand ();
this->queue_.dequeue_operand ();
this->queue_.enqueue_head (TAO_Literal_Constraint (result));
return_value = 0;
}
else
this->queue_.dequeue_operand ();
}
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 = this->queue_.get_left_operand();
const CORBA::Any* any = (const CORBA::Any*) this->queue_.get_right_operand();
if (any != 0)
{
CORBA::Boolean result =
this->sequence_does_contain ((CORBA::Any*) any, left_value);
this->queue_.dequeue_operand ();
this->queue_.dequeue_operand ();
this->queue_.enqueue_head (TAO_Literal_Constraint (result));
return_value = 0;
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -