1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 
17 #ifndef COMBINATION_CONDITION_TRACKER_H
18 #define COMBINATION_CONDITION_TRACKER_H
19 
20 #include "ConditionTracker.h"
21 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
22 
23 namespace android {
24 namespace os {
25 namespace statsd {
26 
27 class CombinationConditionTracker : public virtual ConditionTracker {
28 public:
29     CombinationConditionTracker(const int64_t& id, const int index);
30 
31     ~CombinationConditionTracker();
32 
33     bool init(const std::vector<Predicate>& allConditionConfig,
34               const std::vector<sp<ConditionTracker>>& allConditionTrackers,
35               const std::unordered_map<int64_t, int>& conditionIdIndexMap,
36               std::vector<bool>& stack) override;
37 
38     void evaluateCondition(const LogEvent& event,
39                            const std::vector<MatchingState>& eventMatcherValues,
40                            const std::vector<sp<ConditionTracker>>& mAllConditions,
41                            std::vector<ConditionState>& conditionCache,
42                            std::vector<bool>& changedCache) override;
43 
44     void isConditionMet(const ConditionKey& conditionParameters,
45                         const std::vector<sp<ConditionTracker>>& allConditions,
46                         const vector<Matcher>& dimensionFields,
47                         const bool isSubOutputDimensionFields,
48                         const bool isPartialLink,
49                         std::vector<ConditionState>& conditionCache,
50                         std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const override;
51 
52     ConditionState getMetConditionDimension(
53             const std::vector<sp<ConditionTracker>>& allConditions,
54             const vector<Matcher>& dimensionFields,
55             const bool isSubOutputDimensionFields,
56             std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const override;
57 
58     // Only one child predicate can have dimension.
getChangedToTrueDimensions(const std::vector<sp<ConditionTracker>> & allConditions)59     const std::set<HashableDimensionKey>* getChangedToTrueDimensions(
60             const std::vector<sp<ConditionTracker>>& allConditions) const override {
61         for (const auto& child : mChildren) {
62             auto result = allConditions[child]->getChangedToTrueDimensions(allConditions);
63             if (result != nullptr) {
64                 return result;
65             }
66         }
67         return nullptr;
68     }
69 
70     // Only one child predicate can have dimension.
getChangedToFalseDimensions(const std::vector<sp<ConditionTracker>> & allConditions)71     const std::set<HashableDimensionKey>* getChangedToFalseDimensions(
72             const std::vector<sp<ConditionTracker>>& allConditions) const override {
73         for (const auto& child : mChildren) {
74             auto result = allConditions[child]->getChangedToFalseDimensions(allConditions);
75             if (result != nullptr) {
76                 return result;
77             }
78         }
79         return nullptr;
80     }
81 
IsSimpleCondition()82     bool IsSimpleCondition() const  override { return false; }
83 
IsChangedDimensionTrackable()84     bool IsChangedDimensionTrackable() const  override {
85         return mLogicalOperation == LogicalOperation::AND && mSlicedChildren.size() == 1;
86     }
87 
88     bool equalOutputDimensions(
89         const std::vector<sp<ConditionTracker>>& allConditions,
90         const vector<Matcher>& dimensions) const override;
91 
getTrueSlicedDimensions(const std::vector<sp<ConditionTracker>> & allConditions,std::set<HashableDimensionKey> * dimensions)92     void getTrueSlicedDimensions(
93             const std::vector<sp<ConditionTracker>>& allConditions,
94             std::set<HashableDimensionKey>* dimensions) const override {
95         if (mSlicedChildren.size() == 1) {
96             return allConditions[mSlicedChildren.front()]->getTrueSlicedDimensions(
97                 allConditions, dimensions);
98         }
99     }
100 
101 
102 private:
103     LogicalOperation mLogicalOperation;
104 
105     // Store index of the children Predicates.
106     // We don't store string name of the Children, because we want to get rid of the hash map to
107     // map the name to object. We don't want to store smart pointers to children, because it
108     // increases the risk of circular dependency and memory leak.
109     std::vector<int> mChildren;
110 
111     std::vector<int> mSlicedChildren;
112     std::vector<int> mUnSlicedChildren;
113 
114 };
115 
116 }  // namespace statsd
117 }  // namespace os
118 }  // namespace android
119 
120 #endif  // COMBINATION_CONDITION_TRACKER_H
121