From 79c4f1bbd853dce7d0009b6a369a7dacab3ae1f8 Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Thu, 24 Oct 2019 16:56:13 +0200 Subject: [PATCH] Protobuf to internal graph model, early steps --- .../eu/dnetlib/dhp/graph/ProtoConverter.java | 46 ++++++++++------- .../java/eu/dnetlib/dhp/graph/ProtoUtils.java | 49 +++++++++++++++++++ 2 files changed, 76 insertions(+), 19 deletions(-) diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoConverter.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoConverter.java index 9f22d7875..cc5ba181c 100644 --- a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoConverter.java +++ b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoConverter.java @@ -1,37 +1,45 @@ package eu.dnetlib.dhp.graph; - -import com.googlecode.protobuf.format.JsonFormat; import eu.dnetlib.data.proto.KindProtos; import eu.dnetlib.data.proto.OafProtos; import eu.dnetlib.dhp.schema.oaf.*; import java.io.Serializable; +import java.util.stream.Collectors; +import static eu.dnetlib.dhp.graph.ProtoUtils.mapKV; public class ProtoConverter implements Serializable { - public static Oaf convert(String s) { try { - final OafProtos.Oaf.Builder builder = OafProtos.Oaf.newBuilder(); - JsonFormat.merge(s, builder); + OafProtos.Oaf oaf = ProtoUtils.parse(s); - if (builder.getKind() == KindProtos.Kind.entity) - return convertEntity(builder); + if (oaf.getKind() == KindProtos.Kind.entity) + return convertEntity(oaf); else { - return convertRelation(builder); + return convertRelation(oaf); } } catch (Throwable e) { throw new RuntimeException(e); } } - private static Relation convertRelation(OafProtos.Oaf.Builder oaf) { - return new Relation(); + private static Relation convertRelation(OafProtos.Oaf oaf) { + final Relation rel = new Relation(); + final OafProtos.OafRel r = oaf.getRel(); + return rel + .setSource(r.getSource()) + .setTarget(r.getTarget()) + .setRelType(r.getRelType().toString()) + .setSubRelType(r.getSubRelType().toString()) + .setRelClass(r.getRelClass()) + .setCollectedFrom(r.getCollectedfromList().stream() + .map(kv -> mapKV(kv)) + .collect(Collectors.toList())); } - private static OafEntity convertEntity(OafProtos.Oaf.Builder oaf) { + private static OafEntity convertEntity(OafProtos.Oaf oaf) { switch (oaf.getEntity().getType()) { case result: @@ -47,19 +55,19 @@ public class ProtoConverter implements Serializable { } } - private static Organization convertOrganization(OafProtos.Oaf.Builder oaf) { + private static Organization convertOrganization(OafProtos.Oaf oaf) { return new Organization(); } - private static Datasource convertDataSource(OafProtos.Oaf.Builder oaf) { + private static Datasource convertDataSource(OafProtos.Oaf oaf) { return new Datasource(); } - private static Project convertProject(OafProtos.Oaf.Builder oaf) { + private static Project convertProject(OafProtos.Oaf oaf) { return new Project(); } - private static Result convertResult(OafProtos.Oaf.Builder oaf) { + private static Result convertResult(OafProtos.Oaf oaf) { switch (oaf.getEntity().getResult().getMetadata().getResulttype().getClassid()) { case "dataset": return createDataset(oaf); @@ -74,19 +82,19 @@ public class ProtoConverter implements Serializable { } } - private static Software createSoftware(OafProtos.Oaf.Builder oaf) { + private static Software createSoftware(OafProtos.Oaf oaf) { return new Software(); } - private static OtherResearchProducts createORP(OafProtos.Oaf.Builder oaf) { + private static OtherResearchProducts createORP(OafProtos.Oaf oaf) { return new OtherResearchProducts(); } - private static Publication createPublication(OafProtos.Oaf.Builder oaf) { + private static Publication createPublication(OafProtos.Oaf oaf) { return new Publication(); } - private static Dataset createDataset(OafProtos.Oaf.Builder oaf) { + private static Dataset createDataset(OafProtos.Oaf oaf) { return new Dataset(); } } diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoUtils.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoUtils.java index b0f82ab00..0c8ffda30 100644 --- a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoUtils.java +++ b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/graph/ProtoUtils.java @@ -1,4 +1,53 @@ package eu.dnetlib.dhp.graph; +import com.googlecode.protobuf.format.JsonFormat; +import eu.dnetlib.data.proto.FieldTypeProtos; +import eu.dnetlib.data.proto.OafProtos; +import eu.dnetlib.dhp.schema.oaf.DataInfo; +import eu.dnetlib.dhp.schema.oaf.KeyValue; +import eu.dnetlib.dhp.schema.oaf.Qualifier; +import eu.dnetlib.dhp.schema.oaf.StructuredProperty; + +import java.util.List; + public class ProtoUtils { + + public static OafProtos.Oaf parse(String json) throws JsonFormat.ParseException { + final OafProtos.Oaf.Builder builder = OafProtos.Oaf.newBuilder(); + JsonFormat.merge(json, builder); + return builder.build(); + } + + public static KeyValue mapKV(FieldTypeProtos.KeyValue kv) { + return new KeyValue() + .setKey(kv.getKey()) + .setValue(kv.getValue()) + .setDataInfo(mapDataInfo(kv.getDataInfo())); + } + + public static DataInfo mapDataInfo(FieldTypeProtos.DataInfo d) { + return new DataInfo() + .setDeletedbyinference(d.getDeletedbyinference()) + .setInferenceprovenance(d.getInferenceprovenance()) + .setInferred(d.getInferred()) + .setInvisible(d.getInvisible()) + .setProvenanceaction(mapQualifier(d.getProvenanceaction())); + } + + public static Qualifier mapQualifier(FieldTypeProtos.Qualifier q) { + return new Qualifier() + .setClassid(q.getClassid()) + .setClassname(q.getClassname()) + .setSchemeid(q.getSchemeid()) + .setSchemename(q.getSchemename()) + .setDataInfo(q.hasDataInfo() ? mapDataInfo(q.getDataInfo()) : null); + } + + public static StructuredProperty mapStructuredProperty(FieldTypeProtos.StructuredProperty sp) { + return new StructuredProperty() + .setValue(sp.getValue()) + .setQualifier(mapQualifier(sp.getQualifier())) + .setDataInfo(sp.hasDataInfo() ? mapDataInfo(sp.getDataInfo()) : null); + } + }