1 /*
2  * XplusYgtC.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  * Constraint X + Y{@literal >} C
41  *
42  * @author Krzysztof Kuchcinski and Radoslaw Szymanek
43  * @version 4.8
44  */
45 
46 public class XplusYgtC extends PrimitiveConstraint {
47 
48     final static AtomicInteger idNumber = new AtomicInteger(0);
49 
50     /**
51      * It specifies variable x in constraint x + y{@literal >} c.
52      */
53     final public IntVar x;
54 
55     /**
56      * It specifies variable y in constraint x + y{@literal >} c.
57      */
58     final public IntVar y;
59 
60     /**
61      * It specifies constant c in constraint x + y{@literal >} c.
62      */
63     final public int c;
64 
65     /**
66      * It constructs X+Y{@literal >} C constraint.
67      *
68      * @param x variable x.
69      * @param y variable y.
70      * @param c variable c.
71      */
XplusYgtC(IntVar x, IntVar y, int c)72     public XplusYgtC(IntVar x, IntVar y, int c) {
73 
74         checkInputForNullness(new String[] {"x", "y"}, new Object[] {x, y});
75 
76         numberId = idNumber.incrementAndGet();
77 
78         this.x = x;
79         this.y = y;
80         this.c = c;
81 
82 	checkForOverflow();
83 
84         setScope(x, y);
85     }
86 
checkForOverflow()87     void checkForOverflow() {
88 
89         int sumMin = 0, sumMax = 0;
90 
91         sumMin = Math.addExact(sumMin, x.min());
92         sumMax = Math.addExact(sumMax, x.max());
93 
94         sumMin = Math.addExact(sumMin, y.min());
95         sumMax = Math.addExact(sumMax, y.max());
96 
97         Math.subtractExact(sumMin, c);
98         Math.subtractExact(sumMax, c);
99     }
100 
101 
consistency(final Store store)102     @Override public void consistency(final Store store) {
103 
104         x.domain.inMin(store.level, x, c - y.max() + 1);
105         y.domain.inMin(store.level, y, c - x.max() + 1);
106     }
107 
getDefaultNestedConsistencyPruningEvent()108     @Override protected int getDefaultNestedConsistencyPruningEvent() {
109         return IntDomain.BOUND;
110     }
111 
getDefaultNestedNotConsistencyPruningEvent()112     @Override protected int getDefaultNestedNotConsistencyPruningEvent() {
113         return IntDomain.BOUND;
114     }
115 
getDefaultNotConsistencyPruningEvent()116     @Override protected int getDefaultNotConsistencyPruningEvent() {
117         return IntDomain.BOUND;
118     }
119 
getDefaultConsistencyPruningEvent()120     @Override public int getDefaultConsistencyPruningEvent() {
121         return IntDomain.BOUND;
122     }
123 
notConsistency(final Store store)124     @Override public void notConsistency(final Store store) {
125 
126         x.domain.inMax(store.level, x, c - y.min());
127         y.domain.inMax(store.level, y, c - x.min());
128     }
129 
notSatisfied()130     @Override public boolean notSatisfied() {
131         return x.max() + y.max() <= c;
132     }
133 
satisfied()134     @Override public boolean satisfied() {
135         return x.min() + y.min() > c;
136     }
137 
toString()138     @Override public String toString() {
139 
140         return id() + " : XplusYgtC(" + x + ", " + y + ", " + c + " )";
141     }
142 
143 }
144