m2etis  0.4
GreaterThanPredicateIndex.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_GENERALBOOLEANALGEBRA_GREATERTHANPREDICATEINDEX_H__
23 #define __M2ETIS_PUBSUB_FILTER_GENERALBOOLEANALGEBRA_GREATERTHANPREDICATEINDEX_H__
24 
26 
27 namespace m2etis {
28 namespace pubsub {
29 namespace filter {
30 
31  template <typename EventType, typename AttributeType>
32  class GreaterThanPredicateIndex : public PredicateIndex<EventType> {
33  public:
34  GreaterThanPredicateIndex(AttributeName attribute_id, PredicateIdentifierFactory * predicate_identifier_factory) : PredicateIndex<EventType>(attribute_id, GREATERTHAN, predicate_identifier_factory) {}
35 
36  // adds value of predicate to hashmap
38  // first checking if identical predicate already exists => use same predicate id
39  if (predicate_id_to_attribute_value_.find(greater_than_attribute_filter->get_constants()[0]) != predicate_id_to_attribute_value_.end()) {
40  return predicate_id_to_attribute_value_[greater_than_attribute_filter->get_constants()[0]];
41  }
42  // no identical predicate exists, insert into hash map
44  predicate_id_to_attribute_value_.insert(std::pair<AttributeType, PredicateIdentifierFactory::PredicateID >(greater_than_attribute_filter->get_constants()[0], new_predicate_id));
45 
46  return new_predicate_id;
47  }
48 
49  virtual void removePredicate(std::vector<bool>::size_type predicate_id) override {
50  this->predicate_identifier_factory_->freeID(predicate_id);
51 
52  for (auto attributeValue_predicateID_pair_iter = predicate_id_to_attribute_value_.begin(); attributeValue_predicateID_pair_iter != predicate_id_to_attribute_value_.end(); ++attributeValue_predicateID_pair_iter) {
53  if (attributeValue_predicateID_pair_iter->second == predicate_id) {
54  predicate_id_to_attribute_value_.erase(attributeValue_predicateID_pair_iter);
55  }
56  }
57  }
58 
59  // determines the fulfilled predicates which are in this index
60  // And sets the "bit" in the fulfilled predicate vector to true
61  virtual void determineMatchingPredicates(const EventType & event, std::vector<bool> & fulfilled_predicate_vector) override {
62  // all predicates are fulfilled with a constant less than the value of attribute attribute_id_ in event
63 
64  auto attribute_value_iterator = attributes_accessor_map.find(this->get_attribute_id());
65 
66  if (attribute_value_iterator == attributes_accessor_map.end()) {
67  M2ETIS_THROW_API("GreaterThanPredicateIndex function determineMatchingPredicates", "Attribute not found in attribute map.");
68  }
69 
70  auto itlow =predicate_id_to_attribute_value_.lower_bound((dynamic_cast<AttributeAccessor<EventType, AttributeType> *>(attribute_value_iterator->second.get()))->getAttributeValue(event));
71 
72  for (auto iter_predicates = predicate_id_to_attribute_value_.begin(); iter_predicates != itlow ; ++iter_predicates) {
73  fulfilled_predicate_vector[iter_predicates->second] = true;
74  }
75  }
76 
78 
79  private:
80  // hashmap as index structure to determine matching predicates efficiently
81  // matches a value to the predicate identifier:
82  // identical indexes get assigned the same predicate identifier
83  std::map<AttributeType, PredicateIdentifierFactory::PredicateID > predicate_id_to_attribute_value_;
84  };
85 
86 } /* namespace filter */
87 } /* namespace pubsub */
88 } /* namespace m2etis */
89 
90 #endif /* __M2ETIS_PUBSUB_FILTER_GENERALBOOLEANALGEBRA_GREATERTHANPREDICATEINDEX_H__ */
91 
PredicateIdentifierFactory::PredicateID addPredicate(const GreaterThanAttributeFilter< EventType, AttributeType > *greater_than_attribute_filter)
GreaterThanPredicateIndex(AttributeName attribute_id, PredicateIdentifierFactory *predicate_identifier_factory)
const std::vector< AttributeType > get_constants() const
virtual void removePredicate(std::vector< bool >::size_type predicate_id) override
M2ETIS_API std::map< filter::AttributeName, std::shared_ptr< filter::AttributeAccessor_Basic > > attributes_accessor_map
virtual void determineMatchingPredicates(const EventType &event, std::vector< bool > &fulfilled_predicate_vector) override
AttributeName get_attribute_id() const
#define M2ETIS_THROW_API(module, message)
throws on wrong API usage
Definition: Exceptions.h:40
PredicateIdentifierFactory * predicate_identifier_factory_