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