Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_StandardDependencyXMLConverters.cpp
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
46namespace 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(
57 getShowIfAttributeName(), VisualDependency::getShowIfDefaultValue());
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{
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
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();
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,
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 */,
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
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
216void
219 XMLObject& xmlObj,
220 ValidatortoIDMap& validatorIDsMap) const
221{
222 RCP<const StringValidatorDependency> castedDependency =
223 rcp_dynamic_cast<const StringValidatorDependency>(dependency, true);
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 }
238 pairTag.addAttribute(getValidatorIdAttributeName(),
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(
250 getDefaultValidatorIdAttributeName(),
251 validatorIDsMap.find(defaultVali)->second);
252 }
253}
254
257 const XMLObject& xmlObj,
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());
277 getValidatorIdAttributeName());
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);
283 validatorIDsMap.find(valiID)->second;
284 valueValidatorMap.insert(
286 }
287
288 RCP<ParameterEntryValidator> defaultValidator = null;
289 if(xmlObj.hasAttribute(getDefaultValidatorIdAttributeName())){
292 getDefaultValidatorIdAttributeName());
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
303 dependee, dependents, valueValidatorMap, defaultValidator));
304}
305
306void
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(
326 getTrueValidatorIdAttributeName(),
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(
336 getFalseValidatorIdAttributeName(),
337 validatorIDsMap.find(falseVali)->second);
338 }
339
340}
341
344 const XMLObject& xmlObj,
346 const Dependency::ParameterEntryList dependents,
347 const IDtoValidatorMap& validatorIDsMap) const
348{
349
350 RCP<ParameterEntryValidator> trueValidator = null;
351 RCP<ParameterEntryValidator> falseValidator = null;
352
353 if(xmlObj.hasAttribute(getTrueValidatorIdAttributeName())){
354
357 getTrueValidatorIdAttributeName());
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
373 if(xmlObj.hasAttribute(getFalseValidatorIdAttributeName())){
376 getFalseValidatorIdAttributeName());
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
A database for ConditionXMLConverters.
A collection of standard DependencyXMLConverters.
iterator begin()
void push_back(const value_type &x)
std::vector< std::string >::const_iterator const_iterator
The type of a const forward iterator.
void convertSpecialValidatorAttributes(RCP< const ValidatorDependency > dependency, XMLObject &xmlObj, ValidatortoIDMap &validatorIDsMap) const
A BoolValidatorDependency says the following about the relationship between two parameters: Dependeni...
void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
A bool visual dependency says the following about the relationship between two elements in a Paramete...
void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
A condition visual dependency says the following about the relationship between elements in a Paramet...
static RCP< Condition > convertXML(const XMLObject &xmlObject, const XMLParameterListReader::EntryIDsMap &entryIDsMap)
Given an XMLObject and IDtoConditionMap, converts the XMLObject to a Condition.
static XMLObject convertCondition(RCP< const Condition > condition, const XMLParameterListWriter::EntryIDsMap &entryIDsMap)
Given a condition and ConditiontoIDMap, converts the condition to XML.
static const std::string & getXMLTagName()
std::set< RCP< ParameterEntry >, RCPComp > ParameterEntryList
A list of Dependees.
std::set< RCP< const ParameterEntry >, RCPConstComp > ConstParameterEntryList
A list of dependents.
Maps Validators to integers.
const_iterator end() const
Returns a const_reference to the end of the map.
const_iterator find(int id) const
Retrieves and iterator to a validator and id based on the id given.
Thrown when no condtion tag is found when converting a ConditionVisualDependency from XML.
Thrown when converting a dependency that has validaotrs to and from XML. This excetpion indicates tha...
Thrown when converting a StrinvValidatorDependcny from XML and no valuesAndValidators tag is found.
Smart reference counting pointer class for automatic garbage collection.
void convertSpecialValidatorAttributes(RCP< const ValidatorDependency > dependency, XMLObject &xmlObj, ValidatortoIDMap &validatorIDsMap) const
A StringValidatorDependency says the following about the relationship between two parameters: Depende...
std::pair< std::string, RCP< const ParameterEntryValidator > > ValueToValidatorPair
Conveniece typedef.
std::map< std::string, RCP< const ParameterEntryValidator > > ValueToValidatorMap
Conveniece typedef.
static const std::string & getStringValuesTagName()
Gets the StringValues Tag.
void convertSpecialVisualAttributes(RCP< const VisualDependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap) const
A string visual depdencies says the following about the relationship between two elements in a Parame...
Thrown when a Dependency has too many dependees specified in its XML.
void convertDependency(const RCP< const Dependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap, ValidatortoIDMap &validatorIDsMap) const
RCP< Dependency > convertXML(const XMLObject &xmlObj, const Dependency::ConstParameterEntryList dependees, const Dependency::ParameterEntryList dependets, const XMLParameterListReader::EntryIDsMap &entryIDsMap, const IDtoValidatorMap &validatorIDsMap) const
virtual void convertSpecialValidatorAttributes(RCP< const ValidatorDependency > dependency, XMLObject &xmlObj, ValidatortoIDMap &validatorIDsMap) const =0
Converts any special aspects of a specific validator dependency to xml.
A class for mapping validators to integers.
const_iterator end() const
Returns a const_reference to the end of the map.
void insert(RCP< const ParameterEntryValidator > toInsert)
inserts an IDValidatorPair into the map.
const_iterator find(const RCP< const ParameterEntryValidator > validator) const
Returns an iterator to the validator and id specified by the validator.
Thrown when a StringVisualDependency is being converted from XML and no Values tag is found.
void convertDependency(const RCP< const Dependency > dependency, XMLObject &xmlObj, const XMLParameterListWriter::EntryIDsMap &entryIDsMap, ValidatortoIDMap &validatorIDsMap) const
RCP< Dependency > convertXML(const XMLObject &xmlObj, const Dependency::ConstParameterEntryList dependees, const Dependency::ParameterEntryList dependets, const XMLParameterListReader::EntryIDsMap &entryIDsMap, const IDtoValidatorMap &validatorIDsMap) const
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.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object,...
void addBool(const std::string &name, bool val)
Add a bool as an attribute.
void addChild(const XMLObject &child)
Add a child node to the node.
const std::string & getRequired(const std::string &name) const
Get an attribute, throwing an std::exception if it is not found.
T getWithDefault(const std::string &name, const T &defaultValue) const
Get an attribute, assigning a default value if the requested attribute does not exist.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
const XMLObject & getChild(int i) const
Return the i-th child node.
bool hasAttribute(const std::string &name) const
Find out if the current node has an attribute of the specified name.
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.
int numChildren() const
Return the number of child nodes owned by this node.
std::map< ParameterEntry::ParameterEntryID, RCP< ParameterEntry > > EntryIDsMap
Convenience typedef.
std::map< RCP< const ParameterEntry >, ParameterEntry::ParameterEntryID, RCPConstComp > EntryIDsMap
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.