eventEvaluation.hpp
Go to the documentation of this file.
00001 //\todo omoguci overide external event descriptiona
00002 //\todo uvedi razlicite tipove ekternigh varijabli konitnuirane, flag, ...
00003 /*********************************************************************
00004  * eventEvaluation.hpp
00005  *
00006  *  Created on: May 8, 2014
00007  *      Author: Filip Mandic
00008  *
00009  ********************************************************************/
00010 
00011 /*********************************************************************
00012 * Software License Agreement (BSD License)
00013 *
00014 *  Copyright (c) 2014, LABUST, UNIZG-FER
00015 *  All rights reserved.
00016 *
00017 *  Redistribution and use in source and binary forms, with or without
00018 *  modification, are permitted provided that the following conditions
00019 *  are met:
00020 *
00021 *   * Redistributions of source code must retain the above copyright
00022 *     notice, this list of conditions and the following disclaimer.
00023 *   * Redistributions in binary form must reproduce the above
00024 *     copyright notice, this list of conditions and the following
00025 *     disclaimer in the documentation and/or other materials provided
00026 *     with the distribution.
00027 *   * Neither the name of the LABUST nor the names of its
00028 *     contributors may be used to endorse or promote products derived
00029 *     from this software without specific prior written permission.
00030 *
00031 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00032 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00033 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00034 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00035 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00036 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00037 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00038 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00039 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00040 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00041 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00042 *  POSSIBILITY OF SUCH DAMAGE.
00043 *********************************************************************/
00044 
00045 #ifndef EVENTEVALUATION_HPP_
00046 #define EVENTEVALUATION_HPP_
00047 
00048 #include <labust_mission/labustMission.hpp>
00049 #include <exprtk/exprtk.hpp>
00050 
00051 /*********************************************************************
00052  ***  EventEvaluation class definition
00053  ********************************************************************/
00054 
00055 namespace labust {
00056         namespace event {
00057 
00058                 class EventEvaluation{
00059 
00060                 public:
00061 
00062                         typedef exprtk::symbol_table<double> symbol_table_t;
00063                         typedef exprtk::expression<double> expression_t;
00064                         typedef exprtk::parser<double> parser_t;
00065                         typedef exprtk::parser_error::type error_t;
00066 
00067                         /*****************************************************************
00068                          ***  Class functions
00069                          ****************************************************************/
00070 
00071                         EventEvaluation();
00072 
00073                         vector<uint8_t> evaluateEvents(vector<string> events);
00074 
00075                         double evaluateStringExpression(std::string expression_str);
00076 
00077                         void updateSymbolTable(vector<double> stateVar, vector<double> missionVar, vector<string> missionVarNames);
00078 
00079                         void initializeSymbolTable(vector<double> stateVar, vector<double> missionVar, vector<string> missionVarNames);
00080 
00081                         void resetSymbolTable();
00082 
00083                         /*********************************************************************
00084                          ***  Class variables
00085                          ********************************************************************/
00086 
00087                 private:
00088 
00089                         symbol_table_t symbol_table;
00090                         boost::mutex mtx;
00091                 };
00092 
00093                 EventEvaluation::EventEvaluation(){
00094 
00095                 }
00096 
00097                 vector<uint8_t> EventEvaluation::evaluateEvents(vector<string> events){
00098 
00099                         vector<uint8_t> eventsState;
00100 
00101                         for(vector<string>::iterator it = events.begin(); it != events.end(); ++it){
00102 
00103                                 uint8_t tmp = (evaluateStringExpression((*it).c_str()) == 1.0)?1:0;
00104                                 eventsState.push_back(tmp);
00105                         }
00106 
00107                         return eventsState;
00108                 }
00109 
00110 
00111                 double EventEvaluation::evaluateStringExpression(std::string expression_str){
00112 
00113                         boost::mutex::scoped_lock lock(mtx);
00114                         expression_t expression;
00115                         expression.register_symbol_table(symbol_table);
00116 
00117                         parser_t parser;
00118 
00119                         if (!parser.compile(expression_str,expression))
00120                         {
00121                           ROS_ERROR("Error: %s\tExpression: %s\n",parser.error().c_str(),expression_str.c_str());
00122 
00123                           for (std::size_t i = 0; i < parser.error_count(); ++i)
00124                           {
00125                                  error_t error = parser.get_error(i);
00126                                  ROS_ERROR("Error: %02d Position: %02d Type: [%s] Msg: %s Expr: %s\n",
00127                                                 static_cast<int>(i),
00128                                                 static_cast<int>(error.token.position),
00129                                                 exprtk::parser_error::to_str(error.mode).c_str(),
00130                                                 error.diagnostic.c_str(),
00131                                                 expression_str.c_str());
00132                           }
00133                           return -1;
00134                         }
00135 
00136                         return expression.value();
00137                 }
00138 
00139                 void EventEvaluation::initializeSymbolTable(vector<double> stateVar, vector<double> missionVar, vector<string> missionVarNames){
00140 
00141                         symbol_table.add_constants();
00142 
00143                         double tmp = 0.0;
00144                         int i;
00145 
00146                         for(i = 0; i < stateHatNum; i++ ){
00147 
00148                                 symbol_table.create_variable(stateVarNames[i]);
00149                                 symbol_table.get_variable(stateVarNames[i])->ref() = double(tmp);
00150                         }
00151 
00152                         i = 0;
00153                         for(vector<double>::iterator it = missionVar.begin() ; it != missionVar.end(); ++it){
00154 
00155                                 string varName = missionVarNames[i++];
00156 
00157                                 symbol_table.create_variable(varName.c_str());
00158                                 symbol_table.get_variable(varName.c_str())->ref() = double(tmp);
00159                         }
00160                 }
00161 
00162                 void EventEvaluation::updateSymbolTable(vector<double> stateVar, vector<double> missionVar, vector<string> missionVarNames){
00163 
00164                         boost::mutex::scoped_lock lock(mtx);
00165                         double tmp;
00166                         int i;
00167 
00168                         for(i = 0; i < stateHatNum; i++){
00169 
00170                                 tmp = stateVar[i];
00171                                 symbol_table.get_variable(stateVarNames[i])->ref() = double(tmp);
00172                         }
00173 
00174                     i = 0;
00175                         for(std::vector<double>::iterator it = missionVar.begin() ; it != missionVar.end(); ++it){
00176 
00177                                 string varName = missionVarNames[i++];
00178 
00179                                 double value = (*it);
00180                                 symbol_table.get_variable(varName.c_str())->ref() = double(value);
00181                         }
00182                 }
00183 
00184                 void EventEvaluation::resetSymbolTable(){
00185                         symbol_table.clear();
00186                 }
00187         }
00188 }
00189 
00190 #endif /* EVENTEVALUATION_HPP_ */


labust_mission
Author(s): Filip Mandic
autogenerated on Fri Aug 28 2015 11:22:55