Mapping all relationships supported in ModelConstants and ModelSupport

This commit is contained in:
Alessia Bardi 2022-09-26 11:24:13 +02:00
parent c5eb722170
commit fd63e9bfac
5 changed files with 59 additions and 62 deletions

View File

@ -69,13 +69,15 @@ public abstract class AbstractMdRecordToOafMapper {
nsContext.put("datacite", DATACITE_SCHEMA_KERNEL_3); nsContext.put("datacite", DATACITE_SCHEMA_KERNEL_3);
} }
protected static final Set<String> pidTypeWithAuthority = new HashSet<>(); // lowercase pidTypes as keys, normal casing for the values
protected static final Map<String, String> pidTypeWithAuthority = new HashMap<>();
static { static {
pidTypeWithAuthority.addAll(IdentifierFactory.PID_AUTHORITY.keySet().stream() IdentifierFactory.PID_AUTHORITY
.map(PidType::toString) .keySet()
.map(String::toLowerCase) .stream()
.collect(Collectors.toCollection(HashSet::new))); .forEach(entry -> pidTypeWithAuthority.put(entry.toString().toLowerCase(), entry.toString()));
} }
protected AbstractMdRecordToOafMapper(final VocabularyGroup vocs, final boolean invisible, protected AbstractMdRecordToOafMapper(final VocabularyGroup vocs, final boolean invisible,

View File

@ -22,6 +22,8 @@ import com.google.common.collect.Lists;
import eu.dnetlib.dhp.common.PacePerson; import eu.dnetlib.dhp.common.PacePerson;
import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup; import eu.dnetlib.dhp.common.vocabulary.VocabularyGroup;
import eu.dnetlib.dhp.schema.common.ModelSupport;
import eu.dnetlib.dhp.schema.common.RelationInverse;
import eu.dnetlib.dhp.schema.oaf.*; import eu.dnetlib.dhp.schema.oaf.*;
import eu.dnetlib.dhp.schema.oaf.utils.CleaningFunctions; import eu.dnetlib.dhp.schema.oaf.utils.CleaningFunctions;
import eu.dnetlib.dhp.schema.oaf.utils.IdentifierFactory; import eu.dnetlib.dhp.schema.oaf.utils.IdentifierFactory;
@ -400,71 +402,47 @@ public class OdfToOafMapper extends AbstractMdRecordToOafMapper {
if (StringUtils.isNotBlank(originalId)) { if (StringUtils.isNotBlank(originalId)) {
final String idType = ((Node) o).valueOf("@relatedIdentifierType"); final String idType = ((Node) o).valueOf("@relatedIdentifierType");
final String reltype = ((Node) o).valueOf("@relationType"); final String relType = ((Node) o).valueOf("@relationType");
String otherId = guessRelatedIdentifier(idType, originalId); String otherId = guessRelatedIdentifier(idType, originalId);
if (StringUtils.isNotBlank(otherId)) { if (StringUtils.isNotBlank(otherId)) {
if (reltype.equalsIgnoreCase(IS_SUPPLEMENT_TO)) { res.addAll(getRelations(relType, docId, otherId, entity));
res
.add(
getRelation(
docId, otherId, RESULT_RESULT, SUPPLEMENT, IS_SUPPLEMENT_TO, entity));
res
.add(
getRelation(
otherId, docId, RESULT_RESULT, SUPPLEMENT, IS_SUPPLEMENTED_BY, entity));
} else {
if (reltype.equalsIgnoreCase(IS_SUPPLEMENTED_BY)) {
res
.add(
getRelation(
otherId, docId, RESULT_RESULT, SUPPLEMENT, IS_SUPPLEMENT_TO, entity));
res
.add(
getRelation(
docId, otherId, RESULT_RESULT, SUPPLEMENT, IS_SUPPLEMENTED_BY, entity));
} else {
if (reltype.equalsIgnoreCase(IS_PART_OF)) {
res
.add(
getRelation(
docId, otherId, RESULT_RESULT, PART, IS_PART_OF, entity));
res
.add(
getRelation(
otherId, docId, RESULT_RESULT, PART, HAS_PART, entity));
} else {
if (reltype.equalsIgnoreCase(HAS_PART)) {
res
.add(
getRelation(
otherId, docId, RESULT_RESULT, PART, IS_PART_OF, entity));
res
.add(
getRelation(
docId, otherId, RESULT_RESULT, PART, HAS_PART, entity));
}
// else TODO catch more semantics
}
}
} }
} }
} }
}
return res; return res;
} }
protected String guessRelatedIdentifier(final String idType, final String value) { protected String guessRelatedIdentifier(final String idType, final String value) {
if (StringUtils.isBlank(idType) || StringUtils.isBlank(value)) if (StringUtils.isBlank(idType) || StringUtils.isBlank(value))
return null; return null;
if (idType.equalsIgnoreCase("OPENAIRE")) return createOpenaireId(50, value, false); if (idType.equalsIgnoreCase("OPENAIRE"))
if(pidTypeWithAuthority.contains(idType.toLowerCase())){ return createOpenaireId(50, value, false);
return IdentifierFactory.idFromPid("50", idType, value, true); if (pidTypeWithAuthority.containsKey(idType.toLowerCase())) {
return IdentifierFactory.idFromPid("50", pidTypeWithAuthority.get(idType.toLowerCase()), value, true);
} }
return null; return null;
} }
protected List<Oaf> getRelations(final String reltype, final String entityId, final String otherId,
final OafEntity entity) {
final List<Oaf> res = new ArrayList<>();
RelationInverse rel = ModelSupport.findRelation(reltype);
if (rel != null) {
res
.add(
getRelation(
entityId, otherId, rel.getRelType(), rel.getSubReltype(), rel.getRelClass(), entity));
res
.add(
getRelation(
otherId, entityId, rel.getRelType(), rel.getSubReltype(), rel.getInverseRelClass(), entity));
}
return res;
}
@Override @Override
protected Qualifier prepareResourceType(final Document doc, final DataInfo info) { protected Qualifier prepareResourceType(final Document doc, final DataInfo info) {
return prepareQualifier( return prepareQualifier(

View File

@ -579,8 +579,10 @@ class MappersTest {
final List<Oaf> list = new OdfToOafMapper(vocs, false, true).processMdRecord(xml); final List<Oaf> list = new OdfToOafMapper(vocs, false, true).processMdRecord(xml);
assertEquals(1, list.size()); assertEquals(3, list.size());
assertTrue(list.get(0) instanceof Software); assertTrue(list.get(0) instanceof Software);
assertTrue(list.get(1) instanceof Relation);
assertTrue(list.get(2) instanceof Relation);
final Software s = (Software) list.get(0); final Software s = (Software) list.get(0);
@ -590,6 +592,22 @@ class MappersTest {
assertTrue(s.getAuthor().size() > 0); assertTrue(s.getAuthor().size() > 0);
assertTrue(s.getSubject().size() > 0); assertTrue(s.getSubject().size() > 0);
assertTrue(s.getInstance().size() > 0); assertTrue(s.getInstance().size() > 0);
final Relation r1 = (Relation) list.get(1);
final Relation r2 = (Relation) list.get(2);
assertEquals(s.getId(), r1.getSource());
assertEquals("50|doi_________::b453e7b4b2130ace57ff0c3db470a982", r1.getTarget());
assertEquals(ModelConstants.RESULT_RESULT, r1.getRelType());
assertEquals(ModelConstants.RELATIONSHIP, r1.getSubRelType());
assertEquals(ModelConstants.IS_REFERENCED_BY, r1.getRelClass());
assertEquals(s.getId(), r2.getTarget());
assertEquals("50|doi_________::b453e7b4b2130ace57ff0c3db470a982", r2.getSource());
assertEquals(ModelConstants.RESULT_RESULT, r2.getRelType());
assertEquals(ModelConstants.RELATIONSHIP, r2.getSubRelType());
assertEquals(ModelConstants.REFERENCES, r2.getRelClass());
} }
@Test @Test
@ -945,14 +963,14 @@ class MappersTest {
assertEquals(1, list.stream().filter(o -> o instanceof OtherResearchProduct).count()); assertEquals(1, list.stream().filter(o -> o instanceof OtherResearchProduct).count());
assertEquals(6, list.stream().filter(o -> o instanceof Relation).count()); assertEquals(6, list.stream().filter(o -> o instanceof Relation).count());
for(Oaf oaf : list){ for (Oaf oaf : list) {
if(oaf instanceof Relation){ if (oaf instanceof Relation) {
String source = ((Relation) oaf).getSource(); String source = ((Relation) oaf).getSource();
String target = ((Relation) oaf).getTarget(); String target = ((Relation) oaf).getTarget();
assertNotEquals(source, target); assertNotEquals(source, target);
assertTrue(source.equals(p.getId()) || target.equals(p.getId())); assertTrue(source.equals(p.getId()) || target.equals(p.getId()));
assertNotNull(((Relation) oaf).getSubRelType()); assertNotNull(((Relation) oaf).getSubRelType());
assertNotNull( ((Relation) oaf).getRelClass()); assertNotNull(((Relation) oaf).getRelClass());
assertNotNull(((Relation) oaf).getRelType()); assertNotNull(((Relation) oaf).getRelType());
} }
} }

View File

@ -69,7 +69,6 @@
</dates> </dates>
<resourceType resourceTypeGeneral="Dataset"/> <resourceType resourceTypeGeneral="Dataset"/>
<relatedIdentifiers> <relatedIdentifiers>
<relatedIdentifier relatedIdentifierType="DOI" relationType="IsVersionOf">10.5281/zenodo.3234525</relatedIdentifier>
<relatedIdentifier relatedIdentifierType="URL" relationType="IsPartOf">https://zenodo.org/communities/epfl</relatedIdentifier> <relatedIdentifier relatedIdentifierType="URL" relationType="IsPartOf">https://zenodo.org/communities/epfl</relatedIdentifier>
</relatedIdentifiers> </relatedIdentifiers>
<version>1.0.0</version> <version>1.0.0</version>

View File

@ -20,7 +20,7 @@
<datacite:publisher>bio.tools</datacite:publisher> <datacite:publisher>bio.tools</datacite:publisher>
<datacite:relatedIdentifiers> <datacite:relatedIdentifiers>
<datacite:relatedIdentifier relatedIdentifierType="URL" relationType="IsDocumentedBy">http://maplab.imppc.org/chainy/</datacite:relatedIdentifier> <datacite:relatedIdentifier relatedIdentifierType="URL" relationType="IsDocumentedBy">http://maplab.imppc.org/chainy/</datacite:relatedIdentifier>
<datacite:relatedIdentifier relatedIdentifierType="DOI" relationType="isReferencedBy">10.1093/bioinformatics/btw839</datacite:relatedIdentifier> <datacite:relatedIdentifier relatedIdentifierType="DOI" relationType="isreferencedBy">10.1093/bioinformatics/btw839</datacite:relatedIdentifier>
</datacite:relatedIdentifiers> </datacite:relatedIdentifiers>
<datacite:alternateIdentifiers> <datacite:alternateIdentifiers>
<datacite:alternateIdentifier alternateIdentifierType="LandingPage">https://bio.tools/</datacite:alternateIdentifier> <datacite:alternateIdentifier alternateIdentifierType="LandingPage">https://bio.tools/</datacite:alternateIdentifier>