1
|
package eu.dnetlib.utils.ontologies;
|
2
|
|
3
|
import com.google.common.collect.Maps;
|
4
|
import com.google.gson.Gson;
|
5
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
6
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
7
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
8
|
import org.apache.commons.io.IOUtils;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.dom4j.Document;
|
12
|
import org.dom4j.DocumentException;
|
13
|
import org.dom4j.Element;
|
14
|
import org.dom4j.Node;
|
15
|
import org.dom4j.io.SAXReader;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
18
|
import org.springframework.stereotype.Component;
|
19
|
|
20
|
import javax.annotation.PostConstruct;
|
21
|
import java.io.IOException;
|
22
|
import java.io.InputStream;
|
23
|
import java.io.StringReader;
|
24
|
import java.util.Arrays;
|
25
|
import java.util.List;
|
26
|
import java.util.Map;
|
27
|
import java.util.function.Function;
|
28
|
import java.util.stream.Collectors;
|
29
|
|
30
|
/**
|
31
|
* Created by claudio on 12/12/2016.
|
32
|
*/
|
33
|
@Component
|
34
|
public class OntologyLoader {
|
35
|
|
36
|
private static final Log log = LogFactory.getLog(OntologyLoader.class);
|
37
|
|
38
|
private static final String PATTERN = "classpath*:/eu/dnetlib/test/profiles/OntologyDSResources/OntologyDSResourceType/*.xml";
|
39
|
|
40
|
private static UniqueServiceLocator staticServiceLocator;
|
41
|
|
42
|
@Autowired
|
43
|
private UniqueServiceLocator serviceLocator;
|
44
|
|
45
|
|
46
|
// <TERM code="merges" encoding="organizationOrganization_dedup_merges" english_name="merges" native_name="merges">
|
47
|
public static String fetchInverse(final String relType) throws ISLookUpException {
|
48
|
final String xquery = "let $x:= /RESOURCE_PROFILE["
|
49
|
+ " .//RESOURCE_TYPE/@value = 'OntologyDSResourceType' and "
|
50
|
+ " .//TERM/@encoding='" + relType + "']"
|
51
|
+ "let $y:= $x//TERM[./@encoding='" + relType + "']//RELATION[./@type='inverseOf']/@code/string() "
|
52
|
+ "return $x//TERM[./@code = $y]/@encoding/string()";
|
53
|
return staticServiceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xquery);
|
54
|
}
|
55
|
|
56
|
@PostConstruct
|
57
|
public void init() {
|
58
|
OntologyLoader.staticServiceLocator = serviceLocator;
|
59
|
}
|
60
|
|
61
|
public static Ontology loadOntologyFromCp(final InputStream profile) {
|
62
|
return loadOntologyProfilefromPath(profile);
|
63
|
}
|
64
|
|
65
|
public static Ontologies loadOntologiesFromCp() throws IOException {
|
66
|
final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
|
67
|
final List<InputStream> files = Arrays.asList(resolver.getResources(PATTERN)).stream().map(r -> {
|
68
|
try {
|
69
|
return r.getInputStream();
|
70
|
} catch (IOException e) {
|
71
|
throw new RuntimeException(e);
|
72
|
}
|
73
|
}).collect(Collectors.toList());
|
74
|
|
75
|
return createOntologies(files);
|
76
|
}
|
77
|
|
78
|
public static Ontologies createOntologies(List<InputStream> files) {
|
79
|
final Ontologies ontologies = new Ontologies();
|
80
|
files.stream().map(
|
81
|
s -> loadOntologyProfilefromPath(s))
|
82
|
.collect(Collectors.toMap(Ontology::getCode, Function.identity()))
|
83
|
.entrySet()
|
84
|
.forEach(it -> ontologies.put(it.getKey(), it.getValue()));
|
85
|
return ontologies;
|
86
|
}
|
87
|
|
88
|
public static Ontologies loadOntologiesFromIS() throws ISLookUpException {
|
89
|
final ISLookUpService lookUpService = staticServiceLocator.getService(ISLookUpService.class);
|
90
|
List<String> files = lookUpService.quickSearchProfile("for $x in collection('/db/DRIVER/OntologyDSResources/OntologyDSResourceType') return $x");
|
91
|
final Ontologies ontologies = new Ontologies();
|
92
|
files.stream().map
|
93
|
(OntologyLoader::getOntologyFromProfile)
|
94
|
.collect(Collectors.toMap(Ontology::getCode, Function.identity()))
|
95
|
.entrySet()
|
96
|
.forEach(it -> ontologies.put(it.getKey(), it.getValue()));
|
97
|
return ontologies;
|
98
|
}
|
99
|
|
100
|
private static Ontology loadOntologyProfilefromPath(final InputStream profile) {
|
101
|
final String profileXML;
|
102
|
try {
|
103
|
profileXML = IOUtils.toString(profile);
|
104
|
} catch (IOException e) {
|
105
|
log.error(e);
|
106
|
return null;
|
107
|
}
|
108
|
return getOntologyFromProfile(profileXML);
|
109
|
}
|
110
|
|
111
|
private static Ontology getOntologyFromProfile(final String profile) {
|
112
|
SAXReader saxReader = new SAXReader();
|
113
|
Document doc = null;
|
114
|
StringReader reader = new StringReader(profile);
|
115
|
|
116
|
try {
|
117
|
doc = saxReader.read(reader);
|
118
|
} catch (DocumentException e) {
|
119
|
e.printStackTrace();
|
120
|
throw new RuntimeException(e);
|
121
|
}
|
122
|
return new Ontology()
|
123
|
.setCode(doc.valueOf("//ONTOLOGY_NAME/@code"))
|
124
|
.setDescription(doc.valueOf("//ONTOLOGY_DESCRIPTION/text()"))
|
125
|
.setTerms(asTerms(doc.selectNodes("//TERMS/TERM")));
|
126
|
}
|
127
|
|
128
|
private static Map<String,OntologyTerm> asTerms(final List list) {
|
129
|
final Map<String, OntologyTerm> terms = Maps.newHashMap();
|
130
|
if (list != null) {
|
131
|
list.forEach(o -> {
|
132
|
final Element e = (Element) o;
|
133
|
final OntologyTerm ot = OntologyTerm.newInstance()
|
134
|
.setCode(e.attributeValue("code"))
|
135
|
.setEncoding(e.attributeValue("encoding"))
|
136
|
.setEnglishName(e.attributeValue("english_name"))
|
137
|
.setNativeName(e.attributeValue("native_name"))
|
138
|
.setInverseCode(((Node) o).valueOf("./RELATIONS/RELATION[@type = 'inverseOf']/@code"));
|
139
|
|
140
|
terms.put(ot.getCode(), ot);
|
141
|
});
|
142
|
}
|
143
|
return terms;
|
144
|
}
|
145
|
|
146
|
|
147
|
}
|