34 #include <boost/make_shared.hpp>
35 #include <boost/scoped_ptr.hpp>
36 #include <boost/filesystem/path.hpp>
45 namespace fs = boost::filesystem;
46 namespace pexExcept = lsst::pex::exceptions;
47 namespace dafBase = lsst::daf::base;
58 const char *
const Policy::typeName[] = {
72 : Citizen(typeid(this)), Persistable(), _data(new
PropertySet())
79 : Citizen(typeid(this)), Persistable(), _data(new
PropertySet())
88 : Citizen(typeid(this)), Persistable(), _data(new
PropertySet())
90 createPolicyFile(pathOrUrn,
true)->load(*
this);
97 : Citizen(typeid(this)), Persistable(), _data(new
PropertySet())
99 createPolicyFile(pathOrUrn,
true)->load(*
this);
119 void extractDefaults(Policy& target,
const Dictionary& dict, ValidationError& ve) {
121 dict.definedNames(names);
123 for(list<string>::iterator it = names.begin(); it != names.end(); ++it) {
124 const string&
name = *it;
125 std::auto_ptr<Definition> def(dict.makeDef(name));
126 def->setDefaultIn(target, &ve);
128 if (def->getType() ==
Policy::POLICY && dict.hasSubDictionary(name)) {
130 extractDefaults(*subp, *dict.getSubDictionary(name), ve);
131 if (subp->nameCount() > 0)
132 target.add(name, subp);
151 const fs::path& repository)
152 : Citizen(typeid(this)), Persistable(), _data(new
PropertySet())
157 loadedDict = _dictionary;
160 loadedDict.reset(
new Dictionary(dict));
162 loadedDict->loadPolicyFiles(repository,
true);
165 extractDefaults(*
this, *loadedDict, ve);
166 if (ve.getParamCount() > 0)
throw ve;
173 : Citizen(typeid(this)), Persistable(), _data()
175 _data = pol._data->deepCopy();
182 : Citizen(typeid(this)), Persistable(), _data()
185 _data = pol._data->deepCopy();
191 const fs::path& repository,
bool validate)
193 auto_ptr<Policy> pol(
new Policy());
196 if (pol->isDictionary()) {
198 pol.reset(
new Policy(validate, d, repository));
201 if (doIncludes) pol->loadPolicyFiles(repository,
true);
203 return pol.release();
207 const fs::path& repository,
bool validate)
209 fs::path repos = repository;
211 fs::path filepath(input);
212 if (filepath.has_parent_path()) repos = filepath.parent_path();
214 PolicyFile
file(input);
222 UrnPolicyFile upf(urn,
true,
true);
254 _dictionary = boost::make_shared<Dictionary>(dict);
278 static map<string, Policy::ValueType> nameTypeMap;
280 if (nameTypeMap.size() == 0) {
281 map<string, Policy::ValueType> tmp;
283 for (
int i = 0; i < n; ++i) {
286 string lowered(Policy::typeName[i]);
287 transform(lowered.begin(), lowered.end(), lowered.begin(), ::tolower);
298 if (tmp.count(name) == 1)
return tmp[name];
301 if (nameTypeMap.count(name) == 1)
return nameTypeMap[name];
321 bool topLevelOnly,
bool append,
int want)
const
323 bool shouldCheck =
true;
325 int have = 0, count = 0;
327 src =
_data->propertySetNames(topLevelOnly);
332 src =
_data->names(topLevelOnly);
334 src =
_data->paramNames(topLevelOnly);
336 if (!append) names.erase(names.begin(), names.end());
338 StringArray::iterator i;
339 for(i = src.begin(); i != src.end(); ++i) {
348 if ((have&want) > 0) {
372 bool topLevelOnly,
bool append,
int want)
const
374 bool shouldCheck =
true;
376 int have = 0, count = 0;
378 src =
_data->propertySetNames(topLevelOnly);
383 src =
_data->names(topLevelOnly);
385 src =
_data->paramNames(topLevelOnly);
387 if (!append) names.erase(names.begin(), names.end());
389 StringArray::iterator i;
390 for(i = src.begin(); i != src.end(); ++i) {
399 if ((have&want) > 0) {
408 template <
class T>
void Policy::_validate(
const std::string& name,
const T& value,
int curCount) {
411 boost::scoped_ptr<Definition> def(
_dictionary->makeDef(name));
412 def->validateBasic(name, value, curCount);
413 }
catch(NameNotFound& e) {
415 ve.addError(name, ValidationError::UNKNOWN_NAME);
421 template void Policy::_validate<Policy::Ptr>(std::string
const&,
Policy::Ptr const&, int);
422 template void Policy::_validate<std::string >(std::string
const&, std::string
const&, int);
423 template void Policy::_validate<bool>(std::string
const&,
bool const&, int);
424 template void Policy::_validate<double>(std::string
const&,
double const&, int);
425 template void Policy::_validate<int>(std::string
const&,
int const&, int);
433 const std::type_info& tp =
_data->typeOf(name);
436 if (tp ==
typeid(Persistable::Ptr)) {
443 if (tp ==
typeid(
bool)) {
446 else if(tp ==
typeid(
int)) {
449 else if (tp ==
typeid(
double)) {
452 else if (tp ==
typeid(
string)) {
460 (pexExcept::LogicError,
461 string(
"Policy: illegal type held by PropertySet: ") + tp.name());
463 }
catch (pexExcept::NotFoundError&) {
468 template <>
bool Policy::getValue <bool> (
const string&
name)
const {
469 return getBool(name);
471 template <>
int Policy::getValue <int> (
const string&
name)
const {
474 template <>
double Policy::getValue <double> (
const string&
name)
const {
475 return getDouble(name);
477 template <>
string Policy::getValue <string> (
const string&
name)
const {
478 return getString(name);
482 return getFile(name);
486 return getPolicy(name);
489 template <> vector<bool> Policy::getValueArray<bool>(
const string&
name)
const {
490 return getBoolArray(name);
492 template <> vector<int> Policy::getValueArray<int>(
const string&
name)
const {
493 return getIntArray(name);
495 template <> vector<double> Policy::getValueArray<double>(
const string&
name)
const {
496 return getDoubleArray(name);
498 template <> vector<string> Policy::getValueArray<string>(
const string&
name)
const {
499 return getStringArray(name);
502 return getFileArray(name);
505 return getPolicyArray(name);
508 return getConstPolicyArray(name);
516 template <>
Policy::ValueType Policy::getValueType<Policy::FilePtr>() {
return FILE; }
517 template <>
Policy::ValueType Policy::getValueType<Policy::Ptr>() {
return POLICY; }
518 template <>
Policy::ValueType Policy::getValueType<Policy::ConstPtr>() {
return POLICY; }
524 template <>
void Policy::setValue(
const string& name,
const double& value) {
526 template <>
void Policy::setValue(
const string& name,
const string& value) {
530 template <>
void Policy::setValue(
const string& name,
const FilePtr& value) {
537 template <>
void Policy::addValue(
const string& name,
const double& value) {
539 template <>
void Policy::addValue(
const string& name,
const string& value) {
543 template <>
void Policy::addValue(
const string& name,
const FilePtr& value) {
549 vector<PropertySet::Ptr>::const_iterator i;
550 for(i=psa.begin(); i != psa.end(); ++i)
558 vector<PropertySet::Ptr>::const_iterator i;
559 for(i=psa.begin(); i != psa.end(); ++i)
566 boost::dynamic_pointer_cast<PolicyFile>(
_data->getAsPersistablePtr(name));
576 vector<Persistable::Ptr>::const_iterator i;
578 for(i = pfa.begin(); i != pfa.end(); ++i) {
579 fp = boost::dynamic_pointer_cast<PolicyFile>(*i);
588 void Policy::set(
const string& name,
const FilePtr& value) {
589 _data->set(name, boost::dynamic_pointer_cast<Persistable>(value));
592 void Policy::add(
const string& name,
const FilePtr& value) {
593 _data->add(name, boost::dynamic_pointer_cast<Persistable>(value));
613 fs::path repos = repository;
615 if (repos.empty()) repos =
".";
620 for(list<string>::iterator it=names.begin(); it != names.end(); it++) {
625 pols.reserve(pfiles.size());
627 FilePtrArray::const_iterator pfi;
628 for(pfi=pfiles.begin(); pfi != pfiles.end(); pfi++) {
632 Ptr policy = boost::make_shared<Policy>();
634 fs::path path = (*pfi)->getPath();
636 if (path.is_complete()) {
637 (*pfi)->load(*policy);
640 fs::path localPath = repos / (*pfi)->getPath();
641 PolicyFile(localPath.string()).load(*policy);
644 catch (pexExcept::IoError& e) {
650 catch (ParserError& e) {
658 pols.push_back(policy);
662 for (PolicyPtrArray::iterator pi = pols.begin(); pi != pols.end(); ++pi)
668 for(list<string>::iterator it=names.begin(); it != names.end(); it++) {
672 PolicyPtrArray::iterator pi;
673 for(pi = policies.begin(); pi != policies.end(); pi++)
674 result += (*pi)->loadPolicyFiles(repos, strict);
703 ValidationError *errs)
708 auto_ptr<Policy> pol(0);
709 const Policy *def = &defaultPol;
710 if (def->isDictionary()) {
712 pol.reset(
new Policy(
false, Dictionary(*def)));
717 def->paramNames(params);
718 list<string>::iterator nm;
719 for(nm = params.begin(); nm != params.end(); ++nm) {
721 const std::type_info& tp = def->getTypeInfo(*nm);
722 if (tp ==
typeid(
bool)) {
724 BoolArray::iterator vi;
725 for(vi=a.begin(); vi != a.end(); ++vi)
728 else if (tp ==
typeid(
int)) {
730 IntArray::iterator vi;
731 for(vi=a.begin(); vi != a.end(); ++vi)
734 else if (tp ==
typeid(
double)) {
736 DoubleArray::iterator vi;
737 for(vi=a.begin(); vi != a.end(); ++vi)
740 else if (tp ==
typeid(
string)) {
742 StringArray::iterator vi;
743 for(vi=a.begin(); vi != a.end(); ++vi)
746 else if (def->isFile(*nm)) {
748 FilePtrArray::iterator vi;
749 for(vi=a.begin(); vi != a.end(); ++vi)
755 string(
"Unknown type for \"") + *nm
765 if (keepForValidation) {
766 if (defaultPol.isDictionary())
768 else if (defaultPol.canValidate())
779 else if (defaultPol.isDictionary())
780 Dictionary(defaultPol).validate(*
this, errs);
789 string Policy::str(
const string& name,
const string& indent)
const {
793 const std::type_info& tp =
_data->typeOf(name);
794 if (tp ==
typeid(
bool)) {
796 BoolArray::iterator vi;
797 for(vi=b.begin(); vi != b.end(); ++vi) {
799 if (vi+1 != b.end()) out <<
", ";
802 else if (tp ==
typeid(
int)) {
804 IntArray::iterator vi;
805 for(vi=i.begin(); vi != i.end(); ++vi) {
807 if (vi+1 != i.end()) out <<
", ";
810 else if (tp ==
typeid(
double)) {
812 DoubleArray::iterator vi;
813 for(vi=d.begin(); vi != d.end(); ++vi) {
815 if (vi+1 != d.end()) out <<
", ";
818 else if (tp ==
typeid(
string)) {
820 StringArray::iterator vi;
821 for(vi= s.begin(); vi != s.end(); ++vi) {
822 out <<
'"' << *vi <<
'"';
823 if (vi+1 != s.end()) out <<
", ";
827 vector<PropertySet::Ptr> p =
829 vector<PropertySet::Ptr>::iterator vi;
830 for(vi= p.begin(); vi != p.end(); ++vi) {
832 Policy(*vi).print(out,
"", indent+
" ");
833 out << indent <<
"}";
834 if (vi+1 != p.end()) out <<
", ";
838 else if (tp ==
typeid(Persistable::Ptr)) {
840 FilePtrArray::iterator vi;
841 for(vi= f.begin(); vi != f.end(); ++vi) {
842 out <<
"FILE:" << (*vi)->getPath();
843 if (vi+1 != f.end()) out <<
", ";
849 "Policy: unexpected type held by any");
852 catch (NameNotFound&) {
864 const string& indent)
const
868 if (label.size() > 0)
869 out << indent << label <<
":\n";
870 for(list<string>::iterator n = nms.begin(); n != nms.end(); ++n) {
871 out << indent <<
" " << *n <<
": " <<
str(*n, indent+
" ") << endl;
void addValue(const std::string &name, const T &value)
ConstPolicyPtrArray getConstPolicyArray(const std::string &name) const
definition of the PolicySource class
boost::shared_ptr< const Dictionary > ConstDictPtr
std::vector< bool > BoolArray
definition of the PolicyFile class
table::Key< std::string > name
boost::shared_ptr< PolicyFile > FilePtr
std::vector< std::string > StringArray
static FilePtr createPolicyFile(const std::string &pathOrUrn, bool strict=false)
IntArray getIntArray(const std::string &name) const
int names(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
FilePtr getFile(const std::string &name) const
bool isPolicy(const std::string &name) const
int _names(std::list< std::string > &names, bool topLevelOnly=false, bool append=false, int want=3) const
boost::shared_ptr< PropertySet > Ptr
virtual std::string str(const std::string &name, const std::string &indent="") const
PolicyPtrArray getPolicyArray(const std::string &name) const
definition of Policy parsing exceptions
void set(const std::string &name, const Ptr &value)
boost::shared_ptr< Policy > Ptr
static ValueType getTypeByName(const std::string &name)
static bool looksLikeUrn(const std::string &s, bool strict=false)
std::vector< FilePtr > FilePtrArray
static Policy * createPolicyFromUrn(const std::string &urn, bool validate=true)
DoubleArray getDoubleArray(const std::string &name) const
std::vector< Ptr > PolicyPtrArray
std::vector< int > IntArray
lsst::daf::base::PropertySet PropertySet
void add(const std::string &name, const Ptr &value)
static const char *const typeName[]
int mergeDefaults(const Policy &defaultPol, bool keepForValidation=true, ValidationError *errs=0)
std::vector< lsst::daf::base::Persistable::Ptr > _getPersistList(const std::string &name) const
static Policy * _createPolicy(PolicySource &input, bool doIncludes, const boost::filesystem::path &repos, bool validate)
std::vector< double > DoubleArray
virtual void print(std::ostream &out, const std::string &label="Policy", const std::string &indent="") const
std::vector< ConstPtr > ConstPolicyPtrArray
const ConstDictPtr getDictionary() const
#define LSST_EXCEPT(type,...)
boost::shared_ptr< const Policy > ConstPtr
lsst::daf::base::PropertySet::Ptr _data
void validate(ValidationError *errs=0) const
Class for storing generic metadata.
std::vector< lsst::daf::base::PropertySet::Ptr > _getPropSetList(const std::string &name) const
std::string toString() const
void _validate(const std::string &name, const T &value, int curCount=0)
int fileNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
definition of the Dictionary class
afw::table::Key< double > b
static ValueType getValueType()
Base class for all persistable classes.
void setDictionary(const Dictionary &dict)
BoolArray getBoolArray(const std::string &name) const
int loadPolicyFiles(bool strict=true)
void setValue(const std::string &name, const T &value)
bool isFile(const std::string &name) const
int policyNames(std::list< std::string > &names, bool topLevelOnly=false, bool append=false) const
const char * getTypeName(const std::string &name) const
bool exists(const std::string &name) const
the definition of the UrnPolicyFile class
FilePtrArray getFileArray(const std::string &name) const