30 #include <boost/property_tree/exceptions.hpp>
31 #include <boost/property_tree/xml_parser.hpp>
32 #include <QApplication>
54 CoupledCliController::CoupledCliController(shared_ptr<Ws::CliWorkspace> workspace_model,
bool quiet)
55 : m_workspace_model(move(workspace_model)), m_quiet(quiet),
56 m_sig_int_adaptor(bind(&CoupledCliController::SigIntHandler, this, placeholders::_1)),
57 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
58 m_sig_qt_adaptor(bind(&CoupledCliController::SigQtHandler, this, placeholders::_1, placeholders::_2, placeholders::_3))
60 m_sig_qt_adaptor(bind(&CoupledCliController::SigQtHandler, this, placeholders::_1, placeholders::_2))
64 signal(SIGINT, m_sig_int_adaptor);
67 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
68 qInstallMessageHandler(m_sig_qt_adaptor);
70 qInstallMsgHandler(m_sig_qt_adaptor);
74 CoupledCliController::~CoupledCliController()
76 signal(SIGINT, SIG_DFL);
77 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
78 qInstallMessageHandler(0);
80 qInstallMsgHandler(0);
84 std::shared_ptr<CoupledCliController> CoupledCliController::Create(
const string& workspace,
bool quiet)
86 const string here = string(VL_HERE) +
" exception:\n";
87 shared_ptr<CoupledCliController> ptr;
92 if (static_cast<QApplication*>(QApplication::instance()) ==
nullptr) {
93 throw Exception(here +
"No QApplication instantiated in calling context.");
97 auto workspace_model = make_shared<Ws::CliWorkspace>(workspace);
99 UserMessage(
"Successfully opened workspace " + workspace_model->GetPath());
102 if (ptr ==
nullptr) {
103 throw Exception(here +
"Produced nullptr for " + workspace);
106 UserError(here + e.
what());
109 UserError(here +
"Unknown exception!");
117 int exit_status = EXIT_SUCCESS;
121 bool status = SimulatorRun(task);
122 if (status ==
false) {
123 exit_status = EXIT_FAILURE;
126 catch (exception& e) {
127 exit_status = EXIT_FAILURE;
131 m_timings.Record(chrono_total.
GetName(), chrono_total.
Get());
135 CoupledCliController::Timings CoupledCliController::GetTimings()
const
137 return m_timings.GetRecords();
140 bool CoupledCliController::IsQuiet()
const
145 shared_ptr<Session::SimSessionCoupled>
146 CoupledCliController::OpenProject(
const string& project_name,
const string& file_name)
151 const string here = string(VL_HERE);
152 shared_ptr<Session::SimSessionCoupled> session;
155 auto project_it = m_workspace_model->Find(project_name);
156 auto file_it = project_it->second->Find(file_name);
159 auto file = file_it->second->GetPath();
160 const string extension = QFileInfo(QString::fromStdString(file)).suffix().toStdString();
161 if (extension ==
"xml") {
163 read_xml(file, sim_pt, trim_whitespace);
164 session = make_shared<Session::SimSessionCoupled>(
165 SimShell::Ws::MergedPreferences::Create(
166 m_workspace_model, project_it->second.Project()), sim_pt, m_workspace_model);
170 UserMessage(
"Successfully read coupled project from file " + file_it->second->GetPath());
173 catch (exception& e) {
174 throw Exception(here +
" exception:\n" + e.what());
180 void CoupledCliController::SigIntHandler(
int )
182 signal(SIGINT, SIG_DFL);
183 emit TerminationRequested();
186 #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
187 void CoupledCliController::SigQtHandler(QtMsgType type,
const QMessageLogContext& ,
const QString& msg)
191 UserMessage(
"Qt debug: " + msg.toStdString());
193 #if (QT_VERSION >= QT_VERSION_CHECK(5,5,0))
195 UserMessage(
"Qt info: " + msg.toStdString());
199 UserMessage(
"Qt warning: " + msg.toStdString());
202 UserError(
"Qt critical: " + msg.toStdString());
205 UserError(
"Qt fatal: " + msg.toStdString());
209 UserError(
"Qt fatal: " + msg.toStdString());
215 void CoupledCliController::SigQtHandler(QtMsgType type,
const char* msg)
219 UserMessage(
"Qt debug: " +
string(msg));
222 UserMessage(
"Qt warning: " +
string(msg));
225 UserError(
"Qt critical: " +
string(msg));
228 UserError(
"Qt fatal: " +
string(msg));
232 UserError(
"Qt fatal: " +
string(msg));
239 void CoupledCliController::SimulationError(
const std::string &error)
242 emit TerminationRequested();
245 void CoupledCliController::SimulationInfo(
246 const std::string &message,
const Session::ISession::InfoMessageReason &reason)
249 case Session::ISession::InfoMessageReason::Stepped:
251 UserMessage(message);
254 case Session::ISession::InfoMessageReason::Stopped:
255 emit TerminationRequested();
257 case Session::ISession::InfoMessageReason::Terminated:
258 emit TerminationRequested();
265 int CoupledCliController::SimulatorRun(CliSimulationTask task)
267 bool exit_status = EXIT_FAILURE;
268 const string here = string(VL_HERE) +
"> ";
269 const string project_name = task.GetProjectName();
270 auto ws_project = m_workspace_model->Get(project_name);
271 const string file = task.IsLeafSet() ? task.GetLeaf() : (--ws_project->end())->first;
273 auto session = OpenProject(project_name, file);
276 auto rootViewer = session->CreateRootViewer();
278 connect(session.get(), SIGNAL(InfoMessage(
const std::string&,
const InfoMessageReason&)),
279 this, SLOT(SimulationInfo(
const std::string&,
const InfoMessageReason&)));
280 connect(session.get(), SIGNAL(ErrorMessage(
const std::string&)),
281 this, SLOT(SimulationError(
const std::string&)));
284 UserMessage(
"Opened project " + project_name +
" and " + file);
292 QEventLoop eventLoop(
this);
293 connect(
this, SIGNAL(TerminationRequested()), &eventLoop, SLOT(quit()), Qt::QueuedConnection);
294 session->StartSimulation(task.IsNumStepsSet() ? task.GetNumSteps() : -1);
297 exit_status = EXIT_SUCCESS;
299 m_timings.Merge(session->GetTimings());
301 exit_status = EXIT_FAILURE;
302 UserError(here +
"Failed to open project " + project_name +
" and " + file);
308 void CoupledCliController::Terminate()
310 emit TerminationRequested();
313 void CoupledCliController::UserError(
const string& msg)
315 std::cerr << msg << std::endl;
318 void CoupledCliController::UserMessage(
const string& msg)
320 std::cout << msg << std::endl;
Interface for SimSessionCoupled.
Namespace for miscellaneous utilities.
virtual const char * what() const noexcept
Return error message.
Namespace for SimPT shell package.
std::string GetName() const
Return name of this stopwatch.
Extremely simple Exception root class.
Macro defs for debug and logging.
Interface for CoupledCliController.
Interface for MergedPreferences.
Interfaces for simulator session.
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
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.
Command line interface application controller.
Namespace for clock and timekeeping related classes.