m2etis  0.4
VariableAssignmentVisitor.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_PUBSUB_FILTER_VARIABLEASSIGNMENTVISITOR_H__
23 #define __M2ETIS_PUBSUB_FILTER_VARIABLEASSIGNMENTVISITOR_H__
24 
25 #include <stack>
26 
28 
29 namespace m2etis {
30 namespace pubsub {
31 namespace filter {
32 
33  template <typename EventType>
34  class VariableAssignmentVisitor : public FilterVisitor<EventType> {
35  public:
36  VariableAssignmentVisitor() : predicate_assignment_(0), predicate_number_(0) {} // determining the size of the tree
37  explicit VariableAssignmentVisitor(unsigned long predicate_assignment) : predicate_assignment_(predicate_assignment), predicate_number_(0) {} // evaluating one variable assignment
38 
39  virtual void Visit(const FilterExp<EventType> *) override {}
40 
41  virtual void Visit(const AndExp<EventType> *) override {
42  bool operand1 = operand_stack_.top();
43  operand_stack_.pop();
44  bool operand2 = operand_stack_.top();
45  operand_stack_.pop();
46  operand_stack_.push(operand1 && operand2);
47  }
48 
49  virtual void Visit(const OrExp<EventType> *) override {
50  // operand_stack_.push(operand_stack_.pop() || operand_stack_.pop());
51  bool operand1 = operand_stack_.top();
52  operand_stack_.pop();
53  bool operand2 = operand_stack_.top();
54  operand_stack_.pop();
55  operand_stack_.push(operand1 || operand2);
56  }
57 
58  virtual void Visit(const Predicate<EventType> * current_predicate) override {
59  operand_stack_.push((predicate_assignment_ & (1UL << predicate_number_)) != 0);
60  predicate_index_[predicate_number_] = current_predicate;
61  ++predicate_number_;
62  }
63 
64  void reset() {
65  if (!operand_stack_.empty()) {
66  operand_stack_.pop();
67  }
68  predicate_number_ = 0;
69  }
70 
71  bool get_result() const {
72  return operand_stack_.empty() ? 0 : operand_stack_.top();
73  }
74 
75  int get_predicate_number() const {
76  return predicate_number_;
77  }
78 
79  std::map<int, const Predicate<EventType> *> get_predicate_index() const {
80  return predicate_index_;
81  }
82 
83  void set_predicate_assignment(unsigned long predicate_assignment) {
84  predicate_assignment_ = predicate_assignment;
85  }
86 
87  private:
88  std::stack<bool> operand_stack_; // stack to store operands (=results of 2 children) of match
89  unsigned long predicate_assignment_;
90  // holds the current assignemt of values to variables
91  // first predicate in FilterExp tree is first bit in predicate_assignment, second predicate = second bit ...
92  // sizeof(unsigned long)*8 predicates at most
93  int predicate_number_; // after traversal equal to number of predicates in tree
94  std::map<int, const Predicate<EventType> *> predicate_index_; // to determine predicate at position in predicate_assignment_
95 }; // class VariableAssignmentVisitor
96 
97 } /* namespace filter */
98 } /* namespace pubsub */
99 } /* namespace m2etis */
100 
101 #endif /* __M2ETIS_PUBSUB_FILTER_VARIABLEASSIGNMENTVISITOR_H__ */
102 
void set_predicate_assignment(unsigned long predicate_assignment)
virtual void Visit(const AndExp< EventType > *) override
virtual void Visit(const Predicate< EventType > *current_predicate) override
VariableAssignmentVisitor(unsigned long predicate_assignment)
std::map< int, const Predicate< EventType > * > get_predicate_index() const
virtual void Visit(const OrExp< EventType > *) override
virtual void Visit(const FilterExp< EventType > *) override