18#include "sparta/parsers/ConfigParserYAML.hpp"
19#include "sparta/simulation/ParameterTree.hpp"
96 locfilter_node_(nullptr)
105 : locfilter_(locfilter),
106 locfilter_node_(filternode)
109 "cannot create a location filter for parameter application with a null filter node");
135 "Unhandled ConfigApplciator::ApplyFilter location filter case: "
136 <<
static_cast<uint32_t
>(locfilter_));
155 if(filter ==
nullptr){
156 o <<
"<null config filter ptr>";
177 "Unhandled ConfigApplciator::ApplyFilter location filter case: " <<
static_cast<uint32_t
>(filter.
getLocationFilter()));
204 bool verbose=
false) {
216 bool verbose=
false)
const = 0;
232 const std::string loc_pattern_;
237 const std::string value_;
250 return default_success_cond_;
257 const std::string& value,
259 loc_pattern_(loc_pattern), value_(value), default_success_cond_(required)
263 std::stringstream ss;
264 ss <<
"Parameter \"" << loc_pattern_ <<
"\" <- value: \"" << value_ <<
"\"";
265 switch(default_success_cond_){
269 ss <<
" [optional parameter]";
274 ss <<
" Unknown ApplySuccessCondition value: " << (int)default_success_cond_;
283 bool verbose)
const override
288 std::vector<sparta::TreeNode*> results;
289 uint32_t found = root->
findChildren(loc_pattern_, results);
292 std::vector<sparta::TreeNode*> filtered_results;
293 std::copy_if(results.begin(), results.end(),
294 std::back_inserter(filtered_results), [&](
TreeNode* n)->bool{return filter.test(n);});
296 if(0 == filtered_results.size()){
302 switch(effective_asc){
305 << loc_pattern_ <<
"\" and filter " << filter <<
" for which to set parameter value \""
311 sparta_assert(0,
"ParameterApplicator cannot have success policy of "
312 "ASC_DEFER. required=" << (
int)required <<
" default="
313 << (
int)default_success_cond_ <<
". This is likely a bug in "
314 "sparta::app unless other code is creating ParameterApplicators");
317 sparta_assert(0,
"Unknown ApplySuccessCondition value: " << (
int)required);
333 sparta::ConfigParser::YAML::EventHandler handler(
"<command line>",
341 handler.allowMissingNodes(
false);
342 std::stringstream input(value_);
343 YP::Parser parser(input);
344 while(parser.HandleNextDocument(*((YP::EventHandler*)&handler))) {}
346 if(handler.getErrors().size() != 0){
348 "command line parameter values.\n"
349 "Attempting to intrerpret YAML value '");
350 ex << value_ <<
"' at " << p->
getLocation() <<
"\n";
351 for(
const std::string& es : handler.getErrors()){
362 << found <<
" nodes matching parameter pattern \""
363 << loc_pattern_ <<
"\". " << filtered_results.size() <<
" matched filter "
364 << filter <<
"too. But none of them were actually parameters";
371 ptree.
set(loc_pattern_, value_, required,
"command line");
383 const std::string loc_pattern_;
388 const std::string value_;
401 return default_success_cond_;
408 const std::string& value,
410 loc_pattern_(loc_pattern), value_(value), default_success_cond_(required)
414 std::stringstream ss;
415 ss <<
"Parameter \"" << loc_pattern_ <<
"\" <- arch value: \"" << value_ <<
"\"";
416 switch(default_success_cond_){
420 ss <<
" [optional parameter]";
425 ss <<
" Unknown ApplySuccessCondition value: " << (int)default_success_cond_;
434 bool verbose=
false)
const override
439 std::vector<sparta::TreeNode*> results;
440 uint32_t found = root->
findChildren(loc_pattern_, results);
443 std::vector<sparta::TreeNode*> filtered_results;
444 std::copy_if(results.begin(), results.end(),
445 std::back_inserter(filtered_results), [&](
TreeNode* n)->bool{return filter.test(n);});
447 if(0 == filtered_results.size()){
453 switch(effective_asc){
456 << loc_pattern_ <<
"\" and filter " << filter <<
" for which to set parameter value \""
462 sparta_assert(0,
"ParameterDefaultApplicator cannot have success policy of "
463 "ASC_DEFER. required=" << (
int)required <<
" default="
464 << (
int)default_success_cond_ <<
". This is likely a bug in "
465 "sparta::app unless other code is creating ParameterApplicators");
467 sparta_assert(0,
"Unknown ApplySuccessCondition value: " << (
int)required);
482 sparta::ConfigParser::YAML::EventHandler handler(
"<command line>",
489 handler.allowMissingNodes(
false);
490 handler.writeToDefault(
true);
491 std::stringstream input(value_);
492 YP::Parser parser(input);
493 while(parser.HandleNextDocument(*((YP::EventHandler*)&handler))) {}
495 if(handler.getErrors().size() != 0){
497 "command line parameter values.\n"
498 "Attempting to intrerpret YAML value '");
499 ex << value_ <<
"' at " << p->
getLocation() <<
"\n";
500 for(
const std::string& es : handler.getErrors()){
511 << found <<
" nodes matching parameter pattern \""
512 << loc_pattern_ <<
"\" and filter " << filter <<
" but none of them were actually parameters";
519 ptree.
set(loc_pattern_, value_, required,
"command line");
532 std::string loc_pattern_;
538 std::string filename_;
543 const std::vector<std::string> include_paths_;
553 const std::string& filename,
554 const std::vector<std::string>& include_paths,
555 bool verbose=
false) :
556 loc_pattern_(loc_pattern), filename_(filename),
557 include_paths_(include_paths), verbose_(verbose)
561 std::stringstream ss;
562 ss <<
"Node \"" << loc_pattern_ <<
"\" <- file: \"" << filename_ <<
"\"";
569 bool verbose=
false)
const override
572 "NodeConfigFileApplicator cannot have success policy of "
573 "ASC_DEFER. This is likely a bug in sparta::app unless other code "
574 "is creating ParameterApplicators");
577 std::vector<sparta::TreeNode*> results;
581 std::vector<sparta::TreeNode*> filtered_results;
582 std::copy_if(results.begin(), results.end(),
583 std::back_inserter(filtered_results), [&](
TreeNode* n)->bool{return filter.test(n);});
587 << loc_pattern_ <<
" and filter " << filter <<
" for which to apply configuration file \""
588 << filename_ <<
"\"";
590 sparta::ConfigParser::YAML param_file(filename_, include_paths_);
599 param_file.setParameterApplyFilter(std::bind(&ApplyFilter::test, &filter, std::placeholders::_1));
601 param_file.consumeParameters(node, verbose);
608 sparta::ConfigParser::YAML param_file(filename_, include_paths_);
609 param_file.allowMissingNodes(
true);
610 param_file.consumeParameters(&dummy, verbose);
612 ptree.
create(loc_pattern_,
false)->
appendTree(param_file.getParameterTree().getRoot());
625 const std::string loc_pattern_;
631 const std::string filename_;
636 const std::vector<std::string> include_paths_;
641 const std::string& filename,
642 const std::vector<std::string> & include_paths) :
643 loc_pattern_(loc_pattern), filename_(filename),
644 include_paths_(include_paths)
648 std::stringstream ss;
649 ss <<
"ArchCfg Node \"" << loc_pattern_ <<
"\" <- file: \"" << filename_ <<
"\"";
656 bool verbose=
false)
const override
662 throw SpartaException(
"Cannot \"apply\" ArchNodeConfigFileApplicator - it can only be "
663 "applied to a virtual parameter tree (applyUnbound). It is a bug "
664 "in SPARTA that this function is being called.");
670 sparta::ConfigParser::YAML param_file(filename_, include_paths_);
671 param_file.allowMissingNodes(
true);
672 param_file.consumeParameters(&dummy, verbose);
674 ptree.
create(loc_pattern_,
false)->
appendTree(param_file.getParameterTree().getRoot());
679typedef std::vector<std::pair<std::string, std::string>> StringPairVec;
680typedef std::vector<std::unique_ptr<ConfigApplicator>> ConfigVec;
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Basic Node framework in sparta device tree composite pattern.
Non-templated base class for generic parameter access and iteration.
virtual bool isVector() const =0
Determines whether this Parameter is a vector or a scalar Parameter.
void setValueFromString(const std::string &str, bool poke=false)
Attempts to assign a value to this non-vector Parameter from a string.
virtual void overrideDefaultFromString(const std::string &val)=0
Sets the default value of this non-vector parameter for architecture baseline configuration purposes.
void appendTree(const Node *ot)
Appends a tree as a child of this node.
Virtual Parameter Tree. This represents a tree of parameters read from some source but does not neces...
Node * create(const std::string &path, bool required=false)
Add a node to the tree, with proper priority.
bool set(const std::string &path, const std::string &value, bool required, const std::string &origin="")
Add a parameter to the tree, replacing any existing parameter.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
std::string getLocation() const override final
bool isDescendantOf(const TreeNode *ancestor, int32_t max_depth=-1) const
Determines if this node is a descendant of the specified ancestor node within some number of generati...
uint32_t findChildren(const std::string &pattern, std::vector< TreeNode * > &results, std::vector< std::vector< std::string > > &replacements)
Finds all children starting at this node with a given pattern relative to this node by matching names...
Applies an architectural configuration (parameter defaults) to a virtual parameter tree....
std::string stringize() const override
Render this parameter action as a string.
void tryApply(sparta::TreeNode *root, ApplySuccessCondition asc, ApplyFilter filter=ApplyFilter(), bool verbose=false) const override
Apply the parameter contained in this object to the tree starting at root.
void applyUnbound(sparta::ParameterTree &ptree, bool verbose=false) const override
Apply the parameter contained in this object to the unbound (virtual) parameter tree ptree.
Base class for applying parameters or configuration files to the simulator device tree....
void apply(sparta::TreeNode *root, ApplyFilter filter=ApplyFilter(), bool verbose=false)
Apply the parameter contained in this object to the tree starting at root.
virtual ~ConfigApplicator()
Virtual destructor.
virtual void applyUnbound(sparta::ParameterTree &ptree, bool verbose=false) const =0
Apply the parameter contained in this object to the unbound (virtual) parameter tree ptree.
ApplySuccessCondition
Type for dictating how tryApply should behave.
@ ASC_DEFER
Defer to another layer in the parameter-application process to make the decision (i....
@ ASC_MUST_ASSIGN
Must assign the parameter to success.
@ ASC_IGNORE
Ignore failures to assign the parameter.
virtual std::string stringize() const =0
Render this parameter action as a string.
virtual void tryApply(sparta::TreeNode *root, ApplySuccessCondition required, ApplyFilter filter=ApplyFilter(), bool verbose=false) const =0
Apply the parameter contained in this object to the tree starting at root.
LocationFilter
Type for dictating how to filter the parameter application. (i.e. only apply to certain parts of the ...
@ AT_OR_BELOW_NODE
Apply configuration to any nodes that are or are below a given filter node. Typically,...
@ MAX_AF
Apply configuration only to a given filter node if applicable. (unimplemented)
@ ALL
Apply configuration to any nodes that match pattern and type (typical)
Applies a configuration file to a node pattern.
void tryApply(sparta::TreeNode *root, ApplySuccessCondition asc, ApplyFilter filter=ApplyFilter(), bool verbose=false) const override
Apply the parameter contained in this object to the tree starting at root.
std::string stringize() const override
Render this parameter action as a string.
void applyUnbound(sparta::ParameterTree &ptree, bool verbose=false) const override
Apply the parameter contained in this object to the unbound (virtual) parameter tree ptree.
Applies a value to a parameter node pattern.
void applyUnbound(sparta::ParameterTree &ptree, bool verbose=false) const override
Apply the parameter contained in this object to the unbound (virtual) parameter tree ptree.
void tryApply(sparta::TreeNode *root, ApplySuccessCondition required, ApplyFilter filter, bool verbose) const override
Apply the parameter contained in this object to the tree starting at root.
std::string stringize() const override
Render this parameter action as a string.
Applies a new default value to a parameter node pattern.
void tryApply(sparta::TreeNode *root, ApplySuccessCondition required, ApplyFilter filter=ApplyFilter(), bool verbose=false) const override
Apply the parameter contained in this object to the tree starting at root.
std::string stringize() const override
Render this parameter action as a string.
void applyUnbound(sparta::ParameterTree &ptree, bool verbose=false) const override
Apply the parameter contained in this object to the unbound (virtual) parameter tree ptree.
Macros for handling exponential backoff.
Represents a filter for applying parameters based on tree location.
friend std::ostream & operator<<(std::ostream &o, const ConfigApplicator::ApplyFilter *filter)
Prinit a configuration-application filter.
ApplyFilter()
Construct a null-filter (filters nothing)
LocationFilter getLocationFilter() const
Return the location filter policy.
const TreeNode * getLocationFilterNode() const
RReturn the location filter node (may be none)
ApplyFilter(LocationFilter locfilter, const TreeNode *filternode)
Construct a location-filter.
bool test(const TreeNode *n) const
Test a node against this filter.
friend std::ostream & operator<<(std::ostream &o, const ConfigApplicator::ApplyFilter &filter)
Prinit a configuration-application filter.