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 java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35
36 import org.sat4j.core.ASolverFactory;
37 import org.sat4j.core.VecInt;
38 import org.sat4j.specs.ContradictionException;
39 import org.sat4j.specs.ISolver;
40 import org.sat4j.specs.IVecInt;
41 import org.sat4j.specs.TimeoutException;
42
43 public class CheckMUSSolutionListener implements SolutionFoundListener {
44
45 private List<IVecInt> clauses;
46
47 private String explain;
48
49
50
51
52 private final ASolverFactory<? extends ISolver> factory;
53
54 public CheckMUSSolutionListener(ASolverFactory<? extends ISolver> factory) {
55 this.clauses = new ArrayList<IVecInt>();
56 this.factory = factory;
57 }
58
59
60
61
62
63 public void addOriginalClause(IVecInt clause) {
64 IVecInt newClause = new VecInt(clause.size());
65 if (clauses == null) {
66 this.clauses = new ArrayList<IVecInt>();
67 }
68 clause.copyTo(newClause);
69 clauses.add(newClause);
70 }
71
72
73
74
75
76
77
78
79
80 public boolean checkThatItIsAMUS(IVecInt mus) {
81 boolean result = false;
82
83 ISolver solver = factory.defaultSolver();
84
85 try {
86 for (int i = 0; i < mus.size(); i++) {
87 solver.addClause(clauses.get(mus.get(i) - 1));
88 }
89
90 result = !solver.isSatisfiable();
91
92 if (!result) {
93 explain = "The set of clauses in the MUS is SAT : "
94 + Arrays.toString(solver.model());
95 return result;
96 }
97
98 } catch (ContradictionException e) {
99 result = true;
100 } catch (TimeoutException e) {
101 e.printStackTrace();
102 }
103
104 try {
105 for (int i = 0; i < mus.size(); i++) {
106 solver = factory.defaultSolver();
107 for (int j = 0; j < mus.size(); j++) {
108 if (j != i) {
109 solver.addClause(clauses.get(mus.get(j) - 1));
110 }
111 }
112 result = result && solver.isSatisfiable();
113 if (!result) {
114 explain = "The subset of clauses in the MUS not containing clause "
115 + (i + 1)
116 + " is SAT : "
117 + Arrays.toString(solver.model());
118 return result;
119 }
120 }
121 } catch (ContradictionException e) {
122 result = false;
123 } catch (TimeoutException e) {
124 e.printStackTrace();
125 }
126
127 return result;
128
129 }
130
131 public void onSolutionFound(int[] solution) {
132
133 }
134
135 public void onSolutionFound(IVecInt solution) {
136 if (checkThatItIsAMUS(solution)) {
137 System.out.println(solution + " is a MUS");
138 } else {
139 System.out.println(solution + " is not a MUS \n" + explain);
140 }
141 }
142
143 public void onUnsatTermination() {
144
145 }
146 }