1 /*
2  * XplusClteqZ.java
3  * This file is part of JaCoP.
4  * <p>
5  * JaCoP is a Java Constraint Programming solver.
6  * <p>
7  * Copyright (C) 2000-2008 Krzysztof Kuchcinski and Radoslaw Szymanek
8  * <p>
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  * <p>
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  * <p>
19  * Notwithstanding any other provision of this License, the copyright
20  * owners of this work supplement the terms of this License with terms
21  * prohibiting misrepresentation of the origin of this work and requiring
22  * that modified versions of this work be marked in reasonable ways as
23  * different from the original version. This supplement of the license
24  * terms is in accordance with Section 7 of GNU Affero General Public
25  * License version 3.
26  * <p>
27  * You should have received a copy of the GNU Affero General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 package org.jacop.constraints;
32 
33 import org.jacop.core.IntDomain;
34 import org.jacop.core.IntVar;
35 import org.jacop.core.Store;
36 
37 import java.util.concurrent.atomic.AtomicInteger;
38 
39 /**
40  * Constraints X + C{@literal <=} Z.
41  * <p>
42  * Boundary consistency is used.
43  *
44  * @author Krzysztof Kuchcinski and Radoslaw Szymanek
45  * @version 4.8
46  */
47 
48 public class XplusClteqZ extends PrimitiveConstraint {
49 
50     final static AtomicInteger idNumber = new AtomicInteger(0);
51 
52     /**
53      * It specifies variable x in constraint x+c{@literal <=}z.
54      */
55     final public IntVar x;
56 
57     /**
58      * It specifies constant c in constraint x+c{@literal <=} z.
59      */
60     final public int c;
61 
62     /**
63      * It specifies variable z in constraint x+c{@literal <=} z.
64      */
65     final public IntVar z;
66 
67     /**
68      * It constructs constraint X+C{@literal <=} Z.
69      *
70      * @param x variable x.
71      * @param c constant c.
72      * @param z variable z.
73      */
XplusClteqZ(IntVar x, int c, IntVar z)74     public XplusClteqZ(IntVar x, int c, IntVar z) {
75 
76         checkInputForNullness(new String[] {"x", "z"}, new Object[] {x, z});
77 
78         numberId = idNumber.incrementAndGet();
79 
80         this.x = x;
81         this.c = c;
82         this.z = z;
83 
84 	checkForOverflow();
85 
86         setScope(x, z);
87     }
88 
checkForOverflow()89     void checkForOverflow() {
90 
91         int sumMin = 0, sumMax = 0;
92 
93         sumMin = Math.addExact(sumMin, x.min());
94         sumMax = Math.addExact(sumMax, x.max());
95 
96         sumMin = Math.addExact(sumMin, c);
97         sumMax = Math.addExact(sumMax, c);
98 
99         Math.subtractExact(sumMin, z.max());
100         Math.subtractExact(sumMax, z.min());
101     }
102 
103 
consistency(final Store store)104     @Override public void consistency(final Store store) {
105 
106         x.domain.inMax(store.level, x, z.max() - c);
107         z.domain.inMin(store.level, z, x.min() + c);
108 
109     }
110 
getDefaultNestedNotConsistencyPruningEvent()111     @Override protected int getDefaultNestedNotConsistencyPruningEvent() {
112         return IntDomain.BOUND;
113     }
114 
getDefaultNestedConsistencyPruningEvent()115     @Override protected int getDefaultNestedConsistencyPruningEvent() {
116         return IntDomain.BOUND;
117     }
118 
getDefaultNotConsistencyPruningEvent()119     @Override protected int getDefaultNotConsistencyPruningEvent() {
120         return IntDomain.BOUND;
121     }
122 
getDefaultConsistencyPruningEvent()123     @Override public int getDefaultConsistencyPruningEvent() {
124         return IntDomain.BOUND;
125     }
126 
notConsistency(final Store store)127     @Override public void notConsistency(final Store store) {
128 
129         x.domain.inMin(store.level, x, z.min() - c + 1);
130         z.domain.inMax(store.level, z, x.max() + c - 1);
131     }
132 
notSatisfied()133     @Override public boolean notSatisfied() {
134         return x.min() + c > z.max();
135     }
136 
satisfied()137     @Override public boolean satisfied() {
138         return x.max() + c <= z.min();
139     }
140 
toString()141     @Override public String toString() {
142 
143         return id() + " : XplusClteqZ(" + x + ", " + c + ", " + z + " )";
144     }
145 
146 }
147