1 /*
2  * In.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.*;
34 
35 import java.util.concurrent.atomic.AtomicInteger;
36 
37 /**
38  * Constraints X to belong to a specified domain.
39  * <p>
40  * Domain consistency is used.
41  *
42  * @author Krzysztof Kuchcinski and Radoslaw Szymanek
43  * @version 4.8
44  */
45 
46 public class In extends PrimitiveConstraint {
47 
48     static AtomicInteger idNumber = new AtomicInteger(0);
49 
50     /**
51      * It specifies variable x whose domain must lie within a specified domain.
52      */
53     public IntVar x;
54 
55     /**
56      * It specifies domain d which restricts the possible value of the specified variable.
57      */
58     public IntDomain dom;
59 
60     /**
61      * It specifies all the values which can not be taken by a variable.
62      */
63     private IntDomain DomComplement;
64 
65     /**
66      * It constructs an In constraint to restrict the domain of the variable.
67      *
68      * @param x   variable x for which the restriction is applied.
69      * @param dom the domain to which the variables domain is restricted.
70      */
In(IntVar x, IntDomain dom)71     public In(IntVar x, IntDomain dom) {
72 
73         checkInputForNullness(new String[] {"x", "dom"}, new Object[] {x, dom});
74 
75         numberId = idNumber.incrementAndGet();
76 
77         this.x = x;
78         this.dom = dom;
79         this.DomComplement = dom.complement();
80 
81         setScope(x);
82 
83     }
84 
consistency(Store store)85     @Override public void consistency(Store store) {
86         x.domain.in(store.level, x, dom);
87 
88         removeConstraint();
89     }
90 
91 
getDefaultConsistencyPruningEvent()92     @Override public int getDefaultConsistencyPruningEvent() {
93         return Domain.NONE;
94     }
95 
notConsistency(Store store)96     @Override public void notConsistency(Store store) {
97         x.domain.in(store.level, x, DomComplement);
98     }
99 
notSatisfied()100     @Override public boolean notSatisfied() {
101         return !x.domain.isIntersecting(dom);
102         // !dom.contains(x.domain);
103     }
104 
satisfied()105     @Override public boolean satisfied() {
106         return //x.singleton() &&
107             dom.contains(x.domain);
108     }
109 
getDefaultNestedConsistencyPruningEvent()110     @Override protected int getDefaultNestedConsistencyPruningEvent() {
111         return IntDomain.ANY;
112     }
113 
getDefaultNestedNotConsistencyPruningEvent()114     @Override protected int getDefaultNestedNotConsistencyPruningEvent() {
115         return IntDomain.ANY;
116     }
117 
getDefaultNotConsistencyPruningEvent()118     @Override protected int getDefaultNotConsistencyPruningEvent() {
119         return Domain.NONE;
120     }
121 
toString()122     @Override public String toString() {
123         return id() + " : In(" + x + ", " + dom + " )";
124     }
125 
126 
getGuideConstraint()127     @Override public Constraint getGuideConstraint() {
128         return new XeqC(x, x.min());
129     }
130 
getGuideValue()131     @Override public int getGuideValue() {
132         return x.min();
133     }
134 
getGuideVariable()135     @Override public Var getGuideVariable() {
136         return x;
137     }
138 
supplyGuideFeedback(boolean feedback)139     @Override public void supplyGuideFeedback(boolean feedback) {
140     }
141 
142 }
143