Bug Fix: defining the scope of integer for Alloy
[satune.git] / src / AlloyEnc / alloyenc.cc
1 #include "alloyenc.h"
2 #include <string>
3 #include "signatureenc.h"
4 #include "structs.h"
5 #include "csolver.h"
6 #include "boolean.h"
7 #include "predicate.h"
8 #include "element.h"
9 #include "signature.h"
10 #include "set.h"
11 #include <fstream>
12 #include <regex>
13
14 using namespace std;
15
16 const char * AlloyEnc::alloyFileName = "satune.als";
17 const char * AlloyEnc::solutionFile = "solution.sol";
18
19 AlloyEnc::AlloyEnc(CSolver *_solver): 
20         csolver(_solver),
21         sigEnc(this)
22 {
23         output.open(alloyFileName);
24         if(!output.is_open()){
25                 model_print("AlloyEnc:Error in opening the dump file satune.als\n");
26                 exit(-1);
27         }
28 }
29
30 AlloyEnc::~AlloyEnc(){
31         if(output.is_open()){
32                 output.close();
33         }
34 }
35
36 void AlloyEnc::encode(){
37         SetIteratorBooleanEdge *iterator = csolver->getConstraints();
38         Vector<char *> facts;
39         while(iterator->hasNext()){
40                 BooleanEdge constraint = iterator->next();
41                 string constr = encodeConstraint(constraint);
42                 //model_print("constr=%s\n", constr.c_str());
43                 char *cstr = new char [constr.length()+1];
44                 strcpy (cstr, constr.c_str());
45                 facts.push(cstr);
46         }
47         output << "fact {" << endl;
48         for(uint i=0; i< facts.getSize(); i++){
49                 char *cstr = facts.get(i);
50                 writeToFile(cstr);
51                 delete[] cstr;
52         }
53         output << "}" << endl;
54         delete iterator;
55 }
56
57 int AlloyEnc::getResult(){
58         ifstream input(solutionFile, ios::in);
59         string line;
60         while(getline(input, line)){
61                 if(regex_match(line, regex("Unsatisfiable."))){
62                         return IS_UNSAT;
63                 }
64                 if(regex_match(line, regex(".*Element\\d+.*value=.*Element\\d+.*->\\d+.*"))){
65                         int tmp=0, index=0, value=0;
66                         const char* s = line.c_str();
67                         uint i1, i2, i3;
68                         uint64_t i4;
69                         if (4 == sscanf(s,"%*[^0123456789]%u%*[^0123456789]%d%*[^0123456789]%d%*[^0123456789]%" PRId64 "", &i1, &i2, &i3, &i4)){
70                                 model_print("Element%d = %" PRId64 "\n", i1, i4);
71                                 sigEnc.setValue(i1, i4);
72                         }
73                 }
74         }
75         return IS_SAT;
76 }
77
78 void AlloyEnc::dumpAlloyIntScope(){
79         output << "pred show {}" << endl;
80         output << "run show for " << sigEnc.getAlloyIntScope() << " int" << endl;
81 }
82
83 int AlloyEnc::solve(){
84         dumpAlloyIntScope();
85         int result = IS_INDETER;
86         char buffer [512];
87         if( output.is_open()){
88                 output.close();
89         }
90         snprintf(buffer, sizeof(buffer), "./run.sh java edu.mit.csail.sdg.alloy4whole.ExampleAlloyCompilerNoViz %s > %s", alloyFileName, solutionFile);
91         int status = system(buffer);
92         if (status == 0) {
93                 //Read data in from results file
94                 result = getResult();
95         }
96         return result;
97 }
98
99 string AlloyEnc::encodeConstraint(BooleanEdge c){
100         Boolean *constraint = c.getBoolean();
101         string res;
102         switch(constraint->type){
103                 case LOGICOP:{
104                         res = encodeBooleanLogic((BooleanLogic *) constraint);
105                         break;
106                 }
107                 case PREDICATEOP:{
108                         res = encodePredicate((BooleanPredicate *) constraint);
109                         break;
110                 }
111                 default:
112                         ASSERT(0);
113         }
114         if(c.isNegated()){
115                 return "not ( " + res + " )";
116         }
117         return res;
118 }
119
120 string AlloyEnc::encodeBooleanLogic( BooleanLogic *bl){
121         uint size = bl->inputs.getSize();
122         string array[size];
123         for (uint i = 0; i < size; i++)
124                 array[i] = encodeConstraint(bl->inputs.get(i));
125         switch (bl->op) {
126                 case SATC_AND:{
127                         ASSERT(size >= 2);
128                         string res = "";
129                         res += array[0];
130                         for( uint i=1; i< size; i++){
131                                 res += " and " + array[i];
132                         }
133                         return res;
134                 }
135                 case SATC_NOT:{
136                         return "not " + array[0];
137                 }
138                 case SATC_IFF:
139                         return array[0] + " iff " + array[1];
140                 case SATC_OR:
141                 case SATC_XOR:
142                 case SATC_IMPLIES:
143                 default:
144                         ASSERT(0);
145
146         }
147 }
148
149 string AlloyEnc::encodePredicate( BooleanPredicate *bp){
150         switch (bp->predicate->type) {
151                 case TABLEPRED:
152                         ASSERT(0);
153                 case OPERATORPRED:
154                         return encodeOperatorPredicate(bp);
155                 default:
156                         ASSERT(0);
157         }
158 }
159
160 string AlloyEnc::encodeOperatorPredicate(BooleanPredicate *constraint){
161         PredicateOperator *predicate = (PredicateOperator *) constraint->predicate;
162         ASSERT(constraint->inputs.getSize() == 2);
163         Element *elem0 = constraint->inputs.get(0);
164         ASSERT(elem0->type = ELEMSET);
165         ElementSig *elemSig1 = sigEnc.getElementSignature(elem0);
166         Element *elem1 = constraint->inputs.get(1);
167         ASSERT(elem1->type = ELEMSET);
168         ElementSig *elemSig2 = sigEnc.getElementSignature(elem1);
169         switch (predicate->getOp()) {
170                 case SATC_EQUALS:
171                         return *elemSig1 + " = " + *elemSig2;
172                 case SATC_LT:
173                         return *elemSig1 + " < " + *elemSig2;
174                 case SATC_GT:
175                         return *elemSig1 + " > " + *elemSig2; 
176                 default:
177                         ASSERT(0);
178         }
179         exit(-1);
180 }
181
182 void AlloyEnc::writeToFile(string str){
183         output << str << endl;
184 }
185
186 uint64_t AlloyEnc::getValue(Element * element){
187         ElementEncoding *elemEnc = element->getElementEncoding();
188         if (elemEnc->numVars == 0)//case when the set has only one item
189                 return element->getRange()->getElement(0);
190         return sigEnc.getValue(element);
191 }
192