Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_StandardDependencyXMLConverters.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
44 
45 
46 namespace Teuchos{
47 
48 
50  const XMLObject& xmlObj,
52  const Dependency::ParameterEntryList dependents,
53  const XMLParameterListReader::EntryIDsMap& entryIDsMap,
54  const IDtoValidatorMap& /*validatorIDsMap*/) const
55 {
56  bool showIf = xmlObj.getWithDefault(
59  xmlObj,
60  dependees,
61  dependents,
62  showIf,
63  entryIDsMap);
64 }
65 
67  const RCP<const Dependency> dependency,
68  XMLObject& xmlObj,
69  const XMLParameterListWriter::EntryIDsMap& entryIDsMap,
70  ValidatortoIDMap& /*validatorIDsMap*/) const
71 {
72  RCP<const VisualDependency> castedDep =
73  rcp_dynamic_cast<const VisualDependency>(dependency, true);
74 
75  xmlObj.addBool(getShowIfAttributeName(), castedDep->getShowIf());
76  convertSpecialVisualAttributes(castedDep, xmlObj, entryIDsMap);
77 }
78 
80  const XMLObject& xmlObj,
82  const Dependency::ParameterEntryList dependents,
83  const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/,
84  const IDtoValidatorMap& validatorIDsMap) const
85 {
86  TEUCHOS_TEST_FOR_EXCEPTION(dependees.size() > 1,
88  "A Validator Dependency can only have 1 dependee!" <<
89  std::endl << std::endl);
91  xmlObj, *(dependees.begin()), dependents, validatorIDsMap);
92 }
93 
95  const RCP<const Dependency> dependency,
96  XMLObject& xmlObj,
97  const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/,
98  ValidatortoIDMap& validatorIDsMap) const
99 {
101  rcp_dynamic_cast<const ValidatorDependency>(dependency, true);
102  convertSpecialValidatorAttributes(castedDep, xmlObj, validatorIDsMap);
103 }
104 
105 
107  RCP<const VisualDependency> dependency,
108  XMLObject& xmlObj,
109  const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/) const
110 {
111  RCP<const StringVisualDependency> castedDependency =
112  rcp_dynamic_cast<const StringVisualDependency>(dependency, true);
113  StringVisualDependency::ValueList valueList = castedDependency->getValues();
114  XMLObject valuesTag(getStringValuesTagName());
115  for(
117  it != valueList.end();
118  ++it)
119  {
120  XMLObject stringValue(getStringTagName());
121  stringValue.addAttribute(getValueAttributeName(), *it);
122  valuesTag.addChild(stringValue);
123  }
124  xmlObj.addChild(valuesTag);
125 }
126 
129  const XMLObject& xmlObj,
130  const Dependency::ConstParameterEntryList dependees,
131  const Dependency::ParameterEntryList dependents,
132  bool showIf,
133  const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/) const
134 {
135  TEUCHOS_TEST_FOR_EXCEPTION(dependees.size() > 1,
137  "A StringVisualDependency can only have 1 dependee!" <<
138  std::endl << std::endl);
139 
141  int valuesTagIndex = xmlObj.findFirstChild(getStringValuesTagName());
142 
143  TEUCHOS_TEST_FOR_EXCEPTION(valuesTagIndex < 0,
145  "Couldn't find " << getStringValuesTagName() << " tag for a " <<
146  "StringVisualDependency!" << std::endl <<std::endl);
147 
148  XMLObject valuesTag = xmlObj.getChild(valuesTagIndex);
149 
150  for(int i=0; i<valuesTag.numChildren(); ++i){
151  XMLObject child = valuesTag.getChild(i);
152  valueList.push_back(child.getRequired(getValueAttributeName()));
153  }
154 
155  return rcp(
157  *(dependees.begin()),
158  dependents,
159  valueList,
160  showIf));
161 }
162 
164  RCP<const VisualDependency> /* dependency */,
165  XMLObject& /* xmlObj */,
166  const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/) const
167 {}
168 
171  const XMLObject& /* xmlObj */,
172  const Dependency::ConstParameterEntryList dependees,
173  const Dependency::ParameterEntryList dependents,
174  bool showIf,
175  const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/) const
176 {
177  TEUCHOS_TEST_FOR_EXCEPTION(dependees.size() > 1,
179  "A BoolVisualDependency can only have 1 dependee!" <<
180  std::endl << std::endl);
181  return rcp(new BoolVisualDependency(
182  *(dependees.begin()), dependents, showIf));
183 }
184 
186  RCP<const VisualDependency> dependency,
187  XMLObject& xmlObj,
188  const XMLParameterListWriter::EntryIDsMap& entryIDsMap) const
189 {
190  RCP<const ConditionVisualDependency> castedDependency =
191  rcp_dynamic_cast<const ConditionVisualDependency>(dependency, true);
192  xmlObj.addChild(
194  castedDependency->getCondition(), entryIDsMap));
195 }
196 
199  const XMLObject& xmlObj,
200  const Dependency::ConstParameterEntryList /* dependees */,
201  const Dependency::ParameterEntryList dependents,
202  bool showIf,
203  const XMLParameterListReader::EntryIDsMap& entryIDsMap) const
204 {
205  int conditionIndex = xmlObj.findFirstChild(Condition::getXMLTagName());
206  TEUCHOS_TEST_FOR_EXCEPTION(conditionIndex < 0,
208  "ConditionVisualDependencies must have a Condition tag!"
209  );
210  XMLObject conditionObj = xmlObj.getChild(conditionIndex);
211  Teuchos::RCP<Condition> condition =
212  ConditionXMLConverterDB::convertXML(conditionObj, entryIDsMap);
213  return rcp(new ConditionVisualDependency(condition, dependents, showIf));
214 }
215 
216 void
219  XMLObject& xmlObj,
220  ValidatortoIDMap& validatorIDsMap) const
221 {
222  RCP<const StringValidatorDependency> castedDependency =
223  rcp_dynamic_cast<const StringValidatorDependency>(dependency, true);
224  XMLObject valueMapTag(getValuesAndValidatorsTag());
225  const StringValidatorDependency::ValueToValidatorMap valuesAndValidators =
226  castedDependency->getValuesAndValidators();
227  for(
228  StringValidatorDependency::ValueToValidatorMap::const_iterator it =
229  valuesAndValidators.begin();
230  it != valuesAndValidators.end();
231  ++it)
232  {
233  XMLObject pairTag(getPairTag());
234  pairTag.addAttribute(getValueAttributeName(), it->first);
235  if(validatorIDsMap.find(it->second) == validatorIDsMap.end()){
236  validatorIDsMap.insert(it->second);
237  }
239  validatorIDsMap.find(it->second)->second);
240  valueMapTag.addChild(pairTag);
241  }
242  xmlObj.addChild(valueMapTag);
244  castedDependency->getDefaultValidator();
245  if(nonnull(defaultVali)){
246  if(validatorIDsMap.find(defaultVali) == validatorIDsMap.end()){
247  validatorIDsMap.insert(defaultVali);
248  }
249  xmlObj.addAttribute(
251  validatorIDsMap.find(defaultVali)->second);
252  }
253 }
254 
257  const XMLObject& xmlObj,
258  RCP<const ParameterEntry> dependee,
259  const Dependency::ParameterEntryList dependents,
260  const IDtoValidatorMap& validatorIDsMap) const
261 {
263  int valuesAndValidatorIndex =
265 
266  TEUCHOS_TEST_FOR_EXCEPTION(valuesAndValidatorIndex < 0,
268  "Error: All StringValidatorDependencies must have a " <<
269  getValuesAndValidatorsTag() << "tag!" << std::endl << std::endl);
270 
271  XMLObject valuesAndValidatorTag = xmlObj.getChild(valuesAndValidatorIndex);
272  for(int i=0; i < valuesAndValidatorTag.numChildren(); ++i){
273  XMLObject child = valuesAndValidatorTag.getChild(i);
274  std::string value = child.getRequired(getValueAttributeName());
278  TEUCHOS_TEST_FOR_EXCEPTION(validatorIDsMap.find(valiID) == validatorIDsMap.end(),
280  "Could not find a validator corresponding to the ID " << valiID <<
281  " in the given validatorIDsMap!" << std::endl << std::endl);
282  RCP<ParameterEntryValidator> validator =
283  validatorIDsMap.find(valiID)->second;
284  valueValidatorMap.insert(
286  }
287 
288  RCP<ParameterEntryValidator> defaultValidator = null;
294  validatorIDsMap.find(defaultValiID) == validatorIDsMap.end(),
296  "Could not find a validator (for the default validator) " <<
297  "corresponding to the ID " << defaultValiID <<
298  " in the given validatorIDsMap!" << std::endl << std::endl);
299  defaultValidator = validatorIDsMap.find(defaultValiID)->second;
300  }
301 
302  return rcp(new StringValidatorDependency(
303  dependee, dependents, valueValidatorMap, defaultValidator));
304 }
305 
306 void
309  XMLObject& xmlObj,
310  ValidatortoIDMap& validatorIDsMap) const
311 {
312  RCP<const BoolValidatorDependency> castedDependency =
313  rcp_dynamic_cast<const BoolValidatorDependency>(dependency, true);
314 
316  castedDependency->getTrueValidator();
318  castedDependency->getFalseValidator();
319 
320  if(nonnull(trueVali)){
321  if(validatorIDsMap.find(castedDependency->getTrueValidator()) ==
322  validatorIDsMap.end()){
323  validatorIDsMap.insert(castedDependency->getTrueValidator());
324  }
325  xmlObj.addAttribute(
327  validatorIDsMap.find(castedDependency->getTrueValidator())->second);
328  }
329 
330  if(nonnull(falseVali)){
331  if(validatorIDsMap.find(falseVali) ==
332  validatorIDsMap.end()){
333  validatorIDsMap.insert(falseVali);
334  }
335  xmlObj.addAttribute(
337  validatorIDsMap.find(falseVali)->second);
338  }
339 
340 }
341 
344  const XMLObject& xmlObj,
345  RCP<const ParameterEntry> dependee,
346  const Dependency::ParameterEntryList dependents,
347  const IDtoValidatorMap& validatorIDsMap) const
348 {
349 
350  RCP<ParameterEntryValidator> trueValidator = null;
351  RCP<ParameterEntryValidator> falseValidator = null;
352 
354 
358 
360  validatorIDsMap.find(trueID)
361  ==
362  validatorIDsMap.end(),
364  "Could not find a Validator for the True validator " <<
365  "with ID " << trueID <<
366  " in the given validatorIDsMap!" << std::endl << std::endl);
367 
368  trueValidator =
369  validatorIDsMap.find(trueID)->second;
370  }
371 
372 
377 
379  validatorIDsMap.find(falseID)
380  ==
381  validatorIDsMap.end(),
383  "Could not find a Validator for the False validator " <<
384  "with ID " << falseID <<
385  " in the given validatorIDsMap!" << std::endl << std::endl);
386 
387  falseValidator =
388  validatorIDsMap.find(falseID)->second;
389  }
390 
391  return rcp(new BoolValidatorDependency(
392  dependee, dependents, trueValidator, falseValidator));
393 }
394 
395 
396 
397 } //namespace Teuchos
398 
static const std::string & getStringTagName()
Gets the String Tag.
const_iterator find(int id) const
Retrieves and iterator to a validator and id based on the id given.
Maps Validators to integers.
A BoolValidatorDependency says the following about the relationship between two parameters: Dependeni...
std::pair< std::string, RCP< const ParameterEntryValidator > > ValueToValidatorPair
Conveniece typedef.
Thrown when a StringVisualDependency is being converted from XML and no Values tag is found...
std::set< RCP< const ParameterEntry >, RCPConstComp > ConstParameterEntryList
A list of dependents.
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
An abstract parent class for all visual dependencies.
A string visual depdencies says the following about the relationship between two elements in a Parame...
A bool visual dependency says the following about the relationship between two elements in a Paramete...
static const std::string & getValueAttributeName()
Gets the Value attribute name.
std::set< RCP< ParameterEntry >, RCPComp > ParameterEntryList
A list of Dependees.
static RCP< Condition > convertXML(const XMLObject &xmlObject, const XMLParameterListReader::EntryIDsMap &entryIDsMap)
Given an XMLObject and IDtoConditionMap, converts the XMLObject to a Condition.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void addChild(const XMLObject &child)
Add a child node to the node.
static const std::string & getXMLTagName()
std::map< std::string, RCP< const ParameterEntryValidator > > ValueToValidatorMap
Conveniece typedef.
virtual void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const =0
Converts any special aspects of a specific visual dependency to xml.
static const std::string & getStringValuesTagName()
Gets the StringValues Tag.
void convertDependency(const RCP< const Dependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap, ValidatortoIDMap &validatorIDsMap) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
void convertDependency(const RCP< const Dependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap, ValidatortoIDMap &validatorIDsMap) const
void convertSpecialValidatorAttributes(RCP< const ValidatorDependency > dependency, XMLObject &xmlObj, ValidatortoIDMap &validatorIDsMap) const
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
A collection of standard DependencyXMLConverters.
A database for ConditionXMLConverters.
std::map< RCP< const ParameterEntry >, ParameterEntry::ParameterEntryID, RCPConstComp > EntryIDsMap
const_iterator end() const
Returns a const_reference to the end of the map.
void convertSpecialValidatorAttributes(RCP< const ValidatorDependency > dependency, XMLObject &xmlObj, ValidatortoIDMap &validatorIDsMap) const
A class for mapping validators to integers.
void insert(RCP< const ParameterEntryValidator > toInsert)
inserts an IDValidatorPair into the map.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
virtual void convertSpecialValidatorAttributes(RCP< const ValidatorDependency > dependency, XMLObject &xmlObj, ValidatortoIDMap &validatorIDsMap) const =0
Converts any special aspects of a specific validator dependency to xml.
const_iterator end() const
Returns a const_reference to the end of the map.
std::vector< std::string >::const_iterator const_iterator
The type of a const forward iterator.
const_iterator find(const RCP< const ParameterEntryValidator > validator) const
Returns an iterator to the validator and id specified by the validator.
std::map< ParameterEntry::ParameterEntryID, RCP< ParameterEntry > > EntryIDsMap
Convenience typedef.
Thrown when a Dependency has too many dependees specified in its XML.
static XMLObject convertCondition(RCP< const Condition > condition, const XMLParameterListWriter::EntryIDsMap &entryIDsMap)
Given a condition and ConditiontoIDMap, converts the condition to XML.
A StringValidatorDependency says the following about the relationship between two parameters: Depende...
int numChildren() const
Return the number of child nodes owned by this node.
void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
RCP< Dependency > convertXML(const XMLObject &xmlObj, const Dependency::ConstParameterEntryList dependees, const Dependency::ParameterEntryList dependets, const XMLParameterListReader::EntryIDsMap &entryIDsMap, const IDtoValidatorMap &validatorIDsMap) const
Thrown when converting a StrinvValidatorDependcny from XML and no valuesAndValidators tag is found...
RCP< Dependency > convertXML(const XMLObject &xmlObj, const Dependency::ConstParameterEntryList dependees, const Dependency::ParameterEntryList dependets, const XMLParameterListReader::EntryIDsMap &entryIDsMap, const IDtoValidatorMap &validatorIDsMap) const
An abstract base class for all validator dependencies.
Smart reference counting pointer class for automatic garbage collection.
int findFirstChild(std::string tagName) const
Returns the index of the first child found with the given tag name. Returns -1 if no child is found...
A condition visual dependency says the following about the relationship between elements in a Paramet...
const XMLObject & getChild(int i) const
Return the i-th child node.
static const std::string & getShowIfAttributeName()
Gets the name of the showif attribute.
bool hasAttribute(const std::string &name) const
Find out if the current node has an attribute of the specified name.
Thrown when converting a dependency that has validaotrs to and from XML. This excetpion indicates tha...
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
Thrown when no condtion tag is found when converting a ConditionVisualDependency from XML...
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.