| 64 | } |
| 65 | |
| 66 | bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter) |
| 67 | { |
| 68 | auto& di (rule.GetDebugInfo()); |
| 69 | |
| 70 | CONTEXT("Evaluating 'apply' rule (" << di << ")"); |
| 71 | |
| 72 | Host::Ptr host; |
| 73 | Service::Ptr service; |
| 74 | tie(host, service) = GetHostService(checkable); |
| 75 | |
| 76 | ScriptFrame frame(true); |
| 77 | if (rule.GetScope()) |
| 78 | rule.GetScope()->CopyTo(frame.Locals); |
| 79 | frame.Locals->Set("host", host); |
| 80 | if (service) |
| 81 | frame.Locals->Set("service", service); |
| 82 | |
| 83 | Value vinstances; |
| 84 | |
| 85 | if (rule.GetFTerm()) { |
| 86 | try { |
| 87 | vinstances = rule.GetFTerm()->Evaluate(frame); |
| 88 | } catch (const std::exception&) { |
| 89 | /* Silently ignore errors here and assume there are no instances. */ |
| 90 | return false; |
| 91 | } |
| 92 | } else { |
| 93 | vinstances = new Array({ "" }); |
| 94 | } |
| 95 | |
| 96 | bool match = false; |
| 97 | |
| 98 | if (vinstances.IsObjectType<Array>()) { |
| 99 | if (!rule.GetFVVar().IsEmpty()) |
| 100 | BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di)); |
| 101 | |
| 102 | Array::Ptr arr = vinstances; |
| 103 | |
| 104 | ObjectLock olock(arr); |
| 105 | for (const Value& instance : arr) { |
| 106 | String name = rule.GetName(); |
| 107 | |
| 108 | if (!rule.GetFKVar().IsEmpty()) { |
| 109 | frame.Locals->Set(rule.GetFKVar(), instance); |
| 110 | name += instance; |
| 111 | } |
| 112 | |
| 113 | if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter)) |
| 114 | match = true; |
| 115 | } |
| 116 | } else if (vinstances.IsObjectType<Dictionary>()) { |
| 117 | if (rule.GetFVVar().IsEmpty()) |
| 118 | BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di)); |
| 119 | |
| 120 | Dictionary::Ptr dict = vinstances; |
| 121 | |
| 122 | for (const String& key : dict->GetKeys()) { |
| 123 | frame.Locals->Set(rule.GetFKVar(), key); |