Blame view

src/expressionParser/Main.cc 4.35 KB
e3ca4c3f   Benjamin Renard   adding structure ...
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * 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>

a7f2648e   Benjamin Renard   Parser: First imp...
13
14
#include "log4cxx/logger.h"
#include "DicError.hh"
c1f4db8e   Benjamin Renard   Add process to te...
15
#include "Helper.hh"
a7f2648e   Benjamin Renard   Parser: First imp...
16

e3ca4c3f   Benjamin Renard   adding structure ...
17
18
// Other includes
#include "ExpressionParser.hh"
aae52524   Benjamin Renard   Parser: Write res...
19
#include "ParserResultFileWriter.hh"
e3ca4c3f   Benjamin Renard   adding structure ...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40


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")
a7f2648e   Benjamin Renard   Parser: First imp...
41
		("expression,e", po::value< vector<string> >(), "expression(s) to parse")
c1f4db8e   Benjamin Renard   Add process to te...
42
		("testfile,t", po::value< std::string >(), "CSV test file")
aae52524   Benjamin Renard   Parser: Write res...
43
		("output,o", po::value< std::string >(), "Output XML file")
e3ca4c3f   Benjamin Renard   adding structure ...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	;

	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;
	}

c1f4db8e   Benjamin Renard   Add process to te...
62
63
	bool testRunning = false;
	std::string testFile = "";
e3ca4c3f   Benjamin Renard   adding structure ...
64
	if (!vm.count("expression")) {
c1f4db8e   Benjamin Renard   Add process to te...
65
66
67
68
69
70
71
72
73
		if (vm.count("testfile")) {
			testFile = vm["testfile"].as<std::string>();
			testRunning = true;
		}
		else {
			return result;
		}
	}

aae52524   Benjamin Renard   Parser: Write res...
74
75
76
77
78
	std::string outputFile = "";
	if (vm.count("output")) {
		outputFile = vm["output"].as<std::string>();
	}

c1f4db8e   Benjamin Renard   Add process to te...
79
80
81
82
83
84
85
	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;
		}
e3ca4c3f   Benjamin Renard   adding structure ...
86
87
88
89
90
	}

	AMDA::Common::Application lMain;

	return lMain.main(argc,argv,[&](int , char **, AMDA::helpers::Properties& lProperties) -> int {
aae52524   Benjamin Renard   Parser: Write res...
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
		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);
			}
		}
e3ca4c3f   Benjamin Renard   adding structure ...
118

aae52524   Benjamin Renard   Parser: Write res...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
		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;
			}
		}
e3ca4c3f   Benjamin Renard   adding structure ...
139

aae52524   Benjamin Renard   Parser: Write res...
140
141
142
143
144
145
146
147
148
		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)
			}
		}
e3ca4c3f   Benjamin Renard   adding structure ...
149

aae52524   Benjamin Renard   Parser: Write res...
150
		return result;
4e5d4e69   Benjamin Renard   Skip plugin load ...
151
	}, true); //Skip plugin load
e3ca4c3f   Benjamin Renard   adding structure ...
152
}