Node.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 <Node.h>
00041 
00042 
00043 namespace beliefstate {
00044   Node::Node() {
00045     this->init();
00046   }
00047   
00048   Node::Node(std::string strTitle) {
00049     this->init();
00050     this->setTitle(strTitle);
00051   }
00052   
00053   Node::Node(std::list<CKeyValuePair*> lstDescription) {
00054     this->init();
00055     this->setPrematurelyEnded(false);
00056     this->setDescription(lstDescription);
00057   }
00058   
00059   Node::~Node() {
00060     this->clearSubnodes();
00061     this->clearDescription();
00062   }
00063   
00064   void Node::init() {
00065     m_strTitle = "";
00066     m_ckvpMetaInformation = new CKeyValuePair();
00067     m_ndParent = NULL;
00068     m_nID = 0;
00069   }
00070   
00071   void Node::setDescription(std::list<CKeyValuePair*> lstDescription) {
00072     for(CKeyValuePair* ckvpPair : lstDescription) {
00073       m_lstDescription.push_back(ckvpPair->copy());
00074     }
00075   }
00076 
00077   void Node::clearDescription() {
00078     for(CKeyValuePair* ckvpPair : m_lstDescription) {
00079       delete ckvpPair;
00080     }
00081     
00082     m_lstDescription.clear();
00083   }
00084   
00085   void Node::clearSubnodes() {
00086     for(Node* ndCurrent : m_lstSubnodes) {
00087       delete ndCurrent;
00088     }
00089     
00090     m_lstSubnodes.clear();
00091   }
00092   
00093   std::list<CKeyValuePair*> Node::description() {
00094     return m_lstDescription;
00095   }
00096   
00097   void Node::setTitle(std::string strTitle) {
00098     m_strTitle = strTitle;
00099   }
00100   
00101   std::string Node::title() {
00102     return m_strTitle;
00103   }
00104   
00105   void Node::addSubnode(Node* ndAdd) {
00106     ndAdd->setParent(this);
00107     m_lstSubnodes.push_back(ndAdd);
00108   }
00109   
00110   std::list<Node*> Node::subnodes() {
00111     return m_lstSubnodes;
00112   }
00113   
00114   void Node::setUniqueID(std::string strUniqueID) {
00115     m_strUniqueID = strUniqueID;
00116   }
00117   
00118   std::string Node::uniqueID() {
00119     return m_strUniqueID;
00120   }
00121   
00122   bool Node::includesUniqueID(std::string strUniqueID) {
00123     bool bReturnvalue = false;
00124     
00125     if(m_strUniqueID == strUniqueID) {
00126       bReturnvalue = true;
00127     } else {
00128       for(Node* ndCurrent : m_lstSubnodes) {
00129         if(ndCurrent->includesUniqueID(strUniqueID)) {
00130           bReturnvalue = true;
00131           break;
00132         }
00133       }
00134     }
00135     
00136     return bReturnvalue;
00137   }
00138   
00139   CKeyValuePair* Node::metaInformation() {
00140     return m_ckvpMetaInformation;
00141   }
00142   
00143   void Node::setID(int nID) {
00144     m_nID = nID;
00145   }
00146   
00147   int Node::id() {
00148     return m_nID;
00149   }
00150   
00151   int Node::highestID() {
00152     int nHighestID = m_nID;
00153     
00154     for(Node* ndCurrent : m_lstSubnodes) {
00155       nHighestID = max(nHighestID, ndCurrent->highestID());
00156     }
00157     
00158     return nHighestID;
00159   }
00160   
00161   void Node::setParent(Node* ndParent) {
00162     m_ndParent = ndParent;
00163   }
00164   
00165   Node* Node::parent() {
00166     return m_ndParent;
00167   }
00168   
00169   Node* Node::relativeWithID(int nID, bool bIgnoreSelf) {
00170     if(this->parent() == NULL) {
00171       return NULL;
00172     } else {
00173       if(not bIgnoreSelf && this->id() == nID) {
00174         return this;
00175       } else {
00176         return this->parent()->relativeWithID(nID);
00177       }
00178     }
00179   }
00180   
00181   void Node::setPrematurelyEnded(bool bPrematurelyEnded) {
00182     this->metaInformation()->setValue(string("prematurely-ended"), (bPrematurelyEnded ? 1 : 0));
00183   }
00184   
00185   bool Node::prematurelyEnded() {
00186     return (this->metaInformation()->floatValue("prematurely-ended") == 0 ? false : true);
00187   }
00188   
00189   CKeyValuePair* Node::addDescriptionListItem(std::string strDomain, std::string strPrefix) {
00190     CKeyValuePair* ckvpList = this->metaInformation()->addChild(strDomain);
00191     
00192     std::stringstream sts;
00193     sts << strPrefix << "-";
00194     sts << ckvpList->children().size();
00195     
00196     return ckvpList->addChild(sts.str());
00197   }
00198   
00199   std::string Node::addImage(std::string strOrigin, std::string strFilename, std::string strTimestamp) {
00200     CKeyValuePair* ckvpImage = this->addDescriptionListItem("images", "image");
00201     
00202     ckvpImage->setValue(string("origin"), strOrigin);
00203     ckvpImage->setValue(string("filename"), strFilename);
00204     ckvpImage->setValue(string("time-capture"), strTimestamp);
00205     
00206     return ckvpImage->key();
00207   }
00208   
00209   std::string Node::addObject(std::list<CKeyValuePair*> lstDescription) {
00210     CKeyValuePair* ckvpObject = this->addDescriptionListItem("objects", "object");
00211     
00212     for(CKeyValuePair* ckvpPair : lstDescription) {
00213       ckvpObject->addChild(ckvpPair->copy());
00214     }
00215     
00216     return ckvpObject->key();
00217   }
00218   
00219   std::string Node::addFailure(std::string strCondition, std::string strTimestamp) {
00220     CKeyValuePair* ckvpFailure = this->addDescriptionListItem("failures", "failure");
00221     
00222     ckvpFailure->setValue(string("condition"), strCondition);
00223     ckvpFailure->setValue(string("time-fail"), strTimestamp);
00224     
00225     return ckvpFailure->key();
00226   }
00227   
00228   std::string Node::catchFailure(std::string strFailureID, Node* ndEmitter, std::string strTimestamp) {
00229     std::stringstream sts;
00230     sts << ndEmitter;
00231     
00232     CKeyValuePair* ckvpFailure = this->addDescriptionListItem("caught_failures", "caught_failure");
00233     
00234     ckvpFailure->setValue(string("failure-id"), strFailureID);
00235     ckvpFailure->setValue(string("time-catch"), strTimestamp);
00236     ckvpFailure->setValue(string("emitter-id"), sts.str());
00237     
00238     m_lstCaughtFailures.push_back(make_pair(strFailureID, ndEmitter));
00239     
00240     return ckvpFailure->key();
00241   }
00242   
00243   void Node::removeCaughtFailure(std::string strFailureID) {
00244     for(std::list< pair<std::string, Node*> >::iterator itPr = m_lstCaughtFailures.begin();
00245         itPr != m_lstCaughtFailures.end();
00246         itPr++) {
00247       pair<std::string, Node*> prCurrent = *itPr;
00248       
00249       if(prCurrent.first == strFailureID) {
00250         m_lstCaughtFailures.erase(itPr);
00251         break;
00252       }
00253     }
00254     
00255     CKeyValuePair* ckvpCaughtFailures = this->metaInformation()->childForKey("caught_failures");
00256     if(ckvpCaughtFailures) {
00257       for(CKeyValuePair* ckvpCaughtFailure : ckvpCaughtFailures->children()) {
00258         std::string strCurrentFailureID = ckvpCaughtFailure->stringValue("failure-id");
00259         
00260         if(strCurrentFailureID == strFailureID) {
00261           ckvpCaughtFailures->removeChildForKey(ckvpCaughtFailure->key());
00262           break;
00263         }
00264       }
00265     }
00266   }
00267   
00268   Node* Node::emitterForCaughtFailure(std::string strFailureID, std::string strEmitterID, std::string strTimestamp) {
00269     for(pair<std::string, Node*> prCurrent : m_lstCaughtFailures) {
00270       std::stringstream sts;
00271       sts << prCurrent.second;
00272       
00273       if(prCurrent.first == strFailureID && sts.str() == strEmitterID) {
00274         return prCurrent.second;
00275       }
00276     }
00277     
00278     return NULL;
00279   }
00280   
00281   bool Node::hasFailures() {
00282     CKeyValuePair* ckvpList = this->metaInformation()->childForKey("failures");
00283     
00284     if(ckvpList) {
00285       return (ckvpList->children().size() > 0);
00286     }
00287     
00288     return false;
00289   }
00290   
00291   void Node::addDesignator(std::string strType, std::list<CKeyValuePair*> lstDescription, std::string strUniqueID, std::string strAnnotation) {
00292     CKeyValuePair* ckvpDesignator = this->addDescriptionListItem("designators", "designator");
00293     
00294     ckvpDesignator->setValue(string("type"), strType);
00295     ckvpDesignator->setValue(string("id"), strUniqueID);
00296     ckvpDesignator->setValue(string("annotation"), strAnnotation);
00297     
00298     CKeyValuePair* ckvpDescription = ckvpDesignator->addChild("description");
00299     for(CKeyValuePair* ckvpChild : lstDescription) {
00300       ckvpDescription->addChild(ckvpChild);
00301     }
00302   }
00303   
00304   void Node::setSuccess(bool bSuccess) {
00305     this->metaInformation()->setValue(string("success", (bSuccess ? 1 : 0)));
00306   }
00307   
00308   bool Node::success() {
00309     return (this->metaInformation()->floatValue(string("success")) == 1 ? true : false);
00310   }
00311   
00312   Node* Node::previousNode() {
00313     Node* ndPrevious = NULL;
00314     
00315     if(m_ndParent != NULL) {
00316       std::list<Node*> lstNodes = m_ndParent->subnodes();
00317       Node* ndLast = NULL;
00318       
00319       for(Node* ndNode : lstNodes) {
00320         if(ndNode == this) {
00321           ndPrevious = ndLast;
00322           break;
00323         } else {
00324           ndLast = ndNode;
00325         }
00326       }
00327     }
00328     
00329     return ndPrevious;
00330   }
00331   
00332   void Node::ensureProperty(std::string strKey, std::string strDefaultValue) {
00333     if(this->metaInformation()->childForKey(strKey) == NULL) {
00334       this->metaInformation()->setValue(strKey, strDefaultValue);
00335     }
00336     
00337     for(Node* ndChild : m_lstSubnodes) {
00338       ndChild->ensureProperty(strKey, strDefaultValue);
00339     }
00340   }
00341 }


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