Optika GUI Toolik Version of the Day
Optika_StandardConditions.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_StandardConditions.hpp"
00029 
00030 namespace Optika{
00031 
00032 ParameterCondition::ParameterCondition(std::string parameterName, Teuchos::RCP<Teuchos::ParameterList> parentList, bool whenParamEqualsValue):
00033   Condition(Condition::ParamCon),
00034   parameterName(parameterName),
00035   parentList(parentList),
00036   whenParamEqualsValue(whenParamEqualsValue)
00037 {
00038   parameter = parentList->getEntryPtr(parameterName);
00039   if(parameter == NULL){
00040     throw InvalidConditionException("Oh noes!!!!!!! Looks like the parameter " +
00041     parameterName + " isn't actually contained in the " + parentList->name() + " parameter list. "
00042     "You should go back and check your code. Maybe the information below can help you.\n\n"
00043     "Error: Parameter not contained in specified Parent list:\n"
00044     "Problem Parameter: " + parameterName + "\n"
00045     "Problem List: " + parentList->name());
00046   }
00047 }
00048 
00049 bool ParameterCondition::containsAtLeasteOneParameter(){
00050   return true;
00051 }
00052 
00053 Dependency::ParameterParentMap ParameterCondition::getAllParameters(){
00054   Dependency::ParameterParentMap toReturn;
00055   toReturn.insert(std::pair<std::string, Teuchos::RCP<Teuchos::ParameterList> >(parameterName, parentList));
00056   return toReturn;
00057 }
00058 
00059 BinaryLogicalCondition::BinaryLogicalCondition(ConditionList& conditions):
00060   Condition(Condition::BinLogicCon),
00061   conditions(conditions)
00062 {
00063   if(conditions.size() ==0){
00064     throw InvalidConditionException("Sorry bud, but you gotta at least give me one condition "
00065     "when you're constructing a BinaryLogicalCondition. Looks like you didn't. I'm just gonna "
00066     "chalk it up a silly little mistake though. Take a look over your conditions again and make sure "
00067     "you don't ever give any of your BinaryLogicConditions and empty condition list.\n\n"
00068     "Error: Empty condition list given to a BinaryLogicalCondition constructor.");
00069   }
00070 }
00071 
00072 
00073 void BinaryLogicalCondition::addCondition(Teuchos::RCP<Condition> toAdd){
00074   conditions.append(toAdd);
00075 }
00076 
00077 bool BinaryLogicalCondition::containsAtLeasteOneParameter(){
00078   for(ConditionList::iterator it=conditions.begin(); it!=conditions.end(); ++it){
00079     if((*it)->containsAtLeasteOneParameter()){
00080       return true;
00081     }
00082   }
00083   return false;
00084 }
00085 
00086 Dependency::ParameterParentMap BinaryLogicalCondition::getAllParameters(){
00087   Dependency::ParameterParentMap toReturn;
00088   Dependency::ParameterParentMap currentMap;
00089   for(ConditionList::iterator it = conditions.begin(); it != conditions.end(); ++it){
00090     currentMap = (*it)->getAllParameters();
00091     toReturn.insert(currentMap.begin(), currentMap.end());
00092   }
00093   return toReturn;
00094 }
00095 
00096 OrCondition::OrCondition(ConditionList& conditions):
00097   BinaryLogicalCondition(conditions){}
00098 
00099 bool OrCondition::isConditionTrue(){
00100   ConditionList::iterator it = conditions.begin();
00101   bool toReturn = (*it)->isConditionTrue();
00102   ++it;
00103   for(;it != conditions.end(); ++it){
00104     toReturn = (toReturn || (*it)->isConditionTrue());
00105   }
00106   return toReturn;
00107 }
00108 
00109 AndCondition::AndCondition(ConditionList& conditions):
00110   BinaryLogicalCondition(conditions){}
00111 
00112 bool AndCondition::isConditionTrue(){
00113   ConditionList::iterator it = conditions.begin();
00114   bool toReturn = (*it)->isConditionTrue();
00115   ++it;
00116   for(;it != conditions.end(); ++it){
00117     toReturn = (toReturn && (*it)->isConditionTrue());
00118   }
00119   return toReturn;
00120 }
00121 
00122 EqualsCondition::EqualsCondition(ConditionList& conditions):
00123   BinaryLogicalCondition(conditions){}
00124 
00125 bool EqualsCondition::isConditionTrue(){
00126   ConditionList::iterator it = conditions.begin();
00127   bool toReturn = (*it)->isConditionTrue();
00128   ++it;
00129   for(;it != conditions.end(); ++it){
00130     toReturn = (toReturn == (*it)->isConditionTrue());
00131   }
00132   return toReturn;
00133 }
00134 
00135 NotCondition::NotCondition(Teuchos::RCP<Condition> condition):
00136   Condition(Condition::NotCon),
00137   condition(condition)
00138 {
00139   if(condition.is_null()){
00140     throw InvalidConditionException("OOOOOOOOPppppps! Looks like you tried to give me "
00141     "a null pointer when you were making a not conditon. That's a no no. Go back and "
00142     "checkout your not conditions and make sure you didn't give any of them a null pointer "
00143     "as an argument to the constructor.\n\n"
00144     "Error: Null pointer given to NotCondition constructor.");
00145   }
00146 }
00147 
00148 bool NotCondition::isConditionTrue(){
00149   return (!condition->isConditionTrue());
00150 }
00151 
00152 bool NotCondition::containsAtLeasteOneParameter(){
00153   return condition->getType() == Condition::ParamCon;
00154 }
00155 
00156 Dependency::ParameterParentMap NotCondition::getAllParameters(){
00157   return condition->getAllParameters();
00158 }
00159 
00160 StringCondition::StringCondition(std::string parameterName, Teuchos::RCP<Teuchos::ParameterList> parentList, std::string value, bool whenParamEqualsValue):
00161   ParameterCondition(parameterName, parentList, whenParamEqualsValue), values(ValueList(1,value))
00162 {
00163   if(!parameter->isType<std::string>()){
00164     throw InvalidConditionException("The parameter of a String Condition "
00165     "must be of type string. \n"
00166     "Expected type: std::string\n"
00167     "Actual type: " + parameter->getAny().typeName() + "\n");
00168   }
00169 }
00170 
00171 StringCondition::StringCondition(std::string parameterName, Teuchos::RCP<Teuchos::ParameterList> parentList, ValueList values, bool whenParamEqualsValue):
00172   ParameterCondition(parameterName, parentList, whenParamEqualsValue), values(values)
00173 {
00174   if(!parameter->isType<std::string>()){
00175     throw InvalidConditionException("The parameter of a String Condition "
00176     "must be of type string. \n"
00177     "Expected type: std::string\n"
00178     "Actual type: " + parameter->getAny().typeName() + "\n");
00179   }
00180 }
00181 
00182 bool StringCondition::isConditionTrue(){
00183   bool toReturn = find(values.begin(), values.end(), Teuchos::getValue<std::string>(*parameter)) != values.end();
00184   return whenParamEqualsValue ? toReturn : !toReturn;
00185 }
00186 
00187 BoolCondition::BoolCondition(std::string parameterName, Teuchos::RCP<Teuchos::ParameterList> parentList, bool whenParamEqualsValue):
00188   ParameterCondition(parameterName, parentList, whenParamEqualsValue)
00189 {
00190   if(!parameter->isType<bool>()){
00191     throw InvalidConditionException("The parameter of a Bool Condition "
00192     "must be of type Bool. \n"
00193     "Expected type: Bool\n"
00194     "Actual type: " + parameter->getAny().typeName() + "\n");
00195   }
00196 }
00197 
00198 bool BoolCondition::isConditionTrue(){
00199   bool toReturn = Teuchos::getValue<bool>(*parameter);
00200   return whenParamEqualsValue ? toReturn : !toReturn;
00201 }
00202 
00203 }
00204 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends