platform/include/pion/platform/RuleChain.hpp

00001 // ------------------------------------------------------------------------
00002 // Pion is a development platform for building Reactors that process Events
00003 // ------------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Pion is free software: you can redistribute it and/or modify it under the
00007 // terms of the GNU Affero General Public License as published by the Free
00008 // Software Foundation, either version 3 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Pion is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Affero General Public License
00017 // along with Pion.  If not, see <http://www.gnu.org/licenses/>.
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 {        // begin namespace pion
00034 namespace platform {    // begin namespace platform (Pion Platform Library)
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 // inline members for RuleChain
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         // all comparisons in the rule chain must pass for the Event to be delivered
00147         for (; i != m_comparisons.end(); ++i) {
00148             if (! i->evaluate(*e) )
00149                 return false;
00150         }
00151     } else {
00152         // any one comparison in the rule chain must pass for the Event to be delivered
00153         for (; i != m_comparisons.end(); ++i) {
00154             if ( i->evaluate(*e) )
00155                 break;
00156         }
00157         // return if none matched & at least one rule is defined
00158         if ( i == m_comparisons.end() && ! m_comparisons.empty() )
00159             return false;
00160     }
00161     
00162     return true;
00163 }
00164 
00165 
00166 }   // end namespace platform
00167 }   // end namespace pion
00168 
00169 #endif

Generated on Wed Apr 13 16:38:34 2011 for pion-platform by  doxygen 1.4.7