From 05d6fe937a7702bca64aa1421eff08680e06c1b6 Mon Sep 17 00:00:00 2001 From: Samidh Talsania Date: Mon, 17 Oct 2016 16:42:53 -0700 Subject: [PATCH] Better error handling in lpmln binary --- src/Makefile.am | 3 ++- .../exceptions/NoLanguageSelectedException.h | 19 ++++++++++++++++ src/lpexec_src/FileConfig.cpp | 14 +++++++----- .../NoLanguageSelectedException.cpp | 9 ++++++++ src/lpexec_src/lpexec.cpp | 22 +++++++++++++++++-- 5 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/includes/exceptions/NoLanguageSelectedException.h create mode 100644 src/lpexec_src/NoLanguageSelectedException.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 4185cb6..6d2f079 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -37,4 +37,5 @@ lpmln_LDADD = -luuid lpmln_SOURCES = \ lpexec_src/lpexec.cpp \ - lpexec_src/FileConfig.cpp + lpexec_src/FileConfig.cpp \ + lpexec_src/NoLanguageSelectedException.cpp diff --git a/src/includes/exceptions/NoLanguageSelectedException.h b/src/includes/exceptions/NoLanguageSelectedException.h new file mode 100644 index 0000000..b057689 --- /dev/null +++ b/src/includes/exceptions/NoLanguageSelectedException.h @@ -0,0 +1,19 @@ +#include +#include + +#pragma once + +class NoLanguageSelectedException : public std::exception{ + public: + NoLanguageSelectedException(std::string); + NoLanguageSelectedException(); + ~NoLanguageSelectedException(); + + const char* what() const throw(){ + return str.c_str(); + } + + private: + std::string str; +}; + diff --git a/src/lpexec_src/FileConfig.cpp b/src/lpexec_src/FileConfig.cpp index 04e81cd..828324c 100644 --- a/src/lpexec_src/FileConfig.cpp +++ b/src/lpexec_src/FileConfig.cpp @@ -9,6 +9,8 @@ #include #include +#include "exceptions/NoLanguageSelectedException.h" + using namespace std; FileConfig::FileConfig(){ @@ -16,6 +18,7 @@ FileConfig::FileConfig(){ ifstream configFile; const char *homedir; + bool fileCouldNotBeOpened = false; if ((homedir = getenv("HOME")) == NULL) { homedir = getpwuid(getuid())->pw_dir; @@ -29,9 +32,10 @@ FileConfig::FileConfig(){ #ifdef DEBUG cout<<"Cannot read from config file\n"; #endif - ofstream file; - file.open(filename); - file.close(); + // ofstream file; + // file.open(filename); + // file.close(); + fileCouldNotBeOpened = true; } // Set defaults @@ -184,8 +188,8 @@ FileConfig::FileConfig(){ } } - if(!execute_alch && !execute_cli){ - throw; + if(!execute_alch && !execute_cli || fileCouldNotBeOpened){ + throw NoLanguageSelectedException("No language selected from config File. Check to see if config file has the right syntax."); } //Parsing done diff --git a/src/lpexec_src/NoLanguageSelectedException.cpp b/src/lpexec_src/NoLanguageSelectedException.cpp new file mode 100644 index 0000000..8615766 --- /dev/null +++ b/src/lpexec_src/NoLanguageSelectedException.cpp @@ -0,0 +1,9 @@ +#include "exceptions/NoLanguageSelectedException.h" + +NoLanguageSelectedException::~NoLanguageSelectedException(){} + +NoLanguageSelectedException::NoLanguageSelectedException(std::string _str){ + str = _str; +} + +NoLanguageSelectedException::NoLanguageSelectedException(){} \ No newline at end of file diff --git a/src/lpexec_src/lpexec.cpp b/src/lpexec_src/lpexec.cpp index 09d08f0..d38f0b2 100644 --- a/src/lpexec_src/lpexec.cpp +++ b/src/lpexec_src/lpexec.cpp @@ -10,6 +10,7 @@ #include #include "FileConfig.h" +#include "exceptions/NoLanguageSelectedException.h" #define SPACE " " @@ -129,9 +130,21 @@ int main(int argc, char **argv) execute_cli = cf.getExecuteCli(); execute_tuf = false; } - catch(...){ + catch(NoLanguageSelectedException ex){ // No solver is specified in the config file. Need to pick it up from command line. + + //Default for lpmln: Solver is MLN, Input is MVSM + loptions.insert(pair("input","mvsm")); + //Compile for Clingo + loptions.insert(pair("solver","clingo")); + //max number of solutions for clingo + coptions.insert(pair("solutions","0")); + pickUpFromCommandLine = true; + + execute_cli = true; + + } uuid_t uuid; @@ -181,6 +194,7 @@ int main(int argc, char **argv) execute_cli = false; // loptions loptions["solver"] = "alchemy"; + pickUpFromCommandLine = false; continue; } @@ -190,6 +204,7 @@ int main(int argc, char **argv) execute_alch = false; // loptions.insert(pair("solver","clingo")); loptions["solver"] = "clingo"; + pickUpFromCommandLine = false; continue; } @@ -302,6 +317,10 @@ int main(int argc, char **argv) lpmln += inputFileName; + if(pickUpFromCommandLine){ + std::cout<<"No input language and solver specified in command line and config file.\nUsing ASP Solver.\n"; + } + for (auto it=aoptions.begin(); it!=aoptions.end(); ++it){ string str = it->first; @@ -330,7 +349,6 @@ int main(int argc, char **argv) lpmln += SPACE + string(">") + SPACE + string("/tmp/") + fileName; std::cout<<"Lpmln Executed with Command:\n"+lpmln+"\n"; runProcess(lpmln); - // std::cout<