3 #include "signatureenc.h"
17 const char * AlloyEnc::alloyFileName = "satune.als";
18 const char * AlloyEnc::solutionFile = "solution.sol";
20 AlloyEnc::AlloyEnc(CSolver *_solver):
24 output.open(alloyFileName);
25 if(!output.is_open()){
26 model_print("AlloyEnc:Error in opening the dump file satune.als\n");
31 AlloyEnc::~AlloyEnc(){
37 void AlloyEnc::encode(){
39 SetIteratorBooleanEdge *iterator = csolver->getConstraints();
41 while(iterator->hasNext()){
42 BooleanEdge constraint = iterator->next();
43 string constr = encodeConstraint(constraint);
44 char *cstr = new char [constr.length()+1];
45 strcpy (cstr, constr.c_str());
48 output << "fact {" << endl;
49 for(uint i=0; i< facts.getSize(); i++){
50 char *cstr = facts.get(i);
54 output << "}" << endl;
58 void tokenize(string const &str, const char delim, vector<std::string> &out)
63 while ((start = str.find_first_not_of(delim, end)) != string::npos)
65 end = str.find(delim, start);
66 out.push_back(str.substr(start, end - start));
70 int AlloyEnc::getResult(){
71 ifstream input(solutionFile, ios::in);
73 while(getline(input, line)){
74 if(line.find("Unsatisfiable.")== 0){
77 if(line.find("univ") == 0){
80 if(line.find("this/AbsBool<:value") == 0 || line.find("this/AbsElement<:value=") == 0){
81 vector<string> values;
82 const char delim = ',';
83 tokenize(line, delim, values);
84 for (uint i=0; i<values.size(); i++){
86 if (3 == sscanf(values[i].c_str(),"%*[^0123456789]%u%*[^0123456789]%d%*[^0123456789]%u", &i1, &i2, &i3)){
87 model_print("Signature%u = %u\n", i1, i3);
88 sigEnc.setValue(i1, i3);
96 void AlloyEnc::dumpAlloyFooter(){
97 output << "pred show {}" << endl;
98 output << "run show for " << sigEnc.getAlloyIntScope() << " int" << endl;
101 void AlloyEnc::dumpAlloyHeader(){
102 output << "open util/integer" << endl;
105 int AlloyEnc::solve(){
107 int result = IS_INDETER;
109 if( output.is_open()){
112 snprintf(buffer, sizeof(buffer), "./run.sh java -Xmx10000m edu.mit.csail.sdg.alloy4whole.ExampleAlloyCompilerNoViz %s > %s", alloyFileName, solutionFile);
113 int status = system(buffer);
115 //Read data in from results file
116 result = getResult();
121 string AlloyEnc::encodeConstraint(BooleanEdge c){
122 Boolean *constraint = c.getBoolean();
124 switch(constraint->type){
126 res = encodeBooleanLogic((BooleanLogic *) constraint);
130 res = encodePredicate((BooleanPredicate *) constraint);
134 res = encodeBooleanVar( (BooleanVar *) constraint);
141 return "not ( " + res + " )";
146 string AlloyEnc::encodeBooleanLogic( BooleanLogic *bl){
147 uint size = bl->inputs.getSize();
149 for (uint i = 0; i < size; i++)
150 array[i] = encodeConstraint(bl->inputs.get(i));
178 for( uint i=1; i< size; i++){
179 res += op + array[i];
185 return "not ( " + array[0] + " )";
189 return "( " + array[0] + op + array[1] + " )";
197 string AlloyEnc::encodePredicate( BooleanPredicate *bp){
198 switch (bp->predicate->type) {
202 return encodeOperatorPredicate(bp);
208 string AlloyEnc::encodeBooleanVar( BooleanVar *bv){
209 BooleanSig * boolSig = sigEnc.getBooleanSignature(bv);
210 return *boolSig + " = 1";
213 string AlloyEnc::processElementFunction(ElementFunction* elemFunc, ElementSig *signature){
214 FunctionOperator *function = (FunctionOperator *) elemFunc->getFunction();
215 uint numDomains = elemFunc->inputs.getSize();
216 ASSERT(numDomains > 1);
217 ElementSig *inputs[numDomains];
219 for (uint i = 0; i < numDomains; i++) {
220 Element *elem = elemFunc->inputs.get(i);
221 inputs[i] = sigEnc.getElementSignature(elem);
222 if(elem->type == ELEMFUNCRETURN){
223 result += processElementFunction((ElementFunction *) elem, inputs[i]);
227 switch(function->op){
237 result += *signature + " = " + *inputs[0];
238 for (uint i = 1; i < numDomains; i++) {
239 result += op + "["+ *inputs[i] +"]";
245 string AlloyEnc::encodeOperatorPredicate(BooleanPredicate *constraint){
246 PredicateOperator *predicate = (PredicateOperator *) constraint->predicate;
247 ASSERT(constraint->inputs.getSize() == 2);
249 Element *elem0 = constraint->inputs.get(0);
250 ASSERT(elem0->type == ELEMSET || elem0->type == ELEMFUNCRETURN || elem0->type == ELEMCONST);
251 ElementSig *elemSig1 = sigEnc.getElementSignature(elem0);
252 if(elem0->type == ELEMFUNCRETURN){
253 str += processElementFunction((ElementFunction *) elem0, elemSig1);
255 Element *elem1 = constraint->inputs.get(1);
256 ASSERT(elem1->type == ELEMSET || elem1->type == ELEMFUNCRETURN || elem1->type == ELEMCONST);
257 ElementSig *elemSig2 = sigEnc.getElementSignature(elem1);
258 if(elem1->type == ELEMFUNCRETURN){
259 str += processElementFunction((ElementFunction *) elem1, elemSig2);
261 switch (predicate->getOp()) {
263 str += *elemSig1 + " = " + *elemSig2;
266 str += *elemSig1 + " < " + *elemSig2;
269 str += *elemSig1 + " > " + *elemSig2;
277 void AlloyEnc::writeToFile(string str){
278 output << str << endl;
281 bool AlloyEnc::getBooleanValue(Boolean *b){
282 return (bool)sigEnc.getValue(b);
285 uint64_t AlloyEnc::getValue(Element * element){
286 return (uint64_t)sigEnc.getValue(element);