generator  1.2
ProjectGenerator.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the GeneratorOfDatasets program.
3 
4  GeneratorOfDatasets is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  GeneratorOfDatasets is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with GeneratorOfDatasets. If not, see <http://www.gnu.org/licenses/>.
16 */
33 #include <algorithm>
34 #include <iostream>
35 #include <string>
36 #include <stdexcept>
37 #include <sstream>
38 #include "Settings.h"
39 #include "Generator.h"
40 #include "GeneratorConfig.h"
41 #include "ProjectParameters.h"
43 
44 using namespace std;
45 
48  EXIT_WITH_SUCCESS = 0,
49  INVALID_PARAMETER = 1,
50  INSUFFICIENT_PARAMETERS = 2,
51  INPUT_OUTPUT_ERROR = 4,
52  RUNTIME_ERROR = 8,
53  UNKNOWN_ERROR = 16
54 };
55 
58  cout<<"Generator of instances for energy optimisation of robotics lines."<<endl;
59  cout<<"Author: Libor Bukata and Premysl Sucha"<<endl;
60  cout<<"Licence: GNU General Public License"<<endl;
61  cout<<"Version: "<<PROGRAM_VERSION<<endl<<endl;
62 }
63 
68 void printProgramHelp(const string& progName) {
70  cout<<"Usage:"<<endl;
71  cout<<"\t"<<progName<<" [options+parameters]"<<endl;
72  cout<<"Options:"<<endl;
73  cout<<"\t--project-configuration-file ARG, -pcf ARG, ARG=FILE"<<endl;
74  cout<<"\t\tIt reads the desired parameters of the project from the file."<<endl;
75  cout<<"\t--number-of-instances ARG, -noi ARG, ARG=POSITIVE_INTEGER"<<endl;
76  cout<<"\t\tThe number of generated instances for the project."<<endl;
77  cout<<"\t--output-file ARG, -of ARG, ARG=FILE"<<endl;
78  cout<<"\t\tOutput xml file to which the generated instances will be written."<<endl;
79  cout<<"\t--set-dataset-title ARG, -sdt ARG, ARG=STRING"<<endl;
80  cout<<"\t\tSet short title (<name>ARG</name>) of the dataset."<<endl;
81  cout<<"\t--help, -h"<<endl;
82  cout<<"\t\tIt prints this program help."<<endl;
83  cout<<"\t--verbose"<<endl;
84  cout<<"\t\tIt also prints additional information (program header, progress bar, and runtime)."<<endl<<endl;
85  cout<<"Default settings can be modified at \"DefaultSettings.h\" file."<<endl;
86 }
87 
93 int main(int argc, char* argv[]) {
94 
95  for (int i = 1; i < argc; ++i) {
96 
97  string arg = argv[i];
98 
99  if (arg == "--project-configuration-file" || arg == "-pcf") {
100  if (i+1 < argc) {
101  Settings::PROJECT_CONFIGURATION_FILE = argv[++i];
102  } else {
103  cerr<<"Project configuration file was not specified!"<<endl;
104  return INVALID_PARAMETER;
105  }
106 
107  continue;
108  }
109 
110  if (arg == "--number-of-instances" || arg == "-noi") {
111  if (i+1 < argc) {
112  string number = argv[++i];
113  istringstream istr(number, istringstream::in);
114  istr>>Settings::NUMBER_OF_INSTANCES;
115  if (any_of(number.cbegin(), number.cend(), [](char c) { return (c < '0' || c > '9') ? true : false; }) || istr.fail()) {
116  cerr<<"Cannot parse '"<<number<<"' to number!"<<endl;
117  cout<<"\"--number-of-instances\" invalid parameter!"<<endl;
118  return INVALID_PARAMETER;
119  }
120  } else {
121  cout<<"\"--number-of-instances\" requires the parameter!"<<endl;
122  }
123 
124  continue;
125  }
126 
127  if (arg == "--output-file" || arg == "-of") {
128  if (i+1 < argc) {
129  Settings::OUTPUT_FILE = argv[++i];
130  } else {
131  cerr<<"\"--output-file\" requires the parameter!"<<endl;
132  return INVALID_PARAMETER;
133  }
134 
135  continue;
136  }
137 
138  if (arg == "--set-dataset-title" || arg == "-sdt") {
139  if (i+1 < argc) {
140  Settings::DATASET_TITLE = argv[++i];
141  } else {
142  cerr<<"\"--set-dataset-title\" requires the parameter!"<<endl;
143  return INVALID_PARAMETER;
144  }
145 
146  continue;
147  }
148 
149  if (arg == "--verbose" || arg == "-v") {
150  Settings::VERBOSE = true;
151  continue;
152  }
153 
154  if (arg == "--help" || arg == "-h") {
155  printProgramHelp(argv[0]);
156  return EXIT_WITH_SUCCESS;
157  }
158 
159  cerr<<"Unknown argument \""<<arg<<"\"!"<<endl;
160  cerr<<"Check help by using \"-h\" or \"--help\" arguments."<<endl;
161  return INVALID_PARAMETER;
162  }
163 
164  if (Settings::OUTPUT_FILE.empty()) {
165  cerr<<"Insufficient parameters, at least the output file has to be specified!"<<endl;
166  cerr<<"See the following program help..."<<endl<<endl;
167  printProgramHelp(argv[0]);
168  return INSUFFICIENT_PARAMETERS;
169  }
170 
171  try {
172  if (Settings::VERBOSE == true)
174 
176  ProjectParameters parameters = parser.getParameters();
177  Generator generator;
178  generator.setParameters(parameters);
179  generator.generateProblems();
180  } catch (invalid_argument e) {
181  cerr<<e.what()<<endl;
182  return INPUT_OUTPUT_ERROR;
183  } catch (runtime_error e) {
184  cerr<<e.what()<<endl;
185  return RUNTIME_ERROR;
186  } catch (exception& e) {
187  cerr<<e.what()<<endl;
188  return UNKNOWN_ERROR;
189  }
190 
191  return EXIT_WITH_SUCCESS;
192 }
ProgramReturnedCodes
Return codes of the program.
The structure with desired properties for the generated instances.
void printProgramHelp(const string &progName)
It prints the program header and help.
STL namespace.
int main(int argc, char *argv[])
It handles the program command line arguments, reads desired properties of instances, generates and writes them to the xml file.
The file contains the Generator class.
It reads the desired properties of instances from the text file.
void generateProblems(uint32_t numberOfProblems=Settings::NUMBER_OF_INSTANCES)
It generates the specified number of instances and writes them to the xml file.
Definition: Generator.cpp:30
void printProgramHeader()
It prints the program header to the standard output.
It declares the class for parsing the desired properties of project instances.
The file declares the structure for storing the properties of generated instances.
It declares the namespace for program settings.
It encapsulates the RoboticLine and XmlWriter classes and adds the additional features.
Definition: Generator.h:37