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 |
|
package org.sat4j.reader.csp; |
27 |
|
|
28 |
|
import java.io.FileNotFoundException; |
29 |
|
import java.io.IOException; |
30 |
|
import java.io.InputStreamReader; |
31 |
|
import java.net.URL; |
32 |
|
import java.util.HashMap; |
33 |
|
import java.util.Map; |
34 |
|
|
35 |
|
import org.mozilla.javascript.Context; |
36 |
|
import org.mozilla.javascript.Script; |
37 |
|
import org.mozilla.javascript.Scriptable; |
38 |
|
import org.sat4j.core.Vec; |
39 |
|
import org.sat4j.specs.ContradictionException; |
40 |
|
import org.sat4j.specs.ISolver; |
41 |
|
import org.sat4j.specs.IVec; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@author |
47 |
|
|
|
|
| 0% |
Uncovered Elements: 64 (64) |
Complexity: 11 |
Complexity Density: 0,44 |
|
48 |
|
public class Predicate implements Clausifiable { |
49 |
|
|
50 |
|
private String expr; |
51 |
|
|
52 |
|
private Encoding encoding; |
53 |
|
|
54 |
|
private final IVec<String> variables = new Vec<String>(); |
55 |
|
|
56 |
|
private static Context cx; |
57 |
|
|
58 |
|
private static Scriptable scope; |
59 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0,43 |
|
60 |
0
|
static {... |
61 |
0
|
cx = Context.enter(); |
62 |
|
|
63 |
0
|
scope = cx.initStandardObjects(); |
64 |
|
|
65 |
|
|
66 |
0
|
try { |
67 |
0
|
URL url = Predicate.class.getResource("predefinedfunctions.js"); |
68 |
0
|
cx.evaluateReader(scope, new InputStreamReader(url.openStream()), |
69 |
|
"predefinedfunctions.js", 1, null); |
70 |
|
|
71 |
|
} catch (FileNotFoundException e) { |
72 |
|
|
73 |
0
|
e.printStackTrace(); |
74 |
|
} catch (IOException e) { |
75 |
|
|
76 |
0
|
e.printStackTrace(); |
77 |
|
} |
78 |
|
} |
79 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
80 |
0
|
public Predicate() {... |
81 |
|
} |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0
|
public void setExpression(String expr) {... |
84 |
0
|
this.expr = expr; |
85 |
|
} |
86 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
87 |
0
|
public void addVariable(String name) {... |
88 |
0
|
variables.push(name); |
89 |
|
} |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0,5 |
|
91 |
0
|
private boolean evaluate(int[] values) {... |
92 |
0
|
assert values.length == variables.size(); |
93 |
0
|
for (int i = 0; i < variables.size(); i++) { |
94 |
0
|
scope.put(variables.get(i), scope, values[i]); |
95 |
|
} |
96 |
0
|
Object result = myscript.exec(cx, scope); |
97 |
0
|
return Context.toBoolean(result); |
98 |
|
} |
99 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 3 |
Complexity Density: 0,3 |
|
100 |
0
|
public void toClause(ISolver solver, IVec<Var> vscope, IVec<Evaluable> vars)... |
101 |
|
throws ContradictionException { |
102 |
0
|
if (myscript == null) { |
103 |
0
|
myscript = cx.compileString(expr, "rhino.log", 1, null); |
104 |
|
} |
105 |
0
|
if (vscope.size() == 2) { |
106 |
0
|
encoding = BinarySupportEncoding.instance(); |
107 |
|
} else { |
108 |
0
|
encoding = DirectEncoding.instance(); |
109 |
|
} |
110 |
0
|
encoding.onInit(solver, vscope); |
111 |
0
|
int[] tuple = new int[vars.size()]; |
112 |
0
|
valuemapping.clear(); |
113 |
0
|
find(tuple, 0, vscope, vars, solver); |
114 |
0
|
encoding.onFinish(solver, vscope); |
115 |
|
} |
116 |
|
|
117 |
|
private final Map<Evaluable, Integer> valuemapping = new HashMap<Evaluable, Integer>(); |
118 |
|
|
119 |
|
private Script myscript; |
120 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 6 |
Complexity Density: 0,38 |
|
121 |
0
|
private void find(int[] tuple, int n, IVec<Var> scope,... |
122 |
|
IVec<Evaluable> vars, ISolver solver) throws ContradictionException { |
123 |
0
|
if (valuemapping.size() == scope.size()) { |
124 |
0
|
for (int i = 0; i < tuple.length; i++) { |
125 |
0
|
Evaluable ev = vars.get(i); |
126 |
0
|
Integer value = valuemapping.get(ev); |
127 |
0
|
if (value == null) { |
128 |
0
|
tuple[i] = ev.domain().get(0); |
129 |
|
} else { |
130 |
0
|
tuple[i] = value; |
131 |
|
} |
132 |
|
} |
133 |
0
|
if (evaluate(tuple)) { |
134 |
0
|
encoding.onSupport(solver, scope, valuemapping); |
135 |
|
} else { |
136 |
0
|
encoding.onNogood(solver, scope, valuemapping); |
137 |
|
} |
138 |
|
} else { |
139 |
0
|
Var var = scope.get(n); |
140 |
0
|
Domain domain = var.domain(); |
141 |
0
|
for (int i = 0; i < domain.size(); i++) { |
142 |
0
|
valuemapping.put(var, domain.get(i)); |
143 |
0
|
find(tuple, n + 1, scope, vars, solver); |
144 |
|
} |
145 |
0
|
valuemapping.remove(var); |
146 |
|
} |
147 |
|
} |
148 |
|
} |