#include <Constraint_Visitors.h>
Inheritance diagram for TAO_Constraint_Validator:
Public Member Functions | |
TAO_Constraint_Validator (void) | |
Constructor. | |
virtual | ~TAO_Constraint_Validator (void) |
Destructor. | |
int | validate (TAO_Constraint *root) |
virtual int | visit_constraint (TAO_Unary_Constraint *constraint) |
virtual int | visit_with (TAO_Unary_Constraint *unary_with) |
virtual int | visit_min (TAO_Unary_Constraint *unary_min) |
virtual int | visit_max (TAO_Unary_Constraint *unary_max) |
virtual int | visit_first (TAO_Noop_Constraint *noop_first) |
virtual int | visit_random (TAO_Noop_Constraint *noop_random) |
virtual int | visit_and (TAO_Binary_Constraint *boolean_and) |
The two operands must return a boolean value. | |
virtual int | visit_or (TAO_Binary_Constraint *boolean_or) |
virtual int | visit_not (TAO_Unary_Constraint *unary_not) |
The operand must return a boolean value. | |
virtual int | visit_exist (TAO_Unary_Constraint *unary_exist) |
virtual int | visit_unary_minus (TAO_Unary_Constraint *unary_minus) |
The operand must return a number to be negated. | |
virtual int | visit_add (TAO_Binary_Constraint *boolean_add) |
Both operands must return numeric results. | |
virtual int | visit_sub (TAO_Binary_Constraint *boolean_sub) |
virtual int | visit_mult (TAO_Binary_Constraint *boolean_mult) |
virtual int | visit_div (TAO_Binary_Constraint *boolean_div) |
virtual int | visit_twiddle (TAO_Binary_Constraint *binary_twiddle) |
Both operands must return strings. | |
virtual int | visit_in (TAO_Binary_Constraint *binary_in) |
virtual int | visit_less_than (TAO_Binary_Constraint *boolean_lt) |
The left and right operands must both be of the same simple type. | |
virtual int | visit_less_than_equal (TAO_Binary_Constraint *boolean_lte) |
virtual int | visit_greater_than (TAO_Binary_Constraint *boolean_gt) |
virtual int | visit_greater_than_equal (TAO_Binary_Constraint *boolean_gte) |
virtual int | visit_equal (TAO_Binary_Constraint *boolean_eq) |
virtual int | visit_not_equal (TAO_Binary_Constraint *boolean_neq) |
virtual int | visit_literal (TAO_Literal_Constraint *literal) |
The property must be defined in the service type description. | |
virtual int | visit_property (TAO_Property_Constraint *literal) |
Protected Attributes | |
TAO_Typecode_Table | type_map_ |
Private Member Functions | |
CORBA::TypeCode * | extract_type (TAO_Constraint *expr_type, TAO_Expression_Type &type) |
int | expr_returns_boolean (TAO_Expression_Type expr_type) |
int | expr_returns_number (TAO_Expression_Type expr_type) |
int | expr_returns_string (TAO_Expression_Type expr_type) |
TAO_Constraint_Validator (const TAO_Constraint_Validator &) | |
TAO_Constraint_Validator & | operator= (const TAO_Constraint_Validator &) |
TAO_Constraint_Validator uses the visitor pattern to traverse all the nodes in an expression tree, checking that for each operator node the operands are of the proper data type it they're literals, or that they exist in the service type definition _and_ have the proper type, if they're property names. The algorithm for type checking is as follows: ensure that operand expression(s) return the correct types using expr_returns* methods. If they (or it) return the correct types, call accept on each operand until all return true or one returns false, at which point we can back out of the traversal and indicate failure.
Definition at line 116 of file Constraint_Visitors.h.
|
Constructor.
Definition at line 739 of file Constraint_Visitors.cpp.
00740 {
00741 // No-Op.
00742 }
|
|
Destructor.
Definition at line 744 of file Constraint_Visitors.cpp. References CORBA::release().
00745 { 00746 for (TAO_Typecode_Table::iterator type_iter (this->type_map_); 00747 ! type_iter.done (); 00748 type_iter++) 00749 { 00750 CORBA::TypeCode_ptr corba_type = (*type_iter).int_id_; 00751 CORBA::release (corba_type); 00752 } 00753 } |
|
|
|
expr_returns_boolean returns 1 if , when evaluated, will return a boolean. Definition at line 1164 of file Constraint_Visitors.cpp. References TAO_BOOLEAN, and TAO_Expression_Type. Referenced by visit_and(), visit_constraint(), visit_equal(), visit_in(), and visit_not().
01165 { 01166 // If the expression is a boolean operations, a boolean literal, or 01167 // a boolean property, return 1. 01168 int return_value = 0; 01169 01170 if (expr_type <= TAO_BOOLEAN) 01171 return_value = 1; 01172 01173 return return_value; 01174 } |
|
expr_returns_boolean returns 1 if , when evaluated, will return a number. Definition at line 1178 of file Constraint_Visitors.cpp. References TAO_DOUBLE, TAO_Expression_Type, TAO_NUMBER, TAO_PLUS, and TAO_UNSIGNED. Referenced by visit_add(), visit_div(), visit_equal(), visit_in(), visit_less_than(), and visit_unary_minus().
01179 { 01180 // If the expression is a number operation, a numeric literal, or a 01181 // numeric property, return 1. 01182 int return_value = 0; 01183 01184 if ((expr_type >= TAO_PLUS && expr_type <= TAO_NUMBER) || 01185 (expr_type >= TAO_UNSIGNED && expr_type <= TAO_DOUBLE)) 01186 return_value = 1; 01187 01188 return return_value; 01189 } |
|
expr_returns_boolean returns 1 if , when evaluated, will return a string. Definition at line 1192 of file Constraint_Visitors.cpp. References TAO_Expression_Type, and TAO_STRING. Referenced by visit_equal(), visit_in(), visit_less_than(), and visit_twiddle().
01193 { 01194 // If the expression is an operation with a string return value, a 01195 // string literal, or a property whose type is string, return 1. 01196 int return_value = 0; 01197 01198 if (expr_type == TAO_STRING) 01199 return_value = 1; 01200 01201 return return_value; 01202 } |
|
Definition at line 1145 of file Constraint_Visitors.cpp. References TAO_Literal_Constraint::comparable_type(), TAO_Constraint::expr_type(), ACE_Hash_Map_Manager_Ex< EXT_ID, INT_ID, HASH_KEY, COMPARE_KEYS, ACE_LOCK >::find(), TAO_Property_Constraint::name(), TAO_Expression_Type, TAO_IDENT, and type_map_. Referenced by visit_add(), visit_and(), visit_constraint(), visit_div(), visit_equal(), visit_in(), visit_less_than(), visit_not(), visit_twiddle(), and visit_unary_minus().
01147 { 01148 CORBA::TypeCode* return_value = 0; 01149 01150 type = expr->expr_type (); 01151 if (type == TAO_IDENT) 01152 { 01153 TAO_Property_Constraint* prop = (TAO_Property_Constraint*) expr; 01154 CORBA::String_var prop_name (prop->name ()); 01155 01156 if (this->type_map_.find (prop_name, return_value) == 0) 01157 type = TAO_Literal_Constraint::comparable_type (return_value); 01158 } 01159 01160 return return_value; 01161 } |
|
|
|
Validate returns 1 if the expression tree whose root is makes semantic sense, in that the operands for each operation comply with each other and the types accepted by the operator. Definition at line 756 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(). Referenced by TAO_Constraint_Interpreter::TAO_Constraint_Interpreter(), and TAO_Preference_Interpreter::TAO_Preference_Interpreter().
00757 { 00758 return root->accept(this); 00759 } |
|
Both operands must return numeric results.
Implements TAO_Constraint_Visitor. Definition at line 886 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_number(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), and TAO_Expression_Type. Referenced by visit_mult(), and visit_sub().
00887 { 00888 // All the mathematical operators require numeric operands. 00889 int return_value = -1; 00890 TAO_Constraint* left = boolean_add->left_operand (), 00891 *right = boolean_add->right_operand (); 00892 TAO_Expression_Type left_type, right_type; 00893 this->extract_type (left, left_type); 00894 this->extract_type (right, right_type); 00895 00896 if (this->expr_returns_number (left_type) && 00897 this->expr_returns_number (right_type)) 00898 { 00899 if (left->accept (this) == 0 && 00900 right->accept (this) == 0) 00901 return_value = 0; 00902 } 00903 00904 return return_value; 00905 } |
|
The two operands must return a boolean value.
Implements TAO_Constraint_Visitor. Definition at line 807 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_boolean(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), and TAO_Expression_Type. Referenced by visit_or().
00808 { 00809 int return_value = -1; 00810 TAO_Constraint* left = boolean_and->left_operand (), 00811 *right = boolean_and->right_operand (); 00812 TAO_Expression_Type left_type, right_type; 00813 this->extract_type (left, left_type); 00814 this->extract_type (right, right_type); 00815 00816 // Can only "and" expressions that return a boolean value 00817 if (this->expr_returns_boolean (left_type) && 00818 this->expr_returns_boolean (right_type)) 00819 { 00820 if (left->accept (this) == 0 && 00821 right->accept (this) == 0) 00822 return_value = 0; 00823 } 00824 00825 return return_value; 00826 } |
|
Implements TAO_Constraint_Visitor. Definition at line 762 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_boolean(), extract_type(), TAO_Unary_Constraint::operand(), and TAO_Expression_Type. Referenced by visit_with().
00763 { 00764 int return_value = -1; 00765 TAO_Expression_Type type; 00766 TAO_Constraint* operand = constraint->operand (); 00767 this->extract_type (operand, type); 00768 00769 if (this->expr_returns_boolean (type)) 00770 return_value = operand->accept (this); 00771 00772 return return_value; 00773 } |
|
Implements TAO_Constraint_Visitor. Definition at line 923 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_number(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), TAO_DOUBLE, TAO_Expression_Type, TAO_SIGNED, and TAO_UNSIGNED.
00924 { 00925 // Div not only requires that both of its operands be numeric, but 00926 // also the the demoninator not be zero. However, since the 00927 // denominator can be an expression who's value can only be 00928 // determined while evaulating the constraint expression, this 00929 // method detects only when the demoniator is a literal whose value 00930 // is zero. 00931 int return_value = -1; 00932 TAO_Constraint* left = boolean_div->left_operand (), 00933 *right = boolean_div->right_operand (); 00934 TAO_Expression_Type left_type, right_type; 00935 this->extract_type (left, left_type); 00936 this->extract_type (right, right_type); 00937 00938 if (this->expr_returns_number (left_type) && 00939 this->expr_returns_number (right_type)) 00940 { 00941 // Prevent division by zero, a no no. 00942 int right_isnt_zero = 1; 00943 switch(right->expr_type ()) 00944 { 00945 case TAO_UNSIGNED: 00946 right_isnt_zero = 00947 ((CORBA::ULong) (*((TAO_Literal_Constraint*) right)) != 0); 00948 break; 00949 case TAO_SIGNED: 00950 right_isnt_zero = 00951 ((CORBA::Long) (*((TAO_Literal_Constraint*) right)) != 0); 00952 break; 00953 case TAO_DOUBLE: 00954 right_isnt_zero = 00955 ((CORBA::Double) (*((TAO_Literal_Constraint*) right)) != 0.0); 00956 break; 00957 } 00958 00959 if (right_isnt_zero) 00960 { 00961 if (left->accept (this) == 0 && 00962 right->accept (this) == 0) 00963 return_value = 0; 00964 } 00965 } 00966 00967 return return_value; 00968 } |
|
Implements TAO_Constraint_Visitor. Definition at line 1099 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_boolean(), expr_returns_number(), expr_returns_string(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), and TAO_Expression_Type. Referenced by visit_not_equal().
01100 { 01101 int return_value = -1; 01102 TAO_Constraint* left = boolean_eq->left_operand (), 01103 *right = boolean_eq->right_operand (); 01104 TAO_Expression_Type left_type, right_type; 01105 this->extract_type (left, left_type); 01106 this->extract_type (right, right_type); 01107 01108 if ((this->expr_returns_number (left_type) && 01109 this->expr_returns_number (right_type)) || 01110 (this->expr_returns_string (left_type) && 01111 this->expr_returns_string (right_type)) || 01112 (this->expr_returns_boolean (left_type) && 01113 this->expr_returns_boolean (right_type))) 01114 { 01115 if (left->accept (this) == 0 && 01116 right->accept (this) == 0) 01117 return_value = 0; 01118 } 01119 01120 return return_value; 01121 } |
|
The operand must return a valid (i.e., present in the service type description) property name. Implements TAO_Constraint_Visitor. Definition at line 854 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), TAO_Constraint::expr_type(), TAO_Unary_Constraint::operand(), TAO_Expression_Type, and TAO_IDENT.
00855 { 00856 // Exist simply requires that its operand be a property name 00857 // included in the service type. 00858 int return_value = -1; 00859 TAO_Constraint* operand = unary_exist->operand (); 00860 TAO_Expression_Type type = operand->expr_type (); 00861 00862 if (type == TAO_IDENT) 00863 return_value = operand->accept (this); 00864 00865 return return_value; 00866 } |
|
Implements TAO_Constraint_Visitor. Definition at line 776 of file Constraint_Visitors.cpp.
00777 {
00778 return 0;
00779 }
|
|
Implements TAO_Constraint_Visitor. Definition at line 1085 of file Constraint_Visitors.cpp. References visit_less_than().
01086 { 01087 return this->visit_less_than (boolean_gt); 01088 } |
|
Implements TAO_Constraint_Visitor. Definition at line 1092 of file Constraint_Visitors.cpp. References visit_less_than().
01093 { 01094 return this->visit_less_than(boolean_gte); 01095 } |
|
The right operand must be a sequence of the same simple type as the left operand. Implements TAO_Constraint_Visitor. Definition at line 995 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), ACE_CATCHANY, ACE_DECLARE_NEW_CORBA_ENV, ACE_ENDTRY, ACE_ENV_ARG_PARAMETER, ACE_TRY, ACE_TRY_CHECK, expr_returns_boolean(), expr_returns_number(), expr_returns_string(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), TAO_Sequence_Extracter_Base::sequence_type(), TAO_Expression_Type, and TAO_SEQUENCE.
00996 { 00997 // In requires that the right operand be a sequence of a simple type 00998 // and that its left operand be an expression that evaluates to a 00999 // value of the same simple type. 01000 int return_value = -1; 01001 TAO_Expression_Type left_type, right_type; 01002 TAO_Constraint 01003 *left = binary_in->left_operand (), 01004 *right = binary_in->right_operand (); 01005 01006 CORBA::TypeCode* prop_type = this->extract_type (right, right_type); 01007 this->extract_type (left, left_type); 01008 01009 if (right_type == TAO_SEQUENCE) 01010 { 01011 ACE_DECLARE_NEW_CORBA_ENV; 01012 CORBA::Boolean types_match = 0; 01013 CORBA::TCKind seq_type = CORBA::tk_void; 01014 ACE_TRY 01015 { 01016 seq_type = 01017 TAO_Sequence_Extracter_Base::sequence_type (prop_type ACE_ENV_ARG_PARAMETER); 01018 ACE_TRY_CHECK; 01019 } 01020 ACE_CATCHANY 01021 { 01022 return return_value; 01023 } 01024 ACE_ENDTRY; 01025 01026 if (seq_type != CORBA::tk_void) 01027 { 01028 if (this->expr_returns_number (left_type)) 01029 { 01030 types_match = (seq_type == CORBA::tk_short || 01031 seq_type == CORBA::tk_ushort || 01032 seq_type == CORBA::tk_long || 01033 seq_type == CORBA::tk_ulong || 01034 seq_type == CORBA::tk_float || 01035 seq_type == CORBA::tk_double); 01036 } 01037 else if (this->expr_returns_boolean (left_type)) 01038 types_match = (seq_type == CORBA::tk_boolean); 01039 else if (this->expr_returns_string (left_type)) 01040 types_match = (seq_type == CORBA::tk_string); 01041 01042 if (types_match) 01043 return_value = left->accept (this); 01044 } 01045 } 01046 01047 return return_value; 01048 } |
|
The left and right operands must both be of the same simple type.
Implements TAO_Constraint_Visitor. Definition at line 1052 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_number(), expr_returns_string(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), and TAO_Expression_Type. Referenced by visit_greater_than(), visit_greater_than_equal(), and visit_less_than_equal().
01053 { 01054 // Comparison operations require that both operands be of the same 01055 // simple type. 01056 int return_value = -1; 01057 TAO_Constraint* left = boolean_lt->left_operand (), 01058 *right = boolean_lt->right_operand (); 01059 TAO_Expression_Type left_type, right_type; 01060 this->extract_type (left, left_type); 01061 this->extract_type (right, right_type); 01062 01063 if ((this->expr_returns_number (left_type) && 01064 this->expr_returns_number (right_type)) || 01065 (this->expr_returns_string (left_type) && 01066 this->expr_returns_string (right_type))) 01067 { 01068 if (left->accept (this) == 0 && 01069 right->accept (this) == 0) 01070 return_value = 0; 01071 } 01072 01073 return return_value; 01074 } |
|
Implements TAO_Constraint_Visitor. Definition at line 1078 of file Constraint_Visitors.cpp. References visit_less_than().
01079 { 01080 return this->visit_less_than (boolean_lte); 01081 } |
|
The property must be defined in the service type description.
Implements TAO_Constraint_Visitor. Definition at line 1132 of file Constraint_Visitors.cpp.
01133 {
01134 return 0;
01135 }
|
|
Implements TAO_Constraint_Visitor. Definition at line 800 of file Constraint_Visitors.cpp. References visit_unary_minus().
00801 { 00802 return this->visit_unary_minus (unary_max); 00803 } |
|
Implements TAO_Constraint_Visitor. Definition at line 794 of file Constraint_Visitors.cpp. References visit_unary_minus().
00795 { 00796 return this->visit_unary_minus (unary_min); 00797 } |
|
Implements TAO_Constraint_Visitor. Definition at line 916 of file Constraint_Visitors.cpp. References visit_add().
00917 { 00918 return this->visit_add (boolean_mult); 00919 } |
|
The operand must return a boolean value.
Implements TAO_Constraint_Visitor. Definition at line 838 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_boolean(), extract_type(), TAO_Unary_Constraint::operand(), and TAO_Expression_Type.
00839 { 00840 int return_value = -1; 00841 // Not can only negate an expression that returns a boolean. 00842 TAO_Expression_Type type; 00843 TAO_Constraint* operand = unary_not->operand (); 00844 this->extract_type (operand, type); 00845 00846 if (this->expr_returns_boolean (type)) 00847 return_value = operand->accept (this); 00848 00849 return return_value; 00850 } |
|
Implements TAO_Constraint_Visitor. Definition at line 1125 of file Constraint_Visitors.cpp. References visit_equal().
01126 { 01127 return this->visit_equal (boolean_neq); 01128 } |
|
Implements TAO_Constraint_Visitor. Definition at line 830 of file Constraint_Visitors.cpp. References visit_and().
00831 { 00832 // The types for or are the same as those for and. 00833 return this->visit_and (boolean_or); 00834 } |
|
Implements TAO_Constraint_Visitor. Definition at line 1139 of file Constraint_Visitors.cpp.
01140 {
01141 return 0;
01142 }
|
|
Implements TAO_Constraint_Visitor. Definition at line 782 of file Constraint_Visitors.cpp.
00783 {
00784 return 0;
00785 }
|
|
Implements TAO_Constraint_Visitor. Definition at line 909 of file Constraint_Visitors.cpp. References visit_add().
00910 { 00911 return this->visit_add (boolean_sub); 00912 } |
|
Both operands must return strings.
Implements TAO_Constraint_Visitor. Definition at line 972 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_string(), extract_type(), TAO_Binary_Constraint::left_operand(), TAO_Binary_Constraint::right_operand(), and TAO_Expression_Type.
00973 { 00974 // Twiddle requires that both of its operand be strings. 00975 int return_value = -1; 00976 TAO_Constraint* left = binary_twiddle->left_operand (), 00977 *right = binary_twiddle->right_operand (); 00978 TAO_Expression_Type left_type, right_type; 00979 this->extract_type (left, left_type); 00980 this->extract_type (right, right_type); 00981 00982 if (this->expr_returns_string (left_type) && 00983 this->expr_returns_string (right_type)) 00984 { 00985 if (left->accept (this) == 0 && 00986 right->accept (this) == 0) 00987 return_value = 0; 00988 } 00989 00990 return return_value; 00991 } |
|
The operand must return a number to be negated.
Implements TAO_Constraint_Visitor. Definition at line 870 of file Constraint_Visitors.cpp. References TAO_Constraint::accept(), expr_returns_number(), extract_type(), TAO_Unary_Constraint::operand(), and TAO_Expression_Type. Referenced by visit_max(), and visit_min().
00871 { 00872 // Unary minus can only negate a numeric operand. 00873 int return_value = -1; 00874 TAO_Expression_Type type; 00875 TAO_Constraint* operand = unary_minus->operand (); 00876 this->extract_type (operand, type); 00877 00878 if (this->expr_returns_number (type)) 00879 return_value = operand->accept (this); 00880 00881 return return_value; 00882 } |
|
Implements TAO_Constraint_Visitor. Definition at line 788 of file Constraint_Visitors.cpp. References visit_constraint().
00789 { 00790 return this->visit_constraint (unary_with); 00791 } |
|
A map gleaned from the ServiceTypeStruct, which correlates property names with their types. Definition at line 185 of file Constraint_Visitors.h. Referenced by extract_type(). |