-
Notifications
You must be signed in to change notification settings - Fork 4
/
EdpLcmEvaluationTest.java
268 lines (241 loc) · 9.78 KB
/
EdpLcmEvaluationTest.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
package org.dice_research.opal.licenses;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dice_research.opal.licenses.Attributes;
import org.dice_research.opal.licenses.BackMapping;
import org.dice_research.opal.licenses.Execution;
import org.dice_research.opal.licenses.License;
import org.dice_research.opal.licenses.edplcm.EdpLcmKnowledgeBase;
import org.dice_research.opal.licenses.edplcm.EpdLcmDerivates;
import org.dice_research.opal.licenses.transform.GraphExport;
import org.dice_research.opal.licenses.utils.ArrayUtil;
import org.dice_research.opal.licenses.utils.Cfg;
import org.dice_research.opal.licenses.utils.F1Score;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
/**
* Tests {@link EpdLcmDerivates} and {@link EdpLcmKnowledgeBase}.
*
* @author Adrian Wilke
*/
public class EdpLcmEvaluationTest {
private static final Logger LOGGER = LogManager.getLogger();
private EpdLcmDerivates derivates;
private EdpLcmKnowledgeBase knowledgeBase;
@Before
public void setUp() throws Exception {
// Check execution flag
boolean execute = false;
try {
execute = Boolean.parseBoolean(Cfg.getRunEdpLcmTests());
} catch (Exception e) {
// Handled afterwards
}
Assume.assumeTrue("Execution flag set", execute);
derivates = new EpdLcmDerivates();
knowledgeBase = new EdpLcmKnowledgeBase().load();
}
@Test
public void test() throws IOException {
boolean status = true;
StringBuilder stringBuilder = new StringBuilder();
F1Score f1Score = new F1Score();
GraphExport graph = new GraphExport();
// Combine licenses to check every cell in matrix
for (License licenseA : knowledgeBase.getLicenses()) {
for (License licenseB : knowledgeBase.getLicenses()) {
// IMORTANT
// Use only one input license
List<License> inputLicenses = new ArrayList<>(2);
inputLicenses.add(licenseA);
// Operator used to compute array of internal values
Execution execution = new Execution().setKnowledgeBase(knowledgeBase);
Attributes resultAttributes = execution.applyOperator(inputLicenses);
// Result attributes should equal attributes of license
for (int i = 0; i < resultAttributes.getList().size(); i++) {
if (!resultAttributes.getList().get(i).equals(licenseA.getAttributes().getList().get(i))) {
System.err.println(ArrayUtil.intString(resultAttributes.getValuesArray()));
System.err.println(ArrayUtil.intString(licenseA.getAttributes().getValuesArray()));
throw new RuntimeException("Different values for index " + i);
}
}
// For the check, add a second license
inputLicenses.add(licenseB);
// Back-mapping
List<License> resultingLicenses = new BackMapping().getCompatibleLicensesList(inputLicenses,
resultAttributes, knowledgeBase);
// Check license combination and update result status
boolean result = checkResults(licenseA, licenseB, resultingLicenses, derivates, stringBuilder, f1Score,
graph);
status = status & result;
}
}
// Print debugging info, if test failed
if (!status) {
stringBuilder.append("Tests: ");
stringBuilder.append(f1Score.getAll());
stringBuilder.append(" ");
stringBuilder.append(System.lineSeparator());
stringBuilder.append("Success: ");
stringBuilder.append(f1Score.getTrue());
stringBuilder.append(" (");
stringBuilder.append(f1Score.getPercentageTrue());
stringBuilder.append(")");
stringBuilder.append(System.lineSeparator());
stringBuilder.append("Errors: ");
stringBuilder.append(f1Score.getFalse());
stringBuilder.append(" (");
stringBuilder.append(f1Score.getPercentageFalse());
stringBuilder.append(")");
stringBuilder.append(System.lineSeparator());
stringBuilder.append("F1 score: ");
stringBuilder.append(f1Score.getF1score());
stringBuilder.append(System.lineSeparator());
stringBuilder.append("Precision: ");
stringBuilder.append(f1Score.getPrecision());
stringBuilder.append(System.lineSeparator());
stringBuilder.append("Recall: ");
stringBuilder.append(f1Score.getRecall());
stringBuilder.append(System.lineSeparator());
stringBuilder.append("Cases: ");
stringBuilder.append("TP: ");
stringBuilder.append(f1Score.getTruePositive());
stringBuilder.append(", TN: ");
stringBuilder.append(f1Score.getTrueNegative());
stringBuilder.append(", FP: ");
stringBuilder.append(f1Score.getFalsePositive());
stringBuilder.append(", FN: ");
stringBuilder.append(f1Score.getFalseNegative());
stringBuilder.append(System.lineSeparator());
stringBuilder.append(System.lineSeparator());
stringBuilder.append("Expected compatibility results:");
stringBuilder.append(System.lineSeparator());
stringBuilder.append(derivates);
stringBuilder.append(System.lineSeparator());
stringBuilder.append("KnowledgeBase attributes:");
stringBuilder.append(System.lineSeparator());
stringBuilder.append(knowledgeBase.toLines());
System.out.println(stringBuilder.toString());
}
File file = File.createTempFile(getClass().getName() + ".", ".csv");
graph.export(file);
LOGGER.info("Wrote file: " + file.getAbsolutePath());
Assert.assertTrue("EDP LCM compatibility", status);
}
/**
* Checks single license.
*/
public static boolean checkResults(License licenseA, License licenseB, List<License> resultingLicenses,
EpdLcmDerivates derivates, StringBuilder stringBuilder, F1Score f1Score, GraphExport graph)
throws NullPointerException, IOException {
boolean status = true;
List<String> resultingUris = resultingLicenses.stream().map(l -> l.getUri()).collect(Collectors.toList());
boolean derivatesValue = derivates.getValue(licenseA.getUri(), licenseB.getUri());
// Check result and add debugging information if test failed
if (derivatesValue && !resultingUris.contains(licenseB.getUri())) {
stringBuilder.append("Missing: ");
stringBuilder.append(ArrayUtil.intString(licenseB.getAttributes().getValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(ArrayUtil.intString(licenseB.getAttributes().getInternalValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(licenseB.getUri());
stringBuilder.append(System.lineSeparator());
status = false;
f1Score.falseNegative();
if (graph != null) {
graph.addEdge(licenseA.getName(), licenseB.getName(), "FN");
}
} else if (!derivatesValue && resultingUris.contains(licenseB.getUri())) {
stringBuilder.append("Wrong: ");
stringBuilder.append(ArrayUtil.intString(licenseB.getAttributes().getValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(ArrayUtil.intString(licenseB.getAttributes().getInternalValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(licenseB.getUri());
stringBuilder.append(System.lineSeparator());
status = false;
f1Score.falsePositive();
if (graph != null) {
graph.addEdge(licenseA.getName(), licenseB.getName(), "FP");
}
} else if (derivatesValue && resultingUris.contains(licenseB.getUri())) {
f1Score.truePositive();
if (graph != null) {
graph.addEdge(licenseA.getName(), licenseB.getName(), "TP");
}
} else {
f1Score.trueNegative();
if (graph != null) {
graph.addEdge(licenseA.getName(), licenseB.getName(), "TN");
}
}
// Add debugging information if test failed
if (!status) {
stringBuilder.append("Checked: ");
stringBuilder.append(ArrayUtil.intString(licenseA.getAttributes().getValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(ArrayUtil.intString(licenseA.getAttributes().getInternalValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(licenseA.toString());
stringBuilder.append(System.lineSeparator());
stringBuilder.append(" ");
stringBuilder.append(ArrayUtil.intString(licenseB.getAttributes().getValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(ArrayUtil.intString(licenseB.getAttributes().getInternalValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(licenseB.toString());
stringBuilder.append(System.lineSeparator());
stringBuilder.append(" ");
stringBuilder.append(Arrays.toString(licenseB.getAttributes().getShortFormArray()));
stringBuilder.append(" ");
stringBuilder.append(Arrays.toString(licenseB.getAttributes().getShortFormArray()));
stringBuilder.append(System.lineSeparator());
stringBuilder.append(System.lineSeparator());
}
return status;
}
/**
* Gets sets of licenses which have the same restrictions and the same
* prohibitions. The licenses in one set may differ in permissions.
*/
public void listDiffInPermission() {
Map<String, List<License>> map = new HashMap<>();
for (License licenseA : knowledgeBase.getLicenses()) {
List<License> list = new LinkedList<>();
String keyUri = licenseA.getUri();
loopB: for (License licenseB : knowledgeBase.getLicenses()) {
for (int i = 0; i < licenseA.getAttributes().getList().size(); i++) {
Attribute attributeA = licenseA.getAttributes().getList().get(i);
Attribute attributeB = licenseB.getAttributes().getList().get(i);
if (!attributeA.getType().equals(Permission.TYPE)) {
if (attributeA.getValue() != attributeB.getValue()) {
continue loopB;
}
}
}
list.add(licenseB);
if (keyUri.compareTo(licenseB.getUri()) < 0) {
keyUri = licenseB.getUri();
}
}
map.put(keyUri, list);
}
for (List<License> licenses : map.values()) {
for (License license : licenses) {
System.out.println(ArrayUtil.intString(license.getAttributes().getValuesArray()) + " " + license);
}
System.out.println();
}
}
}