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 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);
115 return "not ( " + res + " )";
120 string AlloyEnc::encodeBooleanLogic( BooleanLogic *bl){
121 uint size = bl->inputs.getSize();
123 for (uint i = 0; i < size; i++)
124 array[i] = encodeConstraint(bl->inputs.get(i));
130 for( uint i=1; i< size; i++){
131 res += " and " + array[i];
136 return "not " + array[0];
139 return array[0] + " iff " + array[1];
149 string AlloyEnc::encodePredicate( BooleanPredicate *bp){
150 switch (bp->predicate->type) {
154 return encodeOperatorPredicate(bp);
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()) {
171 return *elemSig1 + " = " + *elemSig2;
173 return *elemSig1 + " < " + *elemSig2;
175 return *elemSig1 + " > " + *elemSig2;
182 void AlloyEnc::writeToFile(string str){
183 output << str << endl;
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);