m2etis  0.4
NotEqualsPredicateIndex.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_NOTEQUALSPREDICATEINDEX_H__
23 #define __M2ETIS_PUBSUB_FILTER_GENERALBOOLEANALGEBRA_NOTEQUALSPREDICATEINDEX_H__
24 
26 
27 namespace m2etis {
28 namespace pubsub {
29 namespace filter {
30 
31  template <typename EventType, typename AttributeType>
32  class NotEqualsPredicateIndex : public PredicateIndex<EventType> {
33  public:
34  NotEqualsPredicateIndex(AttributeName attribute_id, PredicateIdentifierFactory * predicate_identifier_factory) : PredicateIndex<EventType>(attribute_id, NOTEQUALS, 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(notequals_attribute_filter->get_constants()[0]) != predicate_id_to_attribute_value_.end()) {
40  return predicate_id_to_attribute_value_[notequals_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 >(notequals_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("NotEqualsPredicateIndex function determineMatchingPredicates", "Attribute not found in attribute map.");
67  }
68 
69  for (auto attributevalue_predicateId_pair : predicate_id_to_attribute_value_) {
70  if (attributevalue_predicateId_pair.first != (dynamic_cast<AttributeAccessor<EventType, AttributeType> *>(attribute_value_iterator->second.get()))->getAttributeValue(event)) {
71  fulfilled_predicate_vector[attributevalue_predicateId_pair.second] = true;
72  }
73  }
74  }
75 
77 
78  private:
79  // hashmap as index structure to determine matching predicates efficiently
80  // matches a value to the predicate identifier:
81  // identical indexes get assigned the same predicate identifier
82  std::map<AttributeType, PredicateIdentifierFactory::PredicateID> predicate_id_to_attribute_value_;
83  };
84 
85 } /* namespace filter */
86 } /* namespace pubsub */
87 } /* namespace m2etis */
88 
89 #endif /* __M2ETIS_PUBSUB_FILTER_GENERALBOOLEANALGEBRA_NOTEQUALSPREDICATEINDEX_H__ */
90 
virtual void removePredicate(std::vector< bool >::size_type predicate_id) override
virtual void determineMatchingPredicates(const EventType &event, std::vector< bool > &fulfilled_predicate_vector) override
const std::vector< AttributeType > get_constants() const
M2ETIS_API std::map< filter::AttributeName, std::shared_ptr< filter::AttributeAccessor_Basic > > attributes_accessor_map
PredicateIdentifierFactory::PredicateID addPredicate(const NotEqualsAttributeFilter< EventType, AttributeType > *notequals_attribute_filter)
AttributeName get_attribute_id() const
#define M2ETIS_THROW_API(module, message)
throws on wrong API usage
Definition: Exceptions.h:40
NotEqualsPredicateIndex(AttributeName attribute_id, PredicateIdentifierFactory *predicate_identifier_factory)
PredicateIdentifierFactory * predicate_identifier_factory_