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.xplain;
31
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.HashSet;
35
36 import org.sat4j.core.VecInt;
37 import org.sat4j.specs.ContradictionException;
38 import org.sat4j.specs.IConstr;
39 import org.sat4j.specs.ISolver;
40 import org.sat4j.specs.IVecInt;
41 import org.sat4j.specs.IteratorInt;
42 import org.sat4j.specs.TimeoutException;
43 import org.sat4j.tools.GroupClauseSelectorSolver;
44 import org.sat4j.tools.SolverDecorator;
45
46
47
48
49
50
51
52
53
54
55
56 public class HighLevelXplain<T extends ISolver> extends
57 GroupClauseSelectorSolver<T> implements Explainer {
58
59 private IVecInt assump;
60
61 private MinimizationStrategy xplainStrategy = new DeletionStrategy();
62
63 public HighLevelXplain(T solver) {
64 super(solver);
65 }
66
67 @Override
68 public IConstr addAtLeast(IVecInt literals, int degree)
69 throws ContradictionException {
70 throw new UnsupportedOperationException();
71 }
72
73 @Override
74 public IConstr addAtMost(IVecInt literals, int degree)
75 throws ContradictionException {
76 throw new UnsupportedOperationException();
77 }
78
79
80
81
82 private static final long serialVersionUID = 1L;
83
84
85
86
87
88
89 private IVecInt explanationKeys() throws TimeoutException {
90 assert !isSatisfiable(this.assump);
91 ISolver solver = decorated();
92 if (solver instanceof SolverDecorator<?>) {
93 solver = ((SolverDecorator<? extends ISolver>) solver).decorated();
94 }
95 return this.xplainStrategy.explain(solver, getVarToHighLevel(),
96 this.assump);
97 }
98
99 public int[] minimalExplanation() throws TimeoutException {
100 Collection<Integer> components = explain();
101 int[] model = new int[components.size()];
102 int i = 0;
103 for (int c : components) {
104 model[i++] = c;
105 }
106 Arrays.sort(model);
107 return model;
108 }
109
110
111
112
113
114
115 public Collection<Integer> explain() throws TimeoutException {
116 IVecInt keys = explanationKeys();
117 Collection<Integer> explanation = new HashSet<Integer>(keys.size());
118 for (IteratorInt it = keys.iterator(); it.hasNext();) {
119 explanation.add(getVarToHighLevel().get(it.next()));
120 }
121 return explanation;
122 }
123
124
125
126
127 public void cancelExplanation() {
128 this.xplainStrategy.cancelExplanationComputation();
129 }
130
131 @Override
132 public int[] findModel() throws TimeoutException {
133 this.assump = VecInt.EMPTY;
134 return super.findModel();
135 }
136
137 @Override
138 public int[] findModel(IVecInt assumps) throws TimeoutException {
139 this.assump = assumps;
140 return super.findModel(assumps);
141 }
142
143 @Override
144 public boolean isSatisfiable() throws TimeoutException {
145 this.assump = VecInt.EMPTY;
146 return super.isSatisfiable();
147 }
148
149 @Override
150 public boolean isSatisfiable(boolean global) throws TimeoutException {
151 this.assump = VecInt.EMPTY;
152 return super.isSatisfiable(global);
153 }
154
155 @Override
156 public boolean isSatisfiable(IVecInt assumps) throws TimeoutException {
157 this.assump = assumps;
158 return super.isSatisfiable(assumps);
159 }
160
161 @Override
162 public boolean isSatisfiable(IVecInt assumps, boolean global)
163 throws TimeoutException {
164 this.assump = assumps;
165 return super.isSatisfiable(assumps, global);
166 }
167
168 @Override
169 public String toString(String prefix) {
170 System.out.println(prefix
171 + "High Level Explanation (MUS) enabled solver");
172 System.out.println(prefix + this.xplainStrategy);
173 return super.toString(prefix);
174 }
175
176 public void setMinimizationStrategy(MinimizationStrategy strategy) {
177 this.xplainStrategy = strategy;
178 }
179
180 }