Skip to content

Commit

Permalink
Better error handling in lpmln binary
Browse files Browse the repository at this point in the history
  • Loading branch information
samidhtalsania committed Oct 17, 2016
1 parent 91ed436 commit 05d6fe9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions src/includes/exceptions/NoLanguageSelectedException.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string>
#include <exception>

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

14 changes: 9 additions & 5 deletions src/lpexec_src/FileConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#include <sys/types.h>
#include <pwd.h>

#include "exceptions/NoLanguageSelectedException.h"

using namespace std;

FileConfig::FileConfig(){
map<string,string> options;
ifstream configFile;

const char *homedir;
bool fileCouldNotBeOpened = false;

if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/lpexec_src/NoLanguageSelectedException.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "exceptions/NoLanguageSelectedException.h"

NoLanguageSelectedException::~NoLanguageSelectedException(){}

NoLanguageSelectedException::NoLanguageSelectedException(std::string _str){
str = _str;
}

NoLanguageSelectedException::NoLanguageSelectedException(){}
22 changes: 20 additions & 2 deletions src/lpexec_src/lpexec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <fstream>

#include "FileConfig.h"
#include "exceptions/NoLanguageSelectedException.h"

#define SPACE " "

Expand Down Expand Up @@ -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<string,string>("input","mvsm"));
//Compile for Clingo
loptions.insert(pair<string,string>("solver","clingo"));
//max number of solutions for clingo
coptions.insert(pair<string,string>("solutions","0"));

pickUpFromCommandLine = true;

execute_cli = true;


}

uuid_t uuid;
Expand Down Expand Up @@ -181,6 +194,7 @@ int main(int argc, char **argv)
execute_cli = false;
// loptions
loptions["solver"] = "alchemy";
pickUpFromCommandLine = false;
continue;
}

Expand All @@ -190,6 +204,7 @@ int main(int argc, char **argv)
execute_alch = false;
// loptions.insert(pair<string,string>("solver","clingo"));
loptions["solver"] = "clingo";
pickUpFromCommandLine = false;
continue;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<<lpmln;

if(!isEvidenceUsed && execute_alch == true){
//Make empty db
Expand Down

0 comments on commit 05d6fe9

Please sign in to comment.