CExporter.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 #include <CExporter.h>
00041 
00042 
00043 namespace beliefstate {
00044   CExporter::CExporter() {
00045     m_ckvpConfiguration = new CKeyValuePair();
00046   }
00047 
00048   CExporter::~CExporter() {
00049     // NOTE(winkler): We used to delete the nodes when the exporter
00050     // shuts down. As in the new system the nodes are managed by a
00051     // central `symbolic log' entity, this is not necessary
00052     // anymore. In fact, its harmful to do (as the symbolic log will
00053     // try to `free' the same node addresses -- this results in a
00054     // segfault).
00055 
00056     //this->clearNodes();
00057     
00058     if(m_ckvpConfiguration) {
00059       delete m_ckvpConfiguration;
00060       m_ckvpConfiguration = NULL;
00061     }
00062   }
00063 
00064   void CExporter::addNode(Node* ndAdd) {
00065     m_lstNodes.push_back(ndAdd);
00066   }
00067   
00068   void CExporter::setRootNodes(std::list<Node*> lstRootNodes) {
00069     m_lstRootNodes = lstRootNodes;
00070   }
00071   
00072   void CExporter::addRootNode(Node* ndRoot) {
00073     m_lstRootNodes.push_back(ndRoot);
00074   }
00075   
00076   std::list<Node*> CExporter::rootNodes() {
00077     return m_lstRootNodes;
00078   }
00079   
00080   list<Node*> CExporter::nodes() {
00081     return m_lstNodes;
00082   }
00083 
00084   void CExporter::clearNodes() {
00085     for(Node* ndDelete : m_lstNodes) {
00086       delete ndDelete;
00087     }
00088     
00089     m_lstNodes.clear();
00090   }
00091   
00092   CKeyValuePair* CExporter::configuration() {
00093     return m_ckvpConfiguration;
00094   }
00095   
00096   std::string CExporter::nodeIDPrefix(Node* ndInQuestion, std::string strProposition) {
00097     std::string strPrefix = ndInQuestion->metaInformation()->stringValue("class");
00098     
00099     if(strPrefix == "") {
00100       strPrefix = strProposition;
00101     } else {
00102       strPrefix += "_";
00103     }
00104     
00105     return strPrefix;
00106   }
00107   
00108   void CExporter::renewUniqueIDsForNode(Node *ndRenew) {
00109     std::string strNodeIDPrefix = this->nodeIDPrefix(ndRenew, "node_");
00110     ndRenew->setUniqueID(this->generateUniqueID(strNodeIDPrefix, 8));
00111     
00112     std::list<Node*> lstSubnodes = ndRenew->subnodes();
00113     for(Node* ndNode : lstSubnodes) {
00114       this->renewUniqueIDsForNode(ndNode);
00115     }
00116   }
00117   
00118   void CExporter::renewUniqueIDs() {
00119     for(Node* ndNode : m_lstNodes) {
00120       this->renewUniqueIDsForNode(ndNode);
00121     }
00122   }
00123   
00124   bool CExporter::runExporter(CKeyValuePair* ckvpConfigurationOverlay) {
00125     // NOTE(winkler): This is a dummy, superclass exporter. It does not
00126     // actually export anything. Subclass it to get *actual*
00127     // functionality.
00128     
00129     return true;
00130   }
00131   
00132   std::string CExporter::generateRandomIdentifier(std::string strPrefix, unsigned int unLength) {
00133     std::stringstream sts;
00134     sts << strPrefix;
00135     
00136     for(unsigned int unI = 0; unI < unLength; unI++) {
00137       int nRandom;
00138       
00139       do {
00140         nRandom = rand() % 122 + 48;
00141       } while(nRandom < 48 ||
00142               (nRandom > 57 && nRandom < 65) ||
00143               (nRandom > 90 && nRandom < 97) ||
00144               nRandom > 122);
00145       
00146       char cRandom = (char)nRandom;
00147       sts << cRandom;
00148     }
00149     
00150     return sts.str();
00151   }
00152   
00153   std::string CExporter::generateUniqueID(std::string strPrefix, unsigned int unLength) {
00154     std::string strID;
00155     
00156     do {
00157       strID = this->generateRandomIdentifier(strPrefix, unLength);
00158     } while(this->uniqueIDPresent(strID));
00159   
00160     return strID;
00161   }
00162 
00163   bool CExporter::uniqueIDPresent(std::string strUniqueID) {
00164     for(Node* ndNode : m_lstNodes) {
00165       if(ndNode->includesUniqueID(strUniqueID)) {
00166         return true;
00167       }
00168     }
00169     
00170     return false;
00171   }
00172 
00173   std::string CExporter::replaceString(std::string strOriginal, std::string strReplaceWhat, std::string strReplaceBy) {
00174     size_t found;
00175     
00176     found = strOriginal.find(strReplaceWhat);
00177     while(found != string::npos) {
00178       strOriginal.replace(found, strReplaceWhat.length(), strReplaceBy);
00179       found = strOriginal.find(strReplaceWhat, found + strReplaceBy.length());
00180     };
00181     
00182     return strOriginal;
00183   }
00184   
00185   bool CExporter::nodeHasValidDetailLevel(Node* ndDisplay) {
00186     int nConfigMaxDetailLevel = this->configuration()->floatValue("max-detail-level");
00187     int nNodeDetailLevel = ndDisplay->metaInformation()->floatValue("detail-level");
00188     
00189     return (nNodeDetailLevel <= nConfigMaxDetailLevel);
00190   }
00191   
00192   bool CExporter::nodeDisplayable(Node* ndDisplay) {
00193     bool bDisplaySuccesses = (this->configuration()->floatValue("display-successes") == 1);
00194     bool bDisplayFailures = (this->configuration()->floatValue("display-failures") == 1);
00195     bool bNodeSuccess = (ndDisplay->metaInformation()->floatValue("success") == 1);
00196     
00197     if(this->nodeHasValidDetailLevel(ndDisplay)) {
00198       if((bNodeSuccess && bDisplaySuccesses) || (!bNodeSuccess && bDisplayFailures)) {
00199         return true;
00200       }
00201     }
00202     
00203     return false;
00204   }
00205 
00206   void CExporter::setDesignatorIDs(std::list< std::pair<std::string, std::string> > lstDesignatorIDs) {
00207     m_lstDesignatorIDs = lstDesignatorIDs;
00208   }
00209 
00210   void CExporter::setDesignatorEquations(std::list< std::pair<std::string, std::string> > lstDesignatorEquations) {
00211     m_lstDesignatorEquations = lstDesignatorEquations;
00212   }
00213   
00214   void CExporter::setDesignatorEquationTimes(std::list< std::pair<std::string, std::string> > lstDesignatorEquationTimes) {
00215     m_lstDesignatorEquationTimes = lstDesignatorEquationTimes;
00216   }
00217   
00218   std::list<std::string> CExporter::designatorIDs() {
00219     std::list<std::string> lstResult;
00220     
00221     for(std::pair<std::string, std::string> prPair : m_lstDesignatorIDs) {
00222       lstResult.push_back(prPair.second);
00223     }
00224     
00225     return lstResult;
00226   }
00227   
00228   std::list<std::string> CExporter::parentDesignatorsForID(std::string strID) {
00229     std::list<std::string> lstResult;
00230     
00231     for(std::pair<std::string, std::string> prPair : m_lstDesignatorEquations) {
00232       if(prPair.second == strID) {
00233         lstResult.push_back(prPair.first);
00234       }
00235     }
00236     
00237     return lstResult;
00238   }
00239   
00240   std::list<std::string> CExporter::successorDesignatorsForID(std::string strID) {
00241     std::list<std::string> lstResult;
00242     
00243     for(std::pair<std::string, std::string> prPair : m_lstDesignatorEquations) {
00244       if(prPair.first == strID) {
00245         lstResult.push_back(prPair.second);
00246       }
00247     }
00248     
00249     return lstResult;
00250   }
00251   
00252   std::string CExporter::equationTimeForSuccessorID(std::string strID) {
00253     string strReturn = "";
00254     
00255     for(std::pair<std::string, std::string> prPair : m_lstDesignatorEquationTimes) {
00256       if(prPair.first == strID) {
00257         strReturn = prPair.second;
00258         break;
00259       }
00260     }
00261     
00262     return strReturn;
00263   }
00264 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15