Home | History | Annotate | Download | only in rule
      1 /*
      2  * Copyright (C) 2015 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 package com.android.loganalysis.rule;
     17 
     18 import com.android.loganalysis.item.BugreportItem;
     19 
     20 import org.json.JSONArray;
     21 
     22 import java.util.Collection;
     23 import java.util.LinkedList;
     24 
     25 
     26 /**
     27  * Applies rules to the parsed bugreport
     28  */
     29 public class RuleEngine {
     30 
     31     public enum RuleType{
     32         ALL, POWER;
     33     }
     34 
     35     BugreportItem mBugreportItem;
     36     private Collection<IRule> mRulesList;
     37 
     38     public RuleEngine(BugreportItem bugreportItem) {
     39         mBugreportItem = bugreportItem;
     40         mRulesList = new LinkedList<IRule>();
     41     }
     42 
     43     public void registerRules(RuleType ruleType) {
     44         if (ruleType == RuleType.ALL) {
     45             // add all rules
     46             addPowerRules();
     47         } else if (ruleType == RuleType.POWER) {
     48             addPowerRules();
     49         }
     50     }
     51 
     52     public void executeRules() {
     53         for (IRule rule : mRulesList) {
     54             rule.applyRule();
     55         }
     56     }
     57 
     58     public JSONArray getAnalysis() {
     59         JSONArray result = new JSONArray();
     60         for (IRule rule : mRulesList) {
     61             result.put(rule.getAnalysis());
     62         }
     63         return result;
     64     }
     65 
     66     private void addPowerRules() {
     67         mRulesList.add(new WakelockRule(mBugreportItem));
     68         mRulesList.add(new ProcessUsageRule(mBugreportItem));
     69         mRulesList.add(new LocationUsageRule(mBugreportItem));
     70         mRulesList.add(new WifiStatsRule(mBugreportItem));
     71         mRulesList.add(new InterruptRule(mBugreportItem));
     72     }
     73 }
     74