00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __PION_RULECHAIN_HEADER__
00021 #define __PION_RULECHAIN_HEADER__
00022
00023 #include <string>
00024 #include <vector>
00025 #include <libxml/tree.h>
00026 #include <pion/PionConfig.hpp>
00027 #include <pion/PionException.hpp>
00028 #include <pion/platform/Event.hpp>
00029 #include <pion/platform/Vocabulary.hpp>
00030 #include <pion/platform/Comparison.hpp>
00031
00032
00033 namespace pion {
00034 namespace platform {
00035
00036
00037 class PION_PLATFORM_API RuleChain
00038 {
00039 public:
00040
00042 class UnknownTermException : public PionException {
00043 public:
00044 UnknownTermException(const std::string& term)
00045 : PionException("Unable to find required Vocabulary term for Comparison rule: ", term) {}
00046 };
00047
00049 class EmptyTermException : public std::exception {
00050 public:
00051 virtual const char* what() const throw() {
00052 return "Comparison rule configuration is missing a term identifier";
00053 }
00054 };
00055
00057 class EmptyTypeException : public std::exception {
00058 public:
00059 virtual const char* what() const throw() {
00060 return "Comparison rule configuration does not include a comparison type";
00061 }
00062 };
00063
00065 class EmptyValueException : public std::exception {
00066 public:
00067 virtual const char* what() const throw() {
00068 return "Comparison rule configuration is missing a required comparison value";
00069 }
00070 };
00071
00072
00074 RuleChain(void) {}
00075
00077 virtual ~RuleChain() {}
00078
00086 virtual void setConfig(const pion::platform::Vocabulary& v, const xmlNodePtr config_ptr);
00087
00094 virtual void updateVocabulary(const pion::platform::Vocabulary& v);
00095
00102 inline bool operator()(const pion::platform::EventPtr& e) const;
00103
00104
00105 private:
00106
00108 typedef std::vector<pion::platform::Comparison> ComparisonVector;
00109
00110
00112 static const std::string COMPARISON_ELEMENT_NAME;
00113
00115 static const std::string TERM_ELEMENT_NAME;
00116
00118 static const std::string TYPE_ELEMENT_NAME;
00119
00121 static const std::string VALUE_ELEMENT_NAME;
00122
00124 static const std::string MATCH_ALL_VALUES_ELEMENT_NAME;
00125
00127 static const std::string MATCH_ALL_COMPARISONS_ELEMENT_NAME;
00128
00129
00131 ComparisonVector m_comparisons;
00132
00135 bool m_match_all_comparisons;
00136 };
00137
00138
00139
00140
00141 inline bool RuleChain::operator()(const pion::platform::EventPtr& e) const
00142 {
00143 ComparisonVector::const_iterator i = m_comparisons.begin();
00144
00145 if (m_match_all_comparisons) {
00146
00147 for (; i != m_comparisons.end(); ++i) {
00148 if (! i->evaluate(*e) )
00149 return false;
00150 }
00151 } else {
00152
00153 for (; i != m_comparisons.end(); ++i) {
00154 if ( i->evaluate(*e) )
00155 break;
00156 }
00157
00158 if ( i == m_comparisons.end() && ! m_comparisons.empty() )
00159 return false;
00160 }
00161
00162 return true;
00163 }
00164
00165
00166 }
00167 }
00168
00169 #endif