forked from projekt-opal/licenses
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKnowledgeBase.java
83 lines (70 loc) · 2.18 KB
/
KnowledgeBase.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
package org.dice_research.opal.licenses;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import org.dice_research.opal.licenses.utils.ArrayUtil;
/**
* A knowledge base comprises known attributes and a list of known licenses.
*
* @author Adrian Wilke
*/
public class KnowledgeBase {
private Attributes attributes = new Attributes();
private LinkedHashMap<String, License> urisToLicenses = new LinkedHashMap<>();
private boolean attributesSorted = false;
public KnowledgeBase addAttribute(Attribute attribute) {
attributes.addAttribute(attribute);
attributesSorted = false;
return this;
}
public void addLicense(License license) {
urisToLicenses.put(license.getUri(), license);
}
/**
* Sorts the attributes in knowledge base if not sorted. Returns them.
*/
public Attributes getSortedAttributes() {
if (!attributesSorted) {
attributes.sort();
}
return attributes;
}
public License getLicense(String uri) {
return urisToLicenses.get(uri);
}
public List<License> getLicenses() {
return new ArrayList<License>(urisToLicenses.values());
}
public Set<String> getLicenseUris() {
return urisToLicenses.keySet();
}
public LinkedHashMap<String, License> getUrisToLicenses() {
return urisToLicenses;
}
public String toLines() {
StringBuilder stringBuilder = new StringBuilder();
for (Attribute attribute : getSortedAttributes().getList()) {
stringBuilder.append(attribute);
stringBuilder.append(System.lineSeparator());
}
List<License> licenses = getLicenses();
Collections.sort(licenses, new Comparator<License>() {
@Override
public int compare(License o1, License o2) {
return o1.getUri().compareTo(o2.getUri());
}
});
for (License license : licenses) {
stringBuilder.append(ArrayUtil.intString(license.getAttributes().getValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(ArrayUtil.intString(license.getAttributes().getInternalValuesArray()));
stringBuilder.append(" ");
stringBuilder.append(license.getUri());
stringBuilder.append(System.lineSeparator());
}
return stringBuilder.toString();
}
}