1 package org.sat4j.tools;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 import org.sat4j.specs.ContradictionException;
34 import org.sat4j.specs.IConstr;
35 import org.sat4j.specs.ISolver;
36 import org.sat4j.specs.IVecInt;
37 import org.sat4j.tools.encoding.EncodingStrategyAdapter;
38 import org.sat4j.tools.encoding.Policy;
39
40
41
42
43
44
45
46
47
48 public class ClausalCardinalitiesDecorator<T extends ISolver> extends
49 SolverDecorator<T> {
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56 private final EncodingStrategyAdapter encodingAdapter;
57
58 public ClausalCardinalitiesDecorator(T solver) {
59 super(solver);
60 this.encodingAdapter = new Policy();
61 }
62
63 public ClausalCardinalitiesDecorator(T solver,
64 EncodingStrategyAdapter encodingAd) {
65 super(solver);
66 this.encodingAdapter = encodingAd;
67 }
68
69 @Override
70 public IConstr addAtLeast(IVecInt literals, int k)
71 throws ContradictionException {
72 if (k == 1) {
73 return this.encodingAdapter.addAtLeastOne(decorated(), literals);
74 } else {
75 return this.encodingAdapter.addAtLeast(decorated(), literals, k);
76 }
77 }
78
79 @Override
80 public IConstr addAtMost(IVecInt literals, int k)
81 throws ContradictionException {
82 if (k == 1) {
83 return this.encodingAdapter.addAtMostOne(decorated(), literals);
84 } else {
85 return this.encodingAdapter.addAtMost(decorated(), literals, k);
86 }
87 }
88
89 @Override
90 public IConstr addExactly(IVecInt literals, int k)
91 throws ContradictionException {
92
93 if (k == 1) {
94 return this.encodingAdapter.addExactlyOne(decorated(), literals);
95 } else {
96 return this.encodingAdapter.addExactly(decorated(), literals, k);
97 }
98 }
99
100 @Override
101 public String toString() {
102 return toString("");
103 }
104
105 @Override
106 public String toString(String prefix) {
107 return super.toString(prefix) + "\n"
108 + "Cardinality to SAT encoding: \n" + "Encoding: "
109 + this.encodingAdapter + "\n";
110 }
111
112 }