Added inverse Relation utility

This commit is contained in:
Sandro La Bruzzo 2023-04-28 10:53:24 +02:00
parent 3b5b4c565b
commit 80ecc865f7
2 changed files with 116 additions and 3 deletions

View File

@ -9,18 +9,21 @@ import java.util.Objects;
/**
* Relation models any edge between two nodes in the OpenAIRE graph. It has a source id and a target id pointing to
* graph node identifiers and it is further characterised by the semantic of the link through the fields relType,
* graph node identifiers, and it is further characterised by the semantic of the link through the fields relType,
* subRelType and relClass. Provenance information is modeled according to the dataInfo element and collectedFrom, while
* individual relationship types can provide extra information via the properties field.
*/
public class Relation extends Oaf implements Serializable {
public enum RELTYPE {
resultResult,
resultProject,
resultOrganization,
projectOrganization,
datasourceOrganization
//TODO organizationOrganization ????
}
public enum SUBRELTYPE {
@ -36,8 +39,7 @@ public class Relation extends Oaf implements Serializable {
similarity,
supplement,
version;
public boolean exists(String subreltype) {
public static boolean exists(String subreltype) {
try {
SUBRELTYPE.valueOf(subreltype);
return true;
@ -83,6 +85,7 @@ public class Relation extends Oaf implements Serializable {
IsVariantFormOf,
isProducedBy,
IsMetadataFor,
HasAssociationWith,
Cites,
IsRequiredBy,
IsOriginalFormOf,
@ -95,6 +98,7 @@ public class Relation extends Oaf implements Serializable {
IsDerivedFrom,
Obsoletes,
Reviews,
isSimilarTo,
produces;
public static boolean exists(String relclass) {
@ -105,6 +109,75 @@ public class Relation extends Oaf implements Serializable {
return false;
}
}
@Transient
public SUBRELTYPE getSubRel() {
switch (this) {
case isParticipant:
case hasParticipant:
return SUBRELTYPE.participation;
case isAuthorInstitutionOf:
case hasAuthorInstitution:
return SUBRELTYPE.affiliation;
case isMergedIn:
case merges:
case isSimilarTo:
return SUBRELTYPE.dedup;
case isProducedBy:
case produces:
return SUBRELTYPE.outcome;
case isProvidedBy:
case provides:
return SUBRELTYPE.provision;
case IsAmongTopNSimilarDocuments:
case HasAmongTopNSimilarDocuments:
return SUBRELTYPE.similarity;
case IsSupplementedBy:
case IsSupplementTo:
return SUBRELTYPE.supplement;
case IsPartOf:
case HasPart:
return SUBRELTYPE.part;
case IsCitedBy:
case Cites:
return SUBRELTYPE.citation;
case IsIdenticalTo:
case IsReferencedBy:
case References:
case IsContinuedBy:
case Continues:
case IsDocumentedBy:
case Documents:
case IsDerivedFrom:
case IsSourceOf:
case IsRelatedTo:
case IsCompiledBy:
case Compiles:
case IsDescribedBy:
case Describes:
case IsMetadataFor:
case IsMetadataOf:
case HasAssociationWith:
case IsRequiredBy:
case Requires:
case IsChildOf:
case IsParentOf:
return SUBRELTYPE.relationship;
case IsPreviousVersionOf:
case IsNewVersionOf:
case IsVariantFormOf:
case IsOriginalFormOf:
case IsObsoletedBy:
case Obsoletes:
case IsVersionOf:
case HasVersion:
return SUBRELTYPE.version;
case IsReviewedBy:
case Reviews:
return SUBRELTYPE.review;
}
// makes the compiler happy
throw new IllegalArgumentException("missing SubRel mapping for" + this);
}
@Transient
public RELCLASS getInverse() {
@ -341,6 +414,20 @@ public class Relation extends Oaf implements Serializable {
&& target.equals(relation.target);
}
public Relation inverse() {
final Relation inverse = new Relation();
inverse.setSource(this.target);
inverse.setTarget(this.source);
inverse.setRelClass(this.relClass.getInverse());
inverse.setRelType(this.relType);
inverse.setSubRelType(this.subRelType);
inverse.setProperties(this.properties);
inverse.setValidated(validated);
inverse.setProvenance(provenance);
inverse.setValidationDate(validationDate);
return inverse;
}
@Override
public int hashCode() {
return Objects.hash(relType, subRelType, relClass, source, target, provenance);

View File

@ -0,0 +1,26 @@
package eu.dnetlib.dhp.schema.oaf;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class RelationTest {
@Test
public void checkExistRelType() {
assertTrue(Relation.SUBRELTYPE.exists(Relation.SUBRELTYPE.dedup.toString()));
assertFalse(Relation.SUBRELTYPE.exists("foo"));
}
@Test
public void checSubRelType() {
Relation.RELCLASS rc = Relation.RELCLASS.merges;
assertEquals(Relation.SUBRELTYPE.dedup, rc.getSubRel());
for (Relation.RELCLASS value : Relation.RELCLASS.values()) {
System.out.printf("%s => %s\n", value, value.getSubRel());
}
}
}