1 #include "ace/Monitor_Control/Constraint_Visitor.h"
3 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
5 #include "ace/ETCL/ETCL_y.h"
6 #include "ace/ETCL/ETCL_Constraint.h"
8 #include "ace/OS_NS_string.h"
10 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 namespace Monitor_Control
16 Constraint_Visitor::Constraint_Visitor (
17 const Monitor_Control_Types::Data
& data
)
22 Constraint_Visitor::evaluate_constraint (ETCL_Constraint
*root
)
24 ACE_CDR::Boolean result
= false;
25 this->queue_
.reset ();
27 // Evaluate the constraint in root_;
30 if (root
->accept (this) == 0 && !this->queue_
.is_empty ())
32 ETCL_Literal_Constraint top
;
33 this->queue_
.dequeue_head (top
);
34 result
= (ACE_CDR::Boolean
) top
;
38 // If a property couldn't be evaluated we must return FALSE.
43 Constraint_Visitor::visit_literal (ETCL_Literal_Constraint
*constraint
)
45 this->queue_
.enqueue_head (*constraint
);
50 Constraint_Visitor::visit_identifier (ETCL_Identifier
*ident
)
52 /// TODO - check for strings related to other identifiers
53 /// relevant in MonitorControl, for example the data timestamp.
54 if (ACE_OS::strcmp (ident
->value (), "value") == 0)
56 this->queue_
.enqueue_head (
57 ETCL_Literal_Constraint (this->data_
.value_
));
66 Constraint_Visitor::visit_unary_expr (ETCL_Unary_Expr
*unary_expr
)
68 ETCL_Constraint
*subexpression
= unary_expr
->subexpr ();
70 /// Call to accept() puts the subexpression's evaluation on our queue.
71 if (subexpression
->accept (this) == 0)
73 ETCL_Literal_Constraint subexpr_result
;
74 ACE_CDR::Boolean result
= 0;
75 int op_type
= unary_expr
->type ();
80 this->queue_
.dequeue_head (subexpr_result
);
81 result
= ! (ACE_CDR::Boolean
) subexpr_result
;
82 this->queue_
.enqueue_head (ETCL_Literal_Constraint (result
));
85 /// The leading '-' was parsed separately, so we have to pull
86 /// the literal constraint off the queue, apply the class' own
87 /// unary minus operator, and put it back.
88 this->queue_
.dequeue_head (subexpr_result
);
89 this->queue_
.enqueue_head (-subexpr_result
);
92 /// Leave the literal constraint on the queue. The leading
93 /// '+' was just syntactic sugar - no action is necessary.
96 /// The parser should never construct a ETCL_Unary_Constraint
97 /// behind any operators except the above three.
106 Constraint_Visitor::visit_binary_expr (ETCL_Binary_Expr
*binary_expr
)
108 int bin_op_type
= binary_expr
->type ();
113 return this->visit_or (binary_expr
);
115 return this->visit_and (binary_expr
);
126 return this->visit_binary_op (binary_expr
, bin_op_type
);
127 /// These last two are not supported in non_CORBA ETCL.
136 Constraint_Visitor::visit_or (ETCL_Binary_Expr
*binary
)
138 int return_value
= -1;
139 ACE_CDR::Boolean result
= false;
140 ETCL_Constraint
*lhs
= binary
->lhs ();
142 /// Call to accept() puts the lhs (or its evaluation) on our queue.
143 if (lhs
->accept (this) == 0)
145 ETCL_Literal_Constraint lhs_result
;
146 this->queue_
.dequeue_head (lhs_result
);
147 result
= (ACE_CDR::Boolean
) lhs_result
;
149 /// Short-circuiting OR.
152 ETCL_Constraint
*rhs
= binary
->rhs ();
154 if (rhs
->accept (this) == 0)
156 ETCL_Literal_Constraint rhs_result
;
157 this->queue_
.dequeue_head (rhs_result
);
158 result
= (ACE_CDR::Boolean
) rhs_result
;
168 if (return_value
== 0)
170 this->queue_
.enqueue_head (ETCL_Literal_Constraint (result
));
177 Constraint_Visitor::visit_and (ETCL_Binary_Expr
*binary
)
179 int return_value
= -1;
180 ACE_CDR::Boolean result
= false;
181 ETCL_Constraint
*lhs
= binary
->lhs ();
183 /// Call to accept() puts the lhs (or its evaluation) on our queue.
184 if (lhs
->accept (this) == 0)
186 ETCL_Literal_Constraint lhs_result
;
187 this->queue_
.dequeue_head (lhs_result
);
188 result
= (ACE_CDR::Boolean
) lhs_result
;
190 /// Short-circuiting AND.
193 ETCL_Constraint
*rhs
= binary
->rhs ();
195 if (rhs
->accept (this) == 0)
197 ETCL_Literal_Constraint rhs_result
;
198 this->queue_
.dequeue_head (rhs_result
);
199 result
= (ACE_CDR::Boolean
) rhs_result
;
209 if (return_value
== 0)
211 this->queue_
.enqueue_head (ETCL_Literal_Constraint (result
));
218 Constraint_Visitor::visit_binary_op (ETCL_Binary_Expr
*binary
,
221 int return_value
= -1;
222 ETCL_Constraint
*lhs
= binary
->lhs ();
223 ACE_CDR::Boolean result
= false;
225 /// Perform an operation on the results of evaluating the left and
226 /// right branches of this subtree. The evaluations go on our queue.
227 if (lhs
->accept (this) == 0)
229 ETCL_Literal_Constraint left_operand
;
230 this->queue_
.dequeue_head (left_operand
);
231 ETCL_Constraint
*rhs
= binary
->rhs ();
233 if (rhs
->accept (this) == 0)
235 ETCL_Literal_Constraint right_operand
;
236 this->queue_
.dequeue_head (right_operand
);
242 result
= left_operand
< right_operand
;
243 this->queue_
.enqueue_head (
244 ETCL_Literal_Constraint (result
));
247 result
= left_operand
<= right_operand
;
248 this->queue_
.enqueue_head (
249 ETCL_Literal_Constraint (result
));
252 result
= left_operand
> right_operand
;
253 this->queue_
.enqueue_head (
254 ETCL_Literal_Constraint (result
));
257 result
= left_operand
>= right_operand
;
258 this->queue_
.enqueue_head (
259 ETCL_Literal_Constraint (result
));
262 result
= left_operand
== right_operand
;
263 this->queue_
.enqueue_head (
264 ETCL_Literal_Constraint (result
));
267 result
= left_operand
!= right_operand
;
268 this->queue_
.enqueue_head (
269 ETCL_Literal_Constraint (result
));
272 this->queue_
.enqueue_head (
273 left_operand
+ right_operand
);
276 this->queue_
.enqueue_head (
277 left_operand
- right_operand
);
280 this->queue_
.enqueue_head (
281 left_operand
* right_operand
);
284 this->queue_
.enqueue_head (
285 left_operand
/ right_operand
);
299 ACE_END_VERSIONED_NAMESPACE_DECL
301 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */