- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/ProbabilityRules/RuleTriggering/RuleTrigger.cpp

    00001 //==============================================================================
    00002 // Copyright (c) 2005           Model-based Embedded and Robotic Systems Group
    00003 // All Rights Reserved          Massachusetts Institute of Technology
    00004 //
    00005 // This software is provided as is. Model-based Embedded and Robotic Systems
    00006 // (MERS) group does not assume any responsibility.
    00007 //==============================================================================
    00008
    00009 #include "RuleTrigger.h"
    00010
    00011 #ifdef _MSC_VER
    00012 #pragma warning( disable : 4786 )
    00013 #endif
    00014
    00015 #include <Mers/Estimation/Discrete/ProbabilityRules/Rule.h>
    00016 #include <Mers/Models/Base/BaseModelAssignment.h>
    00017 #include <Mers/Models/Base/BaseModelAssignmentArray.h>
    00018
    00019 using namespace Mers::Estimation::Discrete::ProbabilityRules::RuleTriggering;
    00020 using Mers::Estimation::Discrete::ProbabilityRules::Rule;
    00021 using Mers::Models::Base::BaseModelAssignment;
    00022 using Mers::Models::Base::BaseModelAssignmentArray;
    00023 using std::ostream;
    00024 using std::cout;
    00025 using std::endl;
    00026 using std::vector;
    00027 using std::pair;
    00028 using std::map;
    00029
    00030
    00031         //-----------------------------------------------
    00032         //      CONSTRUCTOR
    00033         //-----------------------------------------------
    00034
    00035
    00036 RuleTrigger::RuleTrigger( vector<Rule *> & theRules, bool hasOwnership, unsigned short db)
    00037 : rules(theRules), isOwner(hasOwnership), debug(db)
    00038 {
    00039         unsigned int varID, valID;
    00040
    00041         if(rules.size() < 1)
    00042         {
    00043                 cout << "<RuleTrigger> WARNING: No rules were provided to trigger." << endl;
    00044         }
    00045
    00046         // Set up the mapping between assignments and the rules
    00047         for(size_t i=0; i<rules.size(); i++)
    00048         {
    00049                 const BaseModelAssignmentArray * assignments = rules[i]->getAllAssignments();
    00050                 for(unsigned int j=0; j<assignments->getLength(); j++)
    00051                 {
    00052                         varID = (*assignments)[j]->getVariable()->getUID();
    00053                         valID = (*assignments)[j]->getValue()->getUID();
    00054                         ruleMap[pair<unsigned int, unsigned int>(varID, valID)].push_back(rules[i]);
    00055                         assigned[pair<unsigned int, unsigned int>(varID, valID)] = false;
    00056                 }
    00057         }
    00058 }
    00059         //-----------------------------------------------
    00060         //      DESTRUCTOR
    00061         //-----------------------------------------------
    00062
    00063
    00064 RuleTrigger::~RuleTrigger()
    00065 {
    00066         Rule * aRule;
    00067         while(rules.size() > 0)
    00068         {
    00069                 aRule = rules.back();
    00070                 rules.pop_back();
    00071                 if(isOwner)
    00072                         delete aRule;
    00073         }
    00074 }
    00075
    00076
    00077
    00078         //-----------------------------------------------
    00079         //      PUBLIC METHODS
    00080         //-----------------------------------------------
    00081
    00082 bool
    00083 RuleTrigger::loadAssignment(unsigned int varID, unsigned int valID)
    00084 {
    00085         bool success = true;
    00086         if(assigned.size() < 1)         // Do nothing if there are no rules
    00087                 return true;
    00088
    00089         if(assigned[pair<unsigned int, unsigned int>(varID, valID)] == true)
    00090         {
    00091                 if(debug > 1)
    00092                         cout << "Already assigned " << varID << ", " << valID << "." << endl;
    00093                 return true;
    00094         }
    00095         for(size_t i=0; i < ruleMap[pair<unsigned int, unsigned int>(varID, valID)].size(); i++)
    00096         {
    00097                 if(debug > 2)
    00098                         cout << "Attempting to assign " << varID << ", " << valID << "." << endl;
    00099                 if(!ruleMap[pair<unsigned int, unsigned int>(varID, valID)][i]->increment())
    00100                         success = false;
    00101         }
    00102         assigned[pair<unsigned int, unsigned int>(varID, valID)] = true;
    00103         return true;
    00104 }
    00105
    00106
    00107 vector<Rule *>
    00108 RuleTrigger::getTriggeredRules(void)
    00109 {
    00110         vector<Rule *> enabledRules;
    00111         for(size_t i=0; i<rules.size(); i++)
    00112         {
    00113                 if(rules[i]->isTriggered())
    00114                         enabledRules.push_back(rules[i]);
    00115         }
    00116         return enabledRules;
    00117 }
    00118
    00119
    00120 vector<Rule *>
    00121 RuleTrigger::getTriggeredRules(unsigned int numSat)
    00122 {
    00123         vector<Rule *> enabledRules;
    00124         for(size_t i=0; i<rules.size(); i++)
    00125         {
    00126                 if(rules[i]->getNumberSatisfied() >= numSat)
    00127                         enabledRules.push_back(rules[i]);
    00128         }
    00129         return enabledRules;
    00130 }
    00131
    00132
    00133 void
    00134 RuleTrigger::reset(void)
    00135 {
    00136         if(assigned.size() < 1)         // Do nothing if there are no rules
    00137                 return;
    00138
    00139         map< pair<unsigned int, unsigned int>, bool >::iterator assignedItr;
    00140         assignedItr = assigned.begin();
    00141         while(assignedItr != assigned.end())
    00142         {
    00143                 (*assignedItr).second = false;
    00144                 assignedItr++;
    00145         }
    00146
    00147         for(size_t i=0; i<rules.size(); i++)
    00148                 rules[i]->reset();
    00149         return;
    00150 }
    00151
    00152 void
    00153 RuleTrigger::setOwnership(bool hasOwnership)
    00154 {
    00155         isOwner = hasOwnership;
    00156         return;
    00157 }
    00158
    00159 void
    00160 RuleTrigger::setDebug(const unsigned short db)
    00161 {
    00162         debug = db;
    00163         return;
    00164 }
    00165
    00166
    00167 ostream&
    00168 Mers::Estimation::Discrete::ProbabilityRules::RuleTriggering::operator<<(ostream& os, RuleTrigger const & rt)
    00169 {
    00170         os << "=== The Rule Trigger ===" << endl;
    00171         for(size_t i=0; i<rt.rules.size(); i++)
    00172         {
    00173                 os << *rt.rules[i] << endl;
    00174         }
    00175         os << "========================" << endl;
    00176
    00177         return os;
    00178 }
    00179         //-----------------------------------------------
    00180         //      PRIVATE METHODS
    00181         //-----------------------------------------------
    00182