Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_StandardConditions.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 
00043 
00044 #include "Teuchos_StandardConditions.hpp"
00045 
00046 namespace Teuchos{
00047 
00048 ParameterCondition::ParameterCondition(RCP<const ParameterEntry> parameter):
00049   parameterEntry_(parameter)
00050 {
00051   TEST_FOR_EXCEPTION(is_null(parameter),
00052     InvalidConditionException,
00053     "Parameter conditions can't be given a null parameter" <<
00054     std::endl << std::endl);
00055 }
00056 
00057 Dependency::ConstParameterEntryList 
00058 ParameterCondition::getAllParameters() const
00059 {
00060   Dependency::ConstParameterEntryList toReturn;
00061   toReturn.insert(getParameter());
00062   return toReturn;
00063 }
00064 
00065 BoolLogicCondition::BoolLogicCondition(ConstConditionList& conditions):
00066   conditions_(conditions)
00067 {
00068   TEST_FOR_EXCEPTION(conditions_.size() ==0,
00069     InvalidConditionException,
00070     "Sorry bud, but you gotta at least give "
00071     "me one condition "
00072     "when you're constructing a BoolLogicCondition. Looks like you didn't. "
00073     "I'm just gonna "
00074     "chalk it up a silly little mistake though. Take a look over your "
00075     "conditions again and make sure "
00076     "you don't ever give any of your BoolLogicConditions and empty "
00077     "condition list." << std::endl << std::endl <<
00078     "Error: Empty condition list given to a BoolLogicCondition "
00079     "constructor.");
00080 }
00081 
00082 
00083 void BoolLogicCondition::addCondition(RCP<const Condition> toAdd){
00084   conditions_.append(toAdd);
00085 }
00086 
00087 bool BoolLogicCondition::isConditionTrue() const{
00088   ConstConditionList::const_iterator it = conditions_.begin();
00089   bool toReturn = (*it)->isConditionTrue();
00090   ++it;
00091   for(;it != conditions_.end(); ++it){
00092     toReturn = applyOperator(toReturn,(*it)->isConditionTrue());
00093   }
00094   return toReturn;
00095 }
00096 
00097 bool BoolLogicCondition::containsAtLeasteOneParameter() const{
00098   for(
00099     ConstConditionList::const_iterator it=conditions_.begin();
00100     it!=conditions_.end();
00101     ++it)
00102   {
00103     if((*it)->containsAtLeasteOneParameter()){
00104       return true;
00105     }
00106   }
00107   return false;
00108 }
00109 
00110 Dependency::ConstParameterEntryList 
00111 BoolLogicCondition::getAllParameters() const{
00112   Dependency::ConstParameterEntryList toReturn;
00113   Dependency::ConstParameterEntryList currentList;
00114   for(
00115     ConstConditionList::const_iterator it = conditions_.begin();
00116     it != conditions_.end();
00117     ++it)
00118   {
00119     currentList = (*it)->getAllParameters();
00120     toReturn.insert(currentList.begin(), currentList.end());
00121   }
00122   return toReturn;
00123 }
00124 
00125 OrCondition::OrCondition(ConstConditionList& conditions):
00126   BoolLogicCondition(conditions){}
00127 
00128 bool OrCondition::applyOperator(bool op1, bool op2) const{
00129   return op1 || op2;
00130 }
00131 
00132 RCP<OrCondition> DummyObjectGetter<OrCondition>::getDummyObject(){
00133   Condition::ConstConditionList dummyList;
00134   dummyList.append(DummyObjectGetter<BoolCondition>::getDummyObject());
00135   return rcp(new OrCondition(dummyList));
00136 }
00137 
00138 AndCondition::AndCondition(ConstConditionList& conditions):
00139   BoolLogicCondition(conditions){}
00140 
00141 bool AndCondition::applyOperator(bool op1, bool op2) const{
00142   return op1 && op2;
00143 }
00144 
00145 RCP<AndCondition> DummyObjectGetter<AndCondition>::getDummyObject(){
00146   Condition::ConstConditionList dummyList;
00147   dummyList.append(DummyObjectGetter<BoolCondition>::getDummyObject());
00148   return rcp(new AndCondition(dummyList));
00149 }
00150 
00151 EqualsCondition::EqualsCondition(ConstConditionList& conditions):
00152   BoolLogicCondition(conditions){}
00153 
00154 bool EqualsCondition::applyOperator(bool op1, bool op2) const{
00155   return op1 == op2;
00156 }
00157 
00158 RCP<EqualsCondition> DummyObjectGetter<EqualsCondition>::getDummyObject(){
00159   Condition::ConstConditionList dummyList;
00160   dummyList.append(DummyObjectGetter<BoolCondition>::getDummyObject());
00161   return rcp(new EqualsCondition(dummyList));
00162 }
00163 
00164 NotCondition::NotCondition(RCP<const Condition> childCondition):
00165   childCondition_(childCondition)
00166 {
00167   TEST_FOR_EXCEPTION(childCondition_.is_null(),
00168     InvalidConditionException,
00169     "OOOOOOOOPppppps! Looks like you tried "
00170     "to give me "
00171     "a null pointer when you were making a not conditon. "
00172     "That's a no no. Go back and "
00173     "checkout your not conditions and make sure you didn't "
00174     "give any of them a null pointer "
00175     "as an argument to the constructor." << std::endl << std::endl <<
00176     "Error: Null pointer given to NotCondition constructor.");
00177 }
00178 
00179 bool NotCondition::isConditionTrue() const{
00180   return (!childCondition_->isConditionTrue());
00181 }
00182 
00183 bool NotCondition::containsAtLeasteOneParameter() const{
00184   return childCondition_->containsAtLeasteOneParameter();
00185 }
00186 
00187 Dependency::ConstParameterEntryList NotCondition::getAllParameters() const{
00188   return childCondition_->getAllParameters();
00189 }
00190 
00191 RCP<NotCondition> DummyObjectGetter<NotCondition>::getDummyObject(){
00192   return rcp(new NotCondition(
00193     DummyObjectGetter<BoolCondition>::getDummyObject()));
00194 }
00195 
00196 StringCondition::StringCondition(
00197   RCP<const ParameterEntry> parameter,
00198   std::string value):
00199   ParameterCondition(parameter), 
00200   values_(ValueList(1,value))
00201 {
00202   checkParameterType();
00203 }
00204 
00205 StringCondition::StringCondition(
00206   RCP<const ParameterEntry> parameter,
00207   ValueList values):
00208   ParameterCondition(parameter), 
00209   values_(values)
00210 {
00211   checkParameterType();
00212 }
00213 
00214 void StringCondition::checkParameterType(){
00215   TEST_FOR_EXCEPTION(!getParameter()->isType<std::string>(),
00216     InvalidConditionException,
00217     "The parameter of a String Condition "
00218     "must be of type string." << std::endl << 
00219     "Expected type: " << TypeNameTraits<std::string>::name() << std::endl <<
00220     "Actual type: " << getParameter()->getAny().typeName() << 
00221     std::endl << std::endl);
00222 }
00223   
00224 
00225 bool StringCondition::evaluateParameter() const{
00226   return  find(
00227     values_.begin(), values_.end(), 
00228     getValue<std::string>(*getParameter())) != values_.end();
00229 }
00230 
00231 RCP<StringCondition> DummyObjectGetter<StringCondition>::getDummyObject(){
00232   std::string empty = "";
00233   return rcp(new StringCondition(rcp(new ParameterEntry(empty)), empty));
00234 }
00235 
00236 BoolCondition::BoolCondition(RCP<const ParameterEntry> parameter):
00237   ParameterCondition(parameter)
00238 {
00239   TEST_FOR_EXCEPTION(!getParameter()->isType<bool>(),
00240     InvalidConditionException,
00241     "The parameter of a Bool Condition "
00242     "must be of type " << TypeNameTraits<bool>::name() << std::endl <<
00243     "Expected type: Bool" << std::endl <<
00244     "Actual type: " << getParameter()->getAny().typeName() <<
00245     std::endl << std::endl);
00246 }
00247 
00248 bool BoolCondition::evaluateParameter() const{
00249   return getValue<bool>(*getParameter());
00250 }
00251 
00252 RCP<BoolCondition> DummyObjectGetter<BoolCondition>::getDummyObject(){
00253   return rcp(new BoolCondition(rcp(new ParameterEntry(true))));
00254 }
00255 
00256 
00257 } //namespace Teuchos
00258 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines