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  //find out if timestamps are corrupted:
21  if (this->nonCorruptedType == TimestampType::NOT_SET)
22  {
23  ARMARX_DEBUG << "Setting timestamp type";
24  this->setNonCorruptedTimestampType(e);
25  ARMARX_DEBUG << VAROUT(this->nonCorruptedType);
26  }
27 
28  if (this->nonCorruptedType == TimestampType::ALL_CORRUPTED)
29  {
30  return true;
31  }
32 
33  if (this->lastTimesPerEntity.end() == this->lastTimesPerEntity.find(memID))
34  {
35  //this happens if the key is not in the map, which means this is the first time this
36  //entity tries to save a snapshot
37  ARMARX_DEBUG << "First time this entity is saved";
38  auto firstIndex = e.getInstanceIndices()[0];
39  auto firstInsance = e.getInstance(firstIndex);
40  auto lastT = this->getNonCorruptedTimestamp(firstInsance, simulatedVersion);
41  //for statistics sake:
42  auto end = std::chrono::high_resolution_clock::now();
43  stats.end_time = end;
44  stats.additional_time += (end - start);
45  stats.accepted += 1;
46  //add this last time to the map:
47  this->lastTimesPerEntity[memID] = lastT;
48  return true; //the first one is always accepted
49  }
50  else
51  {
52  auto lastTime = this->lastTimesPerEntity.at(memID);
53 
54  // check if any one of the instances for this snapshot were sent in the last x
55  // milliseconds since a snapshot was accepted last for this entity:
57  [this, &instances_accepted, &current, &lastTime, &simulatedVersion](
59  {
60  auto t = this->getNonCorruptedTimestamp(i, simulatedVersion);
61  int difference = std::abs(t - lastTime);
62  if (difference > this->maxDifference)
63  { //at least one instance is older than the last saved instance
64  instances_accepted = true;
65  current = this->getNonCorruptedTimestamp(i, simulatedVersion);
66  }
67  });
68 
69  if (instances_accepted)
70  { //if one of the instances was accepted the time when an
71  //instance was accepted last needs to be changed
72  this->lastTimesPerEntity[memID] = current;
73  }
74  }
75 
76  //set stats:
77  auto end = std::chrono::high_resolution_clock::now();
78  stats.end_time = end;
79  stats.additional_time += (end - start);
80 
81  if (instances_accepted)
82  {
83  this->stats.accepted += 1;
84  }
85  else
86  {
87  this->stats.rejected += 1;
88  }
89 
90  return instances_accepted;
91  }
92 
93  void
94  SnapshotFrequencyFilter::setNonCorruptedTimestampType(const armem::wm::EntitySnapshot& e)
95  {
96  auto firstIndex = e.getInstanceIndices()[0];
97  auto firstInsance = e.getInstance(firstIndex);
98  auto sentTime = firstInsance.metadata().sentTime;
99  auto arrivedTime = firstInsance.metadata().arrivedTime;
100  auto referencedTime = firstInsance.metadata().referencedTime;
101 
102  ARMARX_DEBUG << VAROUT(sentTime.toMilliSecondsSinceEpoch());
103  ARMARX_DEBUG << VAROUT(arrivedTime.toMilliSecondsSinceEpoch());
104  ARMARX_DEBUG << VAROUT(referencedTime.toMilliSecondsSinceEpoch());
105 
106  //we want the referenced time, if not set correctly we take sent time, then arrvived time:
107  if (referencedTime.toMilliSecondsSinceEpoch() < 946688400000)
108  {
109  //timestamp corrupted:
110  if (sentTime.toMilliSecondsSinceEpoch() < 946688400000)
111  {
112  //sent also corrupted:
113  if (arrivedTime.toMilliSecondsSinceEpoch() < 946688400000)
114  {
115  //arrived also corrupted:
116  if (!corruptedWarningGiven)
117  {
118  ARMARX_WARNING << "LTM recording does not work correctly, as frequency "
119  "filter is used, but "
120  << "time sent, arrived and referenced are all corrupted. \n"
121  << "Accepting all snapshots.";
122  this->nonCorruptedType = TimestampType::ALL_CORRUPTED;
123  corruptedWarningGiven = true;
124  }
125  }
126  else
127  {
128  if (!corruptedWarningGiven)
129  { //only print this warning once
130  ARMARX_INFO << "Time referenced and sent for snapshot corrupted, using "
131  "time arrived for filtering";
132  corruptedWarningGiven = true;
133  }
134  this->nonCorruptedType = TimestampType::ARRIVED;
135  }
136  }
137  else
138  {
139  if (!corruptedWarningGiven)
140  { //only print this warning once
142  << "Time referenced snapshot corrupted, using time sent for filtering";
143  corruptedWarningGiven = true;
144  }
145  this->nonCorruptedType = TimestampType::SENT;
146  }
147  }
148  else
149  {
150  this->nonCorruptedType = TimestampType::REFERENCED;
151  }
152 
153  /**
154  if(sentTime.toMilliSecondsSinceEpoch() < 946688400000){
155  // we assume the timestamp does not make sense if it is older than the year 2000
156  if(arrivedTime.toMilliSecondsSinceEpoch() < 946688400000){
157  if(referencedTime.toMilliSecondsSinceEpoch() < 946688400000){
158  ARMARX_WARNING << "LTM recording does not work correctly, as frequency filter is used, but "
159  << "time sent, arrived and referenced are all corrupted. \n"
160  << "Accepting all snapshots.";
161  this->nonCorruptedType = TimestampType::ALL_CORRUPTED;
162  } else {
163  if(!corruptedWarningGiven){ //only print this warning once
164  ARMARX_INFO << "Time sent and arrived for snapshot corrupted, using time referenced for filtering";
165  corruptedWarningGiven = true;
166  }
167  //use time referenced from now on
168  this->nonCorruptedType = TimestampType::REFERENCED;
169  }
170  } else {
171  if(!corruptedWarningGiven){ //only print this warning once
172  ARMARX_INFO << "Time sent for snapshot corrupted, using time arrived for filtering";
173  corruptedWarningGiven = true;
174  }
175  //use time arrived from now on
176  this->nonCorruptedType = TimestampType::ARRIVED;
177  }
178  } else {
179  this->nonCorruptedType = TimestampType::SENT;
180  }
181  */
182  }
183 
184  int
185  SnapshotFrequencyFilter::getNonCorruptedTimestamp(const armem::wm::EntityInstance& i,
186  bool simulatedVersion)
187  {
188  switch (this->nonCorruptedType)
189  {
190  case TimestampType::SENT:
191  if (!simulatedVersion)
192  {
193  return i.metadata().sentTime.toMilliSecondsSinceEpoch();
194  }
195  else
196  {
197  [[fallthrough]];
198  }
200  return i.metadata().arrivedTime.toMilliSecondsSinceEpoch();
202  return i.metadata().referencedTime.toMilliSecondsSinceEpoch();
203  default:
204  return -1;
205  }
206  }
207 
208  void
209  SnapshotFrequencyFilter::configure(const nlohmann::json& json)
210  {
211  if (json.find(PARAM_WAITING_TIME) != json.end())
212  {
213  this->maxDifference = json.at(PARAM_WAITING_TIME);
214  ARMARX_INFO << VAROUT(maxDifference);
215  stats.additional_info = "Max Difference in ms: " + std::to_string(this->maxDifference);
216  }
217  stats.start_time = std::chrono::high_resolution_clock::now();
220  aron::similarity::NDArraySimilarity::Type::NONE; //information for statistics export
221  }
222 
225  {
226  stats.end_time = std::chrono::high_resolution_clock::now();
227  return this->stats;
228  }
229 
230  std::string
232  {
233  return this->NAME;
234  }
235 } // 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::wm::EntityInstance
armem::wm::EntityInstance EntityInstance
Definition: forward_declarations.h:65
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::configure
void configure(const nlohmann::json &json) override
Definition: FrequencyFilter.cpp:209
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::end_time
std::chrono::high_resolution_clock::time_point end_time
Definition: Filter.h:43
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:231
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::rejected
double rejected
Definition: Filter.h:38
armarx::armem::server::ltm::processor::filter::REFERENCED
@ REFERENCED
Definition: FrequencyFilter.h:13
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:281
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::NAME
static const constexpr char * NAME
Definition: FrequencyFilter.h:20
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::PARAM_WAITING_TIME
static const constexpr char * PARAM_WAITING_TIME
Definition: FrequencyFilter.h:21
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::number_of_compared_objects
int number_of_compared_objects
Definition: Filter.h:44
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::start_time
std::chrono::high_resolution_clock::time_point start_time
Definition: Filter.h:42
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:25
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::getFilterStatistics
FilterStatistics getFilterStatistics() override
Definition: FrequencyFilter.cpp:224
armarx::armem::server::ltm::processor::filter::NOT_SET
@ NOT_SET
Definition: FrequencyFilter.h:10
armarx::armem::MemoryID::getEntityID
MemoryID getEntityID() const
Definition: MemoryID.cpp:310
armarx::armem::server::ltm::processor::filter::SnapshotFrequencyFilter::accept
virtual bool accept(const armem::wm::EntitySnapshot &e, bool simulatedVersion) override
Definition: FrequencyFilter.cpp:9
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:14
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:198
FrequencyFilter.h
armarx::armem::server::ltm::processor::filter::SENT
@ SENT
Definition: FrequencyFilter.h:11
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::additional_info
std::string additional_info
Definition: Filter.h:40
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::similarity_type
aron::similarity::NDArraySimilarity::Type similarity_type
Definition: Filter.h:41
armarx::armem::server::ltm::processor::filter
Definition: EqualityFilter.cpp:10
armarx::armem::server::ltm::processor::filter::ALL_CORRUPTED
@ ALL_CORRUPTED
Definition: FrequencyFilter.h:14
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics
Definition: Filter.h:35
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::accepted
double accepted
Definition: Filter.h:37
armarx::armem::server::ltm::processor::filter::ARRIVED
@ ARRIVED
Definition: FrequencyFilter.h:12
armarx::armem::server::ltm::processor::SnapshotFilter::FilterStatistics::additional_time
std::chrono::duration< double > additional_time
Definition: Filter.h:39