-
Notifications
You must be signed in to change notification settings - Fork 63
/
CDetectionRule.cc
86 lines (73 loc) · 2.32 KB
/
CDetectionRule.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
#include <model/CDetectionRule.h>
#include <model/CAnomalyDetectorModel.h>
#include <model/CDataGatherer.h>
namespace ml {
namespace model {
void CDetectionRule::action(int action) {
m_Action = action;
}
void CDetectionRule::includeScope(std::string field, const core::CPatternSet& filter) {
m_Scope.include(field, filter);
}
void CDetectionRule::excludeScope(std::string field, const core::CPatternSet& filter) {
m_Scope.exclude(field, filter);
}
void CDetectionRule::addCondition(const CRuleCondition& condition) {
m_Conditions.push_back(condition);
}
bool CDetectionRule::apply(ERuleAction action,
const CAnomalyDetectorModel& model,
model_t::EFeature feature,
const model_t::CResultType& resultType,
std::size_t pid,
std::size_t cid,
core_t::TTime time) const {
if (!(m_Action & action)) {
return false;
}
if (m_Scope.check(model, pid, cid) == false) {
return false;
}
for (const auto& condition : m_Conditions) {
if (condition.test(model, feature, resultType, pid, cid, time) == false) {
return false;
}
}
return true;
}
std::string CDetectionRule::print() const {
std::string result = this->printAction();
result += " IF ";
std::string scopeString = m_Scope.print();
result += scopeString;
if (scopeString.empty() == false && m_Conditions.empty() == false) {
result += " AND ";
}
for (std::size_t i = 0; i < m_Conditions.size(); ++i) {
result += m_Conditions[i].print();
if (i < m_Conditions.size() - 1) {
result += " AND ";
}
}
return result;
}
std::string CDetectionRule::printAction() const {
std::string result;
if (E_SkipResult & m_Action) {
result += "SKIP_RESULT";
}
if (E_SkipModelUpdate & m_Action) {
if (result.empty() == false) {
result += " AND ";
}
result += "SKIP_MODEL_UPDATE";
}
return result;
}
}
}