Moved makelib
[repair.git] / Repair / RepairInterpreter / ActionNotInSet.cc
1 #include <assert.h>
2 #include "ActionNotInSet.h"
3 #include "dmodel.h"
4 #include "normalizer.h"
5 #include "omodel.h"
6 #include "Relation.h"
7 #include "set.h"
8 #include "Hashtable.h"
9 #include "ActionInSet.h"
10
11 ActionNotInSet::ActionNotInSet(DomainRelation *drel, model *m) {
12   domrelation=drel;
13   globalmodel=m;
14 }
15
16 void ActionNotInSet::repairpredicate(Hashtable *env,CoercePredicate *p) {
17   Element *ele=(Element*) env->get(p->getpredicate()->getlabel()->label());
18   switch(p->getpredicate()->getsetexpr()->gettype()) {
19   case SETEXPR_LABEL:
20     domrelation->delfromsetmovetoset(ele, domrelation->getset(p->getpredicate()->getsetexpr()->getsetlabel()->getname()),globalmodel);
21     break;
22   case SETEXPR_REL: {
23     Element *key=(Element*) env->get(p->getpredicate()->getsetexpr()->getlabel()->label());
24     domrelation->getrelation(p->getpredicate()->getsetexpr()->getrelation()->getname())->getrelation()->remove(key,ele);
25   }
26   break;
27   case SETEXPR_INVREL: {
28     Element *key=(Element*) env->get(p->getpredicate()->getsetexpr()->getlabel()->label());
29     domrelation->getrelation(p->getpredicate()->getsetexpr()->getrelation()->getname())->getrelation()->remove(ele,key);
30   }
31   break;
32   }
33 }
34
35
36
37
38 void ActionNotInSet::breakpredicate(Hashtable *env, CoercePredicate *p)
39 {
40 #ifdef DEBUGMESSAGES
41   printf("ActionNotInSet::breakpredicate CALLED\n");
42   p->getpredicate()->print(); printf("\n");
43 #endif
44
45   ActionInSet *a = new ActionInSet(domrelation, globalmodel);
46   a->repairpredicate(env, p);
47 }
48
49
50
51
52 bool ActionNotInSet::conflict(Constraint *c1, CoercePredicate *p1,Constraint *c2, CoercePredicate *p2) {
53   assert(canrepairpredicate(p1));
54   if(comparepredicates(c1,p1,c2,p2))
55     return false; /*same predicates don't conflict*/
56   Setexpr *pse=p1->getpredicate()->getsetexpr();
57   switch(pse->gettype()) {
58   case SETEXPR_LABEL: {
59     /* Go through the sets we add something to */
60     {
61       WorkSet *ws=domrelation->removeconflictaddsets(pse->getsetlabel()->getname(),globalmodel);
62       DomainSet *ds=(DomainSet *)ws->firstelement();
63       while(ds!=NULL) {
64         if (conflictwithaddtoset(ds->getname(),c2,p2)) {
65           delete(ws);
66           return true;
67         }
68         ds=(DomainSet *)ws->getnextelement(ds);
69       }
70       delete(ws);
71     }
72     {
73       WorkSet *ws=domrelation->removeconflictdelsets(pse->getsetlabel()->getname());
74       DomainSet *ds=(DomainSet *)ws->firstelement();
75       while(ds!=NULL) {
76         if (conflictwithremovefromset(NULL,ds->getname(),c2,p2)) {
77           delete(ws);
78           return true;
79         }
80         ds=(DomainSet *)ws->getnextelement(ds);
81       }
82       delete(ws);
83     }
84     return false;
85
86   }
87   case SETEXPR_REL:
88     /* we have !a in v.r */
89     /* remove <v,a> to r */
90     return testforconflictremove(getset(c1,p1->getpredicate()->getsetexpr()->getlabel()->label()),
91                                  getset(c1,p1->getpredicate()->getlabel()->label()),
92                                  p1->getpredicate()->getsetexpr()->getrelation()->getname(),c2,p2);
93   case SETEXPR_INVREL:
94     /* we have !a in v.r */
95     /* remove <v,a> to r */
96     return testforconflictremove(getset(c1,p1->getpredicate()->getlabel()->label()),
97                                  getset(c1,p1->getpredicate()->getsetexpr()->getlabel()->label()),
98                                  p1->getpredicate()->getsetexpr()->getrelation()->getname(),c2,p2);
99   }
100 }
101
102 bool ActionNotInSet::canrepairpredicate(CoercePredicate *cp) {
103   if (cp->getcoercebool()==true)
104     return false;
105   Predicate *p=cp->getpredicate();
106   if (p==NULL)
107     return false;
108   if (p->gettype()!=PREDICATE_SET)
109     return false;
110   Setexpr *se=p->getsetexpr();
111   int setexprtype=se->gettype();
112   if (setexprtype==SETEXPR_REL||
113       setexprtype==SETEXPR_INVREL) {
114     DRelation *dr=domrelation->getrelation(se->getrelation()->getname());
115     if (dr->isstatic())
116       return false; /* Can't change static domain relations */
117   }
118
119   /* Coercing set membership */
120   return true;
121 }