1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "config/objectrule.h"
21 #include "base/logger_fwd.h"
23 using namespace icinga;
25 ObjectRule::RuleMap ObjectRule::m_Rules;
26 ObjectRule::CallbackMap ObjectRule::m_Callbacks;
28 ObjectRule::ObjectRule(const String& name, const AExpression::Ptr& expression,
29 const AExpression::Ptr& filter, const DebugInfo& di, const Dictionary::Ptr& scope)
30 : m_Name(name), m_Expression(expression), m_Filter(filter), m_DebugInfo(di), m_Scope(scope)
33 String ObjectRule::GetName(void) const
38 AExpression::Ptr ObjectRule::GetExpression(void) const
43 AExpression::Ptr ObjectRule::GetFilter(void) const
48 DebugInfo ObjectRule::GetDebugInfo(void) const
53 Dictionary::Ptr ObjectRule::GetScope(void) const
58 void ObjectRule::AddRule(const String& sourceType, const String& name,
59 const AExpression::Ptr& expression, const AExpression::Ptr& filter,
60 const DebugInfo& di, const Dictionary::Ptr& scope)
62 m_Rules[sourceType].push_back(ObjectRule(name, expression, filter, di, scope));
65 bool ObjectRule::EvaluateFilter(const Dictionary::Ptr& scope) const
67 scope->Set("__parent", m_Scope);
68 bool result = m_Filter->Evaluate(scope);
69 scope->Remove("__parent");
73 void ObjectRule::EvaluateRules(bool clear)
75 std::pair<String, Callback> kv;
76 BOOST_FOREACH(kv, m_Callbacks) {
77 const Callback& callback = kv.second;
79 RuleMap::const_iterator it = m_Rules.find(kv.first);
81 if (it == m_Rules.end())
91 void ObjectRule::RegisterType(const String& sourceType, const ObjectRule::Callback& callback)
93 m_Callbacks[sourceType] = callback;
96 bool ObjectRule::IsValidSourceType(const String& sourceType)
98 return m_Callbacks.find(sourceType) != m_Callbacks.end();