forked from projekt-opal/licenses
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CcExperiment.java
202 lines (173 loc) · 5.78 KB
/
CcExperiment.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
package org.dice_research.opal.licenses.cc;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
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.KnowledgeBase;
import org.dice_research.opal.licenses.License;
import org.dice_research.opal.licenses.utils.Cfg;
/**
* Experiment to evaluate the compatibility results of 8 CC licenses.
*
* @author Adrian Wilke
*/
public class CcExperiment {
private static final Logger LOGGER = LogManager.getLogger();
public static void main(String[] args) throws Exception {
new CcExperiment().execute(Cfg.getCcLicenseRdf());
}
public void execute(String dataDirectory) {
// Check availability of data
if (!new File(dataDirectory).exists()) {
LOGGER.error("Directory not found: " + new File(dataDirectory).getAbsolutePath());
}
// Get data
CcData data = new CcData().setSourceDirectory(dataDirectory);
List<File> files = data.getMatixFiles();
KnowledgeBase knowledgeBase = data.createKnowledgeBase(files);
// Execute
List<ResultContainer> results = new LinkedList<>();
for (License licenseA : knowledgeBase.getLicenses()) {
for (License licenseB : knowledgeBase.getLicenses()) {
List<License> inputLicenses = new ArrayList<>(2);
inputLicenses.add(licenseA);
inputLicenses.add(licenseB);
// Operator used to compute array of internal values
Execution execution = new Execution().setKnowledgeBase(knowledgeBase);
Attributes resultAttributes = execution.applyOperator(inputLicenses);
// Back-mapping
Set<License> resultingLicenses = new BackMapping().getCompatibleLicenses(inputLicenses,
resultAttributes, knowledgeBase);
results.add(new ResultContainer(licenseA, licenseB, resultingLicenses));
}
}
for (ResultContainer result : results) {
System.out.println(result);
}
System.out.println();
System.out.println(toTable(results, true));
System.out.println();
System.out.println(toTable(results, false));
}
private String toTable(List<ResultContainer> results, boolean limitToInput) {
StringBuilder stringBuilder = new StringBuilder();
String divider = "\t";
// Header
stringBuilder.append("-");
stringBuilder.append(divider);
ResultContainer lastResult = new ResultContainer(null, null, null);
for (ResultContainer result : results) {
if (!limitToInput && result.inputContainsNd()) {
continue;
}
if (result.licenseA != lastResult.licenseA) {
stringBuilder.append(replaceName(result.licenseA.getName()));
stringBuilder.append(divider);
lastResult = result;
}
}
// Value rows
lastResult = new ResultContainer(null, null, null);
for (ResultContainer result : results) {
if (!limitToInput && result.inputContainsNd()) {
continue;
}
if (result.licenseA != lastResult.licenseA) {
stringBuilder.append(System.lineSeparator());
stringBuilder.append(replaceName(result.licenseA.getName()));
stringBuilder.append(divider);
}
if (result.resultingLicenses.isEmpty()) {
stringBuilder.append("-");
} else {
Set<License> resultingLicenses = new HashSet<License>(result.resultingLicenses);
if (limitToInput) {
List<License> inputLicenses = new LinkedList<>();
inputLicenses.add(result.licenseA);
inputLicenses.add(result.licenseB);
resultingLicenses.retainAll(inputLicenses);
}
if (resultingLicenses.size() == 8) {
stringBuilder.append("all");
} else {
stringBuilder.append(setToString(resultingLicenses, divider));
}
}
stringBuilder.append(divider);
lastResult = result;
}
stringBuilder.append(System.lineSeparator());
return stringBuilder.toString();
}
private StringBuilder setToString(Set<License> licenses, String divider) {
TreeSet<License> orderdLicenses = new TreeSet<>(new Comparator<License>() {
@Override
public int compare(License o1, License o2) {
return o1.getName().compareTo(o2.getName());
}
});
orderdLicenses.addAll(licenses);
StringBuilder stringBuilder = new StringBuilder();
boolean first = true;
for (License license : orderdLicenses) {
if (first) {
first = false;
} else {
stringBuilder.append(", ");
}
stringBuilder.append(replaceName(license.getName()));
}
return stringBuilder;
}
private String replaceName(String name) {
if (name.equals("mark")) {
return "PD";
}
return name.toUpperCase();
}
public class ResultContainer {
public License licenseA;
public License licenseB;
public Set<License> resultingLicenses;
public ResultContainer(License licenseA, License licenseB, Set<License> resultingLicenses) {
this.licenseA = licenseA;
this.licenseB = licenseB;
this.resultingLicenses = resultingLicenses;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(licenseA.getName());
stringBuilder.append(", ");
stringBuilder.append(licenseB.getName());
stringBuilder.append(": ");
for (int i = 0; i < 16 - licenseA.getName().length() - licenseB.getName().length(); i++) {
stringBuilder.append(" ");
}
for (License license : resultingLicenses) {
stringBuilder.append(license.getName());
stringBuilder.append(" ");
}
if (resultingLicenses.isEmpty()) {
stringBuilder.append("-");
}
return stringBuilder.toString();
}
private boolean inputContainsNd() {
if (licenseA.getName().toLowerCase().contains("nd") || licenseB.getName().toLowerCase().contains("nd")) {
return true;
} else {
return false;
}
}
}
}