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 it = resource.listProperties(CRMpe.PP2_provided_by);
85
		return Iterators.transform(it, f -> {
86
			Resource provider = f.getResource();
87
			if (provider.hasProperty(CRM.P1_is_identified_by))
88
				return provider.getProperty(CRM.P1_is_identified_by).getString();
89
			else return "";
90
		});
91
	}
92

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

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

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

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

    
133

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
268

    
269

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