generator  1.2
ProjectParametersParser.cpp
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 */
17 #include <algorithm>
18 #include <fstream>
19 #include <iostream>
20 #include <sstream>
21 #include <stdexcept>
22 #include <vector>
23 #include "Interval.h"
24 #include "ProjectParameters.h"
26 
27 using namespace std;
28 
30  ifstream in(inputFile.c_str());
31  if (!in)
32  throw invalid_argument("ProjectParametersParser(const string&): Cannot open input file \""+inputFile+"\"!");
33 
34  string line;
35  vector<string> fileLines;
36  while (getline(in,line)) {
37  bool onlySpaces = true;
38  for (const char& c : line) {
39  if (c != ' ' && c != '\t')
40  onlySpaces = false;
41  }
42 
43  if (!onlySpaces && find(line.cbegin(), line.cend(), '#') == line.cend())
44  fileLines.push_back(line);
45  }
46  in.close();
47 
48  int lineNumber = 1;
49  bool fileParsedCorrectly = true;
50  for (const string& line : fileLines) {
51  Interval<double> coeff;
52  Interval<double> inputPowerOfMode;
53  Interval<double> minDelayOfMode;
54  istringstream istr(line, istringstream::in);
55  switch (lineNumber) {
56  case 1:
57  if (!(istr>>mParsedParameters.numberOfRobots))
58  cerr<<"Cannot parse the number of robots!"<<endl;
59  break;
60  case 2:
61  while (istr>>inputPowerOfMode)
62  mParsedParameters.inputPowerOfModes.push_back(inputPowerOfMode);
63 
64  if (istr.eof())
65  istr.clear();
66 
67  if (mParsedParameters.inputPowerOfModes.empty())
68  cerr<<"Cannot read the input power of the power save mode!"<<endl;
69  break;
70  case 3:
71  while (istr>>minDelayOfMode)
72  mParsedParameters.minDelayOfModes.push_back(minDelayOfMode);
73 
74  if (istr.eof())
75  istr.clear();
76 
77  if (mParsedParameters.minDelayOfModes.empty())
78  cerr<<"Cannot read the minimal delays of the robot's static power save modes!"<<endl;
79  break;
80  case 4:
81  if (!(istr>>mParsedParameters.minDurationOfMovement>>mParsedParameters.prolongationOfMovement))
82  cerr<<"Cannot read the minimum and maximum time intervals of movements!"<<endl;
83  break;
84  case 5:
85  while (istr>>coeff)
86  mParsedParameters.energyFunctionCoefficients.push_back(coeff);
87 
88  if (istr.eof())
89  istr.clear();
90 
91  if (mParsedParameters.energyFunctionCoefficients.empty())
92  cerr<<"Coefficients of energy functions were not specified!"<<endl;
93  break;
94  case 6:
95  if (!(istr>>mParsedParameters.degreeOfCoefficients))
96  cerr<<"Cannot parse the range of powers for coefficients!"<<endl;
97  break;
98  case 7:
99  if (!(istr>>mParsedParameters.minDurationOfOperation>>mParsedParameters.prolongationOfOperation))
100  cerr<<"Cannot read the minimum and maximum time intervals of staying in the stationary position!"<<endl;
101  break;
102  case 8:
103  if (!(istr>>mParsedParameters.numberOfPoints))
104  cerr<<"Cannot read the number of points interval!"<<endl;
105  break;
106  case 9:
107  if (!(istr>>mParsedParameters.numberOfSequences))
108  cerr<<"The average number of sequences for assembling/disassembling operations was not specified!"<<endl;
109  break;
110  case 10:
111  if (!(istr>>mParsedParameters.sequenceLength))
112  cerr<<"The interval of the length of the assembling/disassembling sequences was not specified!"<<endl;
113  break;
114  case 11:
115  if (!(istr>>mParsedParameters.minimalVertexDegree))
116  cerr<<"The interval of the minimal degree of the vertex cannot be read!"<<endl;
117  break;
118  case 12:
119  if (!(istr>>mParsedParameters.percentageOfTableHandover))
120  cerr<<"Cannot read the probability of handovers using tables!"<<endl;
121  break;
122  case 13:
123  if (!(istr>>mParsedParameters.percentageOfRobotsHandover))
124  cerr<<"Cannot read the probability of the robot handovers!"<<endl;
125  break;
126  case 14:
127  if (!(istr>>mParsedParameters.numberOfCollisions))
128  cerr<<"Cannot read the interval specifying the number of collisions!"<<endl;
129  break;
130  case 15:
131  if (!(istr>>mParsedParameters.dilatationFactor))
132  cerr<<"Cannot read the dilatation factor!"<<endl;
133  break;
134  default:
135  clog<<"Ignoring line "<<lineNumber<<"..."<<endl;
136  }
137 
138  if (istr.fail()) {
139  fileParsedCorrectly = false;
140  break;
141  }
142  ++lineNumber;
143  }
144 
145  if (!fileParsedCorrectly || lineNumber <= 15)
146  throw runtime_error("ProjectParametersParser(const string&): Error has occured during parsing of the configuration file!");
147 
148  if (!checkProjectParameters(mParsedParameters))
149  throw runtime_error("ProjectParametersParser(const string&): Invalid value(s) in the config file!");
150 }
151 
STL namespace.
ProjectParametersParser(std::string inputFile=Settings::PROJECT_CONFIGURATION_FILE)
It parses the configuration file and fills ProjectParameters structure. Input configuration file is b...
It declares the class for parsing the desired properties of project instances.
The file declares the structure for storing the properties of generated instances.
bool checkProjectParameters(const ProjectParameters &par)
The function checks that the desired properties of instances are logically correct.
The file declares the Interval class and its iostream operators.