From 116902c028c5c498be74b5bbe55b78f50c99fb78 Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Mon, 13 Jun 2022 14:31:48 +0200 Subject: [PATCH 1/2] mapping relationship from trasformed records based on oaf:relation --- .../raw/AbstractMdRecordToOafMapper.java | 65 ++++++++++++--- .../dnetlib/dhp/oa/graph/raw/MappersTest.java | 82 +++++++++++++------ .../dnetlib/dhp/oa/graph/raw/oaf_record.xml | 7 ++ 3 files changed, 117 insertions(+), 37 deletions(-) diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java index 3e8ca1763f..bf337e6c9a 100644 --- a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java +++ b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java @@ -18,14 +18,7 @@ import static eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils.oaiIProvenance; import static eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils.qualifier; import static eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils.structuredProperty; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.*; import org.apache.commons.lang3.StringUtils; import org.dom4j.*; @@ -35,6 +28,7 @@ import com.google.common.collect.Sets; import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup; import eu.dnetlib.dhp.schema.common.ModelConstants; +import eu.dnetlib.dhp.schema.common.ModelSupport; import eu.dnetlib.dhp.schema.oaf.AccessRight; import eu.dnetlib.dhp.schema.oaf.Author; import eu.dnetlib.dhp.schema.oaf.Context; @@ -199,8 +193,13 @@ public abstract class AbstractMdRecordToOafMapper { final List oafs = Lists.newArrayList(entity); if (!oafs.isEmpty()) { - oafs.addAll(addProjectRels(doc, entity)); - oafs.addAll(addOtherResultRels(doc, entity)); + Set rels = Sets.newHashSet(); + + rels.addAll(addProjectRels(doc, entity)); + rels.addAll(addOtherResultRels(doc, entity)); + rels.addAll(addRelations(doc, entity)); + + oafs.addAll(rels); } return oafs; @@ -278,6 +277,52 @@ public abstract class AbstractMdRecordToOafMapper { return res; } + private List addRelations(Document doc, OafEntity entity) { + + final List rels = Lists.newArrayList(); + + for (Object o : doc.selectNodes("//oaf:relation")) { + Element element = (Element) o; + + final Relation rel = new Relation(); + rel.setCollectedfrom(entity.getCollectedfrom()); + rel.setDataInfo(entity.getDataInfo()); + rel.setLastupdatetimestamp(entity.getLastupdatetimestamp()); + + String relType = element.attributeValue("relType"); + String subRelType = element.attributeValue("subRelType"); + String relClass = element.attributeValue("relClass"); + String relClassInverse = ModelSupport + .findInverse(ModelSupport.rel(relType, subRelType, relClass)) + .getInverseRelClass(); + + rel.setSource(entity.getId()); + String target = StringUtils.trim(element.getText()); + + final String validationdDate = ((Node) o).valueOf("@validationDate"); + + if (StringUtils.isNotBlank(target)) { + + // TODO discover the target entity type with a dedicated attribute, e.g. @targetType. + final String[] parts = relType.split("(?=\\p{Upper})"); + final String targetType = parts[1].toLowerCase(); + final String targetId = createOpenaireId(targetType, target, true); + + rels + .add( + getRelation( + entity.getId(), targetId, relType, subRelType, relClass, entity, validationdDate)); + rels + .add( + getRelation( + targetId, entity.getId(), relType, subRelType, relClassInverse, entity, validationdDate)); + } + + } + + return rels; + } + protected Relation getRelation(final String source, final String target, final String relType, diff --git a/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/raw/MappersTest.java b/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/raw/MappersTest.java index 7bc2c6e7cf..dd69bae853 100644 --- a/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/raw/MappersTest.java +++ b/dhp-workflows/dhp-graph-mapper/src/test/java/eu/dnetlib/dhp/oa/graph/raw/MappersTest.java @@ -57,14 +57,10 @@ class MappersTest { final List list = new OafToOafMapper(vocs, false, true).processMdRecord(xml); - assertEquals(3, list.size()); - assertTrue(list.get(0) instanceof Publication); - assertTrue(list.get(1) instanceof Relation); - assertTrue(list.get(2) instanceof Relation); + assertEquals(1, list.stream().filter(o -> o instanceof Publication).count()); + assertEquals(4, list.stream().filter(o -> o instanceof Relation).count()); - final Publication p = (Publication) list.get(0); - final Relation r1 = (Relation) list.get(1); - final Relation r2 = (Relation) list.get(2); + Publication p = (Publication) list.stream().filter(o -> o instanceof Publication).findFirst().get(); assertValidId(p.getId()); @@ -125,26 +121,58 @@ class MappersTest { assertNotNull(p.getBestaccessright()); assertEquals("OPEN", p.getBestaccessright().getClassid()); - assertValidId(r1.getSource()); - assertValidId(r1.getTarget()); - assertValidId(r2.getSource()); - assertValidId(r2.getTarget()); - assertValidId(r1.getCollectedfrom().get(0).getKey()); - assertValidId(r2.getCollectedfrom().get(0).getKey()); - assertNotNull(r1.getDataInfo()); - assertNotNull(r2.getDataInfo()); - assertNotNull(r1.getDataInfo().getTrust()); - assertNotNull(r2.getDataInfo().getTrust()); - assertEquals(r1.getSource(), r2.getTarget()); - assertEquals(r2.getSource(), r1.getTarget()); - assertTrue(StringUtils.isNotBlank(r1.getRelClass())); - assertTrue(StringUtils.isNotBlank(r2.getRelClass())); - assertTrue(StringUtils.isNotBlank(r1.getRelType())); - assertTrue(StringUtils.isNotBlank(r2.getRelType())); - assertTrue(r1.getValidated()); - assertTrue(r2.getValidated()); - assertEquals("2020-01-01", r1.getValidationDate()); - assertEquals("2020-01-01", r2.getValidationDate()); + + // RESULT PROJECT + List resultProject = list + .stream() + .filter(o -> o instanceof Relation) + .map(o -> (Relation) o) + .filter(r -> ModelConstants.RESULT_PROJECT.equals(r.getRelType())) + .collect(Collectors.toList()); + + assertEquals(2, resultProject.size()); + final Relation rp1 = resultProject.get(0); + final Relation rp2 = resultProject.get(1); + + verifyRelation(rp1); + verifyRelation(rp2); + + assertTrue(rp1.getValidated()); + assertTrue(rp2.getValidated()); + assertEquals("2020-01-01", rp1.getValidationDate()); + assertEquals("2020-01-01", rp2.getValidationDate()); + + assertEquals(rp1.getSource(), rp2.getTarget()); + assertEquals(rp2.getSource(), rp1.getTarget()); + + // AFFILIATIONS + List affiliation = list + .stream() + .filter(o -> o instanceof Relation) + .map(o -> (Relation) o) + .filter(r -> ModelConstants.RESULT_ORGANIZATION.equals(r.getRelType())) + .collect(Collectors.toList()); + + assertEquals(2, affiliation.size()); + final Relation aff1 = affiliation.get(0); + final Relation aff2 = affiliation.get(1); + + verifyRelation(aff1); + verifyRelation(aff2); + + assertEquals(aff1.getSource(), aff2.getTarget()); + assertEquals(aff2.getSource(), aff1.getTarget()); + } + + private void verifyRelation(Relation r) { + assertValidId(r.getSource()); + assertValidId(r.getTarget()); + assertValidId(r.getCollectedfrom().get(0).getKey()); + assertNotNull(r.getDataInfo()); + assertNotNull(r.getDataInfo().getTrust()); + assertTrue(StringUtils.isNotBlank(r.getRelClass())); + assertTrue(StringUtils.isNotBlank(r.getRelType())); + } @Test diff --git a/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml b/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml index f4b0c477fb..2946ba0b57 100644 --- a/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml +++ b/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml @@ -60,6 +60,13 @@ https://oneecosystem.pensoft.net/article/13718/ One Ecosystem 0001 + ror_________::https://ror.org/02gdcn153 + corda_______::226852 From 4c8e820ff0af53cb2b86d6553281afb2839d1699 Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Tue, 14 Jun 2022 08:49:02 +0200 Subject: [PATCH 2/2] mapping relationship from trasformed records based on oaf:relation --- .../raw/AbstractMdRecordToOafMapper.java | 56 +++++++++---------- .../dnetlib/dhp/oa/graph/raw/oaf_record.xml | 4 +- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java index bf337e6c9a..739be3df42 100644 --- a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java +++ b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/AbstractMdRecordToOafMapper.java @@ -284,42 +284,36 @@ public abstract class AbstractMdRecordToOafMapper { for (Object o : doc.selectNodes("//oaf:relation")) { Element element = (Element) o; - final Relation rel = new Relation(); - rel.setCollectedfrom(entity.getCollectedfrom()); - rel.setDataInfo(entity.getDataInfo()); - rel.setLastupdatetimestamp(entity.getLastupdatetimestamp()); + final String target = StringUtils.trim(element.getText()); + final String relType = element.attributeValue("relType"); + final String subRelType = element.attributeValue("subRelType"); + final String relClass = element.attributeValue("relClass"); - String relType = element.attributeValue("relType"); - String subRelType = element.attributeValue("subRelType"); - String relClass = element.attributeValue("relClass"); - String relClassInverse = ModelSupport - .findInverse(ModelSupport.rel(relType, subRelType, relClass)) - .getInverseRelClass(); + if (StringUtils.isNotBlank(target) && StringUtils.isNotBlank(relType) && StringUtils.isNotBlank(subRelType) + && StringUtils.isNotBlank(relClass)) { - rel.setSource(entity.getId()); - String target = StringUtils.trim(element.getText()); + final String relClassInverse = ModelSupport + .findInverse(ModelSupport.rel(relType, subRelType, relClass)) + .getInverseRelClass(); + final String validationdDate = ((Node) o).valueOf("@validationDate"); - final String validationdDate = ((Node) o).valueOf("@validationDate"); - - if (StringUtils.isNotBlank(target)) { - - // TODO discover the target entity type with a dedicated attribute, e.g. @targetType. - final String[] parts = relType.split("(?=\\p{Upper})"); - final String targetType = parts[1].toLowerCase(); - final String targetId = createOpenaireId(targetType, target, true); - - rels - .add( - getRelation( - entity.getId(), targetId, relType, subRelType, relClass, entity, validationdDate)); - rels - .add( - getRelation( - targetId, entity.getId(), relType, subRelType, relClassInverse, entity, validationdDate)); + if (StringUtils.isNotBlank(target)) { + final String targetType = element.attributeValue("targetType"); + if (StringUtils.isNotBlank(targetType)) { + final String targetId = createOpenaireId(targetType, target, true); + rels + .add( + getRelation( + entity.getId(), targetId, relType, subRelType, relClass, entity, validationdDate)); + rels + .add( + getRelation( + targetId, entity.getId(), relType, subRelType, relClassInverse, entity, + validationdDate)); + } + } } - } - return rels; } diff --git a/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml b/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml index 2946ba0b57..2775781856 100644 --- a/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml +++ b/dhp-workflows/dhp-graph-mapper/src/test/resources/eu/dnetlib/dhp/oa/graph/raw/oaf_record.xml @@ -62,10 +62,12 @@ 0001 ror_________::https://ror.org/02gdcn153 + subRelType="affiliation" + targetType="organization">ror_________::https://ror.org/02gdcn153 corda_______::226852