This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HSTree.java
294 lines (241 loc) · 9.46 KB
/
HSTree.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
*
* * Consistency-based Algorithms for Conflict Detection and Resolution
* *
* * Copyright (c) 2022
* *
* * @author: Viet-Man Le (vietman.le@ist.tugraz.at)
*
*/
package at.tugraz.ist.ase.cacdr.algorithms.hsdag;
import at.tugraz.ist.ase.cacdr.algorithms.hsdag.labeler.IHSLabelable;
import at.tugraz.ist.ase.cacdr.algorithms.hsdag.parameters.AbstractHSParameters;
import at.tugraz.ist.ase.cacdr.checker.ChocoConsistencyChecker;
import at.tugraz.ist.ase.common.LoggerUtils;
import at.tugraz.ist.ase.knowledgebases.core.Constraint;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
import static at.tugraz.ist.ase.cacdr.eval.CAEvaluator.*;
import static at.tugraz.ist.ase.common.ConstraintUtils.hasIntersection;
/**
* Implementation of the HS-tree algorithm.
* IHSLabeler algorithms have to return labels (conflict or diagnosis) which are guaranteed to be minimal.
* For example: QuickXPlain, MXP, FastDiag
*
* source: https://github.com/jaccovs/Master-project
* @author Viet-Man Le (vietman.le@ist.tugraz.at)
*/
@Slf4j
public class HSTree extends AbstractHSConstructor {
@Getter
private Node root = null;
protected final Queue<Node> openNodes = new LinkedList<>();
// Map of <conflict, list of nodes which have the conflict as its label>
protected Map<Set<Constraint>, List<Node>> cs_nodesMap = new LinkedHashMap<>();
public HSTree(IHSLabelable labeler, ChocoConsistencyChecker checker) {
super(labeler, checker);
}
/**
* Builds the HS-tree.
*/
public void construct() {
AbstractHSParameters param = getLabeler().getInitialParameters();
log.debug("{}Constructing the HS-tree for [C={}] >>>", LoggerUtils.tab, param.getC());
LoggerUtils.indent();
start(TIMER_HS_CONSTRUCTION_SESSION);
start(TIMER_DIAGNOSIS);
// generate root if there is none
if (!hasRoot()) {
start(TIMER_CONFLICT);
List<Set<Constraint>> conflicts = getLabeler().getLabel(param);
stop(TIMER_CONFLICT);
if (conflicts.isEmpty()) {
endConstruction();
return;
}
// create root node
Set<Constraint> label = selectConflict(conflicts);
root = Node.createRoot(label, param);
incrementCounter(COUNTER_CONSTRUCTED_NODES);
addConflicts(conflicts); // to reuse conflicts
addItemToCSNodesMap(label, root);
if (stopConstruction()) {
endConstruction();
return;
}
expand(root);
}
while (hasNodesToExpand()) {
Node node = getNextNode();
if (skipNode(node)) continue;
log.trace("{}Processing [node={}]", LoggerUtils.tab, node);
LoggerUtils.indent();
label(node);
if (stopConstruction()) {
LoggerUtils.outdent();
endConstruction();
return;
}
if (node.getStatus() == NodeStatus.Open) {
expand(node);
}
System.gc();
LoggerUtils.outdent();
}
endConstruction();
}
protected void endConstruction() {
LoggerUtils.outdent();
log.debug("{}<<< return [conflicts={}]", LoggerUtils.tab, getConflicts());
log.debug("{}<<< return [diagnoses={}]", LoggerUtils.tab, getDiagnoses());
stop(TIMER_HS_CONSTRUCTION_SESSION);
stop(TIMER_DIAGNOSIS, false);
if (log.isTraceEnabled()) {
Utils.printInfo(root, getConflicts(), getDiagnoses());
}
}
protected void label(Node node) {
if (node.getLabel() == null) {
// Reusing conflicts - H(node) ∩ S = {}, then label node by S
List<Set<Constraint>> conflicts = getReusableConflicts(node);
// compute conflicts if there are none to reuse
if (conflicts.isEmpty()) {
conflicts = computeLabel(node);
}
if (conflicts.isEmpty()) {
node.setStatus(NodeStatus.Checked);
Set<Constraint> diag = new LinkedHashSet<>(node.getPathLabels());
getDiagnoses().add(diag);
log.debug("{}Diagnosis #{} is found: {}", LoggerUtils.tab, getDiagnoses().size(), node.getPathLabels());
stop(TIMER_DIAGNOSIS);
start(TIMER_DIAGNOSIS);
return;
}
Set<Constraint> label = selectConflict(conflicts);
node.setLabel(label);
addItemToCSNodesMap(label, node);
}
}
protected List<Set<Constraint>> getReusableConflicts(Node node) {
List<Set<Constraint>> conflicts = new LinkedList<>();
for (Set<Constraint> conflict : getConflicts()) {
// H(node) ∩ S = {}
if (!hasIntersection(node.getPathLabels(), conflict)) {
conflicts.add(conflict);
incrementCounter(COUNTER_REUSE_CONFLICT);
log.trace("{}Reuse [conflict={}, node={}]", LoggerUtils.tab, conflict, node);
return conflicts;
}
}
return conflicts;
}
protected List<Set<Constraint>> computeLabel(Node node) {
AbstractHSParameters param = node.getParameters();
start(TIMER_CONFLICT);
List<Set<Constraint>> conflicts = getLabeler().getLabel(param);
if (!conflicts.isEmpty()) {
stop(TIMER_CONFLICT);
addConflicts(conflicts);
} else {
// stop TIMER_CONFLICT without saving the time
stop(TIMER_CONFLICT, false);
}
return conflicts;
}
protected void addConflicts(Collection<Set<Constraint>> conflicts) {
for (Set<Constraint> conflict : conflicts) {
getConflicts().add(conflict);
log.debug("{}Conflict #{} is found: {}", LoggerUtils.tab, getConflicts().size(), conflict);
}
}
protected void addItemToCSNodesMap(Set<Constraint> cs, Node node) {
log.trace("{}addItemToCSNodesMap [cs_nodesMap.size={}, cs={}, node={}]", LoggerUtils.tab, cs_nodesMap.size(), cs, node);
LoggerUtils.indent();
if (!cs_nodesMap.containsKey(cs)) {
cs_nodesMap.put(cs, new LinkedList<>());
log.trace("{}Add new item", LoggerUtils.tab);
}
cs_nodesMap.get(cs).add(node);
log.debug("{}Updated [cs_nodesMap.size={}]", LoggerUtils.tab, cs_nodesMap.size());
LoggerUtils.outdent();
}
/**
* Selects a conflict to label a node from a list of conflicts.
* This implementation simply returns the first conflict from the given list.
* @param conflicts list of conflicts
* @return node label
*/
protected Set<Constraint> selectConflict(List<Set<Constraint>> conflicts) {
return conflicts.get(0);
}
protected boolean hasNodesToExpand() {
return !openNodes.isEmpty();
}
protected Node getNextNode() {
return openNodes.remove();
}
protected boolean skipNode(Node node) {
boolean condition1 = getMaxDepth() != 0 && getMaxDepth() <= node.getLevel();
return node.getStatus() != NodeStatus.Open || condition1 || canPrune(node);
}
protected void expand(Node nodeToExpand) {
log.trace("{}Generating the children nodes of [node={}]", LoggerUtils.tab, nodeToExpand);
LoggerUtils.indent();
for (Constraint arcLabel : nodeToExpand.getLabel()) {
AbstractHSParameters param_parentNode = nodeToExpand.getParameters();
AbstractHSParameters new_param = getLabeler().createParameter(param_parentNode, arcLabel);
Node node = Node.builder()
.parent(nodeToExpand)
.parameters(new_param)
.arcLabel(arcLabel)
.build();
incrementCounter(COUNTER_CONSTRUCTED_NODES);
if (!canPrune(node)) {
openNodes.add(node);
}
}
LoggerUtils.outdent();
}
protected boolean canPrune(Node node) {
// 3.i - if n is checked, and n' is such that H(n) ⊆ H(n'), then close the node n'
// n is a diagnosis
for (Set<Constraint> diag : getDiagnoses()) {
if (node.getPathLabels().containsAll(diag)) {
node.setStatus(NodeStatus.Closed);
incrementCounter(COUNTER_CLOSE_1);
log.trace("{}Closed [node={}]", LoggerUtils.tab, node);
return true;
}
}
// 3.ii - if n has been generated and node n' is such that H(n') = H(n), then close node n'
for (Node n : openNodes) {
if (n.getPathLabels().size() == node.getPathLabels().size()
&& Sets.difference(n.getPathLabels(), node.getPathLabels()).isEmpty()) {
node.setStatus(NodeStatus.Closed);
incrementCounter(COUNTER_CLOSE_2);
log.trace("{}Closed [node={}]", LoggerUtils.tab, node);
return true;
}
}
return false;
}
protected boolean hasRoot() {
return this.root != null;
}
@Override
public void resetEngine() {
super.resetEngine();
this.root = null;
this.cs_nodesMap.clear();
this.openNodes.clear();
}
@Override
public void dispose() {
super.dispose();
this.root = null;
this.openNodes.clear();
this.cs_nodesMap.clear();
}
}