Page MenuHomec4science

lm_parsable.cc
No OneTemporary

File Metadata

Created
Sun, Sep 29, 05:52

lm_parsable.cc

/**
* @file lm_parsable.cc
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Thu Jul 24 14:21:58 2014
*
* @brief Common mother for parsable objects
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* LibMultiScale is free software: you can redistribute it and/or modify it
* under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* LibMultiScale is distributed in the hope that it will be useful, but
* WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibMultiScale. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "lm_parsable.hh"
#include "lm_common.hh"
#include "lm_parameter.hh"
#include "lm_parsable_inline_impl.hh"
#include "stimulation_impulse.hh"
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
/* -------------------------------------------------------------------------- */
Parsable::Parsable() : are_parameters_declared(false) {}
/* -------------------------------------------------------------------------- */
Parsable::~Parsable() {
std::map<std::string, Parameter *>::iterator it = params.begin();
std::map<std::string, Parameter *>::iterator end = params.end();
while (it != end) {
if ((*it).second) {
DUMP("freeing parameterTyped " << it->first, DBG_DETAIL);
delete (it->second);
++it;
}
}
}
/* -------------------------------------------------------------------------- */
Parsable::Parsable(const Parsable &p) {
this->are_parameters_declared = p.are_parameters_declared;
const std::map<std::string, Parameter *> &_params = p.params;
std::map<std::string, Parameter *>::const_iterator it = _params.begin();
std::map<std::string, Parameter *>::const_iterator end = _params.end();
while (it != end) {
std::string _id = it->first;
Parameter *_par = it->second->createCopy();
this->params[_id] = _par;
++it;
}
};
/* -------------------------------------------------------------------------- */
void Parsable::parseTag(const std::string &keyword, bool &var,
const default_val<bool> def) {
this->parseKeyword(keyword, var, def);
}
/* -------------------------------------------------------------------------- */
template <>
void Parsable::changeDefault<IntegrationSchemeStage>(
const std::string &keyword, const IntegrationSchemeStage &val) {
this->changeDefault(keyword, IntegrationSchemeMask(val));
}
/* -------------------------------------------------------------------------- */
ParseResult Parsable::parseLine(const std::string &buffer) {
if (!are_parameters_declared) {
declareParams();
generateListKeywords();
are_parameters_declared = true;
}
std::stringstream line(buffer);
std::string mot;
ParseResult count(true, 0);
DUMPFILE(Parser::fout, "Parsing line " << buffer);
while (line.good()) {
try {
Parser::parse(mot, line);
} catch (LibMultiScaleException &e) {
break;
}
if (mot == "BLOCK_PARAMS") {
ParseResult parsed(true, 0);
std::string section_name;
Parser::parse(section_name, line);
++parsed;
std::string filename;
try {
Parser::parse(filename, line);
Parser::parseConfigFile(filename, section_name, "", (*this));
++parsed;
} catch (LibMultiScaleException &e) {
Parser::parseConfigFile(Parser::getCurrentConfigFile(), section_name,
"", (*this));
}
count += parsed;
++count;
return count;
} else {
ParseResult parsed;
try {
// const std::string & ls = line.str(); // just for debugging
parsed = setParam(mot, line);
} catch (LibMultiScaleException &e) {
LM_FATAL(
e.what() << std::endl
<< "parameter " << mot << " was not parsed !! "
<< std::endl
<< "check config file for a syntax problem for line\n\t"
<< Parser::getParserState() << std::endl
<< "\t\t\"" << buffer << "\"" << std::endl);
}
if (!parsed)
LM_FATAL("parameter "
<< mot << " was not parsed !! " << std::endl
<< "check config file for a syntax problem for line\n\t"
<< Parser::getParserState() << std::endl
<< "\t\t\"" << buffer << "\"" << std::endl);
// count the pop parameters
count += parsed;
// count the keyword which have been removed
++count;
}
}
return count;
}
/* -------------------------------------------------------------------------- */
void Parsable::checkAllKeywordsAreParsed() {
for (UInt i = 0; i < forward_parsable.size(); ++i)
forward_parsable[i]->checkAllKeywordsAreParsed();
std::map<std::string, Parameter *>::iterator it = params.begin();
std::map<std::string, Parameter *>::iterator end = params.end();
std::vector<std::string> failed_keywords;
while (it != end) {
if (it->second->isSet() == false) {
failed_keywords.push_back(it->first);
}
++it;
}
std::string mystr;
for (UInt i = 0; i < failed_keywords.size(); ++i) {
mystr += "\t >> \t \"" + failed_keywords[i] + "\"\n";
}
std::string verb1 = "were";
std::string verb2 = "have";
if (failed_keywords.size() == 1) {
verb1 = "was";
verb2 = "has";
}
if (failed_keywords.size() > 0)
LM_THROW("parameters " << std::endl
<< std::endl
<< mystr << std::endl
<< " " << verb1 << " not parsed and"
<< " " << verb2 << " no default value" << std::endl);
}
/* -------------------------------------------------------------------------- */
void Parsable::addSubParsableObject(Parsable &obj) {
addSubParsableObject(&obj);
}
/* -------------------------------------------------------------------------- */
void Parsable::addSubParsableObject(Parsable *ptr) {
forward_parsable.push_back(ptr);
if (!ptr->are_parameters_declared) {
ptr->declareParams();
ptr->generateListKeywords();
ptr->are_parameters_declared = true;
}
}
/* -------------------------------------------------------------------------- */
ParseResult Parsable::setParam(const std::string &keyword,
std::stringstream &line) {
checkKeyword(keyword);
ParseResult parsed;
std::string current_buffer;
UInt pos = line.tellg();
if (pos < line.str().length())
current_buffer = line.str().substr(pos);
for (UInt i = 0; i < forward_parsable.size(); ++i) {
if (!forward_parsable[i]->doAcceptKeyword(keyword))
continue;
std::stringstream sub_line(current_buffer);
ParseResult parsed_forward =
forward_parsable[i]->setParam(keyword, sub_line);
parsed.max(parsed_forward);
}
if (params.count(keyword)) {
std::stringstream sub_line(current_buffer);
Parameter *p = params[keyword];
ParseResult parsed_main = p->setParam(keyword, sub_line);
parsed.max(parsed_main);
}
Parser::shiftLine(parsed, line);
return parsed;
}
/* -------------------------------------------------------------------------- */
void Parsable::checkKeyword(const std::string &keyword) {
if (!doAcceptKeyword(keyword)) {
std::stringstream message;
message << "keyword " << keyword << " is not registered" << std::endl;
message << " the registered keywords are:" << std::endl;
message << listKeywordsToStr();
message << std::endl;
LM_THROW(message.str());
}
}
/* -------------------------------------------------------------------------- */
bool Parsable::doAcceptKeyword(const std::string &keyword) {
return (list_keywords.count(keyword));
}
/* -------------------------------------------------------------------------- */
std::string Parsable::listKeywordsToStr() {
std::stringstream list_keys;
std::set<std::string>::iterator it = list_keywords.begin();
std::set<std::string>::iterator end = list_keywords.end();
while (it != end) {
list_keys << " " << (*it);
++it;
}
return list_keys.str();
}
/* -------------------------------------------------------------------------- */
void Parsable::generateListKeywords() {
std::map<std::string, Parameter *>::iterator it = params.begin();
std::map<std::string, Parameter *>::iterator end = params.end();
while (it != end) {
list_keywords.insert((*it).first);
++it;
}
for (UInt i = 0; i < forward_parsable.size(); ++i) {
std::map<std::string, Parameter *>::iterator it =
forward_parsable[i]->params.begin();
std::map<std::string, Parameter *>::iterator end =
forward_parsable[i]->params.end();
while (it != end) {
list_keywords.insert((*it).first);
++it;
}
forward_parsable[i]->generateListKeywords();
}
}
/* -------------------------------------------------------------------------- */
/* LMDESC PARSABLE
TODO
*/
/* -------------------------------------------------------------------------- */
// Trick to instantiate the cases of the parser without explicitely calling them
/* -------------------------------------------------------------------------- */
#define _parsekeywordstring(nb) \
template void Parsable::parseKeyword<std::string, char[nb]>( \
std::string const &, std::string &, char const(&)[nb]);
#define _parsekeyword(T, V) \
template void Parsable::parseKeyword<T, V>(const std::string &, T &, \
const V &); \
template void Parsable::parseKeyword<T>(const std::string &, T &, \
default_val<T>);
#define _parsevectorkeyword(T, nb) \
template void Parsable::parseVectorKeyword<T, nb>(std::string const &, UInt, \
T(&)[nb], std::vector<T>); \
template void Parsable::parseVectorKeyword<T, nb>( \
std::string const &, UInt, T(&)[nb], default_val<T[nb]>);
#define _parsevectorkeywordquantity(q, nb) \
template void Parsable::parseVectorKeyword<q, nb, Real>( \
const std::string &, UInt, Quantity<q, nb, Real> &, \
const std::vector<Real>); \
template void Parsable::parseVectorKeyword<q, nb, Real>( \
const std::string &, UInt, Quantity<q, nb, Real> &, \
default_val<Quantity<q, nb, Real>>);
#define _parsekeyword_quantity(q) \
_parsekeyword(Quantity<q>, Real) _parsevectorkeywordquantity(q, 1) \
_parsevectorkeywordquantity(q, 2) _parsevectorkeywordquantity(q, 3) \
_parsevectorkeywordquantity(q, 4) _parsevectorkeywordquantity(q, 5) \
_parsevectorkeywordquantity(q, 6) \
_parsevectorkeywordquantity(q, 7) \
_parsevectorkeywordquantity(q, 8) \
_parsevectorkeywordquantity(q, 9) \
_parsevectorkeywordquantity(q, 13)
/* -------------------------------------------------------------------------- */
_parsekeyword_quantity(Length) _parsekeyword_quantity(Mass)
_parsekeyword_quantity(Energy) _parsekeyword_quantity(Time)
_parsekeyword_quantity(MassDensity) _parsekeyword_quantity(Force)
_parsekeyword_quantity(Pressure) _parsekeyword_quantity(Temperature)
/* --------------------------------------------------------------------------
*/
_parsekeyword(UInt, UInt) _parsekeyword(char, char)
_parsekeyword(Real, Real) _parsekeyword(std::string, std::string)
_parsekeyword(DOFType, DOFType)
_parsekeywordstring(1) _parsekeywordstring(2)
_parsekeywordstring(3) _parsekeywordstring(4)
_parsekeywordstring(5) _parsekeywordstring(6)
_parsekeywordstring(7) _parsekeywordstring(8)
_parsekeywordstring(9) _parsekeywordstring(13)
/* --------------------------------------------------------------------------
*/
_parsevectorkeyword(Real, 2u) _parsevectorkeyword(
Real, 3u) _parsevectorkeyword(Real, 4u) _parsevectorkeyword(Real, 5u)
_parsevectorkeyword(Real, 6u) _parsevectorkeyword(Real, 7u)
_parsevectorkeyword(Real, 8u) _parsevectorkeyword(
Real, 9u) _parsevectorkeyword(Real, 13u)
_parsevectorkeyword(UInt, 2u) _parsevectorkeyword(UInt, 3u)
_parsevectorkeyword(UInt, 4u) _parsevectorkeyword(UInt, 5u)
_parsevectorkeyword(UInt, 6u) _parsevectorkeyword(UInt,
7u)
_parsevectorkeyword(UInt, 8u) _parsevectorkeyword(
UInt, 9u) _parsevectorkeyword(UInt, 13u)
_parsevectorkeyword(int, 2) _parsevectorkeyword(
int, 3) _parsevectorkeyword(int, 4)
_parsevectorkeyword(int, 5)
_parsevectorkeyword(int, 6)
_parsevectorkeyword(int, 7)
_parsevectorkeyword(int, 8)
_parsevectorkeyword(int, 9)
_parsevectorkeyword(int,
13)
/* --------------------------------------------------------------------------
*/
_parsekeyword(std::vector<UInt>, std::vector<UInt>)
_parsekeyword(std::vector<int>,
std::vector<int>) _parsekeyword(std::vector<FieldType>,
std::vector<FieldType>)
_parsekeyword(int, int) _parsekeyword(long, long) _parsekeyword(
Operator, Operator) _parsekeyword(bool, bool)
_parsekeyword(LatticeType, LatticeType) _parsekeyword(
UnitSystem, UnitSystem) _parsekeyword(FieldType, FieldType)
_parsekeyword(IntegrationSchemeMask, IntegrationSchemeStage)
_parsekeyword(dislo_orientation, dislo_orientation)
_parsekeyword(Impulse::ImpulseType,
Impulse::ImpulseType)
_parsevectorkeyword(std::string, 3)
_parsevectorkeyword(std::string, 2)
_parsevectorkeyword(bool, 1)
_parsevectorkeyword(bool, 2)
_parsevectorkeyword(bool, 3)
_parsevectorkeyword(UInt, 1)
/* --------------------------------------------------------------------------
*/
template void Parsable::parseKeyword<std::map<UInt, std::string>>(
std::string const &, std::map<unsigned int, std::string> &,
default_val<std::map<UInt, std::string>>);
template void Parsable::parseKeyword<std::vector<std::string>>(
std::string const &, std::vector<std::string> &,
default_val<std::vector<std::string>>);
/* -------------------------------------------------------------------------- */
template void
Parsable::setParam<IntegrationSchemeStage>(std::string const &,
IntegrationSchemeStage const &);
template void
Parsable::setParam<IntegrationSchemeMask>(std::string const &,
IntegrationSchemeMask const &);
template void Parsable::setParam<FieldType>(std::string const &,
FieldType const &);
template void Parsable::setParam<double>(std::string const &, double const &);
template void Parsable::setParam<std::string>(std::string const &,
std::string const &);
template void Parsable::getParam<std::string>(std::string const &,
std::string &);
template void Parsable::setParam<bool>(std::string const &, bool const &);
template void Parsable::getParam<bool>(std::string const &, bool &);
template void Parsable::setParam<bool[1]>(std::string const &,
bool const (&)[1]);
template void Parsable::setParam<bool[2]>(std::string const &,
bool const (&)[2]);
template void Parsable::setParam<bool[3]>(std::string const &,
bool const (&)[3]);
template void Parsable::setParam<UInt>(std::string const &, const UInt &);
template void Parsable::setParam<DOFType>(std::string const &, const DOFType &);
/* -------------------------------------------------------------------------- */
// template void Parsable::parseVectorKeyword<bool, 3u>(std::string const&,
// UInt, bool (&) [3u], default_val<bool [3u]>);
template void Parsable::parseVectorKeyword<double>(std::string const &, UInt,
double &,
default_val<double>);
__END_LIBMULTISCALE__

Event Timeline