Main.cc 4.35 KB
/**
 * 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
}