Mercurial > hg > openmind
changeset 76:f04bd29d284d
renamed replaceSourceRelation() to replaceUniqueSourceRelation() to make intent clearer.
author | casties |
---|---|
date | Fri, 24 Feb 2017 20:22:23 +0100 |
parents | e0be7c0030f5 |
children | a59984fd3c3f |
files | src/main/java/org/mpi/openmind/repository/bo/Entity.java src/main/java/org/mpi/openmind/scripts/DivaImport.java |
diffstat | 2 files changed, 82 insertions(+), 60 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/org/mpi/openmind/repository/bo/Entity.java Thu Feb 23 19:05:47 2017 +0100 +++ b/src/main/java/org/mpi/openmind/repository/bo/Entity.java Fri Feb 24 20:22:23 2017 +0100 @@ -266,82 +266,104 @@ } } - public void replaceTargetRelation(Entity newSrc, String srcObjClass, String relName){ - this.testLightweightState(); - if(!lightweight){ - if(newSrc == null || !newSrc.isValid()){ - this.removeAllTargetRelations(relName, srcObjClass); - }else{ - boolean found = false; - - for(Relation rel : new ArrayList<Relation>(getTargetRelations(relName, srcObjClass))){ - if(rel.getSourceId().equals(newSrc.getId())){ - found = true; - }else{ - this.targetRelations.remove(rel); - } - } - - if(!found){ - this.removeAllTargetRelations(relName, srcObjClass); - Relation rel = new Relation(newSrc, this); - rel.setOwnValue(relName); - } - } - } - } - /** + * Replace all target relations of the given type with one to the given Entity. + * + * newSrc needs to be non-lightweight. + * * This methods should be used for relation one-to-many * like witness->is_part_of->codex. * - * @param newTar + * @param newSrc + * @param srcObjClass + * @param relName + */ + public void replaceUniqueTargetRelation(Entity newSrc, String srcObjClass, String relName) { + this.testLightweightState(); + if (!lightweight) { + if (newSrc == null || !newSrc.isValid()) { + this.removeAllTargetRelations(relName, srcObjClass); + + } else { + boolean found = false; + for (Relation rel : new ArrayList<Relation>(getTargetRelations(relName, srcObjClass))) { + if (rel.getSourceId().equals(newSrc.getId())) { + found = true; + } else { + this.targetRelations.remove(rel); + } + } + + if (!found) { + this.removeAllTargetRelations(relName, srcObjClass); + Relation rel = new Relation(newSrc, this); + rel.setOwnValue(relName); + } + } + } + } + + /** + * Replace all source relations of the given type with one to the given Entity. + * <p/> + * newNonLwTar needs to be non-lightweight. + * <p/> + * This methods should be used for relation one-to-many + * like witness->is_part_of->codex. + * + * @param newNonLwTar * @param tarObjClass * @param relName */ - public void replaceSourceRelation(Entity newNonLwTar, String tarObjClass, String relName){ - testLightweightState(); - if(!lightweight){ - - if(newNonLwTar == null || !newNonLwTar.isValid()){ - this.removeAllSourceRelations(relName, tarObjClass); - }else{ - - boolean found = false; - for(Relation rel : new ArrayList<Relation>(getSourceRelations(relName, tarObjClass))){ - if(rel.getTargetId().equals(newNonLwTar.getId())){ - found = true; - }else{ - this.sourceRelations.remove(rel); - } - } - - if(!found){ - this.removeAllSourceRelations(relName, tarObjClass); - this.addSourceRelation(relName, newNonLwTar); - //Relation rel = new Relation(this, newTar); - //rel.setOwnValue(relName); - } - } - - } - } + public void replaceUniqueSourceRelation(Entity newNonLwTar, String tarObjClass, String relName) { + testLightweightState(); + if (!lightweight) { + if (newNonLwTar == null || !newNonLwTar.isValid()) { + // no new target -> remove all relations of this type + this.removeAllSourceRelations(relName, tarObjClass); + + } else { + boolean found = false; + // check existing relations + for (Relation rel : new ArrayList<Relation>(getSourceRelations(relName, tarObjClass))) { + if (rel.getTargetId().equals(newNonLwTar.getId())) { + // existing relation matches + found = true; + } else { + // remove existing relation + this.sourceRelations.remove(rel); + } + } - /** - * Add a source relation from this entity to another target entity. + if (!found) { + // no existing relation + this.removeAllSourceRelations(relName, tarObjClass); // remove again? + this.addSourceRelation(relName, newNonLwTar); + } + } + + } + } + + + /** + * Add a source relation from this entity to another entity. * * @param relName * @param tar */ public void addSourceRelation(String relName, Entity tar) { if (!containsSourceRelation(relName, tar.getId()) && !tar.containsTargetRelation(relName, this.getId())) { + // relation does not exists on both sides new Relation(this, tar, relName); } else if (!containsSourceRelation(relName, tar.getId())) { + // relation does not exist on source Relation rel = tar.getTargetRelation(relName, this.getId()); if (rel != null) { this.sourceRelations.add(rel); } } else if (!tar.containsTargetRelation(relName, this.getId())) { + // relation does not exist on target Relation rel = this.getSourceRelation(relName, tar.getId()); if (rel != null) { tar.getTargetRelations().add(rel); @@ -350,7 +372,7 @@ } /** - * Add a target relation from another source entity to this entity. + * Add a target relation from another entity to this entity. * * @param relName * @param src @@ -373,7 +395,7 @@ public Relation getTargetRelation(String relName, Long srcId){ for(Relation rel : getTargetRelations()){ - if(rel.getOwnValue().equals(relName) && rel.getSourceId().equals(srcId)){ + if(rel.getObjectClass().equals(relName) && rel.getSourceId().equals(srcId)){ return rel; } } @@ -382,7 +404,7 @@ public Relation getSourceRelation(String relName, Long tarId){ for(Relation rel : getSourceRelations()){ - if(rel.getOwnValue().equals(relName) && rel.getTargetId().equals(tarId)){ + if(rel.getObjectClass().equals(relName) && rel.getTargetId().equals(tarId)){ return rel; } }
--- a/src/main/java/org/mpi/openmind/scripts/DivaImport.java Thu Feb 23 19:05:47 2017 +0100 +++ b/src/main/java/org/mpi/openmind/scripts/DivaImport.java Fri Feb 24 20:22:23 2017 +0100 @@ -139,13 +139,13 @@ if(ismi_id != null && ismi_id != 0){ Entity codex = ontology.getEntityByIdWithContent(ismi_id); - digi.replaceSourceRelation(codex, "CODEX", "is_digitalization_of"); + digi.replaceUniqueSourceRelation(codex, "CODEX", "is_digitalization_of"); countYesCodex++; }else{ List<Entity> list0 = ontology.getEntityByDefAndOW("CODEX", ov, 1); if(list0.size() > 0){ Entity codex = ontology.getEntityByIdWithContent(list0.get(0).getId()); - digi.replaceSourceRelation(codex, "CODEX", "is_digitalization_of"); + digi.replaceUniqueSourceRelation(codex, "CODEX", "is_digitalization_of"); countIdCodex++; }else{ noCodexList.add(ov);