VPTissue Reference Manual
sim_cli.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "sim_cli_mode.h"
21 
22 #include "cli/CliSimulationTask.h"
23 #include "cli/CliController.h"
24 #include "cli/CliConverterTask.h"
30 #include "util/misc/Exception.h"
32 
33 #include <tclap/CmdLine.h>
34 #include <tclap/SwitchArg.h>
35 #include <tclap/ValueArg.h>
36 #include <tclap/ValuesConstraint.h>
37 #include <QApplication>
38 #include <QMetaType>
39 
40 #include <cstdlib>
41 #include <functional>
42 #include <iostream>
43 #include <memory>
44 #include <string>
45 #include <vector>
46 
47 namespace Modes {
48 
49 using namespace std;
50 using namespace TCLAP;
51 using namespace SimPT_Shell;
52 using namespace SimPT_Shell::Session;
53 using namespace SimPT_Sim::ClockMan;
54 using namespace SimPT_Sim::Util;
55 
56 int simPTCLIMode::operator()(int argc, char** argv)
57 {
58  int exit_status = EXIT_SUCCESS;
59 
60  try {
61  // Needs to be instantiated to access some Qt functionality.
62  QCoreApplication app(argc, argv, false);
63  qRegisterMetaType<std::string>("std::string");
64 
65  // Parse command line and create task to be executed.
66  CmdLine cmd("simPT_sim", ' ', "1.0");
67  SwitchArg rev_Arg("r", "revision", "Revision identification", cmd, false);
68  SwitchArg quiet_Arg("q", "quiet", "Quit mode (no output)", cmd, false);
69  ValueArg<unsigned int> num_steps_Arg("s", "stepcount", "number of steps", false, 0, "NUMBER OF STEPS", cmd);
70  ValueArg<string> file_Arg("f", "file", "Tissue file in project", false, "", "TISSUE FILE", cmd);
71  ValueArg<string> project_Arg("p", "project", "Name of project", false, "unnamed", "PROJECT NAME", cmd);
72  ValueArg<string> workspace_Arg("w", "workspace", "Path to workspace", false, "simPT_Default_workspace", "WORKSPACE PATH", cmd);
73 
74  vector<string> convert_output_formats;
75  for (auto f : FileConverterFormats::GetFormats()) {
76  if (!f->IsPostProcessFormat())
77  convert_output_formats.push_back(f->GetName());
78  }
79 
80  vector<string> postprocess_output_formats;
81  for (auto f : FileConverterFormats::GetFormats()) {
82  if (f->IsPostProcessFormat())
83  postprocess_output_formats.push_back(f->GetName());
84  }
85 
86  ValueArg<string> timestep_filter_Arg("t", "timestep-filter",
87  "Filter timesteps to convert, (list of) ranges are accepted, e.g. \"200-300,600\".", false, {}, "", cmd);
88 
89  ValuesConstraint<string> allowedConvertFormats(convert_output_formats);
90  ValueArg<string> convert_Arg("c", "convert",
91  "Convert mode (no simulation): Convert existing files in workspace.",
92  false, "xml", &allowedConvertFormats, cmd);
93 
94  ValuesConstraint<string> allowedPostprocessFormats(postprocess_output_formats);
95  ValueArg<string> postprocess_Arg("o", "postprocess",
96  "Postprocess mode (no simulation): Postprocess existing files in workspace.",
97  false, "png", &allowedPostprocessFormats, cmd);
98 
99  ValuesConstraint<string> allowedInputFormats(convert_output_formats);
100  ValueArg<string> input_format_Arg("z", "input-format-filter",
101  "Only use a specific input format", false, "", &allowedInputFormats, cmd);
102 
103  cmd.parse(argc, argv);
104 
105  if (rev_Arg.isSet()) {
106  cout << RevisionInfo::CompoundId() << endl;
107  }
108 
109  if (workspace_Arg.isSet()) {
110  // Output preamble.
111  cout << "simPT_sim starting up at: " << TimeStamp().ToString() << endl;
112  cout << "Executing: " << argv[0]<< endl;
113  cout << "Revision: " << RevisionInfo::CompoundId() << endl;
114 
115  // Command line arguments.
116  const bool quiet = quiet_Arg.getValue();
117  const string workspace_path = workspace_Arg.getValue();
118  const string project_name = project_Arg.getValue();
119 
120 
121  auto c = CliController::Create(workspace_path, quiet);
122 
123  if (convert_Arg.isSet() || postprocess_Arg.isSet()) {
124  const string timestep_filter = timestep_filter_Arg.getValue();
125  const string output_format_str = convert_Arg.isSet() ? convert_Arg.getValue() : postprocess_Arg.getValue();
126  const string input_format_filter = input_format_Arg.isSet() ? input_format_Arg.getValue() : "";
127 
128  IConverterFormat* output_format = nullptr;
129  for (auto f : FileConverterFormats::GetFormats()) {
130  if (output_format_str == f->GetName()) {
131  output_format = f;
132  }
133  }
134  if (output_format == nullptr) {
135  throw Exception("Convert mode selected but no output format given!");
136  }
137 
138  // Set up task and execute.
139  CliConverterTask task(project_name, timestep_filter, output_format, input_format_filter);
140  exit_status = c->Execute(task);
141  } else {
142  const unsigned int num_steps = num_steps_Arg.getValue();
143  const bool num_steps_set = num_steps_Arg.isSet();
144  const string file = file_Arg.getValue();
145  const bool file_set = file_Arg.isSet();
146 
147  // Setup task.
148  CliSimulationTask task(project_name, file_set, file, num_steps_set, num_steps);
149  exit_status = c->Execute(task);
150  }
151  if (exit_status == EXIT_SUCCESS) {
152  cout << endl << c->GetTimings() << endl << endl;
153  }
154  cout << "SimPT_Sim exiting at: " << TimeStamp().ToString() << endl << endl;
155  }
156  }
157  // Almost all the exceptions that we are currently aware of derive
158  // from std::exception: TCLAP::ArgException, SimPT_Sim::Util::Exception,
159  // boost::property_tree::ptree_error, boost::property_tree::ptree_dad_data,
160  // boost::property_tree::ptree_bad_path, boost::property_tree::xml_parser::xml_parser_error
161  catch (exception& e) {
162  exit_status = EXIT_FAILURE;
163  cerr << e.what() << endl;
164  }
165  catch (...) {
166  exit_status = EXIT_FAILURE;
167  cerr << "Unknown exception." << endl;
168  }
169 
170  return exit_status;
171 }
172 
173 } // namespace
Interface for CliController.
Interface for ClockTraits.
STL namespace.
A CliConverterTask represents an invocation of the converter from the command line.
Info on git revision and commit date.
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
Interface/Implementation for CliTask.
Interface for CumulativeRecords.
Namespace for SimPT shell package.
Definition: Client.cpp:50
Namespace for startup modes for simPT tools.
Definition: mode_manager.h:36
static std::shared_ptr< CliController > Create(const std::string &workspace, bool quiet)
Create controller.
Extremely simple Exception root class.
Definition: Exception.h:28
Interface for IConverterFormat.
Interface for file convenvereter formats.
TimeStamp class.
Interface of file converter formats.
Namespace for SimPT specific session classes.
Provides wall-clock time stamp using the time call.
Definition: TimeStamp.h:37
Interface/Implementation for CliConverterTask.
Header file for Exception class.
std::string ToString() const
Returns string with the time stamp after eliminating newline.
Definition: TimeStamp.h:44
A CliTask represents an invocation of the program from the command line.
Namespace for clock and timekeeping related classes.
Definition: ClockCLib.h:27
Interface/Implementation for the exec modes.