3 #include "signatureenc.h"
15 const char * AlloyEnc::alloyFileName = "satune.als";
16 const char * AlloyEnc::solutionFile = "solution.sol";
18 AlloyEnc::AlloyEnc(CSolver *_solver):
22 output.open(alloyFileName);
23 if(!output.is_open()){
24 model_print("AlloyEnc:Error in opening the dump file satune.als\n");
29 AlloyEnc::~AlloyEnc(){
35 void AlloyEnc::encode(){
36 SetIteratorBooleanEdge *iterator = csolver->getConstraints();
38 while(iterator->hasNext()){
39 BooleanEdge constraint = iterator->next();
40 string constr = encodeConstraint(constraint);
41 //model_print("constr=%s\n", constr.c_str());
42 char *cstr = new char [constr.length()+1];
43 strcpy (cstr, constr.c_str());
46 output << "fact {" << endl;
47 for(uint i=0; i< facts.getSize(); i++){
48 char *cstr = facts.get(i);
52 output << "}" << endl;
56 int AlloyEnc::getResult(){
57 ifstream input(solutionFile, ios::in);
59 while(getline(input, line)){
60 if(regex_match(line, regex("Unsatisfiable."))){
63 if(regex_match(line, regex(".*Element\\d+.*value=.*Element\\d+.*->\\d+.*"))){
64 int tmp=0, index=0, value=0;
65 const char* s = line.c_str();
68 if (4 == sscanf(s,"%*[^0123456789]%u%*[^0123456789]%d%*[^0123456789]%d%*[^0123456789]%" PRId64 "", &i1, &i2, &i3, &i4)){
69 model_print("Element%d = %" PRId64 "\n", i1, i4);
70 sigEnc.setValue(i1, i4);
77 int AlloyEnc::solve(){
78 int result = IS_INDETER;
80 if( output.is_open()){
83 snprintf(buffer, sizeof(buffer), "./run.sh java edu.mit.csail.sdg.alloy4whole.ExampleAlloyCompilerNoViz %s > %s", alloyFileName, solutionFile);
84 int status = system(buffer);
86 //Read data in from results file
92 string AlloyEnc::encodeConstraint(BooleanEdge c){
93 Boolean *constraint = c.getBoolean();
95 switch(constraint->type){
97 res = encodeBooleanLogic((BooleanLogic *) constraint);
101 res = encodePredicate((BooleanPredicate *) constraint);
108 return "not ( " + res + " )";
113 string AlloyEnc::encodeBooleanLogic( BooleanLogic *bl){
114 uint size = bl->inputs.getSize();
116 for (uint i = 0; i < size; i++)
117 array[i] = encodeConstraint(bl->inputs.get(i));
123 for( uint i=1; i< size; i++){
124 res += " and " + array[i];
129 return "not " + array[0];
132 return array[0] + " iff " + array[1];
142 string AlloyEnc::encodePredicate( BooleanPredicate *bp){
143 switch (bp->predicate->type) {
147 return encodeOperatorPredicate(bp);
153 string AlloyEnc::encodeOperatorPredicate(BooleanPredicate *constraint){
154 PredicateOperator *predicate = (PredicateOperator *) constraint->predicate;
155 ASSERT(constraint->inputs.getSize() == 2);
156 Element *elem0 = constraint->inputs.get(0);
157 ASSERT(elem0->type = ELEMSET);
158 ElementSig *elemSig1 = sigEnc.getElementSignature(elem0);
159 Element *elem1 = constraint->inputs.get(1);
160 ASSERT(elem1->type = ELEMSET);
161 ElementSig *elemSig2 = sigEnc.getElementSignature(elem1);
162 switch (predicate->getOp()) {
164 return *elemSig1 + " = " + *elemSig2;
166 return *elemSig1 + " < " + *elemSig2;
168 return *elemSig1 + " > " + *elemSig2;
175 void AlloyEnc::writeToFile(string str){
176 output << str << endl;
179 uint64_t AlloyEnc::getValue(Element * element){
180 return sigEnc.getValue(element);