1
|
package eu.dnetlib.data.graph.model;
|
2
|
|
3
|
import com.google.common.base.Function;
|
4
|
import com.google.common.base.Predicate;
|
5
|
import com.google.common.collect.Iterables;
|
6
|
import com.google.common.collect.Lists;
|
7
|
import com.google.common.collect.Sets;
|
8
|
import com.google.protobuf.Descriptors;
|
9
|
import com.google.protobuf.GeneratedMessage;
|
10
|
import eu.dnetlib.data.proto.*;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Set;
|
14
|
|
15
|
/**
|
16
|
* Created by sandro on 12/13/16.
|
17
|
*/
|
18
|
public class DNGFUtils {
|
19
|
|
20
|
public static Set<String> entities() {
|
21
|
return Sets.newHashSet(Iterables.transform(Lists.newArrayList(TypeProtos.Type.values()), t -> t.toString()));
|
22
|
}
|
23
|
|
24
|
public static Predicate<DNGFProtos.DNGF> relationFilter() {
|
25
|
return dngf -> dngf.getKind().equals(KindProtos.Kind.relation);
|
26
|
}
|
27
|
|
28
|
public static Predicate<DNGFProtos.DNGF> entityFilter() {
|
29
|
return dngf -> dngf.getKind().equals(KindProtos.Kind.entity);
|
30
|
}
|
31
|
|
32
|
public static Function<DNGFDecoder, String> idDecoder() {
|
33
|
return input -> input.getEntityId();
|
34
|
}
|
35
|
|
36
|
public static Predicate<FieldTypeProtos.StructuredProperty> mainTitleFilter() {
|
37
|
return sp -> (sp.getQualifier() != null) && sp.getQualifier().getClassname().equals("main title");
|
38
|
}
|
39
|
|
40
|
public static Set<String> getFieldNames(final Descriptors.Descriptor d, final Integer... tag) {
|
41
|
return Sets.newHashSet(Iterables.transform(Arrays.asList(tag), i -> {
|
42
|
final Descriptors.FieldDescriptor fd = d.findFieldByNumber(i);
|
43
|
if (fd == null) throw new IllegalArgumentException("undefined tag: " + i + " for type: " + d.getFullName());
|
44
|
return fd.getName();
|
45
|
}));
|
46
|
}
|
47
|
|
48
|
public static FieldTypeProtos.StringField sf(final String value) {
|
49
|
return FieldTypeProtos.StringField.newBuilder().setValue(value).build();
|
50
|
}
|
51
|
|
52
|
public static FieldTypeProtos.StructuredProperty sp(final String value, final String classid, final String schemeid) {
|
53
|
return sp(value, classid, schemeid, di());
|
54
|
}
|
55
|
|
56
|
public static FieldTypeProtos.StructuredProperty sp(final String value, final String classid, final String schemeid, final FieldTypeProtos.DataInfo di) {
|
57
|
return FieldTypeProtos.StructuredProperty.newBuilder().setValue(value).setQualifier(simpleQualifier(classid, schemeid)).setDataInfo(di).build();
|
58
|
}
|
59
|
|
60
|
public static FieldTypeProtos.KeyValue kv(final String k, final String v) {
|
61
|
return FieldTypeProtos.KeyValue.newBuilder().setKey(k).setValue(v).build();
|
62
|
}
|
63
|
|
64
|
public static FieldTypeProtos.Qualifier simpleQualifier(final String classid, final String schemeid) {
|
65
|
return q(classid, classid, schemeid, schemeid);
|
66
|
}
|
67
|
|
68
|
public static FieldTypeProtos.Qualifier q(final String classid, final String classname, final String schemeid, final String schemename) {
|
69
|
return FieldTypeProtos.Qualifier.newBuilder()
|
70
|
.setClassid(classid)
|
71
|
.setClassname(classname)
|
72
|
.setSchemename(schemeid)
|
73
|
.setSchemeid(schemename).build();
|
74
|
}
|
75
|
|
76
|
public static FieldTypeProtos.DataInfo di(final String trust) {
|
77
|
return di(false, "", false, simpleQualifier("", ""), trust);
|
78
|
}
|
79
|
|
80
|
public static FieldTypeProtos.DataInfo di() {
|
81
|
return di("");
|
82
|
}
|
83
|
|
84
|
public static FieldTypeProtos.DataInfo di(Boolean deletedByInference, String inferenceProvenance, Boolean inferred, FieldTypeProtos.Qualifier provenanceAction, String trust) {
|
85
|
return FieldTypeProtos.DataInfo.newBuilder()
|
86
|
.setDeletedbyinference(deletedByInference)
|
87
|
.setInferenceprovenance(inferenceProvenance)
|
88
|
.setInferred(inferred)
|
89
|
.setProvenanceaction(provenanceAction)
|
90
|
.setTrust(trust).build();
|
91
|
}
|
92
|
|
93
|
public static FieldTypeProtos.Context context(final String id) {
|
94
|
return FieldTypeProtos.Context.newBuilder().setId(id).build();
|
95
|
}
|
96
|
|
97
|
|
98
|
public static DNGFDecoder embed(final GeneratedMessage msg,
|
99
|
final KindProtos.Kind kind,
|
100
|
final boolean deletedByInference,
|
101
|
final boolean inferred,
|
102
|
final String provenance,
|
103
|
final String action) {
|
104
|
|
105
|
final DNGFProtos.DNGF.Builder DNGF = DNGFProtos.DNGF
|
106
|
.newBuilder()
|
107
|
.setKind(kind)
|
108
|
.setLastupdatetimestamp(System.currentTimeMillis())
|
109
|
.setDataInfo(di(deletedByInference, provenance, inferred, simpleQualifier(action, action), "0.5"));
|
110
|
switch (kind) {
|
111
|
case entity:
|
112
|
DNGF.setEntity((DNGFProtos.DNGFEntity) msg);
|
113
|
break;
|
114
|
case relation:
|
115
|
DNGF.setRel((DNGFProtos.DNGFRel) msg);
|
116
|
break;
|
117
|
default:
|
118
|
break;
|
119
|
}
|
120
|
|
121
|
return DNGFDecoder.decode(DNGF.build());
|
122
|
}
|
123
|
|
124
|
public static DNGFDecoder embed(final GeneratedMessage msg, final KindProtos.Kind kind) {
|
125
|
return embed(msg, kind, false, false, "inference_provenance", "provenance_action");
|
126
|
}
|
127
|
|
128
|
|
129
|
}
|