30 #include <QApplication>
48 CliController::CliController(shared_ptr<Ws::CliWorkspace> workspace_model,
bool quiet)
49 : m_workspace_model(move(workspace_model)), m_quiet(quiet),
50 m_sig_int_adaptor(bind(&CliController::SigIntHandler, this, placeholders::_1)),
51 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
52 m_sig_qt_adaptor(bind(&CliController::SigQtHandler, this, placeholders::_1, placeholders::_2, placeholders::_3))
54 m_sig_qt_adaptor(bind(&CliController::SigQtHandler, this, placeholders::_1, placeholders::_2))
58 signal(SIGINT, m_sig_int_adaptor);
61 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
62 qInstallMessageHandler(m_sig_qt_adaptor);
64 qInstallMsgHandler(m_sig_qt_adaptor);
68 CliController::~CliController()
70 signal(SIGINT, SIG_DFL);
71 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
72 qInstallMessageHandler(0);
74 qInstallMsgHandler(0);
78 shared_ptr<CliController> CliController::Create(
const string& workspace,
bool quiet)
80 const string here = string(VL_HERE) +
" exception:\n";
81 shared_ptr<CliController> ptr;
86 if (static_cast<QApplication*>(QApplication::instance()) ==
nullptr) {
87 throw Exception(here +
"No QApplication instantiated in calling context!");
91 auto workspace_model = make_shared<Ws::CliWorkspace>(workspace);
93 UserMessage(
"Successfully opened workspace " + workspace_model->GetPath());
95 ptr = shared_ptr<CliController>(
new CliController(move(workspace_model), quiet));
97 throw Exception(here +
"Produced nullptr for " + workspace);
100 }
catch (exception& e) {
101 UserError(here + e.what());
104 UserError(here +
"Unknown exception!");
110 shared_ptr<CliController> CliController::Create(shared_ptr<Ws::CliWorkspace> workspace,
bool quiet)
112 const string here = string(VL_HERE) +
" exception:\n";
113 shared_ptr<CliController> ptr;
114 assert((workspace !=
nullptr) &&
"Workspace pointer cannot be nullptr.");
119 if (static_cast<QApplication*>(QApplication::instance()) ==
nullptr) {
120 throw Exception(here +
"No QApplication instantiated in calling context!");
123 ptr = shared_ptr<CliController>(
new CliController(move(workspace), quiet));
124 if (ptr ==
nullptr) {
125 throw Exception(here +
"Produced nullptr for " + workspace->GetPath());
128 }
catch (exception& e) {
129 UserError(here + e.what());
132 UserError(here +
"Unknown exception!");
135 assert((ptr !=
nullptr) &&
"CliController pointer cannot be nullptr.");
141 int exit_status = EXIT_SUCCESS;
150 std::transform(inputfilter.begin(), inputfilter.end(), inputfilter.begin(), ::tolower);
153 auto prefs = SimShell::Ws::MergedPreferences::Create(m_workspace_model, project);
154 string prefix =
"leaf";
156 vector<int> timesteps;
157 for (
auto& file : *project) {
158 bool valid_inputformat=
true;
163 std::string path = tissue_file->
GetPath();
164 std::transform(path.begin(), path.end(), path.begin(), ::tolower);
166 if (path.length()>=inputfilter.length()) {
167 valid_inputformat = path.substr(
168 path.length()-inputfilter.length(), inputfilter.length())==inputfilter;
171 if (valid_inputformat) {
172 for (
int step : static_pointer_cast<Ws::StartupFileBase>(file.second)->GetTimeSteps()) {
173 timesteps.push_back(step);
178 vector<int> filtered_timesteps;
179 for (
int step : timesteps) {
181 filtered_timesteps.push_back(step);
187 static_cast<Ws::Project*>(project.get()), filtered_timesteps, prefs,
188 format, project->GetPath(), prefix);
192 [&](
int i) { UserMessage(
"Converted " +
ToString(i) +
" files."); }
195 m_timings.Record(chrono_total.
GetName(), chrono_total.
Get());
202 int exit_status = EXIT_SUCCESS;
206 exit_status = SimulatorRun(task);
208 catch (std::exception& e) {
209 exit_status = EXIT_FAILURE;
213 m_timings.Record(chrono_total.
GetName(), chrono_total.
Get());
217 CliController::Timings CliController::GetTimings()
const
219 return m_timings.GetRecords();
222 bool CliController::IsQuiet()
const
227 void CliController::SigIntHandler(
int )
229 signal(SIGINT, SIG_DFL);
230 emit TerminationRequested();
233 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
234 void CliController::SigQtHandler(QtMsgType type,
const QMessageLogContext& ,
const QString& msg)
238 UserMessage(
"Qt debug: " + msg.toStdString());
240 #if (QT_VERSION >= QT_VERSION_CHECK(5,5,0))
242 UserMessage(
"Qt info: " + msg.toStdString());
246 UserMessage(
"Qt warning: " + msg.toStdString());
249 UserError(
"Qt critical: " + msg.toStdString());
252 UserError(
"Qt fatal: " + msg.toStdString());
258 void CliController::SigQtHandler(QtMsgType type,
const char* msg)
262 UserMessage(
"Qt debug: " +
string(msg));
265 UserMessage(
"Qt warning: " +
string(msg));
268 UserError(
"Qt critical: " +
string(msg));
271 UserError(
"Qt fatal: " +
string(msg));
278 void CliController::SimulationError(
const std::string& error)
281 emit TerminationRequested();
284 void CliController::SimulationInfo(
const std::string& message,
285 const Session::ISession::InfoMessageReason& reason)
288 case Session::ISession::InfoMessageReason::Stepped:
290 UserMessage(message);
293 case Session::ISession::InfoMessageReason::Stopped:
294 emit TerminationRequested();
296 case Session::ISession::InfoMessageReason::Terminated:
297 emit TerminationRequested();
304 int CliController::SimulatorRun(CliSimulationTask task)
306 int exit_status = EXIT_FAILURE;
307 const string here = string(VL_HERE) +
"> ";
308 const string project_name = task.GetProjectName();
309 auto project = m_workspace_model->Get(project_name);
310 const string file = task.IsLeafSet() ? task.GetLeaf() : (--project->end())->first;
313 if (project->IsOpened()) {
315 auto rootViewer = project->Session().CreateRootViewer();
317 connect(&project->Session(), SIGNAL(InfoMessage(
const std::string&,
const InfoMessageReason&)),
318 this, SLOT(SimulationInfo(
const std::string&,
const InfoMessageReason&)));
319 connect(&project->Session(), SIGNAL(ErrorMessage(
const std::string&)),
320 this, SLOT(SimulationError(
const std::string&)));
322 UserMessage(
"Opened project " + project_name +
" and " + file);
330 QEventLoop eventLoop(
this);
331 connect(
this, SIGNAL(TerminationRequested()), &eventLoop, SLOT(quit()), Qt::QueuedConnection);
332 project->Session().StartSimulation(task.IsNumStepsSet() ? task.GetNumSteps() : -1);
335 exit_status = EXIT_SUCCESS;
337 m_timings.Merge(project->Session().GetTimings());
341 exit_status = EXIT_FAILURE;
342 UserError(here +
"Failed to open project " + project_name +
" and " + file);
348 void CliController::Terminate()
350 emit TerminationRequested();
353 void CliController::UserError(
const string& msg)
355 std::cerr << msg << std::endl;
358 void CliController::UserMessage(
const string& msg)
360 std::cout << msg << std::endl;
bool Contains(int step) const
Checks whether the selection contains a specified step.
Interface for CliController.
A CliConverterTask represents an invocation of the converter from the command line.
Interface for Conversion.
Namespace for miscellaneous utilities.
string ToString(Type w)
Converts a WallType::Type value to corresponding name.
Namespace for SimPT shell package.
std::string GetName() const
Return name of this stopwatch.
const std::string GetProjectName() const
Plain getter.
Interface for StepSelection.
void SetSelectionText(const std::string &ranges)
Sets the selected ranges of steps.
Extremely simple Exception root class.
Macro defs for debug and logging.
Command line interface application controller.
const std::string GetTimeStepFilter() const
Plain getter.
const std::string GetInputFormatFilter() const
Plain getter.
virtual const std::string & GetPath() const
Interface for MergedPreferences.
Interfaces for simulator session.
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
Base class representing the file types used to initiate a session.
String manipulation utilities.
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Header file for Exception class.
A CliTask represents an invocation of the program from the command line.
Class handling user input of ranges of steps.
Manages file conversion operations specifications.
IConverterFormat * GetOutputFormat() const
Plain getter.