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 |
|
package org.sat4j.reader.csp; |
26 |
|
|
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import org.sat4j.specs.ContradictionException; |
31 |
|
import org.sat4j.specs.ISolver; |
32 |
|
import org.sat4j.specs.IVec; |
33 |
|
|
|
|
| 0% |
Uncovered Elements: 65 (65) |
Complexity: 9 |
Complexity Density: 0,36 |
|
34 |
|
public abstract class Supports implements Relation { |
35 |
|
|
36 |
|
private Encoding encoding; |
37 |
|
|
38 |
|
private final int arity; |
39 |
|
|
40 |
|
private int[][] tuples; |
41 |
|
|
42 |
|
private int lastmatch; |
43 |
|
|
44 |
|
private Map<Evaluable, Integer> mtuple; |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
46 |
0
|
public Supports(int arity, int nbtuples) {... |
47 |
0
|
this.arity = arity; |
48 |
0
|
tuples = new int[nbtuples][]; |
49 |
|
} |
50 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
51 |
0
|
public void addTuple(int index, int[] tuple) {... |
52 |
0
|
tuples[index] = tuple; |
53 |
|
} |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
55 |
0
|
public int arity() {... |
56 |
0
|
return arity; |
57 |
|
} |
58 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0,14 |
|
59 |
0
|
public void toClause(ISolver solver, IVec<Var> scope, IVec<Evaluable> vars)... |
60 |
|
throws ContradictionException { |
61 |
0
|
assert vars.size() == 0; |
62 |
0
|
assert scope.size() == arity; |
63 |
0
|
int[] tuple = new int[scope.size()]; |
64 |
0
|
mtuple = new HashMap<Evaluable, Integer>(); |
65 |
0
|
lastmatch = -1; |
66 |
0
|
encoding = chooseEncoding(scope); |
67 |
0
|
encoding.onInit(solver, scope); |
68 |
0
|
find(tuple, 0, scope, solver); |
69 |
0
|
encoding.onFinish(solver, scope); |
70 |
|
} |
71 |
|
|
72 |
|
protected abstract Encoding chooseEncoding(IVec<Var> scope); |
73 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 4 |
Complexity Density: 0,4 |
|
74 |
0
|
private void find(int[] tuple, int n, IVec<Var> scope, ISolver solver)... |
75 |
|
throws ContradictionException { |
76 |
0
|
if (n == scope.size()) { |
77 |
0
|
assert mtuple.size() == n; |
78 |
0
|
if (notPresent(tuple)) { |
79 |
0
|
encoding.onNogood(solver, scope, mtuple); |
80 |
|
} else { |
81 |
0
|
encoding.onSupport(solver, scope, mtuple); |
82 |
|
} |
83 |
|
} else { |
84 |
0
|
Domain domain = scope.get(n).domain(); |
85 |
0
|
for (int i = 0; i < domain.size(); i++) { |
86 |
0
|
tuple[n] = domain.get(i); |
87 |
0
|
mtuple.put(scope.get(n), tuple[n]); |
88 |
0
|
find(tuple, n + 1, scope, solver); |
89 |
|
} |
90 |
0
|
mtuple.remove(scope.get(n)); |
91 |
|
|
92 |
|
} |
93 |
|
|
94 |
|
} |
95 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 6 |
Complexity Density: 0,33 |
|
96 |
0
|
private boolean notPresent(int[] tuple) {... |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
0
|
int i = lastmatch + 1; |
101 |
0
|
int j = 0; |
102 |
0
|
final int[][] ltuples = tuples; |
103 |
0
|
int searchedvalue, currentvalue; |
104 |
0
|
while (i < ltuples.length && j < tuple.length) { |
105 |
0
|
searchedvalue = ltuples[i][j]; |
106 |
0
|
currentvalue = tuple[j]; |
107 |
0
|
if (searchedvalue < currentvalue) { |
108 |
0
|
i++; |
109 |
0
|
j = 0; |
110 |
0
|
continue; |
111 |
|
} |
112 |
0
|
if (searchedvalue > currentvalue) |
113 |
0
|
return true; |
114 |
0
|
j++; |
115 |
|
} |
116 |
0
|
if (j == tuple.length) { |
117 |
0
|
lastmatch = i; |
118 |
0
|
return false; |
119 |
|
} |
120 |
0
|
return true; |
121 |
|
} |
122 |
|
} |