3 #include "ActionGEQ1.h"
5 #include "normalizer.h"
11 #include "Hashtable.h"
15 ActionGEQ1::ActionGEQ1(DomainRelation *drel, model *m) {
20 void ActionGEQ1::repairpredicate(Hashtable *env,CoercePredicate *p) {
22 printf("ActionGEQ1::repairpredicate CALLED\n");
25 switch(p->getpredicate()->getsetexpr()->gettype()) {
27 /* Set should be too small if we are doing a repair
28 We need to add 1 element */
29 Guidance *g=globalmodel->getguidance();
30 char * newsetname=p->getpredicate()->getsetexpr()->getsetlabel()->getname();
31 Source s=g->sourceforsetsize(newsetname);
32 if (s.setname!=NULL) {
33 /* just pick an element from s.setname */
34 char *setname=s.setname;
35 if (equivalentstrings(s.setname,"int")) {
36 /* special case for ints*/
37 WorkSet *wsnew=domrelation->getset(newsetname)->getset();
39 Element *e=new Element(i);
40 if (!wsnew->contains(e)) {
42 domrelation->addtoset(e,domrelation->getset(newsetname),globalmodel);
48 WorkSet *ws=domrelation->getset(setname)->getset();
49 WorkSet *wsnew=domrelation->getset(newsetname)->getset();
50 Element *e=(Element *)ws->firstelement();
52 if (!wsnew->contains(e)) {
54 domrelation->addtoset(e,domrelation->getset(newsetname),globalmodel);
55 //printf("HERE?!\n"); fflush(NULL);
58 e=(Element *)ws->getnextelement(e);
60 printf("Error...set %s doesn't have enough elements for %s\n",setname,newsetname);
64 /* call functionpointer */
65 DomainSet *newset=domrelation->getset(newsetname);
66 char *type=newset->getelementtype();
67 structure *st=globalmodel->getstructure(type);
68 Element *ele=s.functionptr(st,globalmodel);
70 printf("Error...allocation function doesn't return structure for %s\n",newsetname);
73 domrelation->addtoset(ele,newset,globalmodel);
82 /* Set should be too small if we are doing a repair
83 We need to add 1 element */
84 Guidance *g=globalmodel->getguidance();
85 char * relationname=p->getpredicate()->getsetexpr()->getrelation()->getname();
86 Element *key=(Element *)env->get(p->getpredicate()->getsetexpr()->getlabel()->label());
87 DRelation *relation=domrelation->getrelation(relationname);
88 char *rangeset=relation->getrange();
89 Source s=g->sourceforrelation(rangeset);
90 if (s.setname!=NULL) {
91 /* just pick an element from s.setname */
92 char *setname=s.setname;
93 if (equivalentstrings(s.setname,"int")) {
94 /* special case for ints*/
96 WorkSet *wsnew=domrelation->getrelation(relationname)->getrelation()->getset(key);
97 Element *e=new Element(i);
98 if (wsnew==NULL||!wsnew->contains(e)) {
100 domrelation->addtoset(e,domrelation->getset(rangeset),globalmodel);
101 relation->getrelation()->put(key,e);
107 WorkSet *ws=domrelation->getset(s.setname)->getset();
108 Element *e=(Element *)ws->firstelement();
110 WorkSet *wsnew=domrelation->getrelation(relationname)->getrelation()->getset(key);
111 if (wsnew==NULL||!wsnew->contains(e)) {
112 /* Got our element */
113 domrelation->addtoset(e,domrelation->getset(rangeset),globalmodel);
114 relation->getrelation()->put(key,e);
117 e=(Element *)ws->getnextelement(e);
119 printf("Error...set %s doesn't have enough elements for relation\n",setname);
123 /* call functionpointer */
124 DomainSet *newset=domrelation->getset(rangeset);
125 char *type=newset->getelementtype();
126 structure *st=globalmodel->getstructure(type);
127 Element *ele=s.functionptr(st,globalmodel);
129 printf("Error...allocation function doesn't return structure for %s\n",rangeset);
132 domrelation->addtoset(ele,domrelation->getset(rangeset),globalmodel);
133 relation->getrelation()->put(key,ele);
140 case SETEXPR_INVREL: {
141 /* Set should be too small if we are doing a repair
142 We need to add 1 element */
143 Guidance *g=globalmodel->getguidance();
144 char * relationname=p->getpredicate()->getsetexpr()->getrelation()->getname();
145 Element *key=(Element *)env->get(p->getpredicate()->getsetexpr()->getlabel()->label());
146 DRelation *relation=domrelation->getrelation(relationname);
147 char *domainset=relation->getdomain();
148 Source s=g->sourceforrelation(domainset);
149 if (s.setname!=NULL) {
150 /* just pick an element from s.setname */
151 char *setname=s.setname;
152 if (equivalentstrings(s.setname,"int")) {
153 /* special case for ints*/
155 Element *e=new Element(i);
156 WorkSet *wsnew=domrelation->getrelation(relationname)->getrelation()->invgetset(key);
157 if (wsnew==NULL||!wsnew->contains(e)) {
158 /* Got our element */
159 domrelation->addtoset(e,domrelation->getset(domainset),globalmodel);
160 relation->getrelation()->put(e,key);
166 WorkSet *ws=domrelation->getset(s.setname)->getset();
167 Element *e=(Element *)ws->firstelement();
169 WorkSet *wsnew=domrelation->getrelation(relationname)->getrelation()->invgetset(key);
170 if (wsnew==NULL||!wsnew->contains(e)) {
171 /* Got our element */
172 domrelation->addtoset(e,domrelation->getset(domainset),globalmodel);
173 relation->getrelation()->put(e,key);
176 e=(Element *)ws->getnextelement(e);
178 printf("Error...set %s doesn't have enough elements for relation\n",setname);
182 /* call functionpointer */
183 DomainSet *newset=domrelation->getset(domainset);
184 char *type=newset->getelementtype();
185 structure *st=globalmodel->getstructure(type);
186 Element *ele=s.functionptr(st,globalmodel);
188 printf("Error...allocation function doesn't return structure for %s\n",domainset);
191 domrelation->addtoset(ele,domrelation->getset(domainset),globalmodel);
192 relation->getrelation()->put(ele,key);
203 void ActionGEQ1::breakpredicate(Hashtable *env, CoercePredicate *p)
206 printf("ActionGEQ1::breakpredicate CALLED\n");
207 p->getpredicate()->print(); printf("\n");
210 switch(p->getpredicate()->getsetexpr()->gettype()) {
218 char *setname=p->getpredicate()->getsetexpr()->getsetlabel()->getname();
219 DomainSet *ds=domrelation->getset(setname); //S
221 // simply delete all elements that S currently contains
222 Guidance *g=globalmodel->getguidance();
223 WorkSet *ws=ds->getset();
226 Element *e=(Element *)ws->firstelement();
227 domrelation->delfromsetmovetoset(e,domrelation->getset(setname), globalmodel);
240 DRelation *dr=domrelation->getrelation(p->getpredicate()->getsetexpr()->getrelation()->getname());
241 WorkRelation *wr=dr->getrelation(); //R
242 Element *key=(Element *)env->get(p->getpredicate()->getsetexpr()->getlabel()->label()); //V
243 WorkSet *ws=wr->getset(key); //V.R
245 // simply delete all elements that V.R currently contains
247 for(int i=0; i<size; i++)
249 void *objtoremove=ws->firstelement();
250 wr->remove(key, objtoremove);
262 DRelation *dr=domrelation->getrelation(p->getpredicate()->getsetexpr()->getrelation()->getname());
263 //dr->print(); printf("\n");
264 WorkRelation *wr=dr->getrelation(); //R
265 Element *key=(Element *)env->get(p->getpredicate()->getsetexpr()->getlabel()->label()); //V
266 WorkSet *ws=wr->invgetset(key); //R.V
269 printf("ws size = %d\n", ws->size());
272 // simply delete all elements that R.V currently contains
274 for(int i=0; i<size; i++)
276 void *objtoremove = ws->firstelement();
277 wr->remove(objtoremove, key);
281 printf("INVREL finished\n");
290 bool ActionGEQ1::conflict(Constraint *c1, CoercePredicate *p1,Constraint *c2, CoercePredicate *p2) {
291 assert(canrepairpredicate(p1));
292 Setexpr *pse=p1->getpredicate()->getsetexpr();
293 if(comparepredicates(c1,p1,c2,p2))
294 return false; /*same predicates don't conflict*/
296 DomainSet *fromset=domrelation->getsource(domrelation->getset(pse->getsetlabel()->getname()));
298 fromname=fromset->getname();
299 /* FIXME: fromname should cycle through set of possibilities ...
300 if it is null, it should be replaced with the type name the object will have for the REL tests...this is okay already for the label tests...
303 switch(pse->gettype()) {
304 case SETEXPR_LABEL: {
305 char *boundname=NULL;
306 Guidance *g=globalmodel->getguidance();
307 DomainSet *fromset=domrelation->getsource(domrelation->getset(pse->getsetlabel()->getname()));
310 Source s=g->sourceforsetsize(fromset->getname());
314 char *setname=pse->getsetlabel()->getname();
317 WorkSet *ws=domrelation->conflictaddsets(pse->getsetlabel()->getname(),boundname,globalmodel);
318 DomainSet *ds=(DomainSet *) ws->firstelement();
320 if (conflictwithaddtoset(ds->getname(),c2,p2)) {
324 ds=(DomainSet *) ws->getnextelement(ds);
328 /* Check conflicts arrising from deletions from set */
330 WorkSet *ws=domrelation->conflictdelsets(pse->getsetlabel()->getname(), boundname);
331 DomainSet *ds=(DomainSet *) ws->firstelement();
333 if (conflictwithremovefromset(NULL,ds->getname(),c2,p2)) {
337 ds=(DomainSet *) ws->getnextelement(ds);
345 /* we have a in v.r */
348 (p2->getpredicate()->gettype()==PREDICATE_GTE1||
349 p2->getpredicate()->gettype()==PREDICATE_EQ1)&&
350 p2->getpredicate()->getsetexpr()->gettype()==SETEXPR_REL) {
353 /* Compute bounding set if there is one */
356 DomainRelation *drel=globalmodel->getdomainrelation();
357 char *insertset=drel->getrelation(pse->getrelation()->getname())->getrange();
358 Guidance *g=globalmodel->getguidance();
359 Source s=g->sourceforsetsize(insertset);
362 /* Check conflicts arrising from addition to set */
364 WorkSet *ws=domrelation->conflictaddsets(insertset,s.setname,globalmodel);
365 DomainSet *ds=(DomainSet *) ws->firstelement();
367 if (conflictwithaddtoset(ds->getname(),c2,p2)) {
371 ds=(DomainSet *) ws->getnextelement(ds);
375 /* Check conflicts arrising from deletions from set */
377 WorkSet *ws=domrelation->conflictdelsets(insertset, s.setname);
378 DomainSet *ds=(DomainSet *) ws->firstelement();
380 if (conflictwithremovefromset(NULL,ds->getname(),c2,p2)) {
384 ds=(DomainSet *) ws->getnextelement(ds);
390 return testforconflict(getset(c1,p1->getpredicate()->getsetexpr()->getlabel()->label()),
392 p1->getpredicate()->getsetexpr()->getrelation()->getname(),c2,p2);
394 case SETEXPR_INVREL: {
395 /* we have a in v.r */
398 (p2->getpredicate()->gettype()==PREDICATE_GTE1||
399 p2->getpredicate()->gettype()==PREDICATE_EQ1)&&
400 p2->getpredicate()->getsetexpr()->gettype()==SETEXPR_INVREL) {
403 /* Compute bounding set if there is one */
406 DomainRelation *drel=globalmodel->getdomainrelation();
407 char *insertset=drel->getrelation(pse->getrelation()->getname())->getdomain();
408 Guidance *g=globalmodel->getguidance();
409 Source s=g->sourceforsetsize(insertset);
412 /* Check conflicts arrising from addition to set */
414 WorkSet *ws=domrelation->conflictaddsets(insertset,s.setname,globalmodel);
415 DomainSet *ds=(DomainSet *) ws->firstelement();
417 if (conflictwithaddtoset(ds->getname(),c2,p2)) {
421 ds=(DomainSet *) ws->getnextelement(ds);
425 /* Check conflicts arrising from deletions from set */
427 WorkSet *ws=domrelation->conflictdelsets(insertset, s.setname);
428 DomainSet *ds=(DomainSet *) ws->firstelement();
430 if (conflictwithremovefromset(NULL,ds->getname(),c2,p2)) {
434 ds=(DomainSet *) ws->getnextelement(ds);
439 return testforconflict(fromname,
440 getset(c1,p1->getpredicate()->getsetexpr()->getlabel()->label()),
441 p1->getpredicate()->getsetexpr()->getrelation()->getname(),c2,p2);
446 bool ActionGEQ1::canrepairpredicate(CoercePredicate *cp) {
447 if (cp->getcoercebool()==false)
449 Predicate *p=cp->getpredicate();
451 if (p->gettype()!=PREDICATE_GTE1)
454 Setexpr *se=p->getsetexpr();
455 int setexprtype=se->gettype();
456 if (setexprtype==SETEXPR_REL||
457 setexprtype==SETEXPR_INVREL) {
458 DRelation *dr=domrelation->getrelation(se->getrelation()->getname());
460 return false; /* Can't change static domain relations */
463 /* Coercing set membership */