1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.strategy.homotypicgroup;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.List;
|
13
|
import java.util.Set;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.logging.log4j.LogManager;
|
17
|
import org.apache.logging.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
20
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
21
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
22
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
23
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
24
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
25
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
26
|
|
27
|
/**
|
28
|
* This class tries to guess all basionym relationships for the synonyms of a given taxon
|
29
|
* by evaluating the name parts including authors.
|
30
|
* It adds all {@link TaxonName taxon names} that seem to belong to the same
|
31
|
* basionym to the homotypic group of this basionym and creates the basionym relationship
|
32
|
* if not yet added/created.<BR>
|
33
|
* Also it changes the {@link SynonymType synonym type} of the synonyms
|
34
|
* that are homotypic to the accepted taxon to
|
35
|
* {@link SynonymType#HOMOTYPIC_SYNONYM_OF() homotypic synonym of}.
|
36
|
*
|
37
|
* NOTE: It is still unclear where to put this kind of operations.
|
38
|
* The base class, package and even the module may change in future.
|
39
|
*
|
40
|
* @author a.mueller
|
41
|
* @since 22.04.2017
|
42
|
*/
|
43
|
public class BasionymRelationCreator extends StrategyBase {
|
44
|
|
45
|
private static final long serialVersionUID = -4711438819176248413L;
|
46
|
@SuppressWarnings("unused")
|
47
|
private static final Logger logger = LogManager.getLogger(BasionymRelationCreator.class);
|
48
|
|
49
|
private UUID uuid = UUID.fromString("e9e1d1f5-e398-4ba7-81a6-92875573d7cb");
|
50
|
|
51
|
@Override
|
52
|
protected UUID getUuid() {
|
53
|
return uuid;
|
54
|
}
|
55
|
|
56
|
public void invoke (Taxon taxon){
|
57
|
Set<Synonym> synonyms = taxon.getSynonyms();
|
58
|
|
59
|
//compare accepted against synonyms
|
60
|
for (Synonym synonym: synonyms){
|
61
|
TaxonName basionym = compareHomotypic(taxon.getName(), synonym.getName());
|
62
|
if (basionym != null){
|
63
|
synonym.setType(SynonymType.HOMOTYPIC_SYNONYM_OF());
|
64
|
adaptHomotypicGroup(basionym, taxon.getName(), synonym.getName());
|
65
|
}
|
66
|
}
|
67
|
List<Synonym> synonymList = new ArrayList<>(synonyms);
|
68
|
|
69
|
//compareEachSynonymAgainstEachOther;
|
70
|
for (int i = 0; i < synonymList.size()-1; i++){
|
71
|
for (int j = i + 1; j < synonymList.size(); j++){
|
72
|
Synonym syn1 = synonymList.get(i);
|
73
|
Synonym syn2 = synonymList.get(j);
|
74
|
TaxonName basionym = compareHomotypic(syn1.getName(), syn2.getName());
|
75
|
if (basionym != null){
|
76
|
adaptHomotypicGroup(basionym, syn1.getName(), syn2.getName());
|
77
|
if (taxon.getName().getBasionyms().contains(basionym)){
|
78
|
syn1.setType(SynonymType.HOMOTYPIC_SYNONYM_OF());
|
79
|
syn2.setType(SynonymType.HOMOTYPIC_SYNONYM_OF());
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
}
|
85
|
|
86
|
private void adaptHomotypicGroup(TaxonName basionym,
|
87
|
TaxonName name1, TaxonName name2) {
|
88
|
if (basionym.equals(name1)){
|
89
|
if (!name2.getBasionyms().contains(name1)){
|
90
|
name2.addBasionym(name1);
|
91
|
}
|
92
|
}else if (basionym.equals(name2)){
|
93
|
if (!name1.getBasionyms().contains(name2)){
|
94
|
name1.addBasionym(name2);
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
|
99
|
private TaxonName compareHomotypic(TaxonName name1, TaxonName name2) {
|
100
|
if (name1 == null || name2 == null){
|
101
|
return null;
|
102
|
}
|
103
|
TaxonName basionymCandidate = checkAuthors(name1, name2);
|
104
|
if (basionymCandidate == null){
|
105
|
return null;
|
106
|
}else{
|
107
|
TaxonName newCombinationCandidate
|
108
|
= basionymCandidate == name1? name2: name1;
|
109
|
boolean isBasionym = compareNameParts(basionymCandidate, newCombinationCandidate);
|
110
|
if (isBasionym){
|
111
|
return basionymCandidate;
|
112
|
}else{
|
113
|
return null;
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
private boolean compareNameParts(TaxonName basionymCandidate,
|
119
|
TaxonName newCombinationCandidate) {
|
120
|
if (basionymCandidate.isGenusOrSupraGeneric() || newCombinationCandidate.isGenusOrSupraGeneric()){
|
121
|
return false;
|
122
|
}else if (matchLastNamePart(basionymCandidate, newCombinationCandidate)){
|
123
|
return true;
|
124
|
}
|
125
|
return false;
|
126
|
}
|
127
|
|
128
|
private TaxonName checkAuthors(TaxonName name1, TaxonName name2) {
|
129
|
if (hasBasionymAuthorOf(name1, name2)){
|
130
|
return name1;
|
131
|
}else if (hasBasionymAuthorOf(name2, name1)){
|
132
|
return name2;
|
133
|
}else{
|
134
|
return null;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
private boolean hasBasionymAuthorOf(TaxonName name1, TaxonName name2) {
|
139
|
TeamOrPersonBase<?> basAuthor2 = name2.getBasionymAuthorship();
|
140
|
TeamOrPersonBase<?> combinationAuthor = name1.getCombinationAuthorship();
|
141
|
TeamOrPersonBase<?> basAuthor1 = name1.getBasionymAuthorship();
|
142
|
if (basAuthor2 != null && basAuthor1 == null){
|
143
|
if (matches(basAuthor2, combinationAuthor)){
|
144
|
return true;
|
145
|
}
|
146
|
}
|
147
|
return false;
|
148
|
}
|
149
|
|
150
|
private boolean matches(TeamOrPersonBase<?> basAuthor, TeamOrPersonBase<?> combinationAuthor) {
|
151
|
//TODO better do with a CDM matcher that also compares other fields and
|
152
|
//returns false if other fields are contradictory
|
153
|
if (basAuthor == null || combinationAuthor == null){
|
154
|
return false;
|
155
|
}else if (basAuthor == combinationAuthor || basAuthor.equals(combinationAuthor)){
|
156
|
return true;
|
157
|
}else if (CdmUtils.nonEmptyEquals(basAuthor.getNomenclaturalTitleCache(), combinationAuthor.getNomenclaturalTitleCache())){
|
158
|
return true;
|
159
|
}else{
|
160
|
return false;
|
161
|
}
|
162
|
}
|
163
|
|
164
|
public static boolean matchLastNamePart(TaxonName name1, TaxonName name2) {
|
165
|
String familyNamePart1 = name1.getLastNamePart();
|
166
|
String familyNamePart2 = name2.getLastNamePart();
|
167
|
if (familyNamePart1 != null && familyNamePart2 != null){
|
168
|
familyNamePart1 = normalizeBasionymNamePart(familyNamePart1);
|
169
|
familyNamePart2 = normalizeBasionymNamePart(familyNamePart2);
|
170
|
return (familyNamePart1.equals(familyNamePart2));
|
171
|
}else{
|
172
|
return false;
|
173
|
}
|
174
|
}
|
175
|
|
176
|
private static String normalizeBasionymNamePart(String familyNamePart) {
|
177
|
String namePart = familyNamePart.toLowerCase()
|
178
|
.replaceAll("(um|us|a|is|e|os|on|or)$", "")
|
179
|
.replaceAll("er$", "r") //e.g. ruber <-> rubra
|
180
|
.replaceAll("ese$", "s"); //e.g. cayanensis <-> cayanenese
|
181
|
//TODO tampensis / tampense
|
182
|
return namePart;
|
183
|
}
|
184
|
}
|