Skip to content

Commit

Permalink
Merged old-state with current branch. Made MVSM input syntax more all…
Browse files Browse the repository at this point in the history
…igned with f2lp syntax
  • Loading branch information
samidhtalsania committed Nov 30, 2016
2 parents 76e1145 + 1bcc41b commit 6a219ed
Show file tree
Hide file tree
Showing 4 changed files with 861 additions and 15 deletions.
83 changes: 68 additions & 15 deletions src/lpmln2wc_src/lpmln2wc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,28 @@ int main(int argc, char **argv){

ifstream file(argv[1], ios_base::in | ios_base::binary);

int mode = 0;
// int mode = 0;

if(argc == 3)
mode = *argv[2] - '0';
// if(argc == 3)
// mode = *argv[2] - '0';

int unsatcount = 0;
string str;

//Default mode is 0, which translates all rules.
//Mode 1 translates only soft rules.
int mode = 0;

if(argc >= 3){
std::stringstream ss(argv[2]);
ss >> mode;
if(mode != 0 && mode != 1){
cout << "Invalid 3rd argument. Can be either 0 or 1.\n";
exit(1);
}

}

if(file){

try{
Expand All @@ -116,6 +130,8 @@ int main(int argc, char **argv){
bool f = false;
for(str; getline(in, str); ){

if(str.length() == 0) continue;

if(str[0] == '%'){
//ignore comments
cout<<str+"\n";
Expand Down Expand Up @@ -147,18 +163,19 @@ int main(int argc, char **argv){
}

trim(str);

//splits the rule to head and body
vector<string> splitVec;


boost::algorithm::split_regex(splitVec, str, regex(":-"));
//splits the head into weight part and rule part
vector<string> splitVecSpace;
int weight = 0;


if(splitVec.size() == 1){
//process it if its of the form
// W H.
// W H. or H.

split(splitVecSpace, splitVec[0], is_any_of(" "), token_compress_on);
try{
Expand Down Expand Up @@ -197,8 +214,31 @@ int main(int argc, char **argv){
unsatcount++;
}
catch(const std::exception& ex){
// It could be of type H. Process it.

if(mode == 1){
//Hard facts are untouched in Mode 1
cout << str + "\n";
continue;
}

//Remove . at the end
str.pop_back();

cout << str + "\n";
set<string> s = findVariables(str);
string vars;
for(auto itr = s.begin(); itr!=s.end();++itr)
vars += *itr + ",";
if(vars.length() != 0){
vars.pop_back();
vars = "," + vars;
}

string tempstr = "unsat("+to_string(unsatcount)+vars+") :- not " + str + ".\n" +
str + " :- not unsat(" + to_string(unsatcount) +vars+ ").\n" +
":~ unsat(" + to_string(unsatcount) +vars+"). [1@1," + to_string(unsatcount) + vars+ "]";
unsatcount++;
cout << tempstr + "\n";
}


Expand All @@ -217,10 +257,12 @@ int main(int argc, char **argv){
}
string tempstr;
string weightString;
set<string> s;
set<string> s;
bool issoft = false;
try{
weight = (int)(stof(splitVecSpace[0]));
weightString = to_string(weight) + "@0";
issoft = true;
s = findFreeVariables(newStr, splitVec[1]);
}
catch (const std::invalid_argument& ia) {
Expand Down Expand Up @@ -254,19 +296,30 @@ int main(int argc, char **argv){
tempstr = ":~ "+ splitVec[1]+ "." + "["+ weightString +","+ to_string(unsatcount)+ vars +"]\n" ;

}
// Hard rules/constraints are ignored.
else{
if(newStr.empty())
tempstr = ":~ "+ splitVec[1]+ "." + "["+ weightString +","+ to_string(unsatcount)+ vars +"]\n" ;
else
if(mode == 1)
tempstr = "unsat(" + to_string(unsatcount) + vars + ") :-" + splitVec[1] + ",not " + newStr + ".\n" + newStr + ":-" + splitVec[1] + ", " + "not unsat(" + to_string(unsatcount) + vars + ")" + ".\n"+":~" + "unsat(" + to_string(unsatcount) + vars +")." + " [" + weightString + ","+ to_string(unsatcount)+vars +"]\n";
else
tempstr = str;
if(newStr.empty()){
//if soft rule translate it irrespective of the mode.
if(issoft || (!issoft && mode == 0))
tempstr = ":~ "+ splitVec[1]+ "." + "["+ weightString +","+ to_string(unsatcount)+ vars +"]\n" ;
else{
tempstr = str + "\n";
}
}
else if(issoft || (!issoft && mode == 0)){
//Soft rules are translated irespective of the mode
//Hard rules are translated only in mode 0
tempstr = "unsat(" + to_string(unsatcount) + vars + ") :-" + splitVec[1] + ",not " + newStr + ".\n" + newStr + ":-" + splitVec[1] + ", " + "not unsat(" + to_string(unsatcount) + vars + ")" + ".\n"+":~" + "unsat(" + to_string(unsatcount) + vars +")." + " [" + weightString + ","+ to_string(unsatcount)+vars +"]\n";
}
else if(!issoft && mode == 1){
//Hard rules are translated only in mode 0
// cout << str + "\n";
tempstr = str + "\n";
}
}
cout<<tempstr;
unsatcount++;
}

}
}
catch(const std::exception& ia) {
Expand Down
Loading

0 comments on commit 6a219ed

Please sign in to comment.