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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 
13 
14 namespace Teuchos{
15 
16 
18  const XMLObject& xmlObj,
20  const Dependency::ParameterEntryList dependents,
21  const XMLParameterListReader::EntryIDsMap& entryIDsMap,
22  const IDtoValidatorMap& /*validatorIDsMap*/) const
23 {
24  bool showIf = xmlObj.getWithDefault(
27  xmlObj,
28  dependees,
29  dependents,
30  showIf,
31  entryIDsMap);
32 }
33 
35  const RCP<const Dependency> dependency,
36  XMLObject& xmlObj,
37  const XMLParameterListWriter::EntryIDsMap& entryIDsMap,
38  ValidatortoIDMap& /*validatorIDsMap*/) const
39 {
40  RCP<const VisualDependency> castedDep =
41  rcp_dynamic_cast<const VisualDependency>(dependency, true);
42 
43  xmlObj.addBool(getShowIfAttributeName(), castedDep->getShowIf());
44  convertSpecialVisualAttributes(castedDep, xmlObj, entryIDsMap);
45 }
46 
48  const XMLObject& xmlObj,
50  const Dependency::ParameterEntryList dependents,
51  const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/,
52  const IDtoValidatorMap& validatorIDsMap) const
53 {
54  TEUCHOS_TEST_FOR_EXCEPTION(dependees.size() > 1,
56  "A Validator Dependency can only have 1 dependee!" <<
57  std::endl << std::endl);
59  xmlObj, *(dependees.begin()), dependents, validatorIDsMap);
60 }
61 
63  const RCP<const Dependency> dependency,
64  XMLObject& xmlObj,
65  const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/,
66  ValidatortoIDMap& validatorIDsMap) const
67 {
69  rcp_dynamic_cast<const ValidatorDependency>(dependency, true);
70  convertSpecialValidatorAttributes(castedDep, xmlObj, validatorIDsMap);
71 }
72 
73 
75  RCP<const VisualDependency> dependency,
76  XMLObject& xmlObj,
77  const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/) const
78 {
79  RCP<const StringVisualDependency> castedDependency =
80  rcp_dynamic_cast<const StringVisualDependency>(dependency, true);
81  StringVisualDependency::ValueList valueList = castedDependency->getValues();
82  XMLObject valuesTag(getStringValuesTagName());
83  for(
85  it != valueList.end();
86  ++it)
87  {
88  XMLObject stringValue(getStringTagName());
89  stringValue.addAttribute(getValueAttributeName(), *it);
90  valuesTag.addChild(stringValue);
91  }
92  xmlObj.addChild(valuesTag);
93 }
94 
97  const XMLObject& xmlObj,
99  const Dependency::ParameterEntryList dependents,
100  bool showIf,
101  const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/) const
102 {
103  TEUCHOS_TEST_FOR_EXCEPTION(dependees.size() > 1,
105  "A StringVisualDependency can only have 1 dependee!" <<
106  std::endl << std::endl);
107 
109  int valuesTagIndex = xmlObj.findFirstChild(getStringValuesTagName());
110 
111  TEUCHOS_TEST_FOR_EXCEPTION(valuesTagIndex < 0,
113  "Couldn't find " << getStringValuesTagName() << " tag for a " <<
114  "StringVisualDependency!" << std::endl <<std::endl);
115 
116  XMLObject valuesTag = xmlObj.getChild(valuesTagIndex);
117 
118  for(int i=0; i<valuesTag.numChildren(); ++i){
119  XMLObject child = valuesTag.getChild(i);
120  valueList.push_back(child.getRequired(getValueAttributeName()));
121  }
122 
123  return rcp(
125  *(dependees.begin()),
126  dependents,
127  valueList,
128  showIf));
129 }
130 
132  RCP<const VisualDependency> /* dependency */,
133  XMLObject& /* xmlObj */,
134  const XMLParameterListWriter::EntryIDsMap& /*entryIDsMap*/) const
135 {}
136 
139  const XMLObject& /* xmlObj */,
140  const Dependency::ConstParameterEntryList dependees,
141  const Dependency::ParameterEntryList dependents,
142  bool showIf,
143  const XMLParameterListReader::EntryIDsMap& /*entryIDsMap*/) const
144 {
145  TEUCHOS_TEST_FOR_EXCEPTION(dependees.size() > 1,
147  "A BoolVisualDependency can only have 1 dependee!" <<
148  std::endl << std::endl);
149  return rcp(new BoolVisualDependency(
150  *(dependees.begin()), dependents, showIf));
151 }
152 
154  RCP<const VisualDependency> dependency,
155  XMLObject& xmlObj,
156  const XMLParameterListWriter::EntryIDsMap& entryIDsMap) const
157 {
158  RCP<const ConditionVisualDependency> castedDependency =
159  rcp_dynamic_cast<const ConditionVisualDependency>(dependency, true);
160  xmlObj.addChild(
162  castedDependency->getCondition(), entryIDsMap));
163 }
164 
167  const XMLObject& xmlObj,
168  const Dependency::ConstParameterEntryList /* dependees */,
169  const Dependency::ParameterEntryList dependents,
170  bool showIf,
171  const XMLParameterListReader::EntryIDsMap& entryIDsMap) const
172 {
173  int conditionIndex = xmlObj.findFirstChild(Condition::getXMLTagName());
174  TEUCHOS_TEST_FOR_EXCEPTION(conditionIndex < 0,
176  "ConditionVisualDependencies must have a Condition tag!"
177  );
178  XMLObject conditionObj = xmlObj.getChild(conditionIndex);
179  Teuchos::RCP<Condition> condition =
180  ConditionXMLConverterDB::convertXML(conditionObj, entryIDsMap);
181  return rcp(new ConditionVisualDependency(condition, dependents, showIf));
182 }
183 
184 void
187  XMLObject& xmlObj,
188  ValidatortoIDMap& validatorIDsMap) const
189 {
190  RCP<const StringValidatorDependency> castedDependency =
191  rcp_dynamic_cast<const StringValidatorDependency>(dependency, true);
192  XMLObject valueMapTag(getValuesAndValidatorsTag());
193  const StringValidatorDependency::ValueToValidatorMap valuesAndValidators =
194  castedDependency->getValuesAndValidators();
195  for(
196  StringValidatorDependency::ValueToValidatorMap::const_iterator it =
197  valuesAndValidators.begin();
198  it != valuesAndValidators.end();
199  ++it)
200  {
201  XMLObject pairTag(getPairTag());
202  pairTag.addAttribute(getValueAttributeName(), it->first);
203  if(validatorIDsMap.find(it->second) == validatorIDsMap.end()){
204  validatorIDsMap.insert(it->second);
205  }
207  validatorIDsMap.find(it->second)->second);
208  valueMapTag.addChild(pairTag);
209  }
210  xmlObj.addChild(valueMapTag);
212  castedDependency->getDefaultValidator();
213  if(nonnull(defaultVali)){
214  if(validatorIDsMap.find(defaultVali) == validatorIDsMap.end()){
215  validatorIDsMap.insert(defaultVali);
216  }
217  xmlObj.addAttribute(
219  validatorIDsMap.find(defaultVali)->second);
220  }
221 }
222 
225  const XMLObject& xmlObj,
226  RCP<const ParameterEntry> dependee,
227  const Dependency::ParameterEntryList dependents,
228  const IDtoValidatorMap& validatorIDsMap) const
229 {
231  int valuesAndValidatorIndex =
233 
234  TEUCHOS_TEST_FOR_EXCEPTION(valuesAndValidatorIndex < 0,
236  "Error: All StringValidatorDependencies must have a " <<
237  getValuesAndValidatorsTag() << "tag!" << std::endl << std::endl);
238 
239  XMLObject valuesAndValidatorTag = xmlObj.getChild(valuesAndValidatorIndex);
240  for(int i=0; i < valuesAndValidatorTag.numChildren(); ++i){
241  XMLObject child = valuesAndValidatorTag.getChild(i);
242  std::string value = child.getRequired(getValueAttributeName());
246  TEUCHOS_TEST_FOR_EXCEPTION(validatorIDsMap.find(valiID) == validatorIDsMap.end(),
248  "Could not find a validator corresponding to the ID " << valiID <<
249  " in the given validatorIDsMap!" << std::endl << std::endl);
250  RCP<ParameterEntryValidator> validator =
251  validatorIDsMap.find(valiID)->second;
252  valueValidatorMap.insert(
254  }
255 
256  RCP<ParameterEntryValidator> defaultValidator = null;
262  validatorIDsMap.find(defaultValiID) == validatorIDsMap.end(),
264  "Could not find a validator (for the default validator) " <<
265  "corresponding to the ID " << defaultValiID <<
266  " in the given validatorIDsMap!" << std::endl << std::endl);
267  defaultValidator = validatorIDsMap.find(defaultValiID)->second;
268  }
269 
270  return rcp(new StringValidatorDependency(
271  dependee, dependents, valueValidatorMap, defaultValidator));
272 }
273 
274 void
277  XMLObject& xmlObj,
278  ValidatortoIDMap& validatorIDsMap) const
279 {
280  RCP<const BoolValidatorDependency> castedDependency =
281  rcp_dynamic_cast<const BoolValidatorDependency>(dependency, true);
282 
284  castedDependency->getTrueValidator();
286  castedDependency->getFalseValidator();
287 
288  if(nonnull(trueVali)){
289  if(validatorIDsMap.find(castedDependency->getTrueValidator()) ==
290  validatorIDsMap.end()){
291  validatorIDsMap.insert(castedDependency->getTrueValidator());
292  }
293  xmlObj.addAttribute(
295  validatorIDsMap.find(castedDependency->getTrueValidator())->second);
296  }
297 
298  if(nonnull(falseVali)){
299  if(validatorIDsMap.find(falseVali) ==
300  validatorIDsMap.end()){
301  validatorIDsMap.insert(falseVali);
302  }
303  xmlObj.addAttribute(
305  validatorIDsMap.find(falseVali)->second);
306  }
307 
308 }
309 
312  const XMLObject& xmlObj,
313  RCP<const ParameterEntry> dependee,
314  const Dependency::ParameterEntryList dependents,
315  const IDtoValidatorMap& validatorIDsMap) const
316 {
317 
318  RCP<ParameterEntryValidator> trueValidator = null;
319  RCP<ParameterEntryValidator> falseValidator = null;
320 
322 
326 
328  validatorIDsMap.find(trueID)
329  ==
330  validatorIDsMap.end(),
332  "Could not find a Validator for the True validator " <<
333  "with ID " << trueID <<
334  " in the given validatorIDsMap!" << std::endl << std::endl);
335 
336  trueValidator =
337  validatorIDsMap.find(trueID)->second;
338  }
339 
340 
345 
347  validatorIDsMap.find(falseID)
348  ==
349  validatorIDsMap.end(),
351  "Could not find a Validator for the False validator " <<
352  "with ID " << falseID <<
353  " in the given validatorIDsMap!" << std::endl << std::endl);
354 
355  falseValidator =
356  validatorIDsMap.find(falseID)->second;
357  }
358 
359  return rcp(new BoolValidatorDependency(
360  dependee, dependents, trueValidator, falseValidator));
361 }
362 
363 
364 
365 } //namespace Teuchos
366 
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 dependency 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.