1 /*
2  * AndBool.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.IntVar;
34 import org.jacop.core.Store;
35 
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39 
40 /**
41  * AndBool constraint implements logic and operation on its arguments
42  * and returns result.
43  *
44  * @author Krzysztof Kuchcinski and Radoslaw Szymanek
45  * @version 4.8
46  */
47 
48 public class AndBool extends DecomposedConstraint<PrimitiveConstraint> {
49 
50     PrimitiveConstraint c = null;
51 
52     /**
53      * It constructs and constraint on variables.
54      *
55      * @param a      parameters variable.
56      * @param result variable.
57      */
AndBool(IntVar[] a, IntVar result)58     public AndBool(IntVar[] a, IntVar result) {
59 
60         IntVar[] r = filter(a);
61 
62         if (r == null)
63             c = new XeqC(result, 0);
64         else if (r.length == 1)
65             c = new XeqY(r[0], result);
66         else if (r.length == 2) {
67             c = new AndBoolSimple(r[0], r[1], result);
68         } else
69             c = new AndBoolVector(r, result);
70     }
71 
72     /**
73      * It constructs and constraint on variables.
74      *
75      * @param a      parameters variable.
76      * @param result variable.
77      */
AndBool(List<IntVar> a, IntVar result)78     public AndBool(List<IntVar> a, IntVar result) {
79         this(a.toArray(new IntVar[a.size()]), result);
80     }
81 
82     /**
83      * It constructs and constraint on variables.
84      *
85      * @param a      parameter variable.
86      * @param b      parameter variable.
87      * @param result variable.
88      */
AndBool(IntVar a, IntVar b, IntVar result)89     public AndBool(IntVar a, IntVar b, IntVar result) {
90         this(new IntVar[] {a, b}, result);
91     }
92 
imposeDecomposition(Store store)93     @Override public void imposeDecomposition(Store store) {
94         store.impose(c);
95     }
96 
decompose(Store store)97     @Override public List<PrimitiveConstraint> decompose(Store store) {
98         return Collections.singletonList(c);
99     }
100 
toString()101     public String toString() {
102         return c.toString();
103     }
104 
filter(IntVar[] xs)105     IntVar[] filter(IntVar[] xs) {
106         List<IntVar> result = new ArrayList<>();
107         for (IntVar x : xs)
108             if (x.max() == 0)
109                 return null;
110             else if (x.min() == 1)
111                 continue;
112             else
113                 result.add(x);
114 
115         return result.toArray(new IntVar[result.size()]);
116     }
117 }
118