27 m_desc.add_options()(
"json",
28 "Write JSON output (otherwise CSV, unless output "
29 "extension is '.json' or '.JSON')")(
31 "Only write selected configurations to output. Can be used by itself or "
32 "in conjunction with other options")(
33 "xor",
"Performs logical XOR on two configuration selections")(
34 "not",
"Performs logical NOT on configuration selection")(
36 "Write configurations selected in any of the input lists. Equivalent to "
38 "Write configurations selected in all of the input lists. "
39 "Equivalent to logical AND")(
44 "Add configurations to selection if they meet specified criteria. Call "
45 "using 'casm select --set-on [\"criteria\"]'")(
50 "Remove configurations from selection if they meet specified criteria. "
51 "Call using 'casm select --set-off [\"criteria\"]'")(
53 po::value<std::vector<std::string> >(&
m_criteria_vec)->multitoken(),
54 "Create a selection of Configurations that meet specified criteria. "
55 "Call using 'casm select --set [\"criteria\"]'")(
"force,f",
56 "Overwrite output file");
72 virtual int help()
const;
74 virtual int desc()
const;
76 virtual int run()
const;
89 <<
"Use query commands to specify objects that should be selected or "
92 "By default, the input and output selection is the MASTER selection, "
94 "or more input selections may be specified via --selections (-c), and "
96 "selection may be specified via --output (-o).\n\n"
98 "For complete query options description, use '--help operators' or \n"
99 "'--help properties' along with '--type <typename>'.\n\n"
101 "The type of objects acted on is specified via --type (-t).\n\n";
123 template <
typename DataObject>
128 int help()
const override;
130 int desc()
const override;
132 int run()
const override;
170 Log &
log,
bool only_selected)
const;
188 template <
typename DataObject>
192 m_Ntot(m_cmd.
primclex().template generic_db<DataObject>().size()) {}
194 template <
typename DataObject>
196 if (!m_cmd.opt().help_opt_vec().size()) {
200 for (
const std::string &str : m_cmd.opt().help_opt_vec()) {
206 log() <<
"Available operators for use within selection criteria:"
209 }
else if (str[0] ==
'p') {
210 log() <<
"Available property tags are currently:" << std::endl;
219 template <
typename DataObject>
224 template <
typename DataObject>
226 _write_input_stats();
231 }
else if (_count(
"set-on")) {
233 }
else if (_count(
"set-off")) {
235 }
else if (_count(
"and")) {
237 }
else if (_count(
"or")) {
239 }
else if (_count(
"xor")) {
241 }
else if (_count(
"not")) {
244 err_log() <<
"ERROR: No valid command recognized." << std::endl;
249 bool force_write = _count(
"force") || (_output_path().string() ==
"MASTER");
250 if (fs::exists(_output_path()) && !force_write) {
251 log() <<
"File " << _output_path()
252 <<
" already exists. Use --force to force overwrite." << std::endl;
256 bool only_selected =
false;
257 if (_count(
"subset")) {
259 only_selected =
true;
262 log() <<
"selection time: " <<
log().
lap_time() <<
" (s)\n" << std::endl;
265 _sel(0).write(_dict(), _output_path(), _count(
"json"), only_selected);
267 log() <<
"write: " << _output_path() <<
"\n" << std::endl;
270 _output_path().
string());
271 _write_selection_stats(m_Ntot, _sel(0),
log(), only_selected);
278 template <
typename DataObject>
280 if (_criteria_vec().size() == 0) {
282 }
else if (_criteria_vec().size() == 1) {
283 return _criteria_vec()[0];
286 <<
"ERROR: Selection criteria must be a single string. You provided "
287 << _criteria_vec().size() <<
" strings:\n";
288 for (
const std::string &str : _criteria_vec())
289 err_log() <<
" - " << str <<
"\n";
294 template <
typename DataObject>
297 _sel(0).set(_dict(), _criteria());
300 template <
typename DataObject>
303 _sel(0).set(_dict(), _criteria(),
true);
306 template <
typename DataObject>
309 _sel(0).set(_dict(), _criteria(),
false);
312 template <
typename DataObject>
314 log().
custom(std::string(
"and(") + _sel_str() +
")");
315 for (
int i = 1; i < _sel_size(); i++) {
316 for (
const auto &val : _sel(i).data()) {
317 auto find_it = _sel(0).data().find(val.first);
318 if (find_it != _sel(0).data().end()) {
319 find_it->second = (find_it->second && val.second);
321 _sel(0).data()[val.first] =
false;
327 template <
typename DataObject>
329 log().
custom(std::string(
"or(") + _sel_str() +
")");
330 for (
int i = 1; i < _sel_size(); i++) {
331 for (
const auto &val : _sel(i).data()) {
333 _sel(0).data()[val.first] =
true;
339 template <
typename DataObject>
341 log().
custom(_selection_paths(0).
string() +
" xor " +
342 _selection_paths(1).
string());
343 for (
const auto &val : _sel(1).data()) {
351 auto find_it = _sel(0).data().find(val.first);
352 if (find_it == _sel(0).data().end()) {
353 _sel(0).data().insert(val);
357 find_it->second = !find_it->second;
362 template <
typename DataObject>
364 log().
custom(std::string(
"not ") + _selection_paths(0).
string());
365 for (
auto &value : _sel(0).data()) {
366 value.second = !value.second;
370 template <
typename DataObject>
372 auto it = _sel(0).data().cbegin();
373 auto end = _sel(0).data().cend();
376 it = _sel(0).data().erase(it);
383 template <
typename DataObject>
387 _selection_paths(0).
string());
388 _write_selection_stats(m_Ntot, _sel(0),
log(),
false);
391 for (
int i = 1; i < _selection_paths().size(); ++i) {
393 _selection_paths(i).
string());
394 _write_selection_stats(m_Ntot, _sel(i),
log(),
false);
399 template <
typename DataObject>
402 bool only_selected)
const {
404 auto Ninclude = only_selected ? Nselected : sel.
size();
411 << Nselected <<
"\n";
432 std::vector<std::string> allowed_cmd = {
"and",
"or",
"xor",
"not",
433 "set-on",
"set-off",
"set"};
436 for (
const std::string &cmd_str : allowed_cmd) {
444 err_log() <<
"Error in 'casm select'. No more than one of the following "
446 << allowed_cmd << std::endl;
449 opt().selection_paths().size() != 1) {
451 <<
"ERROR: 'casm select --subset' expects zero or one list as argument."
457 (cmd ==
"or" || cmd ==
"and" || cmd ==
"xor" || cmd ==
"not")) {
458 err_log() <<
"ERROR: 'casm select --" << cmd
459 <<
"' expects an --output file." << std::endl;
463 if ((cmd ==
"set-on" || cmd ==
"set-off" || cmd ==
"set") &&
464 vm().
count(
"selections") &&
opt().selection_paths().size() != 1) {
465 err_log() <<
"Error in 'casm select " << cmd <<
"'. "
467 <<
" selections were specified, "
468 "but no more than one selection is allowed (MASTER list is "
470 "other is specified)."
475 if (cmd ==
"xor" &&
opt().selection_paths().size() != 2) {
477 <<
"ERROR: Option --xor requires exactly 2 selections as argument\n";
493 if (!
opt().db_type_opts().
count(
opt().db_type())) {
494 std::stringstream msg;
495 msg <<
"--type " <<
opt().
db_type() <<
" is not allowed for 'casm "
504 m_impl = notstd::make_unique<SelectCommandImplBase>(*
this);
511 sout <<
"The allowed types are:\n";
513 for (
const auto &db_type :
opt().db_type_opts()) {
514 sout <<
" " << db_type << std::endl;
std::set< std::string > & s
#define ERR_EXISTING_FILE
const OptionType & opt() const
const po::variables_map & vm() const
int count(std::string s) const
void add_selections_suboption(const fs::path &_default="MASTER")
Add –selections suboption (defaults to MASTER)
const po::options_description & desc()
Get the program options, filled with the initialized values.
po::options_description m_desc
void add_db_type_suboption(std::string _default, std::set< std::string > _configtype_opts)
void add_general_help_suboption()
Add a smart –help suboption that takes "properties" or "operators".
void add_output_suboption()
Add a –output suboption. Expects to allow "STDOUT" to print to screen.
const std::vector< std::string > & criteria_vec() const
void initialize() override
Fill in the options descriptions accordingly.
std::string db_type() const
const std::vector< fs::path > & selection_paths() const
Returns the string corresponding to add_config_suboption()
const fs::path output_path() const
Returns the path corresponding to add_output_suboption()
std::vector< std::string > m_criteria_vec
Index selected_size() const
void custom(const std::string &what)
void error(const std::string &what)
void write(const std::string &what)
SelectCommandImplBase & impl() const
std::unique_ptr< SelectCommandImplBase > m_impl
int help() const override
void print_names(std::ostream &sout) const
int desc() const override
static const std::string name
SelectCommand(const CommandArgs &_args, Completer::SelectOption &_opt)
int vm_count_check() const override
Defaults used if DataObject type doesn't matter or not given.
SelectCommandImplBase(const SelectCommand &cmd)
const SelectCommand & m_cmd
virtual ~SelectCommandImplBase()
const std::vector< fs::path > & _selection_paths() const
std::string _criteria() const
const DataFormatterDictionary< DataObject > & _dict() const
DB::InterfaceData< DataObject > m_data
SelectCommandImpl(const SelectCommand &cmd)
void _write_selection_stats(Index Ntot, const DB::Selection< DataObject > &sel, Log &log, bool only_selected) const
void _write_input_stats() const
DB::Selection< DataObject > & _sel(Index i=0) const
int help() const override
int desc() const override
int _count(std::string s) const
fs::path _output_path() const
fs::path _selection_paths(Index i) const
std::string _sel_str() const
const std::vector< std::string > & _criteria_vec() const
void for_type_short(std::string short_name, F f)
const std::set< std::string > & types_short()
std::set of all QueryTraits<DataObject>::short_name
std::string help()
Uses 'multiline_help<T>()' by default.
INDEX_TYPE Index
For long integer indexing:
Data structure holding basic CASM command info.