CASM  1.1.0
A Clusters Approach to Statistical Mechanics
update.cc
Go to the documentation of this file.
1 #include "casm/app/update.hh"
2 
5 #include "casm/casm_io/Log.hh"
7 #include "casm/clex/PrimClex.hh"
11 
12 // need to add specializations here
14 
15 namespace CASM {
16 namespace Completer {
18 
21 
22  fs::path _default = "ALL";
23  add_configlist_suboption(_default);
24 
27 
28  bool required = false;
29  add_settings_suboption(required);
30  add_input_suboption(required);
31 
32  return;
33 }
34 
35 } // namespace Completer
36 
37 // -- class UpdateCommandImplBase --------------------------------------------
38 
41  public:
43 
45 
46  virtual int help() const;
47 
48  virtual int desc() const;
49 
50  virtual int run() const;
51 
52  protected:
54 };
55 
57  : m_cmd(cmd) {}
58 
60  log() << std::endl << m_cmd.opt().desc() << std::endl;
62  return 0;
63 }
64 
66  help();
67 
68  log() << "Update calculation data for configurations specified by "
69  "--selection. \n\n"
70 
71  "Calculated structures are mapped to the closest matching "
72  "configuration \n"
73  "consistent with the primitive crystal structure. \n\n"
74 
75  "For complete update options description for a particular config "
76  "type, use\n"
77  "--desc along with '--type <typename>'.\n\n";
78 
79  return 0;
80 }
81 
83  err_log() << "ERROR: No --type\n";
85  return ERR_INVALID_ARG;
86 }
87 
88 // -- template<typename DataObject> class UpdateCommandImpl -----------------
89 
97 template <typename DataObject>
99  public:
101 
102  int help() const override;
103 
104  int desc() const override;
105 
106  int run() const override;
107 };
108 
109 template <typename DataObject>
111  log()
112  << std::endl
113  << m_cmd.opt().desc() << std::endl
114  << "For complete options description for a particular config type, use:\n"
115  << " casm " << UpdateCommand::name << " --desc --type "
116  << traits<DataObject>::short_name << "\n\n";
117  return 0;
118 }
119 
120 template <typename DataObject>
122  log() << DB::Update<DataObject>::desc << std::endl;
123  return 0;
124 }
125 
126 template <typename DataObject>
128  return DB::Update<DataObject>::run(m_cmd.primclex(), m_cmd.input(),
129  m_cmd.opt());
130 }
131 
132 // -- class UpdateCommand ----------------------------------------------------
133 
134 const std::string UpdateCommand::name = "update";
135 
138  : APICommand<Completer::UpdateOption>(_args, _opt) {}
139 
141 
142 int UpdateCommand::vm_count_check() const { return 0; }
143 
144 int UpdateCommand::help() const { return impl().help(); }
145 
146 int UpdateCommand::desc() const { return impl().desc(); }
147 
148 int UpdateCommand::run() const { return impl().run(); }
149 
151  if (!m_impl) {
152  if (vm().count("type")) {
153  if (!opt().configtype_opts().count(opt().configtype())) {
154  std::stringstream msg;
155  msg << "--type " << opt().configtype() << " is not allowed for 'casm "
156  << name << "'.";
157  print_names(err_log());
158  throw CASM::runtime_error(msg.str(), ERR_INVALID_ARG);
159  }
160 
162  opt().configtype(), DB::ConstructImpl<UpdateCommand>(m_impl, *this));
163  } else {
164  m_impl = notstd::make_unique<UpdateCommandImplBase>(*this);
165  }
166  }
167  return *m_impl;
168 }
169 
170 void UpdateCommand::print_names(std::ostream &sout) const {
171  sout << "The allowed types are:\n";
172 
173  for (const auto &configtype : opt().configtype_opts()) {
174  sout << " " << configtype << std::endl;
175  }
176 }
177 
179  if (count("settings")) {
180  return jsonParser{opt().settings_path()};
181  } else if (count("input")) {
182  return jsonParser::parse(opt().input_str());
183  }
184  // use defaults
185  return jsonParser();
186 }
187 
188 } // namespace CASM
#define ERR_INVALID_ARG
Definition: errors.hh:7
const OptionType & opt() const
Definition: APICommand.hh:61
const po::variables_map & vm() const
Definition: APICommand.hh:59
void add_input_suboption(bool required=true)
Definition: Handlers.cc:610
const po::options_description & desc()
Get the program options, filled with the initialized values.
Definition: Handlers.cc:321
void add_settings_suboption(bool required=true)
Definition: Handlers.cc:588
void add_help_suboption()
Add a plain –help and –desc suboptions.
Definition: Handlers.cc:561
void add_configlist_suboption(const fs::path &_default="MASTER")
Add –config suboption (defaults to MASTER)
Definition: Handlers.cc:431
void add_configtype_suboption(std::string _default, std::set< std::string > _configtype_opts)
Add –type suboption (default, set of short_name of allowed ConfigTypes)
Definition: Handlers.cc:511
std::string configtype() const
Definition: Handlers.cc:530
const fs::path settings_path() const
Returns the path corresponding to add_settings_suboption.
Definition: Handlers.cc:364
void initialize() override
Fill in the options descriptions accordingly.
Definition: update.cc:19
int help() const override
Definition: update.cc:144
int vm_count_check() const override
Definition: update.cc:142
static const std::string name
Definition: update.hh:41
int desc() const override
Definition: update.cc:146
void print_names(std::ostream &sout) const
Definition: update.cc:170
UpdateCommandImplBase & impl() const
Definition: update.cc:150
jsonParser input() const
Definition: update.cc:178
int run() const override
Definition: update.cc:148
std::unique_ptr< UpdateCommandImplBase > m_impl
Definition: update.hh:64
UpdateCommand(const CommandArgs &_args, Completer::UpdateOption &_opt)
Definition: update.cc:136
Defaults used if DataObject type doesn't matter or not given.
Definition: update.cc:40
virtual int desc() const
Definition: update.cc:65
const UpdateCommand & m_cmd
Definition: update.cc:53
virtual int run() const
Definition: update.cc:82
UpdateCommandImplBase(const UpdateCommand &cmd)
Definition: update.cc:56
virtual int help() const
Definition: update.cc:59
virtual ~UpdateCommandImplBase()
Definition: update.cc:44
int help() const override
Definition: update.cc:110
int desc() const override
Definition: update.cc:121
int run() const override
Definition: update.cc:127
UpdateCommandImpl(const UpdateCommand &cmd)
Definition: update.cc:100
static jsonParser parse(const std::string &str)
Construct a jsonParser from a string containing JSON data.
Definition: jsonParser.hh:382
void for_config_type_short(std::string short_name, F f)
const std::set< std::string > & config_types_short()
std::set of all QueryTraits<ConfigType>::short_name
Main CASM namespace.
Definition: APICommand.hh:8
Log & log()
Definition: Log.hh:424
Log & err_log()
Definition: Log.hh:426
Data structure holding basic CASM command info.