3 #include "fieldcheck.h"
10 #include "normalizer.h"
12 FieldCheck::FieldCheck(model *m) {
19 bool FieldCheck::testsatisfy(WorkSet *satisfies, FieldTuple *ft) {
20 FieldTuple *copy=new FieldTuple(ft);
22 while(!sat&©!=NULL) {
23 if(satisfies->contains(copy)) {
27 copy->set=globalmodel->getdomainrelation()->getsuperset(globalmodel->getdomainrelation()->getset(copy->set))->getname();
33 int FieldCheck::getindexthatprovides(char *set, char *relation) {
34 FieldTuple *copy=new FieldTuple(set,relation);
36 if(propertytonf->contains(copy)) {
37 /* Found!!! Do stuff */
38 NormalForm *nf=(NormalForm *)propertytonf->get(copy);
39 for(int i=0;i<globalmodel->getnumconstraints();i++) {
40 if (nf==globalmodel->getnormalform(i)) {
45 printf("Error in getindexthatprovides\n");
48 copy->set=globalmodel->getdomainrelation()->getsuperset(globalmodel->getdomainrelation()->getset(copy->set))->getname();
51 return -1; /* Couldn't find...too bad */
54 void FieldCheck::analyze() {
55 WorkSet *satisfied=new WorkSet();
56 propertytonf=new Hashtable(NULL,NULL);
58 bool allsatisfied=true;
59 FieldTuple *badft=NULL;
63 for(int i=0;i<globalmodel->getnumconstraints();i++) {
64 NormalForm *nf=globalmodel->getnormalform(i);
66 for(int j=0;j<nf->getnumsentences();j++) {
67 CoerceSentence *cs=nf->getsentence(j);
68 for (int k=0;k<cs->getnumpredicates();k++) {
69 CoercePredicate *cp=cs->getpredicate(k);
70 WorkSet *reqs=(WorkSet *)cptoreqs->get(cp);
72 for(FieldTuple *ft=(FieldTuple *)reqs->firstelement();ft!=NULL;
73 ft=(FieldTuple*)reqs->getnextelement(ft)) {
74 if (!testsatisfy(satisfied,ft)) {
86 WorkSet *provides=(WorkSet *)nftoprovides->get(nf);
88 for(FieldTuple *ft=(FieldTuple *)provides->firstelement();ft!=NULL;
89 ft=(FieldTuple*)provides->getnextelement(ft)) {
90 if(!testsatisfy(satisfied,ft)) {
91 /* something to add */
92 satisfied->addobject(ft);
93 propertytonf->put(ft,nf);
103 printf("Some relations can't be established:<%s,%s>",badft->relation,badft->set);
109 void FieldCheck::buildmaps() {
110 cptoreqs=new Hashtable(NULL,NULL);
111 nftoprovides=new Hashtable(NULL,NULL);
113 for(int i=0;i<globalmodel->getnumconstraints();i++) {
114 NormalForm *nf=globalmodel->getnormalform(i);
115 Constraint *c=globalmodel->getconstraint(i);
117 for(int j=0;j<nf->getnumsentences();j++) {
118 WorkSet *curr=new WorkSet();
119 CoerceSentence *cs=nf->getsentence(j);
120 for (int k=0;k<cs->getnumpredicates();k++) {
121 CoercePredicate *cp=cs->getpredicate(k);
122 WorkSet *wr=requireswhat(c,cp);
124 cptoreqs->put(cp,wr);
125 FieldTuple *ft=provideswhat(c,cp);
134 for(FieldTuple *ft=(FieldTuple *)old->firstelement();ft!=NULL;
135 ft=(FieldTuple *)old->getnextelement(ft)) {
136 if (!curr->contains(ft))
137 old->removeobject(ft);
146 nftoprovides->put(nf, old);
151 WorkSet * FieldCheck::requireswhat(Constraint *c,CoercePredicate *cp) {
152 Predicate *p=cp->getpredicate();
153 int type=p->gettype();
154 if (type==PREDICATE_LT ||
155 type==PREDICATE_LTE ||
156 type==PREDICATE_EQUALS ||
157 type==PREDICATE_GT ||
158 type==PREDICATE_GTE) {
159 WorkSet *ws=new WorkSet();
160 Elementexpr *ee=p->geteleexpr();
169 void FieldCheck::processee(WorkSet *ws, Constraint *c, Elementexpr *ee) {
170 switch(ee->gettype()) {
171 case ELEMENTEXPR_SUB:
172 case ELEMENTEXPR_ADD:
173 case ELEMENTEXPR_MULT:
174 processee(ws,c,ee->getleft());
175 processee(ws,c,ee->getright());
177 case ELEMENTEXPR_RELATION: {
178 /* Interesting case */
179 char *rel=ee->getrelation()->getname();
180 Elementexpr *left=ee->getleft();
181 int tleft=left->gettype();
182 if (tleft==ELEMENTEXPR_LABEL) {
183 ws->addobject(new FieldTuple(rel,getset(c, left->getlabel()->label())));
184 } else if (tleft==ELEMENTEXPR_RELATION) {
185 DRelation *drl=globalmodel->getdomainrelation()->getrelation(left->getrelation()->getname());
186 char *rangeofl=drl->getrange();
187 ws->addobject(new FieldTuple(rel,rangeofl));
190 printf("Can't determine domain\n");
200 bool FieldCheck::setok(Constraint *c, char *set) {
201 for(int i=0;i<c->numquants();i++) {
202 Quantifier *q=c->getquant(i);
203 char *qs=q->getset()->getname();
204 DomainRelation *dr=globalmodel->getdomainrelation();
205 DomainSet *iset=dr->getset(set);
206 DomainSet *qset=dr->getset(qs);
207 if (dr->issupersetof(iset,qset))
213 FieldTuple * FieldCheck::provideswhat(Constraint *c,CoercePredicate *cp) {
214 if (cp->getcoercebool())
216 Predicate *p=cp->getpredicate();
217 switch(p->gettype()) {
220 case PREDICATE_EQUALS:
223 char *relation=p->getvalueexpr()->getrelation()->getname();
224 char *var=p->getvalueexpr()->getlabel()->label();
225 char *set=getset(c,var);
227 return new FieldTuple(relation,set);
233 case PREDICATE_GTE1: {
234 Setexpr *se=p->getsetexpr();
235 if (se->gettype()==SETEXPR_REL) {
236 char *relation=se->getrelation()->getname();
237 char *var=se->getlabel()->label();
238 char *set=getset(c,var);
240 return new FieldTuple(relation,set);
250 char *getset(Constraint *c, char *var) {
251 for (int i=0;i<c->numquants();i++) {
252 Quantifier *q=c->getquant(i);
253 if (equivalentstrings(var,q->getlabel()->label())) {
254 return q->getset()->getname();
260 unsigned int FieldTuple::hashCode() {
261 return hashstring(relation)^hashstring(set);
264 bool FieldTuple::equals(ElementWrapper *other) {
265 if (other->type()!=ELEMENT_FTUPLE)
267 FieldTuple *oft=(FieldTuple *)other;
268 if (equivalentstrings(relation, oft->relation)&&
269 equivalentstrings(set,oft->set))
275 int FieldTuple::type() {
276 return ELEMENT_FTUPLE;
279 FieldTuple::FieldTuple(char * r, char *s) {
284 FieldTuple::FieldTuple(FieldTuple *o) {
285 this->relation=o->relation;