Newer
Older
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
package fr.emse.gitlab.saref.jobs.library;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.Lang;
import fr.emse.gitlab.saref.jobs.AbstractJobRunner;
import fr.emse.gitlab.saref.utils.SAREF;
public class ReadOntologies extends AbstractJobRunner {
static final Map<String, String> PREFIXES = new HashMap<String, String>();
static {
PREFIXES.put("owl", "http://www.w3.org/2002/07/owl#");
PREFIXES.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
PREFIXES.put("xsd", "http://www.w3.org/2001/XMLSchema#");
PREFIXES.put("dcterms", "http://purl.org/dc/terms/");
PREFIXES.put("vann", "http://purl.org/vocab/vann/");
PREFIXES.put("foaf", "http://xmlns.com/foaf/0.1/");
PREFIXES.put("schema", "http://schema.org/");
PREFIXES.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
PREFIXES.put("voaf", "http://purl.org/vocommons/voaf#");
PREFIXES.put("dce", "http://purl.org/dc/elements/1.1/");
PREFIXES.put("dct", "http://purl.org/dc/terms/");
PREFIXES.put("xml", "http://www.w3.org/XML/1998/namespace/");
PREFIXES.put("saref", "https://saref.etsi.org/core/");
}
public ReadOntologies(File dir) {
super(dir);
}
@Override
protected void doJob0() {
String repoName = getRepoName();
String ontologyName = repoName.equals("saref-core") ? "saref.ttl" : repoName + ".ttl";
checkOntology(ontologyName);
checkExamples();
}
private void checkOntology(String ontologyName) {
File ontologyFile = new File(directory, "ontology/" + ontologyName);
Model model = ModelFactory.createDefaultModel();
try (FileInputStream input = new FileInputStream(ontologyFile)) {
model.read(input, null, Lang.TTL.getLabel());
} catch (Exception ex) {
error("Exception while reading the ontology file", ex);
return;
}
final Map<String, String> prefixes = model.getNsPrefixMap();
for(String s : PREFIXES.keySet()) {
if(prefixes.containsKey(s)) {
if(!prefixes.get(s).equals(PREFIXES.get(s))) {
failure(String.format("Prefix `%s:` in the ontology file is expected to be equal to `<%s>`. Got: `<%s>`", s, PREFIXES.get(s), prefixes.get(s)));
}
}
}
for(Map.Entry<String, String> entry : prefixes.entrySet()) {
String s = entry.getKey();
String l = entry.getValue();
if(l.contains("saref")) {
if(!l.matches(SAREF.REGEX_ONTO_SERIES_URI)) {
failure(String.format("Prefix `%s:` in the ontology file contains string \"saref\", but does not seem to match the official SAREF ontologies namespaces: `\\\\%s\\\\`. Got: `<%s>`", s, SAREF.REGEX_ONTO_SERIES_URI, l));
}
}
}
}
private void checkExamples() {
File dir = new File(directory, "examples");
try {
Files.walk(dir.toPath()).filter(p -> {
return p.endsWith(".ttl");
}).forEach(p -> {
File exampleFile = p.toFile();
Model model = ModelFactory.createDefaultModel();
try (FileInputStream input = new FileInputStream(exampleFile)) {
model.read(input, null, Lang.TTL.getLabel());
} catch (Exception ex) {
failure(String.format("Exception while reading the example file %s", exampleFile.toString()),
ex.getClass().getSimpleName(), ex.getMessage());
}
});
} catch (IOException ex) {
error("Exception while walking the example directory", ex);
}
}
private String getRepoName() {
String REGEX = "^(?<ext>saref-core|saref4[a-z][a-z][a-z][a-z])";
String name = directory.getName();
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(name);
if (!matcher.find()) {
error(String.format(
"The SAREF pipeline must be run inside a directory whose name begins with `saref-core`, or `saref4abcd` (where abcd can be any sequence of four letters). Got: %s",
name));
return null;
}
return matcher.group("ext");
}
}