1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  *  Main authors:
4  *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
5  *
6  *  Copyright:
7  *     Vincent Barichard, 2013
8  *
9  *  Last modified:
10  *     $Date$ by $Author$
11  *     $Revision$
12  *
13  *  This file is part of Quacode:
14  *     http://quacode.barichard.com
15  *
16  * This file is based on gecode/int/bool.cpp
17  * and is under the same license as given below:
18  *
19  *  Main authors:
20  *     Christian Schulte <schulte@gecode.org>
21  *
22  *  Copyright:
23  *     Christian Schulte, 2002
24  *
25  *  This file is part of Gecode, the generic constraint
26  *  development environment:
27  *     http://www.gecode.org
28  *
29  *  Permission is hereby granted, free of charge, to any person obtaining
30  *  a copy of this software and associated documentation files (the
31  *  "Software"), to deal in the Software without restriction, including
32  *  without limitation the rights to use, copy, modify, merge, publish,
33  *  distribute, sublicense, and/or sell copies of the Software, and to
34  *  permit persons to whom the Software is furnished to do so, subject to
35  *  the following conditions:
36  *
37  *  The above copyright notice and this permission notice shall be
38  *  included in all copies or substantial portions of the Software.
39  *
40  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
41  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
43  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
44  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
45  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
46  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47  *
48  */
49 
50 #include <gecode/int/bool.hh>
51 #include <gecode/int/rel.hh>
52 #include <quacode/qint/qbool.hh>
53 #include <quacode/qcsp.hh>
54 
55 namespace Gecode {
56 
57   void
qrel(Home home,QBoolVar qx0,BoolOpType o,QBoolVar qx1,BoolVar x2)58   qrel(Home home, QBoolVar qx0, BoolOpType o, QBoolVar qx1, BoolVar x2) {
59     using namespace Int;
60     if (home.failed()) return;
61     switch (o) {
62     case BOT_AND:
63       {
64         NegBoolView n0(qx0.x); NegBoolView n1(qx1.x); NegBoolView n2(x2);
65         GECODE_ES_FAIL((Bool::QOr<NegBoolView,NegBoolView,NegBoolView>
66                         ::post(home,n0,qx0.q,qx0.r,n1,qx1.q,qx1.r,n2)));
67       }
68       break;
69     case BOT_OR:
70       GECODE_ES_FAIL((Bool::QOr<BoolView,BoolView,BoolView>
71                       ::post(home,qx0.x,qx0.q,qx0.r,qx1.x,qx1.q,qx1.r,x2)));
72       break;
73     case BOT_IMP:
74       {
75         NegBoolView n0(qx0.x);
76         GECODE_ES_FAIL((Bool::QOr<NegBoolView,BoolView,BoolView>
77                         ::post(home,n0,qx0.q,qx0.r,qx1.x,qx1.q,qx1.r,x2)));
78       }
79       break;
80     case BOT_EQV:
81       GECODE_ES_FAIL((Bool::QEqv<BoolView,BoolView,BoolView>
82                       ::post(home,qx0,qx1,x2)));
83       break;
84     case BOT_XOR:
85       GECODE_ES_FAIL((Bool::QXorv<BoolView,BoolView,BoolView>
86                       ::post(home,qx0,qx1,x2)));
87       break;
88     default:
89       throw UnknownOperation("Int::rel");
90     }
91   }
92 
93   void
qclause(Home home,BoolOpType o,QBoolVarArgs x,QBoolVarArgs y,int n)94   qclause(Home home, BoolOpType o, QBoolVarArgs x, QBoolVarArgs y, int n) {
95     using namespace Int;
96     if ((n < 0) || (n > 1))
97       throw NotZeroOne("Int::rel");
98     if (home.failed()) return;
99     switch (o) {
100     case BOT_AND:
101       if (n == 0) {
102         x.unique(); y.unique();
103         ViewArray<NegBoolView> xv(home,x.size());
104         QuantArgs qx(x.size());
105         IntArgs rx(x.size());
106         for (int i=x.size(); i--; ) {
107           NegBoolView n(x[i].x); xv[i]=n; qx[i]=x[i].q; rx[i]=x[i].r;
108         }
109         ViewArray<BoolView> yv(home,y.size());
110         QuantArgs qy(y.size());
111         IntArgs ry(y.size());
112         for (int i=y.size(); i--; ) {
113           yv[i]=y[i].x; qy[i]=y[i].q; ry[i]=y[i].r;
114         }
115         GECODE_ES_FAIL((Bool::QClauseTrue<NegBoolView,BoolView>
116                         ::post(home,xv,qx,rx,yv,qy,ry)));
117       } else {
118         for (int i=x.size(); i--; ) {
119           BoolView b(x[i].x); GECODE_ME_FAIL(b.one(home));
120         }
121         for (int i=y.size(); i--; ) {
122           BoolView b(y[i].x); GECODE_ME_FAIL(b.zero(home));
123         }
124       }
125       break;
126     case BOT_OR:
127       if (n == 0) {
128         for (int i=x.size(); i--; ) {
129           BoolView b(x[i].x); GECODE_ME_FAIL(b.zero(home));
130         }
131         for (int i=y.size(); i--; ) {
132           BoolView b(y[i].x); GECODE_ME_FAIL(b.one(home));
133         }
134       } else {
135         x.unique(); y.unique();
136         ViewArray<BoolView> xv(home,x.size());
137         QuantArgs qx(x.size());
138         IntArgs rx(x.size());
139         for (int i=x.size(); i--; ) {
140           xv[i]=x[i].x; qx[i]=x[i].q; rx[i]=x[i].r;
141         }
142         ViewArray<NegBoolView> yv(home,y.size());
143         QuantArgs qy(y.size());
144         IntArgs ry(y.size());
145         for (int i=y.size(); i--; ) {
146           NegBoolView n(y[i].x); yv[i]=n; qy[i]=y[i].q; ry[i]=y[i].r;
147         }
148         GECODE_ES_FAIL((Bool::QClauseTrue<BoolView,NegBoolView>
149                         ::post(home,xv,qx,rx,yv,qy,ry)));
150       }
151       break;
152     default:
153       throw IllegalOperation("Int::clause");
154     }
155   }
156 
157   void
qclause(Home home,BoolOpType o,QBoolVarArgs x,QBoolVarArgs y,BoolVar z)158   qclause(Home home, BoolOpType o, QBoolVarArgs x, QBoolVarArgs y, BoolVar z) {
159     using namespace Int;
160     if (home.failed()) return;
161     switch (o) {
162     case BOT_AND:
163       {
164         x.unique(); y.unique();
165         ViewArray<NegBoolView> xv(home,x.size());
166         QuantArgs qx(x.size());
167         IntArgs rx(x.size());
168         for (int i=x.size(); i--; ) {
169           NegBoolView n(x[i].x); xv[i]=n; qx[i]=x[i].q; rx[i]=x[i].r;
170         }
171         ViewArray<BoolView> yv(home,y.size());
172         QuantArgs qy(y.size());
173         IntArgs ry(y.size());
174         for (int i=y.size(); i--; ) {
175           yv[i]=y[i].x; qy[i]=y[i].q; ry[i]=y[i].r;
176         }
177         NegBoolView nz(z);
178         GECODE_ES_FAIL((Bool::QClause<NegBoolView,BoolView>
179                         ::post(home,xv,qx,rx,yv,qy,ry,nz)));
180       }
181       break;
182     case BOT_OR:
183       {
184         x.unique(); y.unique();
185         ViewArray<BoolView> xv(home,x.size());
186         QuantArgs qx(x.size());
187         IntArgs rx(x.size());
188         for (int i=x.size(); i--; ) {
189           xv[i]=x[i].x; qx[i]=x[i].q; rx[i]=x[i].r;
190         }
191         ViewArray<NegBoolView> yv(home,y.size());
192         QuantArgs qy(y.size());
193         IntArgs ry(y.size());
194         for (int i=y.size(); i--; ) {
195           NegBoolView n(y[i].x); yv[i]=n; qy[i]=y[i].q; ry[i]=y[i].r;
196         }
197         GECODE_ES_FAIL((Bool::QClause<BoolView,NegBoolView>
198                         ::post(home,xv,qx,rx,yv,qy,ry,z)));
199       }
200       break;
201     default:
202       throw IllegalOperation("Int::clause");
203     }
204   }
205 
206 }
207 
208 // STATISTICS: int-post
209