1
|
package eu.dnetlib.data.mapreduce.hbase.bulktag;
|
2
|
|
3
|
import eu.dnetlib.data.bulktag.CommunityConfiguration;
|
4
|
import eu.dnetlib.data.bulktag.Pair;
|
5
|
import eu.dnetlib.data.proto.FieldTypeProtos;
|
6
|
import eu.dnetlib.data.proto.OafProtos;
|
7
|
import eu.dnetlib.data.proto.ResultProtos;
|
8
|
import org.apache.hadoop.mapreduce.Mapper;
|
9
|
|
10
|
import java.util.*;
|
11
|
import java.util.stream.Collectors;
|
12
|
|
13
|
/**
|
14
|
* Created by miriam on 02/08/2018.
|
15
|
*/
|
16
|
public class ResultTagger {
|
17
|
private final static String DATA_INFO_TYPE = "bulktagging::community";
|
18
|
private final static String SCHEMA_NAME = "dnet:provenanceActions";
|
19
|
private final static String CLASS_ID = "bulktagging::community";
|
20
|
private final static String SCHEMA_ID = "dnet:provenanceActions";
|
21
|
private final static String COUNTER_GROUP = "Bulk Tagging";
|
22
|
|
23
|
|
24
|
public OafProtos.Oaf enrichContext(final OafProtos.Oaf oaf, final CommunityConfiguration conf, final Mapper.Context context) {
|
25
|
|
26
|
final OafProtos.Oaf.Builder builder = OafProtos.Oaf.newBuilder(oaf);
|
27
|
|
28
|
|
29
|
if(oaf.getDataInfo().getDeletedbyinference())
|
30
|
return null;
|
31
|
|
32
|
final List<ResultProtos.Result.Context> contextList = oaf.getEntity().getResult().getMetadata().getContextList();
|
33
|
|
34
|
//communities contains all the communities to be added as context for the result
|
35
|
final Set<String> communities = new HashSet<>();
|
36
|
List<FieldTypeProtos.StructuredProperty> subjectSet = oaf.getEntity().getResult().getMetadata().getSubjectList();
|
37
|
communities.addAll(analiseSubjects(subjectSet, conf));
|
38
|
|
39
|
List<Pair<String, String>> datasourceSet = oaf.getEntity().getResult().getInstanceList()
|
40
|
.stream()
|
41
|
.map(i -> new Pair<>(i.getCollectedfrom().getKey(), i.getHostedby().getKey()))
|
42
|
.collect(Collectors.toList());
|
43
|
|
44
|
communities.addAll(analiseDatasource(datasourceSet, conf));
|
45
|
|
46
|
//TODO: add code for Zenodo Communities
|
47
|
|
48
|
|
49
|
final Map<String,Pair<Integer,ResultProtos.Result.Context>> contexts = new HashMap<>();
|
50
|
|
51
|
for(int i = 0; i<contextList.size(); i++){
|
52
|
contexts.put(contextList.get(i).getId(),new Pair<>(i,contextList.get(i)));
|
53
|
}
|
54
|
|
55
|
for(String contextId:communities){
|
56
|
final Pair<Integer,ResultProtos.Result.Context> pair = contexts.get(contextId);
|
57
|
ResultProtos.Result.Context c;
|
58
|
if(pair == null)
|
59
|
c = null;
|
60
|
else
|
61
|
c = pair.getSnd();
|
62
|
if (c != null) {
|
63
|
// add new dataInfo if needed
|
64
|
|
65
|
Set<String> set = new HashSet<>();
|
66
|
set.addAll(c.getDataInfoList().stream().map(datainfo->datainfo.getInferenceprovenance()).collect(Collectors.toList()));
|
67
|
if (!set.contains(DATA_INFO_TYPE)) {
|
68
|
builder.getEntityBuilder().getResultBuilder().getMetadataBuilder().getContextBuilder(pair.getFst()).addDataInfo(buildDataInfo());
|
69
|
|
70
|
context.getCounter(COUNTER_GROUP, "add provenance").increment(1);
|
71
|
}
|
72
|
|
73
|
|
74
|
} else {
|
75
|
builder.getEntityBuilder().getResultBuilder().getMetadataBuilder().addContext(buildContext(contextId));
|
76
|
|
77
|
context.getCounter(COUNTER_GROUP, "add context").increment(1);
|
78
|
|
79
|
}
|
80
|
}
|
81
|
|
82
|
return builder.build();
|
83
|
}
|
84
|
|
85
|
private Set<String> analiseDatasource(List<Pair<String, String>> datasourceSet, final CommunityConfiguration conf) {
|
86
|
Set<String> set = new HashSet<>();
|
87
|
for(Pair<String,String> d: datasourceSet){
|
88
|
if(!d.getFst().equalsIgnoreCase(d.getSnd())){
|
89
|
set.addAll(conf.getCommunityForDatasourceValue(d.getFst().substring(d.getFst().indexOf("|")+1)));
|
90
|
}
|
91
|
set.addAll((conf.getCommunityForDatasourceValue(d.getSnd().substring(d.getSnd().indexOf("|")+1))));
|
92
|
}
|
93
|
return set;
|
94
|
}
|
95
|
|
96
|
private Set<String> analiseSubjects(List<FieldTypeProtos.StructuredProperty> subjectList, final CommunityConfiguration conf) {
|
97
|
Set<String> set = new HashSet<>();
|
98
|
for(FieldTypeProtos.StructuredProperty s:subjectList){
|
99
|
set.addAll(conf.getCommunityForSubjectValue( s.getValue()));
|
100
|
}
|
101
|
|
102
|
return set;
|
103
|
}
|
104
|
|
105
|
private ResultProtos.Result.Context buildContext(final String contextId) {
|
106
|
return ResultProtos.Result.Context.newBuilder()
|
107
|
.setId(contextId)
|
108
|
.addDataInfo(buildDataInfo())
|
109
|
.build();
|
110
|
}
|
111
|
|
112
|
private FieldTypeProtos.DataInfo buildDataInfo() {
|
113
|
FieldTypeProtos.DataInfo.Builder builder = FieldTypeProtos.DataInfo.newBuilder()
|
114
|
.setInferred(true)
|
115
|
.setProvenanceaction(
|
116
|
FieldTypeProtos.Qualifier.newBuilder()
|
117
|
.setClassid(CLASS_ID)
|
118
|
.setClassname("Bulk Tagging for Communities")
|
119
|
.setSchemeid(SCHEMA_ID)
|
120
|
.setSchemename(SCHEMA_NAME))
|
121
|
.setInferenceprovenance(DATA_INFO_TYPE)
|
122
|
.setTrust("0.85");
|
123
|
return builder
|
124
|
.build();
|
125
|
}
|
126
|
|
127
|
|
128
|
}
|