m2etis  0.4
DecisionTreeLeaf.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_DECISIONTREE_DECISIONTREELEAF_H__
23 #define __M2ETIS_PUBSUB_FILTER_DECISIONTREE_DECISIONTREELEAF_H__
24 
25 #include <set>
26 #include <string>
27 
28 #include "boost/shared_ptr.hpp"
29 
30 namespace m2etis {
31 namespace pubsub {
32 namespace filter {
33 
34  template<typename EventType, typename NetworkType>
35  class DecisionTreeLeaf : public DecisionTreeNode<EventType, NetworkType> {
36  public:
37  DecisionTreeLeaf() = default;
38 
39  explicit DecisionTreeLeaf(const typename NetworkType::Key & subscriber) {
40  addSubscriber(subscriber);
41  }
42 
43  // v is a leaf node of Tree: output(v)
44  virtual void visit(const EventType &, std::set<typename NetworkType::Key> & matching_subscribers) override {
45  matching_subscribers.insert(subscribers_.begin(), subscribers_.end());
46  }
47 
48  // merges another subscription tree into the existing one
49  virtual void merge(boost::shared_ptr<DecisionTreeNode<EventType, NetworkType>> &, typename NetworkType::Key subscriber) override {
50  changeSubscriber(subscriber);
51  } // merge
52 
53  virtual void changeSubscriber(typename NetworkType::Key subscriber) override {
54  subscribers_.clear();
55  subscribers_.insert(subscriber);
56  } // changeSubscriber
57 
58  // adds subscriber
59  virtual void addSubscriber(const typename NetworkType::Key & subscriber) override {
60  subscribers_.insert(subscriber);
61  }
62 
63  // removes the subscriber and returns the number of subscribers left
64  virtual void removeSubscriber(const typename NetworkType::Key & subscriber) override {
65  subscribers_.erase(subscriber);
66  }
67 
68  virtual bool hasSubscriber() override {
69  if (subscribers_.size() == 0) {
70  return false;
71  }
72  return true;
73  }
74 
75  // Testing whether there are subscriptions to the subscriber
76  virtual bool hasSubscription(typename NetworkType::Key subscriber) override {
77  if (subscribers_.find(subscriber) != subscribers_.end()) {
78  return true;
79  }
80  return false;
81  }
82 
83  // for debugging purposes:
84  virtual operator std::string() const {
85  return "leaf node with first subscriber: " + (*(subscribers_.begin())).toStr();
86  };
87 
88  virtual ~DecisionTreeLeaf() {}
89 
90  private:
91  std::set<typename NetworkType::Key> subscribers_;
92 
94  template <typename Archive>
95  void serialize(Archive & ar, const unsigned int version) {
96  ar & boost::serialization::base_object<DecisionTreeNode<EventType, NetworkType> >(*this);
97  ar & subscribers_;
98  }
99  };
100 
101 } /* namespace filter */
102 } /* namespace pubsub */
103 } /* namespace m2etis */
104 
105 #endif /* __M2ETIS_PUBSUB_FILTER_DECISIONTREE_DECISIONTREELEAF_H__ */
106 
virtual void changeSubscriber(typename NetworkType::Key subscriber) override
virtual void addSubscriber(const typename NetworkType::Key &subscriber) override
virtual void visit(const EventType &, std::set< typename NetworkType::Key > &matching_subscribers) override
friend class boost::serialization::access
virtual void merge(boost::shared_ptr< DecisionTreeNode< EventType, NetworkType >> &, typename NetworkType::Key subscriber) override
virtual void removeSubscriber(const typename NetworkType::Key &subscriber) override
virtual bool hasSubscription(typename NetworkType::Key subscriber) override
DecisionTreeLeaf(const typename NetworkType::Key &subscriber)