m2etis  0.4
EqualsPredicateIndex.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_EQUALSPREDICATEINDEX_H__
23 #define __M2ETIS_PUBSUB_FILTER_GENERALBOOLEANALGEBRA_EQUALSPREDICATEINDEX_H__
24 
26 
27 namespace m2etis {
28 namespace pubsub {
29 namespace filter {
30 
31  template <typename EventType, typename AttributeType>
32  class EqualsPredicateIndex : public PredicateIndex<EventType> {
33  public:
34  EqualsPredicateIndex(AttributeName attribute_id, PredicateIdentifierFactory* predicate_identifier_factory) : PredicateIndex<EventType>(attribute_id, EQUALS, 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(equals_attribute_filter->get_constants()[0]) != predicate_id_to_attribute_value_.end()) {
40  return predicate_id_to_attribute_value_[equals_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>(equals_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 the constant having the same value (which all have the same predicate id)
63  auto attribute_value_iterator = attributes_accessor_map.find(this->get_attribute_id());
64 
65  if (attribute_value_iterator == attributes_accessor_map.end()) {
66  M2ETIS_THROW_API("EqualsPredicateIndex function determineMatchingPredicates", "Attribute not found in attribute map.");
67  }
68 
69  fulfilled_predicate_vector[predicate_id_to_attribute_value_[(dynamic_cast<AttributeAccessor<EventType, AttributeType> *>(attribute_value_iterator->second.get()))->getAttributeValue(event)]] = true;
70  }
71 
72  virtual ~EqualsPredicateIndex() {}
73 
74  private:
75  // hashmap as index structure to determine matching predicates efficiently
76  // matches a value to the predicate identifier:
77  // identical indexes get assigned the same predicate identifier
78  std::map<AttributeType, PredicateIdentifierFactory::PredicateID > predicate_id_to_attribute_value_;
79  };
80 
81 } /* namespace filter */
82 } /* namespace pubsub */
83 } /* namespace m2etis */
84 
85 #endif /* __M2ETIS_PUBSUB_FILTER_GENERALBOOLEANALGEBRA_EQUALSPREDICATEINDEX_H__ */
86 
PredicateIdentifierFactory::PredicateID addPredicate(const EqualsAttributeFilter< EventType, AttributeType > *equals_attribute_filter)
const std::vector< AttributeType > get_constants() const
EqualsPredicateIndex(AttributeName attribute_id, PredicateIdentifierFactory *predicate_identifier_factory)
virtual void determineMatchingPredicates(const EventType &event, std::vector< bool > &fulfilled_predicate_vector) override
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
AttributeName get_attribute_id() const
#define M2ETIS_THROW_API(module, message)
throws on wrong API usage
Definition: Exceptions.h:40
PredicateIdentifierFactory * predicate_identifier_factory_