Optika GUI Toolik Version of the Day
Optika_DependencySheet.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //         Optika: A Tool For Developing Parameter Obtaining GUIs
00005 //                Copyright (2009) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, with Sandia Corporation, the 
00008 // U.S. Government retains certain rights in this software.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Kurtis Nusbaum (klnusbaum@gmail.com) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 #include "Optika_DependencySheet.hpp"
00029 #include <QString>
00030 
00031 namespace Optika{
00032 
00033 
00034 DependencySheet::DependencySheet(Teuchos::RCP<Teuchos::ParameterList> rootList):name("DEP_ANONYMOUS"), rootList(rootList){}
00035 
00036 DependencySheet::DependencySheet(Teuchos::RCP<Teuchos::ParameterList> rootList, const std::string &name):name(name), rootList(rootList){}
00037 
00038 bool DependencySheet::addDependency(Teuchos::RCP<Optika::Dependency> dependency){
00039   validateExistanceInRoot(dependency);
00040   bool successfulAdd = true;
00041   Dependency::ParameterParentMap dependees = dependency->getDependees();
00042   for(Dependency::ParameterParentMap::iterator it  = dependees.begin(); it != dependees.end(); ++it){
00043     if(!dependencies[it->second->getEntryPtr(it->first)].insert(dependency).second){
00044       successfulAdd = false;
00045     }
00046   }
00047   return successfulAdd;
00048 }
00049 
00050 bool DependencySheet::removeDependency(Teuchos::RCP<Optika::Dependency> dependency){
00051   bool succesfulRemove = true;  
00052   Dependency::ParameterParentMap dependees = dependency->getDependees();
00053   for(Dependency::ParameterParentMap::iterator it  = dependees.begin(); it != dependees.end(); ++it){
00054     bool successFullCurrentRemove = false;
00055     Teuchos::ParameterEntry* currentDependee = it->second->getEntryPtr(it->first);
00056     for(DepSet::iterator it2 = dependencies[currentDependee].begin(); it2 != dependencies[currentDependee].end(); ++it2){
00057       if((*it2) == dependency){
00058         dependencies[currentDependee].erase(it2);
00059         successFullCurrentRemove = true;
00060         break;
00061       }
00062     }
00063     if(!successFullCurrentRemove){
00064       succesfulRemove = false;
00065     }
00066   }
00067   return succesfulRemove;
00068 }
00069 
00070 bool DependencySheet::hasDependents(const Teuchos::ParameterEntry* dependee) const{
00071   return (dependencies.find(dependee) != dependencies.end() && dependencies.find(dependee)->second.size() > 0);
00072 }
00073 
00074 const DependencySheet::DepSet& DependencySheet::getDependenciesForParameter(const Teuchos::ParameterEntry* dependee) const{
00075   return dependencies.find(dependee)->second;
00076 }
00077 
00078 DependencySheet::DepMap::iterator DependencySheet::depBegin(){
00079   return dependencies.begin();
00080 }
00081 
00082 DependencySheet::DepMap::iterator DependencySheet::depEnd(){
00083   return dependencies.end();
00084 }
00085 
00086 DependencySheet::DepMap::const_iterator DependencySheet::depBegin() const{
00087   return dependencies.begin();
00088 }
00089 
00090 DependencySheet::DepMap::const_iterator DependencySheet::depEnd() const{
00091   return dependencies.end();
00092 }
00093 
00094 void DependencySheet::printDeps(){
00095   std::cout << "Dependency Sheet: " << name << "\n\n";
00096   for(DepMap::iterator it = depBegin(); it != depEnd(); ++it){
00097     const Teuchos::ParameterEntry* dependee = it->first;
00098     for(DepSet::iterator it2 = dependencies.find(dependee)->second.begin(); it2 != dependencies.find(dependee)->second.end(); ++it2){
00099       std::set<std::string> dependeeNames = (*it2)->getDependeeNames();
00100       std::cout << "Dependees: \n";
00101       for(std::set<std::string>::iterator it3 = dependeeNames.begin(); it3 != dependeeNames.end(); ++it3){
00102         std::cout << "\t" << *it3 << "\n";
00103       }
00104       std::set<std::string> dependentNames = (*it2)->getDependentNames();
00105       std::cout << "Dependents: \n";
00106       for(std::set<std::string>::iterator it3 = dependentNames.begin(); it3 != dependentNames.end(); ++it3){
00107         std::cout << "\t" << *it3 << "\n";
00108       }
00109       std::cout << "\n";
00110       std::cout << "Type: " << (*it2)->getType() << "\n\n";
00111     }
00112   }
00113 }
00114 
00115 void DependencySheet::validateExistanceInRoot(Teuchos::RCP<Optika::Dependency> dependency){
00116   Dependency::ParameterParentMap::const_iterator it;
00117   Teuchos::ParameterEntry *currentDependee;
00118   Dependency::ParameterParentMap dependees = dependency->getDependees();
00119   for(it = dependees.begin(); it != dependees.end(); ++it){ 
00120     currentDependee = it->second->getEntryPtr(it->first);
00121     if(!Dependency::doesListContainList(rootList, it->second)){
00122       throw InvalidDependencyException(
00123       "FAILED TO ADD DEPENDENCY!\n\n"
00124       "Sorry for the yelling there, but this is kind of a big deal. Dependencies are hard and complex so don't beat "
00125       "yourself up too much. Mistakes are easy to make when dealing with dependencies. "
00126       "And besides, I'm gonna do my best to help you out! I'm sure with the informationg below you'll be able to figure out what "
00127       "exactly went wrong. I've got confidence in you! :)\n\n"
00128       "Error:\n"
00129       "An attempt was made to add a dependency containing a the dependee parameter \"" + it->first + "\""
00130       " to the Dependency Sheet \"" + name + "\"."
00131       " The Dependency Sheet's root list does not contain nor does it have"
00132       " child ParameterLists that contain the parameter.\n"
00133       "Dependency Sheet: " + name + "\n"
00134       "Dependency Type: " + QString::number(dependency->getType()).toStdString() + "\n"
00135       "Bad Dependee Name: " + it->first);
00136     }
00137   }
00138 
00139   Teuchos::ParameterEntry *currentDependent;
00140   Dependency::ParameterParentMap dependents = dependency->getDependents();
00141   for(it = dependents.begin(); it != dependents.end(); ++it){ 
00142     currentDependent = it->second->getEntryPtr(it->first);
00143     if(!Dependency::doesListContainList(rootList, it->second)){
00144       throw InvalidDependencyException(
00145       "FAILED TO ADD DEPENDENCY!\n\n"
00146       "Sorry for the yelling there, but this is kind of a big deal. Dependencies are hard and complex so don't beat "
00147       "yourself up too much. Mistakes are easy to make when dealing with dependencies. "
00148       "And besides, I'm gonna do my best to help you out! I'm sure with the informationg below you'll be able to figure out what "
00149       "exactly went wrong. I've got confidence in you! :)\n\n"
00150       "Error:\n"
00151       "An attempt was made to add a dependency containing a the dependent parameter \"" + it->first + "\""
00152       " to the Dependency Sheet \"" + name + "\"."
00153       " The Dependency Sheet's root list does not contain nor does it have"
00154       " child ParameterLists that contain the parameter.\n"
00155       "Dependency Sheet: " + name + "\n"
00156       "Dependency Type: " + QString::number(dependency->getType()).toStdString() + "\n"
00157       "Bad Dependent Name: " + it->first);
00158     }
00159   }
00160 }
00161 
00162 }
00163 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends