| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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 | |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
| 18 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 19 | #include "condition_util.h" |
| 20 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 21 | #include <log/event_tag_map.h> |
| 22 | #include <log/log_event_list.h> |
| 23 | #include <log/logprint.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <unordered_map> |
| Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 26 | #include "../matchers/matcher_util.h" |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 27 | #include "ConditionTracker.h" |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 28 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 29 | #include "stats_util.h" |
| 30 | |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 35 | using std::set; |
| 36 | using std::string; |
| 37 | using std::unordered_map; |
| 38 | using std::vector; |
| 39 | |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 40 | |
| 41 | ConditionState evaluateCombinationCondition(const std::vector<int>& children, |
| 42 | const LogicalOperation& operation, |
| 43 | const std::vector<ConditionState>& conditionCache) { |
| 44 | ConditionState newCondition; |
| 45 | |
| 46 | bool hasUnknown = false; |
| 47 | bool hasFalse = false; |
| 48 | bool hasTrue = false; |
| 49 | |
| 50 | for (auto childIndex : children) { |
| 51 | ConditionState childState = conditionCache[childIndex]; |
| 52 | if (childState == ConditionState::kUnknown) { |
| 53 | hasUnknown = true; |
| 54 | break; |
| 55 | } |
| 56 | if (childState == ConditionState::kFalse) { |
| 57 | hasFalse = true; |
| 58 | } |
| 59 | if (childState == ConditionState::kTrue) { |
| 60 | hasTrue = true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // If any child condition is in unknown state, the condition is unknown too. |
| 65 | if (hasUnknown) { |
| 66 | return ConditionState::kUnknown; |
| 67 | } |
| 68 | |
| 69 | switch (operation) { |
| 70 | case LogicalOperation::AND: { |
| 71 | newCondition = hasFalse ? ConditionState::kFalse : ConditionState::kTrue; |
| 72 | break; |
| 73 | } |
| 74 | case LogicalOperation::OR: { |
| 75 | newCondition = hasTrue ? ConditionState::kTrue : ConditionState::kFalse; |
| 76 | break; |
| 77 | } |
| 78 | case LogicalOperation::NOT: |
| 79 | newCondition = (conditionCache[children[0]] == ConditionState::kFalse) |
| 80 | ? ConditionState::kTrue |
| 81 | : ConditionState::kFalse; |
| 82 | break; |
| 83 | case LogicalOperation::NAND: |
| 84 | newCondition = hasFalse ? ConditionState::kTrue : ConditionState::kFalse; |
| 85 | break; |
| 86 | case LogicalOperation::NOR: |
| 87 | newCondition = hasTrue ? ConditionState::kFalse : ConditionState::kTrue; |
| 88 | break; |
| Stefan Lafon | cfed20b | 2017-11-18 09:26:53 -0800 | [diff] [blame] | 89 | case LogicalOperation::LOGICAL_OPERATION_UNSPECIFIED: |
| 90 | newCondition = ConditionState::kFalse; |
| 91 | break; |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 92 | } |
| 93 | return newCondition; |
| 94 | } |
| 95 | |
| Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 96 | ConditionState operator|(ConditionState l, ConditionState r) { |
| 97 | return l >= r ? l : r; |
| 98 | } |
| Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 99 | } // namespace statsd |
| 100 | } // namespace os |
| 101 | } // namespace android |