3 #include "signatureenc.h"
16 const char * AlloyEnc::alloyFileName = "satune.als";
17 const char * AlloyEnc::solutionFile = "solution.sol";
19 AlloyEnc::AlloyEnc(CSolver *_solver):
23 output.open(alloyFileName);
24 if(!output.is_open()){
25 model_print("AlloyEnc:Error in opening the dump file satune.als\n");
30 AlloyEnc::~AlloyEnc(){
36 void AlloyEnc::encode(){
37 SetIteratorBooleanEdge *iterator = csolver->getConstraints();
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());
47 output << "fact {" << endl;
48 for(uint i=0; i< facts.getSize(); i++){
49 char *cstr = facts.get(i);
53 output << "}" << endl;
57 int AlloyEnc::getResult(){
58 ifstream input(solutionFile, ios::in);
60 while(getline(input, line)){
61 if(regex_match(line, regex("Unsatisfiable."))){
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();
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);
78 void AlloyEnc::dumpAlloyIntScope(){
79 output << "pred show {}" << endl;
80 output << "run show for " << sigEnc.getAlloyIntScope() << " int" << endl;
83 int AlloyEnc::solve(){
85 int result = IS_INDETER;
87 if( output.is_open()){
90 snprintf(buffer, sizeof(buffer), "./run.sh java -Xmx10000m edu.mit.csail.sdg.alloy4whole.ExampleAlloyCompilerNoViz %s > %s", alloyFileName, solutionFile);
91 int status = system(buffer);
93 //Read data in from results file
99 string AlloyEnc::encodeConstraint(BooleanEdge c){
100 Boolean *constraint = c.getBoolean();
102 switch(constraint->type){
104 res = encodeBooleanLogic((BooleanLogic *) constraint);
108 res = encodePredicate((BooleanPredicate *) constraint);
112 res = encodeBooleanVar( (BooleanVar *) constraint);
119 return "not ( " + res + " )";
124 string AlloyEnc::encodeBooleanLogic( BooleanLogic *bl){
125 uint size = bl->inputs.getSize();
127 for (uint i = 0; i < size; i++)
128 array[i] = encodeConstraint(bl->inputs.get(i));
134 for( uint i=1; i< size; i++){
135 res += " and " + array[i];
140 return "not " + array[0];
143 return array[0] + " iff " + array[1];
153 string AlloyEnc::encodePredicate( BooleanPredicate *bp){
154 switch (bp->predicate->type) {
158 return encodeOperatorPredicate(bp);
164 string AlloyEnc::encodeBooleanVar( BooleanVar *bv){
165 BooleanSig * boolSig = sigEnc.getBooleanSignature(bv);
166 return *boolSig + " = 1";
169 string AlloyEnc::encodeOperatorPredicate(BooleanPredicate *constraint){
170 PredicateOperator *predicate = (PredicateOperator *) constraint->predicate;
171 ASSERT(constraint->inputs.getSize() == 2);
172 Element *elem0 = constraint->inputs.get(0);
173 ASSERT(elem0->type = ELEMSET);
174 ElementSig *elemSig1 = sigEnc.getElementSignature(elem0);
175 Element *elem1 = constraint->inputs.get(1);
176 ASSERT(elem1->type = ELEMSET);
177 ElementSig *elemSig2 = sigEnc.getElementSignature(elem1);
178 switch (predicate->getOp()) {
180 return *elemSig1 + " = " + *elemSig2;
182 return *elemSig1 + " < " + *elemSig2;
184 return *elemSig1 + " > " + *elemSig2;
191 void AlloyEnc::writeToFile(string str){
192 output << str << endl;
195 bool AlloyEnc::getBooleanValue(Boolean *b){
196 if (b->boolVal == BV_MUSTBETRUE)
198 else if (b->boolVal == BV_MUSTBEFALSE)
200 return sigEnc.getBooleanSignature(b);
203 uint64_t AlloyEnc::getValue(Element * element){
204 ElementEncoding *elemEnc = element->getElementEncoding();
205 if (elemEnc->numVars == 0)//case when the set has only one item
206 return element->getRange()->getElement(0);
207 return sigEnc.getValue(element);