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 #include "Log.h"
18 
19 #include "condition_util.h"
20 
21 #include "../matchers/matcher_util.h"
22 #include "ConditionTracker.h"
23 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
24 #include "stats_util.h"
25 
26 namespace android {
27 namespace os {
28 namespace statsd {
29 
30 using std::vector;
31 
32 
evaluateCombinationCondition(const std::vector<int> & children,const LogicalOperation & operation,const std::vector<ConditionState> & conditionCache)33 ConditionState evaluateCombinationCondition(const std::vector<int>& children,
34                                             const LogicalOperation& operation,
35                                             const std::vector<ConditionState>& conditionCache) {
36     ConditionState newCondition;
37 
38     bool hasUnknown = false;
39     bool hasFalse = false;
40     bool hasTrue = false;
41 
42     for (auto childIndex : children) {
43         ConditionState childState = conditionCache[childIndex];
44         if (childState == ConditionState::kUnknown) {
45             hasUnknown = true;
46             break;
47         }
48         if (childState == ConditionState::kFalse) {
49             hasFalse = true;
50         }
51         if (childState == ConditionState::kTrue) {
52             hasTrue = true;
53         }
54     }
55 
56     // If any child condition is in unknown state, the condition is unknown too.
57     if (hasUnknown) {
58         return ConditionState::kUnknown;
59     }
60 
61     switch (operation) {
62         case LogicalOperation::AND: {
63             newCondition = hasFalse ? ConditionState::kFalse : ConditionState::kTrue;
64             break;
65         }
66         case LogicalOperation::OR: {
67             newCondition = hasTrue ? ConditionState::kTrue : ConditionState::kFalse;
68             break;
69         }
70         case LogicalOperation::NOT:
71             newCondition = children.empty() ? ConditionState::kUnknown :
72                               ((conditionCache[children[0]] == ConditionState::kFalse) ?
73                                   ConditionState::kTrue : ConditionState::kFalse);
74             break;
75         case LogicalOperation::NAND:
76             newCondition = hasFalse ? ConditionState::kTrue : ConditionState::kFalse;
77             break;
78         case LogicalOperation::NOR:
79             newCondition = hasTrue ? ConditionState::kFalse : ConditionState::kTrue;
80             break;
81         case LogicalOperation::LOGICAL_OPERATION_UNSPECIFIED:
82             newCondition = ConditionState::kFalse;
83             break;
84     }
85     return newCondition;
86 }
87 
operator |(ConditionState l,ConditionState r)88 ConditionState operator|(ConditionState l, ConditionState r) {
89     return l >= r ? l : r;
90 }
91 }  // namespace statsd
92 }  // namespace os
93 }  // namespace android
94