1 |
8f0a4be2
|
m.venin
|
package eu.etaxonomy.cdm.api.service;
|
2 |
|
|
|
3 |
fcb72b7b
|
m.venin
|
import java.util.List;
|
4 |
8f0a4be2
|
m.venin
|
import java.util.Map;
|
5 |
|
|
|
6 |
|
|
import eu.etaxonomy.cdm.model.common.Language;
|
7 |
|
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
8 |
|
|
import eu.etaxonomy.cdm.model.description.NaturalLanguageTerm;
|
9 |
|
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
|
10 |
|
|
import eu.etaxonomy.cdm.model.description.TextData;
|
11 |
|
|
import eu.etaxonomy.cdm.model.description.TextFormat;
|
12 |
|
|
|
13 |
1c1beac9
|
m.venin
|
/**
|
14 |
|
|
* @author m.venin
|
15 |
|
|
*
|
16 |
|
|
*/
|
17 |
8f0a4be2
|
m.venin
|
public class DefaultQuantitativeDescriptionBuilder extends AbstractQuantitativeDescriptionBuilder {
|
18 |
2106a8ee
|
m.venin
|
|
19 |
1c1beac9
|
m.venin
|
String space = " ";
|
20 |
8f0a4be2
|
m.venin
|
|
21 |
|
|
@Override
|
22 |
fcb72b7b
|
m.venin
|
protected TextData doBuild(Map<StatisticalMeasure,Float> measures, MeasurementUnit mUnit, List<Language> languages){
|
23 |
5ae5b15c
|
m.venin
|
StringBuilder QuantitativeDescription = new StringBuilder(); // this StringBuilder is used to concatenate the different words of the description before saving it in the TextData
|
24 |
|
|
TextData textData = TextData.NewInstance(); // TextData that will contain the description and the language corresponding
|
25 |
|
|
// booleans indicating whether a kind of value is present or not and the float that will eventually hold the value
|
26 |
8f0a4be2
|
m.venin
|
|
27 |
2106a8ee
|
m.venin
|
String unit = "";
|
28 |
5ae5b15c
|
m.venin
|
if ((mUnit!=null)&&(mUnit.getLabel()!=null)){
|
29 |
|
|
unit = mUnit.getLabel();
|
30 |
|
|
}
|
31 |
8f0a4be2
|
m.venin
|
|
32 |
5ae5b15c
|
m.venin
|
// the different linking words are taken from NaturalLanguageTerm.class (should this be changed ?)
|
33 |
8f0a4be2
|
m.venin
|
NaturalLanguageTerm nltFrom = NaturalLanguageTerm.FROM();
|
34 |
fcb72b7b
|
m.venin
|
String from = nltFrom.getPreferredRepresentation(languages).getLabel();
|
35 |
8f0a4be2
|
m.venin
|
NaturalLanguageTerm nltTo = NaturalLanguageTerm.TO();
|
36 |
fcb72b7b
|
m.venin
|
String to = nltTo.getPreferredRepresentation(languages).getLabel();
|
37 |
8f0a4be2
|
m.venin
|
NaturalLanguageTerm nltUp_To = NaturalLanguageTerm.UP_TO();
|
38 |
fcb72b7b
|
m.venin
|
String up_To = nltUp_To.getPreferredRepresentation(languages).getLabel();
|
39 |
8f0a4be2
|
m.venin
|
NaturalLanguageTerm nltMost_Frequently = NaturalLanguageTerm.MOST_FREQUENTLY();
|
40 |
fcb72b7b
|
m.venin
|
String most_Frequently = nltMost_Frequently.getPreferredRepresentation(languages).getLabel();
|
41 |
8f0a4be2
|
m.venin
|
NaturalLanguageTerm nltOn_Average = NaturalLanguageTerm.ON_AVERAGE();
|
42 |
fcb72b7b
|
m.venin
|
String on_Average = nltOn_Average.getPreferredRepresentation(languages).getLabel();
|
43 |
8f0a4be2
|
m.venin
|
NaturalLanguageTerm nltMore_Or_Less = NaturalLanguageTerm.MORE_OR_LESS();
|
44 |
fcb72b7b
|
m.venin
|
String more_Or_Less = nltMore_Or_Less.getPreferredRepresentation(languages).getLabel();
|
45 |
1c1beac9
|
m.venin
|
|
46 |
8f0a4be2
|
m.venin
|
|
47 |
5ae5b15c
|
m.venin
|
// the booleans and floats are updated according to the presence or absence of values
|
48 |
2106a8ee
|
m.venin
|
|
49 |
|
|
Boolean max, min, upperb, lowerb, average, sd;
|
50 |
|
|
|
51 |
|
|
String averagevalue = getValue(measures,StatisticalMeasure.AVERAGE());
|
52 |
|
|
if (averagevalue!=null) average=true; else average=false;
|
53 |
|
|
String sdvalue = getValue(measures,StatisticalMeasure.STANDARD_DEVIATION());
|
54 |
|
|
if (sdvalue!=null) sd=true; else sd=false;
|
55 |
|
|
String minvalue = getValue(measures,StatisticalMeasure.MIN());
|
56 |
|
|
if (minvalue!=null) min=true; else min=false;
|
57 |
|
|
String maxvalue = getValue(measures,StatisticalMeasure.MAX());
|
58 |
|
|
if (maxvalue!=null) max=true; else max=false;
|
59 |
|
|
String lowerbvalue = getValue(measures,StatisticalMeasure.TYPICAL_LOWER_BOUNDARY());
|
60 |
|
|
if (lowerbvalue!=null) lowerb=true; else lowerb=false;
|
61 |
|
|
String upperbvalue = getValue(measures,StatisticalMeasure.TYPICAL_UPPER_BOUNDARY());
|
62 |
|
|
if (upperbvalue!=null) upperb=true; else upperb=false;
|
63 |
|
|
|
64 |
|
|
|
65 |
5ae5b15c
|
m.venin
|
// depending on the different associations of values, a sentence is built
|
66 |
8f0a4be2
|
m.venin
|
if (max && min) {
|
67 |
|
|
QuantitativeDescription.append(space + from + space + minvalue + space + to + space + maxvalue + space + unit);
|
68 |
|
|
}
|
69 |
|
|
else if (min) {
|
70 |
|
|
QuantitativeDescription.append(space + from + space + minvalue + space + unit);
|
71 |
|
|
}
|
72 |
|
|
else if (max) {
|
73 |
|
|
QuantitativeDescription.append(space + up_To + space + maxvalue + space + unit);
|
74 |
|
|
}
|
75 |
|
|
if ((max||min)&&(lowerb||upperb)) {
|
76 |
1c1beac9
|
m.venin
|
QuantitativeDescription.append(separator); // merge with below ?
|
77 |
8f0a4be2
|
m.venin
|
}
|
78 |
|
|
if ((lowerb||upperb)&&(min||max)) {
|
79 |
ac22062d
|
m.venin
|
QuantitativeDescription.append(space + most_Frequently);
|
80 |
8f0a4be2
|
m.venin
|
}
|
81 |
|
|
if (upperb && lowerb) {
|
82 |
|
|
QuantitativeDescription.append(space + from + space + lowerbvalue + space + to + space + upperbvalue + space + unit);
|
83 |
|
|
}
|
84 |
|
|
else if (lowerb) {
|
85 |
5ae5b15c
|
m.venin
|
QuantitativeDescription.append(space + from + space + lowerbvalue + space + unit);
|
86 |
8f0a4be2
|
m.venin
|
}
|
87 |
|
|
else if (upperb) {
|
88 |
|
|
QuantitativeDescription.append(space + up_To + space + upperbvalue + space + unit);
|
89 |
|
|
}
|
90 |
|
|
if (((max||min)&&(average))||((lowerb||upperb)&&(average))) {
|
91 |
1c1beac9
|
m.venin
|
QuantitativeDescription.append(separator);
|
92 |
8f0a4be2
|
m.venin
|
}
|
93 |
|
|
if (average) {
|
94 |
|
|
QuantitativeDescription.append(space + averagevalue + space + unit + space + on_Average);
|
95 |
|
|
if (sd) {
|
96 |
|
|
QuantitativeDescription.append("("+ more_Or_Less + space + sdvalue + ")");
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
e66d9d39
|
Katja Luther
|
textData.putText(languages.get(0), QuantitativeDescription.toString()); // which language should be put here ?
|
100 |
1c1beac9
|
m.venin
|
textData.setFormat(TextFormat.NewInstance(null, "Text",null ));
|
101 |
8f0a4be2
|
m.venin
|
|
102 |
|
|
return textData;
|
103 |
|
|
}
|
104 |
97f09ce1
|
m.venin
|
|
105 |
1c1beac9
|
m.venin
|
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Returns the value of a given type of measure as a String. If the value is an integer it is printed
|
109 |
|
|
* as an integer instead of a float.
|
110 |
|
|
* If no value of this type is present, returns null.
|
111 |
|
|
*
|
112 |
|
|
* @param measures the map with the values
|
113 |
|
|
* @param key the desired measure
|
114 |
|
|
* @return
|
115 |
|
|
*/
|
116 |
2106a8ee
|
m.venin
|
private String getValue(Map<StatisticalMeasure,Float> measures, Object key) {
|
117 |
|
|
Float floatValue;
|
118 |
|
|
Integer intValue;
|
119 |
|
|
if(measures.containsKey(key)) {
|
120 |
|
|
floatValue = measures.get(key);
|
121 |
|
|
intValue=floatValue.intValue();
|
122 |
|
|
if (floatValue.equals(intValue.floatValue())) return intValue.toString();
|
123 |
|
|
else return floatValue.toString();
|
124 |
|
|
}
|
125 |
|
|
else return null;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
8f0a4be2
|
m.venin
|
}
|