Adding timeout for Alloy interpreter
[satune.git] / src / AlloyEnc / signatureenc.cc
1 #include "signatureenc.h"
2 #include "element.h"
3 #include "set.h"
4 #include "signature.h"
5 #include "alloyenc.h"
6 #include "math.h"
7
8 SignatureEnc::SignatureEnc(AlloyEnc *ae): 
9         alloyEncoder(ae),
10         maxValue(0)
11 {
12 }
13
14 SignatureEnc::~SignatureEnc(){
15         for(uint i=0; i<signatures.getSize(); i++){
16                 Signature *s = signatures.get(i);
17                 delete s;
18         }
19 }
20
21 int SignatureEnc::getAlloyIntScope(){
22         double mylog = log2(maxValue + 1);
23         return floor(mylog) == mylog ? (int)mylog + 1: (int)mylog + 2;
24 }
25
26 void SignatureEnc::updateMaxValue(Set *set){
27         for(uint i=0; i< set->getSize(); i++){
28                 if(set->getElement(i) > maxValue){
29                         maxValue = set->getElement(i);
30                 }
31         }
32 }
33
34 BooleanSig *SignatureEnc::getBooleanSignature(Boolean *bvar){
35         BooleanSig *bsig = (BooleanSig *)encoded.get((void *)bvar);
36         if(bsig == NULL){
37                 bsig = new BooleanSig(getUniqueSigID());
38                 encoded.put(bvar, bsig);
39                 signatures.push(bsig);
40                 alloyEncoder->writeToFile(bsig->getSignature());
41         }
42         return bsig;
43 }
44
45 ElementSig *SignatureEnc::getElementSignature(Element *element){
46         ElementSig *esig = (ElementSig *)encoded.get((void *)element);
47         if(esig == NULL){
48                 Set *set = element->getRange();
49                 SetSig *ssig = (SetSig *)encoded.get((void *)set);
50                 if(ssig == NULL){
51                         ssig = new SetSig(getUniqueSigID(), set);
52                         encoded.put(set, ssig);
53                         signatures.push(ssig);
54                         alloyEncoder->writeToFile(ssig->getSignature());
55                         updateMaxValue(set);
56                 }
57                 esig = new ElementSig(getUniqueSigID(), ssig);
58                 encoded.put(element, esig);
59                 signatures.push(esig);
60                 alloyEncoder->writeToFile(esig->getSignature());
61
62         }
63         return esig;
64 }
65
66 void SignatureEnc::setValue(uint id, uint value){
67         ValuedSignature *sig = getValuedSignature(id);
68         ASSERT(sig != NULL);
69         sig->setValue(value);
70 }
71
72 int SignatureEnc::getValue(void *astnode){
73         ValuedSignature *sig = (ValuedSignature *)encoded.get(astnode);
74         ASSERT(sig != NULL);
75         return sig->getValue();
76 }