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