Main.cc
4.35 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
/**
* Main.cc
* Created on: 15 oct. 2012
* Author: AKKA IS
*/
#include <iostream>
#include <boost/program_options.hpp>
#include "AMDA-Kernel_Config.hh"
#include <Application.hh>
#include "log4cxx/logger.h"
#include "DicError.hh"
#include "Helper.hh"
// Other includes
#include "ExpressionParser.hh"
#include "ParserResultFileWriter.hh"
using namespace std;
namespace po = boost::program_options;
using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr logger(Logger::getLogger("AMDA-Kernel"));
/**
* Main function
*/
int main(int argc, char *argv[]) {
int result = AMDA_EXIT_OK;
/// Parse command line
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "Produce help message")
("version,v", "Program version")
("expression,e", po::value< vector<string> >(), "expression(s) to parse")
("testfile,t", po::value< std::string >(), "CSV test file")
("output,o", po::value< std::string >(), "Output XML file")
;
po::positional_options_description p;
p.add("expression", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return result;
}
if (vm.count("version")) {
cout << "Version: " << AMDA_Kernel_VERSION << "\n";
return result;
}
bool testRunning = false;
std::string testFile = "";
if (!vm.count("expression")) {
if (vm.count("testfile")) {
testFile = vm["testfile"].as<std::string>();
testRunning = true;
}
else {
return result;
}
}
std::string outputFile = "";
if (vm.count("output")) {
outputFile = vm["output"].as<std::string>();
}
std::vector<std::vector<std::string> > testExpressions;
if (testRunning)
{
testExpressions = AMDA::Helpers::Helper::loadCSVFile(testFile.c_str(), ';');
if (testExpressions.empty()) {
cout << "Cannot load test file or is empty: " << testFile << std::endl;
}
}
AMDA::Common::Application lMain;
return lMain.main(argc,argv,[&](int , char **, AMDA::helpers::Properties& lProperties) -> int {
std::vector<AMDA::parser::ParserResultFileWriter::ParserResult> parserResults;
std::vector<std::string> waitingResults; //Only used in test running mode
if (testRunning) {
//Expressions from test CSV file
for (std::vector<std::vector<std::string>>::iterator it = testExpressions.begin(); it != testExpressions.end(); ++it) {
if (it == testExpressions.begin()) {
//Skip header line
continue;
}
if ((*it).size() >= 2) {
AMDA::parser::ParserResultFileWriter::ParserResult parserRes;
parserRes.ihmExpression = (*it)[0];
parserResults.push_back(parserRes);
waitingResults.push_back((*it)[1]);
}
}
}
else {
//Expressions from command line
std::vector<std::string> lExpressionList = vm["expression"].as< std::vector<string> >();
for(auto expression :lExpressionList) {
AMDA::parser::ParserResultFileWriter::ParserResult parserRes;
parserRes.ihmExpression = expression;
parserResults.push_back(parserRes);
}
}
std::vector<std::string>::iterator itWaitingRes = waitingResults.begin(); //Only used in test running mode
for(auto &parserRes : parserResults){
LOG4CXX_INFO(logger,"Parsing expression: " << parserRes.ihmExpression);
if (AMDA::parser::ExpressionParser::parse(parserRes.ihmExpression, lProperties, parserRes.kernelExpression, parserRes.paramsList)) {
parserRes.parseSuccess = true;
LOG4CXX_INFO(logger,"Success: Kernel expression is: " << parserRes.kernelExpression << ",and " << parserRes.paramsList.size() << " param(s) found")
if (testRunning && (parserRes.kernelExpression.compare(*itWaitingRes) != 0)) {
LOG4CXX_ERROR(logger, parserRes.ihmExpression << " parsed as : " << parserRes.kernelExpression << " but waiting result is : " << (*itWaitingRes))
parserRes.parseSuccess = false;
}
}
else {
LOG4CXX_ERROR(logger, "Error occured during parse of: " << parserRes.ihmExpression)
parserRes.parseSuccess = false;
}
if (testRunning) {
++itWaitingRes;
}
}
if (!outputFile.empty()) {
//Write result file
if (!AMDA::parser::ParserResultFileWriter::write(outputFile.c_str(), parserResults)) {
LOG4CXX_ERROR(logger, "Cannot write result file")
}
else {
LOG4CXX_INFO(logger,"Success: Result file written : " << outputFile)
}
}
return result;
}, true); //Skip plugin load
}