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