1 /** 2 * Copyright © 2019 IBM Corporation 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 #pragma once 17 18 #include "action.hpp" 19 #include "action_environment.hpp" 20 #include "rule.hpp" 21 22 #include <string> 23 24 namespace phosphor::power::regulators 25 { 26 27 /** 28 * @class RunRuleAction 29 * 30 * Runs the specified rule. 31 * 32 * Implements the run_rule action in the JSON config file. 33 */ 34 class RunRuleAction : public Action 35 { 36 public: 37 // Specify which compiler-generated methods we want 38 RunRuleAction() = delete; 39 RunRuleAction(const RunRuleAction&) = delete; 40 RunRuleAction(RunRuleAction&&) = delete; 41 RunRuleAction& operator=(const RunRuleAction&) = delete; 42 RunRuleAction& operator=(RunRuleAction&&) = delete; 43 virtual ~RunRuleAction() = default; 44 45 /** 46 * Constructor. 47 * 48 * @param ruleID rule ID 49 */ 50 explicit RunRuleAction(const std::string& ruleID) : ruleID{ruleID} 51 {} 52 53 /** 54 * Executes this action. 55 * 56 * Runs the rule specified in the constructor. 57 * 58 * Returns the return value from the last action in the rule. 59 * 60 * Throws an exception if an error occurs and an action cannot be 61 * successfully executed. 62 * 63 * @param environment action execution environment 64 * @return return value from last action in rule 65 */ 66 virtual bool execute(ActionEnvironment& environment) override 67 { 68 // Increment rule call stack depth since we are running a rule. Rule 69 // depth is used to detect infinite recursion. 70 environment.incrementRuleDepth(ruleID); 71 72 // Execute rule 73 bool returnValue = environment.getRule(ruleID).execute(environment); 74 75 // Decrement rule depth since rule has returned 76 environment.decrementRuleDepth(); 77 78 return returnValue; 79 } 80 81 /** 82 * Returns the rule ID. 83 * 84 * @return rule ID 85 */ 86 const std::string& getRuleID() const 87 { 88 return ruleID; 89 } 90 91 /** 92 * Returns a string description of this action. 93 * 94 * @return description of action 95 */ 96 virtual std::string toString() const override 97 { 98 return "run_rule: " + ruleID; 99 } 100 101 private: 102 /** 103 * Rule ID. 104 */ 105 const std::string ruleID{}; 106 }; 107 108 } // namespace phosphor::power::regulators 109