Home | History | Annotate | Download | only in condition
      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 <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>
     26 #include "../matchers/matcher_util.h"
     27 #include "ConditionTracker.h"
     28 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
     29 #include "stats_util.h"
     30 
     31 namespace android {
     32 namespace os {
     33 namespace statsd {
     34 
     35 using std::set;
     36 using std::string;
     37 using std::unordered_map;
     38 using std::vector;
     39 
     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;
     89         case LogicalOperation::LOGICAL_OPERATION_UNSPECIFIED:
     90             newCondition = ConditionState::kFalse;
     91             break;
     92     }
     93     return newCondition;
     94 }
     95 
     96 ConditionState operator|(ConditionState l, ConditionState r) {
     97     return l >= r ? l : r;
     98 }
     99 }  // namespace statsd
    100 }  // namespace os
    101 }  // namespace android
    102