Project

General

Profile

1
package eu.dnetlib.parthenos.rdf;
2

    
3
import java.util.Iterator;
4
import java.util.List;
5
import java.util.Set;
6

    
7
import com.google.common.collect.Iterators;
8
import com.google.common.collect.Lists;
9
import eu.dnetlib.parthenos.CRM;
10
import eu.dnetlib.parthenos.CRMpe;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.apache.jena.assembler.AssemblerHelp;
14
import org.apache.jena.rdf.model.RDFNode;
15
import org.apache.jena.rdf.model.Resource;
16
import org.apache.jena.rdf.model.Statement;
17
import org.apache.jena.rdf.model.StmtIterator;
18
import org.apache.jena.vocabulary.RDF;
19
import org.apache.jena.vocabulary.RDFS;
20
import org.springframework.stereotype.Component;
21

    
22
/**
23
 * Created by Alessia Bardi on 11/12/2017.
24
 *
25
 * @author Alessia Bardi
26
 */
27
@Component
28
public class ResourceReader {
29

    
30
	private static final Log log = LogFactory.getLog(ResourceReader.class);
31

    
32
	public String getTitle(final Resource resource) {
33
		String title = "";
34
		final Statement s = resource.getProperty(CRM.P1_is_identified_by);
35
		if (s != null) {
36
			RDFNode obj = s.getObject();
37
			if(obj.isLiteral()) title = obj.asLiteral().getLexicalForm();
38
		}
39
		return title;
40
	}
41

    
42
	public String getDescription(final Resource resource) {
43
		if (resource.hasProperty(CRM.P3_has_note)) {
44
			return resource.getProperty(CRM.P3_has_note).getString().replace("'", "\'");
45
		} else return "";
46
	}
47

    
48
	public String getLabel(final Resource resource) {
49
		if (resource.hasProperty(RDFS.label)) {
50
			return resource.getProperty(RDFS.label).getString().replace("'", "\'");
51
		} else return "";
52
	}
53

    
54
	public String getCompetence(final Resource resource) {
55
		String comp = "";
56
		if (resource.hasProperty(CRMpe.PP45_has_competency)) {
57
			Resource compRes = resource.getProperty(CRMpe.PP45_has_competency).getResource();
58
			comp = getLabel(compRes);
59
		}
60
		return comp;
61
	}
62

    
63
	public String getAvailability(final Resource resource) {
64
		//TODO: implement this when George adds the rel to the model
65
		return "";
66
	}
67

    
68
	public String getConditionOfUse(final Resource resource) {
69
		//TODO: implement me
70
		return "";
71
	}
72

    
73
	public Iterator<String> getRDFClassNames(final Resource resource){
74
		StmtIterator it = resource.listProperties(RDF.type);
75
		return Iterators.transform(it, f -> f.getResource().getLocalName());
76
	}
77

    
78
	public Iterator<String> getActivityTypes(final Resource resource){
79
		StmtIterator it = resource.listProperties(CRM.P2_has_type);
80
		return Iterators.transform(it, f -> f.getResource().asLiteral().getLexicalForm());
81
	}
82

    
83
	public Iterator<String> getProviderNames(final Resource resource){
84
		StmtIterator sit = resource.listProperties(CRMpe.PP2_provided_by);
85
		StmtIterator sit2 = resource.listProperties(CRMpe.PP25_has_maintaining_RI);
86
		return Iterators.transform(Iterators.concat(sit, sit2), f -> {
87
			Resource provider = f.getResource();
88
			if (provider.hasProperty(CRM.P1_is_identified_by))
89
				return provider.getProperty(CRM.P1_is_identified_by).getString();
90
			else return "";
91
		});
92
	}
93

    
94
	public Iterator<String> getProviderContactPoints(final Resource resource){
95
		StmtIterator it = resource.listProperties(CRMpe.PP2_provided_by);
96
		return Iterators.transform(it, f -> {
97
			Resource provider = f.getResource();
98
			if (provider.hasProperty(CRM.P76_has_contact_point)) {
99
				Resource contactPoint = provider.getPropertyResourceValue(CRM.P76_has_contact_point);
100
				return getLabel(contactPoint);
101
			}
102
			else return "";
103
		});
104
	}
105

    
106
	public Iterator<String> getResourceDirectContactPoints(final Resource resource){
107
		StmtIterator it = resource.listProperties(CRM.P76_has_contact_point);
108
		return Iterators.transform(it, f -> f.getResource().getURI());
109
	}
110

    
111
	public Iterator<String> getHostedStuff(final Resource resource){
112
		StmtIterator sit4 = resource.listProperties(CRMpe.PP4_hosts_object);
113
		StmtIterator sit6 = resource.listProperties(CRMpe.PP6_hosts_digital_object);
114
		StmtIterator sit7 = resource.listProperties(CRMpe.PP7_hosts_software_object);
115
		StmtIterator sit8 = resource.listProperties(CRMpe.PP8_hosts_dataset);
116
		Iterator<String>  it4 = Iterators.transform(sit4, f -> f.getResource().getURI());
117
		Iterator<String>  it6 = Iterators.transform(sit6, f -> f.getResource().getURI());
118
		Iterator<String>  it7 = Iterators.transform(sit7, f -> f.getResource().getURI());
119
		Iterator<String>  it8 = Iterators.transform(sit8, f -> f.getResource().getURI());
120
		return Iterators.concat(it4,it6, it7, it8);
121
	}
122

    
123
	public Iterator<String> getHostedBys(final Resource resource){
124
		StmtIterator sit4 = resource.listProperties(CRMpe.PP4i_is_object_hosted_by);
125
		StmtIterator sit6 = resource.listProperties(CRMpe.PP6i_is_digital_object_hosted_by);
126
		StmtIterator sit7 = resource.listProperties(CRMpe.PP7i_is_software_object_hosted_by);
127
		StmtIterator sit8 = resource.listProperties(CRMpe.PP8i_is_dataset_hosted_by);
128
		Iterator<String>  it6 = Iterators.transform(sit6, f -> f.getResource().getURI());
129
		Iterator<String>  it7 = Iterators.transform(sit7, f -> f.getResource().getURI());
130
		Iterator<String>  it8 = Iterators.transform(sit8, f -> f.getResource().getURI());
131
		return Iterators.concat(it6, it7, it8);
132
	}
133

    
134

    
135
	public Iterator<String> getCuratedObjects(final Resource resource){
136
		StmtIterator sit32 = resource.listProperties(CRMpe.PP32_curates);
137
		StmtIterator sit11 = resource.listProperties(CRMpe.PP11_curates_volatile_digital_object);
138
		StmtIterator sit12 = resource.listProperties(CRMpe.PP12_curates_volatile_software);
139
		StmtIterator sit13 = resource.listProperties(CRMpe.PP13_curates_volatile_dataset);
140
		Iterator<String>  it32 = Iterators.transform(sit32, f -> f.getResource().getURI());
141
		Iterator<String>  it11 = Iterators.transform(sit11, f -> f.getResource().getURI());
142
		Iterator<String>  it12 = Iterators.transform(sit12, f -> f.getResource().getURI());
143
		Iterator<String>  it13 = Iterators.transform(sit13, f -> f.getResource().getURI());
144
		return Iterators.concat(it32, it11, it12, it13);
145
	}
146

    
147
	public Iterator<String> getCuratorUrls(final Resource resource){
148
		StmtIterator sit32 = resource.listProperties(CRMpe.PP32i_is_curated_by);
149
		StmtIterator sit11 = resource.listProperties(CRMpe.PP11i_is_volatile_digital_object_curated_by);
150
		StmtIterator sit12 = resource.listProperties(CRMpe.PP12i_is_volatile_software_curated_by);
151
		StmtIterator sit13 = resource.listProperties(CRMpe.PP13i_is_volatile_dataset_curated_by);
152
		Iterator<String>  it32 = Iterators.transform(sit32, f -> f.getResource().getURI());
153
		Iterator<String>  it11 = Iterators.transform(sit11, f -> f.getResource().getURI());
154
		Iterator<String>  it12 = Iterators.transform(sit12, f -> f.getResource().getURI());
155
		Iterator<String>  it13 = Iterators.transform(sit13, f -> f.getResource().getURI());
156
		return Iterators.concat(it32, it11, it12, it13);
157
	}
158

    
159
	public Iterator<String> getDeliversOnRequest(final Resource resource){
160
		StmtIterator sit = resource.listProperties(CRMpe.PP15_delivers_on_request);
161
		return Iterators.transform(sit, f -> f.getResource().getURI());
162
	}
163

    
164
	public Iterator<String> getAccessPoints(final Resource resource){
165
		StmtIterator it = resource.listProperties(CRMpe.PP28_has_designated_access_point);
166
		return Iterators.transform(it, f -> f.getResource().getURI());
167
	}
168

    
169
	public Iterator<String> getDeclarativeTimes(final Resource resource){
170
		StmtIterator it = resource.listProperties(CRMpe.PP42_has_declarative_time);
171
		return Iterators.transform(it, f -> f.getString());
172
	}
173

    
174
	public Iterator<String> getProtocols(final Resource resource){
175
		StmtIterator it = resource.listProperties(CRMpe.PP29_uses_access_protocol);
176
		return Iterators.transform(it, f -> f.getResource().getURI());
177
	}
178

    
179
	public Iterator<String> getCurationPlans(final Resource resource){
180
		StmtIterator it = resource.listProperties(CRMpe.PP31_uses_curation_plan);
181
		return Iterators.transform(it, f -> f.getResource().getURI());
182
	}
183

    
184
	public Iterator<String> getMemberUrls(final Resource resource){
185
		StmtIterator it = resource.listProperties(CRM.P107_has_current_or_former_member);
186
		return Iterators.transform(it, f -> f.getResource().getURI());
187
	}
188

    
189
	public Iterator<String> isMemberOf(final Resource resource){
190
		StmtIterator it = resource.listProperties(CRM.P107i_is_current_or_former_member_of);
191
		return Iterators.transform(it, f -> f.getResource().getURI());
192
	}
193

    
194
	public Iterator<String> getProvidedServiceUrls(final Resource resource){
195
		StmtIterator it = resource.listProperties(CRMpe.PP2i_provides);
196
		return Iterators.transform(it, f -> f.getResource().getURI());
197
	}
198

    
199
	public Iterator<String> getIsPartOfUrls(final Resource resource){
200
		StmtIterator it = resource.listProperties(CRMpe.PP23i_is_dataset_part_of);
201
		return Iterators.transform(it, f -> f.getResource().getURI());
202
	}
203

    
204
	public Iterator<String> getHasPartUrls(final Resource resource){
205
		StmtIterator it = resource.listProperties(CRMpe.PP23_has_dataset_part);
206
		return Iterators.transform(it, f -> f.getResource().getURI());
207
	}
208

    
209
	public Iterator<String> getSubjects(final Resource resource){
210
		StmtIterator it = resource.listProperties(CRM.P129_is_about);
211
		return Iterators.transform(it, f -> getLabel(f.getResource()));
212
	}
213

    
214
	public List<String> getTemporalCoverages(final Resource resource){
215
		List<String> cov = Lists.newArrayList();
216
		StmtIterator it = resource.listProperties(CRM.P129_is_about);
217
		while(it.hasNext()){
218
			Resource r = it.next().getResource();
219
			if(r.hasProperty(RDF.type, CRM.E4_Period)){
220
				cov.add(getLabel(r));
221
			}
222
		}
223
		return cov;
224
	}
225

    
226
	public List<String> getSpatialCoverages(final Resource resource){
227
		List<String> cov = Lists.newArrayList();
228
		StmtIterator it = resource.listProperties(CRM.P129_is_about);
229
		while(it.hasNext()){
230
			Resource r = it.next().getResource();
231
			if(r.hasProperty(RDF.type, CRM.E53_Place)){
232
				cov.add(getLabel(r));
233
			}
234
		}
235
		return cov;
236
	}
237

    
238
	public Iterator<String> getOfferedServiceUrls(final Resource resource){
239
		StmtIterator it = resource.listProperties(CRMpe.PP1_currently_offers);
240
		return Iterators.transform(it, f -> f.getResource().getURI());
241
	}
242

    
243
	public Iterator<String> getStartTimes(final Resource resource){
244
		StmtIterator sit1 = resource.listProperties(CRM.P4_has_time_span);
245
		StmtIterator sit2 = resource.listProperties(CRM.P82a_begin_of_the_begin);
246
		Iterator<String>  it1 = Iterators.transform(sit1, f -> getLabel(f.getResource()));
247
		Iterator<String>  it2 = Iterators.transform(sit2, f -> getLabel(f.getResource()));
248
		return Iterators.concat(it1, it2);
249
	}
250

    
251
	public Iterator<String> getMaintainerUrls(final Resource resource){
252
		StmtIterator it = resource.listProperties(CRMpe.PP44_has_maintaining_team);
253
		return Iterators.transform(it, f -> f.getResource().getURI());
254
	}
255

    
256
	public List<String> getMaintainerContacts(final Resource resource){
257
		List<String> res = Lists.newArrayList();
258
		StmtIterator it = resource.listProperties(CRMpe.PP44_has_maintaining_team);
259
		while(it.hasNext()){
260
			Resource maintainer = it.next().getResource();
261
			Iterator<String> itM = getResourceDirectContactPoints(maintainer);
262
			while(itM.hasNext()){
263
				res.add(itM.next());
264
			}
265
		}
266
		return res;
267
	}
268

    
269

    
270

    
271
	/**
272
	 * Finds the most specific type of res.
273
	 *
274
	 * @param res          Resource you want to find the most specific type
275
	 * @param fallbackType Resource representing the type to return if there is no type or if we get AmbiguousSpecificTypeException
276
	 * @return Resource: the most specific type, if any. fallbackType otherwise
277
	 */
278
	public Resource findSpecificType(final Resource res, final Resource fallbackType) {
279
		Resource type = fallbackType;
280
		Set<Resource> types = AssemblerHelp.findSpecificTypes(res, fallbackType);
281
		if (types == null || types.isEmpty()) {
282
			log.warn("No specific type found. Returning the fallback type: " + fallbackType);
283
		}
284
		if (types.size() == 1) {
285
			type = types.iterator().next();
286
		}
287
		if (types.size() > 1) {
288
			log.warn("Found more than one possible specific type: choosing the first");
289
			types.stream().forEach((t) -> log.warn(t));
290
			type = types.iterator().next();
291
		}
292
		return type;
293
	}
294
}
(2-2/2)