blob: 691356b5edc62fd416a55ab96476acff7b0bebd8 [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001/*
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 Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
18
Yao Chencaf339d2017-10-06 16:01:10 -070019#include "condition_util.h"
20
Yao Chencaf339d2017-10-06 16:01:10 -070021#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 Chen729093d2017-10-16 10:33:26 -070026#include "../matchers/matcher_util.h"
Yao Chencaf339d2017-10-06 16:01:10 -070027#include "ConditionTracker.h"
Yao Chencaf339d2017-10-06 16:01:10 -070028#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
29#include "stats_util.h"
30
Joe Onorato9fc9edf2017-10-15 20:08:52 -070031namespace android {
32namespace os {
33namespace statsd {
34
Yao Chencaf339d2017-10-06 16:01:10 -070035using std::set;
36using std::string;
37using std::unordered_map;
38using std::vector;
39
Yao Chencaf339d2017-10-06 16:01:10 -070040
41ConditionState 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 Lafoncfed20b2017-11-18 09:26:53 -080089 case LogicalOperation::LOGICAL_OPERATION_UNSPECIFIED:
90 newCondition = ConditionState::kFalse;
91 break;
Yao Chencaf339d2017-10-06 16:01:10 -070092 }
93 return newCondition;
94}
95
Yangster-mac20877162017-12-22 17:19:39 -080096ConditionState operator|(ConditionState l, ConditionState r) {
97 return l >= r ? l : r;
98}
Yao Chencaf339d2017-10-06 16:01:10 -070099} // namespace statsd
100} // namespace os
101} // namespace android