Scine::Readuct  4.0.0
This is the SCINE module Readuct.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Task.h
Go to the documentation of this file.
1 
7 #ifndef READUCT_TASK_H_
8 #define READUCT_TASK_H_
9 
11 #include <Core/Log.h>
12 #include <Utils/Settings.h>
13 #include <yaml-cpp/yaml.h>
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 namespace Scine {
20 namespace Core {
21 class Calculator;
22 }
23 namespace Readuct {
24 
28 class Task {
29  public:
30  using SystemsMap = std::map<std::string, std::shared_ptr<Core::Calculator>>;
31 
32  static std::shared_ptr<Core::Calculator> copyCalculator(SystemsMap& systems, const std::string& name,
33  const std::string& taskName) {
34  const auto findIter = systems.find(name);
35  if (findIter == systems.end()) {
36  throw std::runtime_error("System name '" + name + "' is missing in " + taskName);
37  }
38  return std::shared_ptr<Core::Calculator>(findIter->second->clone().release());
39  }
40 
47  Task(std::vector<std::string> input, std::vector<std::string> output, std::shared_ptr<Core::Log> logger = nullptr)
48  : _input(std::move(input)), _output(std::move(output)), _logger(std::move(logger)) {
49  if (!_logger) {
50  _logger = std::make_shared<Core::Log>();
51  }
52 
53  if (_input.empty()) {
54  throw std::runtime_error("No input systems specified!");
55  }
56  }
57 
58  virtual ~Task() = default;
59 
64  virtual std::string name() const = 0;
72  virtual bool run(SystemsMap& systems, Utils::UniversalSettings::ValueCollection taskSettings, bool test = false) const = 0;
77  const std::vector<std::string>& input() const {
78  return _input;
79  };
84  const std::vector<std::string>& output() const {
85  return _output;
86  };
91  if (_input.size() > 1) {
92  _logger->warning
93  << " Warning: More than one input system was specified. Only taking first and ignoring all others.\n";
94  }
95  };
100  if (_output.size() > 1) {
101  _logger->warning
102  << " Warning: More than one output system was specified. Only taking first and ignoring all others.\n";
103  }
104  };
105 
106  bool stopOnErrorExtraction(Utils::UniversalSettings::ValueCollection& taskSettings) const {
107  bool stopOnError = true;
108  if (taskSettings.valueExists("allow_unconverged")) {
109  _logger->warning << " The option 'allow_unconverged' is deprecated.\n"
110  << " It has been replaced with 'stop_on_error',\n"
111  << " which is now available for all tasks and is defaulted to 'true'.\n\n";
112  stopOnError = !taskSettings.extract("allow_unconverged", false);
113  }
114  return taskSettings.extract("stop_on_error", stopOnError);
115  };
116 
117  static std::string falseTaskSettingsErrorMessage(const std::string& name) {
118  return " You gave Task settings for the " + name + ",\n" + " but the only possible setting for this task, are the\n" +
119  " 'stop_on_error' option to control whether ReaDuct fails\n" +
120  " with a failed calculation or simply returns false\n" +
121  " and the 'silent_stdout_calculator' option to control whether\n"
122  " the standard output of the calculator should be printed.\n"
123  " You might want to specify the settings you put into the task settings\n" +
124  " in the systems section.";
125  }
126 
127  protected:
128  const std::vector<std::string> _input;
129  const std::vector<std::string> _output;
130  std::shared_ptr<Core::Log> _logger;
131 };
132 
133 } // namespace Readuct
134 } // namespace Scine
135 
136 #endif // READUCT_TASK_H_
void warningIfMultipleOutputsGiven() const
Warn if more than one output system was specified.
Definition: Task.h:99
const std::vector< std::string > & input() const
Getter for the expected names of the input systems.
Definition: Task.h:77
virtual bool run(SystemsMap &systems, Utils::UniversalSettings::ValueCollection taskSettings, bool test=false) const =0
Executes the actual task represented by this class.
Task(std::vector< std::string > input, std::vector< std::string > output, std::shared_ptr< Core::Log > logger=nullptr)
Construct a new Task.
Definition: Task.h:47
virtual std::string name() const =0
Getter for the tasks name.
void warningIfMultipleInputsGiven() const
Warn if more than one input system was specified.
Definition: Task.h:90
const std::vector< std::string > & output() const
Getter for the names of the output systems generated by this task.
Definition: Task.h:84
The base class for all tasks in Readuct.
Definition: Task.h:28