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 static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertTrue;
34
35 import org.junit.Test;
36 import org.sat4j.core.VecInt;
37 import org.sat4j.minisat.SolverFactory;
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 BackboneTest {
44
45 @Test
46 public void testEasyCaseWithOnlyOneModel() throws ContradictionException,
47 TimeoutException {
48 ISolver solver = SolverFactory.newDefault();
49 IVecInt clause = new VecInt();
50 clause.push(1).push(2).push(3);
51 solver.addClause(clause);
52 clause.clear();
53 clause.push(-1).push(-2);
54 solver.addClause(clause);
55 clause.clear();
56 clause.push(-1).push(-3);
57 solver.addClause(clause);
58 clause.clear();
59 clause.push(1);
60 solver.addClause(clause);
61 clause.clear();
62 IVecInt backbone = Backbone.compute(solver);
63 assertTrue(backbone.contains(1));
64 assertTrue(backbone.contains(-2));
65 assertTrue(backbone.contains(-3));
66 }
67
68 @Test
69 public void testEmptyBackbone() throws ContradictionException,
70 TimeoutException {
71 ISolver solver = SolverFactory.newDefault();
72 IVecInt clause = new VecInt();
73 clause.push(1).push(2).push(3);
74 solver.addClause(clause);
75 clause.clear();
76 clause.push(-1).push(-2);
77 solver.addClause(clause);
78 clause.clear();
79 clause.push(-1).push(-3);
80 solver.addClause(clause);
81 clause.clear();
82 IVecInt backbone = Backbone.compute(solver);
83 assertEquals(0, backbone.size());
84 }
85
86 @Test
87 public void testCaseWithUnsatProblem() throws ContradictionException,
88 TimeoutException {
89 ISolver solver = SolverFactory.newDefault();
90 IVecInt clause = new VecInt();
91 clause.push(1).push(2);
92 solver.addClause(clause);
93 clause.clear();
94 clause.push(-1).push(2);
95 solver.addClause(clause);
96 clause.clear();
97 clause.push(1).push(-2);
98 solver.addClause(clause);
99 clause.clear();
100 clause.push(-1).push(-2);
101 solver.addClause(clause);
102 clause.clear();
103 IVecInt backbone = Backbone.compute(solver);
104 assertEquals(0, backbone.size());
105 }
106
107
108
109
110
111
112
113
114
115 @Test
116 public void testBugUnitClauses() throws ContradictionException,
117 TimeoutException {
118 ISolver solver1 = SolverFactory.newDefault();
119 ISolver solver2 = SolverFactory.newDefault();
120 ISolver solver3 = SolverFactory.newDefault();
121
122 int[][] cnf1 = new int[][] { new int[] { 1 }, new int[] { 1, -2 },
123 new int[] { 1, -3 }, new int[] { -1, 2 } };
124
125
126 int[][] cnf2 = new int[][] { new int[] { -1, 2 }, new int[] { 1, -2 },
127 new int[] { 1, -3 }, new int[] { 1 } };
128
129
130
131
132 int[][] cnf3 = new int[][] { new int[] { 1, -2 }, new int[] { -1, 3 },
133 new int[] { 1, -3 }, new int[] { 1 } };
134
135 for (int[] is : cnf1) {
136 solver1.addClause(new VecInt(is));
137 }
138 for (int[] is : cnf2) {
139 solver2.addClause(new VecInt(is));
140 }
141 for (int[] is : cnf3) {
142 solver3.addClause(new VecInt(is));
143 }
144
145 IVecInt vecInt1 = Backbone.compute(solver1);
146 assertEquals(vecInt1.size(), 2);
147 assertTrue(vecInt1.contains(1));
148 assertTrue(vecInt1.contains(2));
149
150 IVecInt vecInt2 = Backbone.compute(solver2);
151 assertEquals(vecInt2.size(), 2);
152 assertTrue(vecInt2.contains(1));
153 assertTrue(vecInt2.contains(2));
154
155 IVecInt vecInt3 = Backbone.compute(solver3);
156 assertEquals(vecInt3.size(), 2);
157 assertTrue(vecInt3.contains(1));
158 assertTrue(vecInt3.contains(3));
159 }
160 }