FrequencyFilter.cpp
Go to the documentation of this file.
1 #include "FrequencyFilter.h"
2 
3 #include <chrono>
4 
6 {
7 
8  bool
10  {
11  //accepting to many elements makes the filter slow and brings problems with the buffer with itself!
12 
13  // set default values for used variables:
14  auto start = std::chrono::high_resolution_clock::now();
15  bool instances_accepted = false;
16  std::int64_t current = 0;
17  // get entity id of this snapshot:
18  auto memID = e.id().getEntityID();
19 
20  if(this->lastTimesPerEntity.end() == this->lastTimesPerEntity.find(memID)){
21  //this happens if the key is not in the map, which means this is the first time this
22  //entity tries to save a snapshot
23  ARMARX_INFO << "First time this entity is saved";
24  auto firstIndex = e.getInstanceIndices()[0];
25  auto firstInsance = e.getInstance(firstIndex);
26  auto lastT = firstInsance.metadata().sentTime.toMilliSecondsSinceEpoch();
27  //for statistics sake:
28  auto end = std::chrono::high_resolution_clock::now();
29  stats.end_time = end;
30  stats.additional_time += (end - start);
31  stats.accepted += 1;
32  //add this last time to the map:
33  this->lastTimesPerEntity[memID] = lastT;
34  return true; //the first one is always accepted
35  } else {
36 
37  auto lastTime = this->lastTimesPerEntity.at(memID);
38 
39  // check if any one of the instances for this snapshot were sent in the last x
40  // milliseconds since a snapshot was accepted last for this entity:
41  e.forEachInstance([this, &instances_accepted, &current, &lastTime](armem::wm::EntityInstance& i){
42  int difference = std::abs(i.metadata().sentTime.toMilliSecondsSinceEpoch() - lastTime);
43  if(difference > this->maxDifference){ //at least one instance is older than the last saved instance
44  instances_accepted = true;
46  }
47  });
48 
49  if(instances_accepted){ //if one of the instances was accepted the time when an
50  //instance was accepted last needs to be changed
51  this->lastTimesPerEntity[memID] = current;
52  }
53  }
54 
55  //set stats:
56  auto end = std::chrono::high_resolution_clock::now();
57  stats.end_time = end;
58  stats.additional_time += (end - start);
59 
60  if(instances_accepted){
61  this->stats.accepted += 1;
62  } else {
63  this->stats.rejected += 1;
64  }
65 
66  return instances_accepted;
67  }
68 
69  void
70  SnapshotFrequencyFilter::configure(const nlohmann::json& json)
71  {
72  if (json.find(PARAM_WAITING_TIME) != json.end())
73  {
74  this->maxDifference = json.at(PARAM_WAITING_TIME);
75  ARMARX_INFO << VAROUT(maxDifference);
76  stats.additional_info = "Max Difference in ms: " + std::to_string(this->maxDifference);
77  }
78  stats.start_time = std::chrono::high_resolution_clock::now();
80  stats.similarity_type = aron::similarity::NDArraySimilarity::Type::NONE; //information for statistics export
81  }
82 
84  {
85  stats.end_time = std::chrono::high_resolution_clock::now();
86  return this->stats;
87  }
88 
90  {
91  return this->NAME;
92  }
93 } // namespace armarx::armem::server::ltm::processor::filter
armarx::armem::base::EntitySnapshotBase::getInstance
EntityInstanceT & getInstance(int index)
Get the given instance.
Definition: EntitySnapshotBase.h:114
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::configure
void configure(const nlohmann::json &json) override
Definition: FrequencyFilter.cpp:70
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::end_time
std::chrono::high_resolution_clock::time_point end_time
Definition: Filter.h:42
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::accept
virtual bool accept(const armem::wm::EntitySnapshot &e) override
Definition: FrequencyFilter.cpp:9
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::base::EntitySnapshotBase::forEachInstance
bool forEachInstance(InstanceFunctionT &&func)
Definition: EntitySnapshotBase.h:178
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::getName
std::string getName() override
Definition: FrequencyFilter.cpp:89
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::rejected
double rejected
Definition: Filter.h:37
armarx::armem::base::EntitySnapshotBase::getInstanceIndices
std::vector< int > getInstanceIndices() const
Definition: EntitySnapshotBase.h:231
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::armem::base::EntityInstanceMetadata::sentTime
Time sentTime
Time when this value was sent to the memory.
Definition: EntityInstanceBase.h:49
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::NAME
static const constexpr char * NAME
Definition: FrequencyFilter.h:12
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::PARAM_WAITING_TIME
static const constexpr char * PARAM_WAITING_TIME
Definition: FrequencyFilter.h:13
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::number_of_compared_objects
int number_of_compared_objects
Definition: Filter.h:43
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::start_time
std::chrono::high_resolution_clock::time_point start_time
Definition: Filter.h:41
armarx::armem::server::ltm::processor::SnapshotFilter::stats
struct armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics stats
armarx::armem::wm::EntitySnapshot
Client-side working memory entity snapshot.
Definition: memory_definitions.h:80
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::getFilterStatistics
FilterStatistics getFilterStatistics() override
Definition: FrequencyFilter.cpp:83
armarx::core::time::DateTime::toMilliSecondsSinceEpoch
std::int64_t toMilliSecondsSinceEpoch() const
Definition: DateTime.cpp:102
armarx::armem::MemoryID::getEntityID
MemoryID getEntityID() const
Definition: MemoryID.cpp:305
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
FrequencyFilter.h
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::additional_info
std::string additional_info
Definition: Filter.h:39
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::similarity_type
aron::similarity::NDArraySimilarity::Type similarity_type
Definition: Filter.h:40
armarx::armem::server::ltm::processor::filter
Definition: EqualityFilter.cpp:9
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics
Definition: Filter.h:35
armarx::armem::base::EntityInstanceBase::metadata
MetadataT & metadata()
Definition: EntityInstanceBase.h:117
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::accepted
double accepted
Definition: Filter.h:36
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::additional_time
std::chrono::duration< double > additional_time
Definition: Filter.h:38