-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
247 lines (224 loc) · 5.72 KB
/
main.cpp
File metadata and controls
247 lines (224 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* main.cpp
*
* Created on: 28.11.2014
* Author: Thomas Schultz
* Mail: thomas.schultz@mytum.de
*
* main program which does the command line parsing and
* starts the code generation
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include "OS.h"
#include "CLI.h"
#include "aml/Blackboard.h"
#include "sfc/SFCFile.h"
#include "sfc/skillStep/StepSelector.h"
#include "process/Flow.h"
#include "tools/String.h"
#include "exceptions/Exception.h"
using namespace std;
using namespace sfc;
using namespace aml;
using namespace process;
using namespace except;
bool CLI::fail;
bool CLI::verbose;
static String version = "1.0.3";
std::String build_date() {
return __DATE__;
}
static bool fileexists(String file) {
ifstream infile(file.c_str());
return infile.good();
}
int main(int argc, char* argv[]) {
// Initialize program options
String in_file = "";
String out_file = "";
bool sfc = 0;
bool validate = 0;
bool printAML = 0;
bool printSFC = 0;
bool printFlw = 0;
bool printOrd = 0;
#ifdef DEBUG
/*
* testing purpose only
*/
cout << ">> DEBUG-Mode active <<" << endl;
CLI::printVersion(version, build_date());
String filename, name, output;
filename = "C:/Users/Thomas/workspace/PostProcessor/Blackboard.aml";
//filename = "Planungsdaten/LIN_reduziert_final_2_Aufspgen.xml";
if (!OS::isWindows()) {
filename = "/home/thomas/workspace/PostProcessor/" + filename;
name = "PostProcessor";
} else {
filename = "c:/Users/Thomas/workspace/PostProcessor/" + filename;
name = "PostProcessor.exe";
}
output = "output";
char *arg[2];
arg[0] = &name[0];
arg[1] = &filename[0];
arg[2] = &output[0];
argv = arg;
argc = 3;
sfc = 0;
CLI::setVerbose(1);
validate = 0;
printAML = 0;
printSFC = 0;
printFlw = 0;
printOrd = 0;
#endif
// parse command line
int n = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0) {
CLI::printHelp();
return 0;
} else if (strcmp(argv[i], "--warn") == 0) {
CLI::setVerbose(true);
continue;
} else if (strcmp(argv[i], "--info") == 0) {
sfc::StepSelector::init();
CLI::printVersion(version, build_date());
CLI::printLine(" Supported SkillSteps:\n " + StepSelector::getSupportedSteps());
return 0;
} else if (strcmp(argv[i], "--validate") == 0) {
CLI::setVerbose(true);
validate = true;
continue;
} else if (strcmp(argv[i], "--sfc") == 0) {
sfc = true;
validate = true;
CLI::setVerbose(true);
continue;
} else if (strcmp(argv[i], "-pB") == 0) {
printAML = true;
continue;
} else if (strcmp(argv[i], "-pS") == 0) {
printSFC = true;
continue;
} else if (strcmp(argv[i], "-pF") == 0) {
printFlw = true;
continue;
} else if (strcmp(argv[i], "-pO") == 0) {
printOrd = true;
continue;
} else {
if (n == 0)
in_file = argv[i];
else if (n == 1)
out_file = argv[i];
else
CLI::printUsage();
n++;
}
}
if (argc < 3) {
CLI::printUsage();
return (0);
}
if (in_file.size() == 0) {
CLI::printLine("no file specified!");
}
stringstream out;
Blackboard* blackboard = new Blackboard();
SFCFile *sfc_file = new SFCFile();
Flow *flow = new Flow();
// parse the blackboard file if specified, otherwise skip
if (!sfc) {
if (!fileexists(in_file)) {
CLI::printLine("blackboard-file '" + in_file + "' not found!");
return -1;
}
try {
blackboard = new Blackboard(in_file);
blackboard->parse(validate);
in_file = blackboard->getSFCPath();
} catch (Exception &e) {
CLI::printLine(e.getMessage());
return -1;
} catch (...) {
CLI::printLine("AN Error occurred while parsing the blackboard");
return -1;
}
if (validate && blackboard->getErrors() > 0)
out << "Blackboard parsing failed! Total of " << blackboard->getErrors() << " errors detected." << endl;
}
if (printAML)
blackboard->printResources();
if (!fileexists(in_file)) {
CLI::printLine("sfc-file '" + in_file + "' not found!");
return -1;
}
// parse the sfc file
try {
sfc_file = new SFCFile(in_file);
sfc_file->parse(validate);
} catch (Exception &e) {
CLI::printLine(e.getMessage());
return 1;
} catch (...) {
CLI::printLine("An unknown error occurred while parsing the sfc-file");
return -1;
}
if (validate && sfc_file->getErrors() > 0) {
out << "SFC parsing failed! Total of " << sfc_file->getErrors() << " errors detected." << endl;
if (sfc) {
if (printSFC)
sfc_file->printSteps();
if (printFlw) {
flow = new Flow(sfc_file, blackboard);
flow->printFlow();
}
CLI::printLine(out.str());
return -1;
}
}
if (printSFC)
sfc_file->printSteps();
if (!validate && (blackboard->getErrors() > 0 || sfc_file->getErrors() > 0 || CLI::hasErrors())) {
out << "Parsing failed" << endl;
CLI::printLine(out.str());
return -1;
}
CLI::printLine("Parsing completed");
// creating the program flow with the given sfc and blackboard file
flow = new Flow(sfc_file, blackboard);
if (validate) {
flow->check();
if (flow->getErrors() > 0 || CLI::hasErrors())
out << "Object linking failed! Total of " << flow->getErrors() << " errors detected." << endl;
}
// printing information, mainly for testing purpose
if (printFlw)
flow->printFlow();
if (printOrd)
flow->printOrder();
if (validate) {
if (blackboard->getErrors() + sfc_file->getErrors() + flow->getErrors() > 0 || CLI::hasErrors())
out << "Input validation failed!" << endl;
else
out << "Input validation successful!" << endl;
CLI::printLine("\n" + out.str());
return 0;
}
// generate code from given input
Program *prog = new Program(blackboard->getPath(), out_file);
try {
flow->process(prog);
prog->saveToFile();
blackboard->setCodeURI(out_file);
blackboard->saveToFile();
} catch (Exception &e) {
CLI::printLine(e.getMessage());
return -1;
}
return 0;
}