| LSSTApplications
    20.0.0
    LSSTDataManagementBasePackage | 
 
 
 
Go to the documentation of this file.
   35 #include <boost/filesystem/path.hpp> 
   57 const char* 
const Policy::typeName[] = {
"undefined", 
"bool",   
"int",       
"double",
 
   58                                         "string",    
"Policy", 
"PolicyFile"};
 
   63 Policy::Policy() : Persistable(), _data(new PropertySet()) {}
 
   73 Policy::Policy(
const string& pathOrUrn) : Persistable(), _data(new PropertySet()) {
 
   74     createPolicyFile(pathOrUrn, 
true)->load(*
this);
 
   80 Policy::Policy(
const char* pathOrUrn) : Persistable(), _data(new PropertySet()) {
 
   81     createPolicyFile(pathOrUrn, 
true)->load(*
this);
 
  101 void extractDefaults(Policy& 
target, 
const Dictionary& dict, ValidationError& ve) {
 
  103     dict.definedNames(names);
 
  106         const string& 
name = *it;
 
  108         def->setDefaultIn(
target, &ve);
 
  112             extractDefaults(*subp, *dict.getSubDictionary(
name), ve);
 
  113             if (subp->nameCount() > 0) 
target.add(
name, subp);
 
  132         : Persistable(), _data(new PropertySet()) {
 
  136         loadedDict = _dictionary;
 
  138         loadedDict.reset(
new Dictionary(dict));
 
  140     loadedDict->loadPolicyFiles(repository, 
true);
 
  143     extractDefaults(*
this, *loadedDict, ve);
 
  144     if (ve.getParamCount() > 0) 
throw ve;
 
  150 Policy::Policy(
const Policy& pol) : Persistable(), _data() { _data = pol._data->deepCopy(); }
 
  157         _data = pol._data->deepCopy();
 
  162 Policy* Policy::_createPolicy(PolicySource& 
source, 
bool doIncludes, 
const fs::path& repository,
 
  167     if (pol->isDictionary()) {
 
  172     if (doIncludes) pol->loadPolicyFiles(repository, 
true);
 
  174     return pol.release();
 
  177 Policy* Policy::_createPolicy(
const string& input, 
bool doIncludes, 
const fs::path& repository,
 
  179     fs::path repos = repository;
 
  181         fs::path filepath(input);
 
  182         if (filepath.has_parent_path()) repos = filepath.parent_path();
 
  184     PolicyFile 
file(input);
 
  185     return _createPolicy(
file, doIncludes, repos, 
validate);
 
  191     UrnPolicyFile upf(urn, 
true, 
true);
 
  192     return _createPolicy(upf, 
true, fs::path(), 
false);
 
  218 void Policy::setDictionary(
const Dictionary& dict) { _dictionary = std::make_shared<Dictionary>(dict); }
 
  232         throw LSST_EXCEPT(DictionaryError, 
"No dictionary set.");
 
  234         _dictionary->validate(*
this, errs);
 
  245     if (nameTypeMap.
size() == 0) {
 
  248         for (
int i = 0; i < n; ++i) {
 
  252             transform(lowered.begin(), lowered.end(), lowered.begin(), ::tolower);
 
  265         return nameTypeMap[
name];
 
  285     bool shouldCheck = 
true;
 
  287     int have = 0, 
count = 0;
 
  289         src = _data->propertySetNames(topLevelOnly);
 
  292     } 
else if (want == 7)
 
  293         src = _data->names(topLevelOnly);
 
  295         src = _data->paramNames(topLevelOnly);
 
  299     StringArray::iterator i;
 
  300     for (i = 
src.begin(); i != 
src.end(); ++i) {
 
  309         if ((have & want) > 0) {
 
  332 int Policy::_names(
list<string>& names, 
bool topLevelOnly, 
bool append, 
int want)
 const {
 
  333     bool shouldCheck = 
true;
 
  335     int have = 0, 
count = 0;
 
  337         src = _data->propertySetNames(topLevelOnly);
 
  340     } 
else if (want == 7)
 
  341         src = _data->names(topLevelOnly);
 
  343         src = _data->paramNames(topLevelOnly);
 
  347     StringArray::iterator i;
 
  348     for (i = 
src.begin(); i != 
src.end(); ++i) {
 
  357         if ((have & want) > 0) {
 
  367 void Policy::_validate(
const std::string& 
name, 
const T& value, 
int curCount) {
 
  371             def->validateBasic(
name, value, curCount);
 
  372         } 
catch (NameNotFound& e) {
 
  374             ve.addError(
name, ValidationError::UNKNOWN_NAME);
 
  382 template void Policy::_validate<bool>(
std::string const&, 
bool const&, 
int);
 
  383 template void Policy::_validate<double>(
std::string const&, 
double const&, 
int);
 
  384 template void Policy::_validate<int>(
std::string const&, 
int const&, 
int);
 
  403         if (tp == 
typeid(
bool)) {
 
  405         } 
else if (tp == 
typeid(
int)) {
 
  407         } 
else if (tp == 
typeid(
double)) {
 
  409         } 
else if (tp == 
typeid(
string)) {
 
  415                               string(
"Policy: illegal type held by PropertySet: ") + tp.
name());
 
  423 bool Policy::getValue<bool>(
const string& 
name)
 const {
 
  427 int Policy::getValue<int>(
const string& 
name)
 const {
 
  431 double Policy::getValue<double>(
const string& 
name)
 const {
 
  435 string Policy::getValue<string>(
const string& 
name)
 const {
 
  440     return getFile(
name);
 
  444     return getPolicy(
name);
 
  465     return getFileArray(
name);
 
  469     return getPolicyArray(
name);
 
  473     return getConstPolicyArray(
name);
 
  576     FilePtr out = std::dynamic_pointer_cast<PolicyFile>(_data->getAsPersistablePtr(
name));
 
  586     for (i = pfa.
begin(); i != pfa.
end(); ++i) {
 
  587         fp = std::dynamic_pointer_cast<PolicyFile>(*i);
 
  596     _data->set(
name, std::dynamic_pointer_cast<Persistable>(value));
 
  600     _data->add(
name, std::dynamic_pointer_cast<Persistable>(value));
 
  620     fs::path repos = repository;
 
  622     if (repos.empty()) repos = 
".";
 
  631         pols.reserve(pfiles.size());
 
  633         FilePtrArray::const_iterator pfi;
 
  634         for (pfi = pfiles.begin(); pfi != pfiles.end(); pfi++) {
 
  638             Ptr policy = std::make_shared<Policy>();
 
  640                 fs::path path = (*pfi)->getPath();
 
  642                 if (path.is_complete()) {
 
  643                     (*pfi)->load(*policy);
 
  645                     fs::path localPath = repos / (*pfi)->getPath();
 
  646                     PolicyFile(localPath.string()).load(*policy);
 
  653             } 
catch (ParserError& e) {
 
  661             pols.push_back(policy);
 
  665         for (PolicyPtrArray::iterator pi = pols.begin(); pi != pols.end(); ++pi) 
add(*it, *pi);
 
  674         PolicyPtrArray::iterator pi;
 
  675         for (pi = policies.begin(); pi != policies.end(); pi++)
 
  676             result += (*pi)->loadPolicyFiles(repos, strict);
 
  708     const Policy* def = &defaultPol;
 
  709     if (def->isDictionary()) {
 
  711         pol.reset(
new Policy(
false, Dictionary(*def)));
 
  716     def->paramNames(params);
 
  718     for (nm = params.
begin(); nm != params.
end(); ++nm) {
 
  721             if (tp == 
typeid(
bool)) {
 
  723                 BoolArray::iterator vi;
 
  724                 for (vi = 
a.begin(); vi != 
a.end(); ++vi) 
add(*nm, *vi);
 
  725             } 
else if (tp == 
typeid(
int)) {
 
  727                 IntArray::iterator vi;
 
  728                 for (vi = 
a.begin(); vi != 
a.end(); ++vi) 
add(*nm, *vi);
 
  729             } 
else if (tp == 
typeid(
double)) {
 
  731                 DoubleArray::iterator vi;
 
  732                 for (vi = 
a.begin(); vi != 
a.end(); ++vi) 
add(*nm, *vi);
 
  733             } 
else if (tp == 
typeid(
string)) {
 
  735                 StringArray::iterator vi;
 
  736                 for (vi = 
a.begin(); vi != 
a.end(); ++vi) 
add(*nm, *vi);
 
  737             } 
else if (def->isFile(*nm)) {
 
  739                 FilePtrArray::iterator vi;
 
  740                 for (vi = 
a.begin(); vi != 
a.end(); ++vi) 
add(*nm, *vi);
 
  744                                   string(
"Unknown type for \"") + *nm + 
"\": \"" + 
getTypeName(*nm) + 
"\"");
 
  753     if (keepForValidation) {
 
  754         if (defaultPol.isDictionary())
 
  756         else if (defaultPol.canValidate())
 
  766     else if (defaultPol.isDictionary())
 
  767         Dictionary(defaultPol).validate(*
this, errs);
 
  781         if (tp == 
typeid(
bool)) {
 
  783             BoolArray::iterator vi;
 
  784             for (vi = 
b.begin(); vi != 
b.end(); ++vi) {
 
  786                 if (vi + 1 != 
b.end()) out << 
", ";
 
  788         } 
else if (tp == 
typeid(
int)) {
 
  790             IntArray::iterator vi;
 
  791             for (vi = i.begin(); vi != i.end(); ++vi) {
 
  793                 if (vi + 1 != i.end()) out << 
", ";
 
  795         } 
else if (tp == 
typeid(
double)) {
 
  797             DoubleArray::iterator vi;
 
  798             for (vi = d.begin(); vi != d.end(); ++vi) {
 
  800                 if (vi + 1 != d.end()) out << 
", ";
 
  802         } 
else if (tp == 
typeid(
string)) {
 
  804             StringArray::iterator vi;
 
  805             for (vi = s.begin(); vi != s.end(); ++vi) {
 
  806                 out << 
'"' << *vi << 
'"';
 
  807                 if (vi + 1 != s.end()) out << 
", ";
 
  812             for (vi = p.
begin(); vi != p.
end(); ++vi) {
 
  814                 Policy(*vi).print(out, 
"", indent + 
"  ");
 
  815                 out << indent << 
"}";
 
  816                 if (vi + 1 != p.
end()) out << 
", ";
 
  821             FilePtrArray::iterator vi;
 
  822             for (vi = f.begin(); vi != f.end(); ++vi) {
 
  823                 out << 
"FILE:" << (*vi)->getPath();
 
  824                 if (vi + 1 != f.end()) out << 
", ";
 
  830     } 
catch (NameNotFound&) {
 
  844     if (label.
size() > 0) out << indent << label << 
":\n";
 
  846         out << indent << 
"  " << *n << 
": " << 
str(*n, indent + 
"  ") << 
endl;
 
  
bool getBool(const std::string &name) const
return a boolean value associated with the given name.
std::vector< double > DoubleArray
const std::string getString(const std::string &name) const
return a string value associated with the given name .
static std::type_info const  & typeOfT()
Get type info for the specified class.
BoolArray getBoolArray(const std::string &name) const
return an array of values associated with the given name
virtual void print(std::ostream &out, const std::string &label="Policy", const std::string &indent="") const
print the contents of this policy to an output stream.
static ValueType getTypeByName(const std::string &name)
Given the human-readable name of a type ("bool", "int", "policy", etc), what is its ValueType (BOOL,...
ValueType
an enumeration for the supported policy types
std::shared_ptr< const Policy > ConstPtr
DoubleArray getDoubleArray(const std::string &name) const
static const char *const typeName[]
c-string forms for the supported value types.
int loadPolicyFiles(bool strict=true)
Recursively replace all PolicyFile values with the contents of the files they refer to.
Reports attempts to access elements using an invalid key.
std::vector< std::string > StringArray
static bool looksLikeUrn(const std::string &s, bool strict=false)
Does s look like a URN? That is, does it start with URN_PREFIX or URN_PREFIX_ABBREV?
int names(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
load the names of parameters into a given list.
const ConstDictPtr getDictionary() const
The dictionary (if any) that this policy uses to validate itself, including checking set() and add() ...
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
void setDictionary(const Dictionary &dict)
Update this policy's dictionary that it uses to validate itself.
std::shared_ptr< const Dictionary > ConstDictPtr
std::vector< Ptr > PolicyPtrArray
int fileNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
FilePtrArray getFileArray(const std::string &name) const
return an array of PolicyFile pointers associated with the given name.
void validate(ValidationError *errs=0) const
Validate this policy, using its stored dictionary.
std::vector< bool > BoolArray
std::shared_ptr< RecordT > src
bool exists(const std::string &name) const
return true if a value exists in this policy for the given name.
std::vector< FilePtr > FilePtrArray
FilePtr getFile(const std::string &name) const
return a PolicyFile (a reference to a file with "sub-Policy" data) identified by a given name.
bool isFile(const std::string &name) const
return true if the value pointed to by the given name is a PolicyFile
definition of Policy parsing exceptions
std::vector< ConstPtr > ConstPolicyPtrArray
the definition of the UrnPolicyFile class
double getDouble(const std::string &name) const
return a double value associated with the given name.
const char * source()
Source function that allows astChannel to source from a Stream.
StringArray getStringArray(const std::string &name) const
std::shared_ptr< Policy > Ptr
std::shared_ptr< PolicyFile > FilePtr
static ValueType getValueType()
A template-ized way to get the ValueType.
int mergeDefaults(const Policy &defaultPol, bool keepForValidation=true, ValidationError *errs=0)
use the values found in the given policy as default values for parameters not specified in this polic...
std::vector< int > IntArray
Reports errors in the logical structure of the program.
PolicyPtrArray getPolicyArray(const std::string &name) const
return an array of Policy pointers associated with the given name.
const char * getTypeName(const std::string &name) const
return a string name for the type associated with the parameter of a given name.
void setValue(const std::string &name, const T &value)
Set a value with the given name.
IntArray getIntArray(const std::string &name) const
definition of the PolicyFile class
A base class for image defects.
definition of the Dictionary class
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
int policyNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
Key< Flag > const  & target
void remove(const std::string &name)
Remove all values with a given name.
bool canValidate() const
Can this policy validate itself – that is, does it have a dictionary that it can use to validate itse...
virtual std::string str(const std::string &name, const std::string &indent="") const
return a string representation of the value given by a name.
Reports errors in external input/output operations.
static FilePtr createPolicyFile(const std::string &pathOrUrn, bool strict=false)
Create a PolicyFile or UrnPolicyFile from pathOrUrn.
Base class for all persistable classes.
void addValue(const std::string &name, const T &value)
Add a value to an array of values with a given name.
static Policy * createPolicyFromUrn(const std::string &urn, bool validate=true)
Create a Policy from a file specified by a URN.
#define LSST_EXCEPT_HERE
For internal use; gathers the file, line, and function for a tracepoint.
definition of the PolicySource class
Class for storing generic metadata.
ConstPolicyPtrArray getConstPolicyArray(const std::string &name) const
void add(const std::string &name, const Ptr &value)
std::string toString() const
convert the entire contents of this policy to a string.
virtual ~Policy()
destroy this policy
int getInt(const std::string &name) const
return an integer value associated with the given name.
void set(const std::string &name, const Ptr &value)
bool isPolicy(const std::string &name) const
return true if the value pointed to by the given name is a Policy
Policy()
Create an empty policy.