| ... |
... |
@@ -20,7 +20,6 @@ |
|
20
|
20
|
*/
|
|
21
|
21
|
package fr.ird.t3.actions.data.level0;
|
|
22
|
22
|
|
|
23
|
|
-import com.google.common.collect.Maps;
|
|
24
|
23
|
import fr.ird.t3.entities.T3Functions;
|
|
25
|
24
|
import fr.ird.t3.entities.data.Activity;
|
|
26
|
25
|
import fr.ird.t3.entities.data.Trip;
|
| ... |
... |
@@ -41,14 +40,14 @@ import org.nuiton.util.TimeLog; |
|
41
|
40
|
import java.util.List;
|
|
42
|
41
|
import java.util.Map;
|
|
43
|
42
|
import java.util.Set;
|
|
|
43
|
+import java.util.TreeMap;
|
|
44
|
44
|
|
|
45
|
45
|
import static org.nuiton.i18n.I18n.l;
|
|
46
|
46
|
|
|
47
|
47
|
/**
|
|
48
|
|
- * Compute for the all activities of selected trips, the set duration and the
|
|
49
|
|
- * positive set count.
|
|
|
48
|
+ * Compute for the all activities of selected trips, the set duration and the positive set count.
|
|
50
|
49
|
* <p/>
|
|
51
|
|
- * <strong>Note:</strong> The rf2 msut have been computed on each trip.
|
|
|
50
|
+ * <strong>Note:</strong> The rf2 must have been computed on each trip.
|
|
52
|
51
|
*
|
|
53
|
52
|
* @author Tony Chemit - dev@tchemit.fr
|
|
54
|
53
|
* @since 1.0
|
| ... |
... |
@@ -56,24 +55,21 @@ import static org.nuiton.i18n.I18n.l; |
|
56
|
55
|
public class ComputeSetDurationAndPositiveSetCountAction extends AbstractLevel0Action<ComputeSetDurationAndPositiveSetCountConfiguration> {
|
|
57
|
56
|
|
|
58
|
57
|
/** Logger. */
|
|
59
|
|
- private static final Log log = LogFactory.getLog(
|
|
60
|
|
- ComputeSetDurationAndPositiveSetCountAction.class);
|
|
61
|
|
-
|
|
|
58
|
+ private static final Log log = LogFactory.getLog(ComputeSetDurationAndPositiveSetCountAction.class);
|
|
62
|
59
|
@InjectDAO(entityType = SetDuration.class)
|
|
63
|
|
- protected SetDurationTopiaDao setDurationDAO;
|
|
64
|
|
-
|
|
65
|
|
- /** Count of treated activites. */
|
|
66
|
|
- protected int nbActivities;
|
|
|
60
|
+ private SetDurationTopiaDao setDurationDAO;
|
|
|
61
|
+ /** Count of treated activities. */
|
|
|
62
|
+ private int nbActivities;
|
|
|
63
|
+ /** Count of positive activities found. */
|
|
|
64
|
+ private int nbPositiveActivities;
|
|
67
|
65
|
/** Count of set (sum(a.setCount) on each activity trip). */
|
|
68
|
|
- protected int nbSet;
|
|
69
|
|
- /** Count of positive activites found. */
|
|
70
|
|
- protected int nbPositiveActivities;
|
|
|
66
|
+ private int nbSet;
|
|
71
|
67
|
/**
|
|
72
|
68
|
* Cache of setDuration (improve a lot performance!).
|
|
73
|
69
|
*
|
|
74
|
70
|
* @since 1.3
|
|
75
|
71
|
*/
|
|
76
|
|
- protected Map<String, SetDuration> setDurations;
|
|
|
72
|
+ private Map<String, SetDuration> setDurations;
|
|
77
|
73
|
|
|
78
|
74
|
public ComputeSetDurationAndPositiveSetCountAction() {
|
|
79
|
75
|
super(Level0Step.COMPUTE_SET_DURATION_AND_POSITIVE_SET_COUNT);
|
| ... |
... |
@@ -98,8 +94,7 @@ public class ComputeSetDurationAndPositiveSetCountAction extends AbstractLevel0A |
|
98
|
94
|
for (Trip trip : tripList) {
|
|
99
|
95
|
nbActivities += trip.sizeActivity();
|
|
100
|
96
|
}
|
|
101
|
|
-
|
|
102
|
|
- setDurations = Maps.newTreeMap();
|
|
|
97
|
+ setDurations = new TreeMap<>();
|
|
103
|
98
|
}
|
|
104
|
99
|
|
|
105
|
100
|
@Override
|
| ... |
... |
@@ -108,177 +103,112 @@ public class ComputeSetDurationAndPositiveSetCountAction extends AbstractLevel0A |
|
108
|
103
|
}
|
|
109
|
104
|
|
|
110
|
105
|
@Override
|
|
111
|
|
- protected boolean executeAction() throws Exception {
|
|
112
|
|
-
|
|
|
106
|
+ protected boolean executeAction() {
|
|
113
|
107
|
boolean result = false;
|
|
114
|
|
-
|
|
115
|
108
|
if (CollectionUtils.isNotEmpty(trips)) {
|
|
116
|
|
-
|
|
117
|
109
|
setNbSteps(2 * nbActivities);
|
|
118
|
|
-
|
|
119
|
|
- // do action for each data using the prepared action context
|
|
120
|
|
-
|
|
121
|
110
|
for (Trip trip : trips) {
|
|
122
|
|
-
|
|
123
|
111
|
result |= executeForTrip(trip);
|
|
124
|
112
|
}
|
|
125
|
113
|
}
|
|
126
|
|
-
|
|
127
|
114
|
return result;
|
|
128
|
115
|
}
|
|
129
|
116
|
|
|
130
|
|
- protected boolean executeForTrip(Trip trip) throws TopiaException {
|
|
131
|
|
-
|
|
|
117
|
+ private boolean executeForTrip(Trip trip) throws TopiaException {
|
|
132
|
118
|
long s0 = TimeLog.getTime();
|
|
133
|
|
-
|
|
134
|
119
|
String tripStr = decorate(trip, DecoratorService.WITH_ID);
|
|
135
|
|
-
|
|
136
|
|
- if (!trip.isActivityEmpty()) {
|
|
137
|
|
-
|
|
|
120
|
+ if (trip.isActivityNotEmpty()) {
|
|
138
|
121
|
int year = T3Functions.TRIP_TO_LANDING_YEAR.apply(trip);
|
|
139
|
|
-
|
|
140
|
122
|
Set<Species> species = trip.getElementaryCatchSpecies();
|
|
141
|
|
-
|
|
142
|
123
|
for (Activity activity : trip.getActivity()) {
|
|
|
124
|
+ String activityStr = String.format("Activity %s - %s", tripStr, decorate(activity));
|
|
|
125
|
+ int setCount = activity.getSetCount();
|
|
|
126
|
+ nbSet += setCount;
|
|
143
|
127
|
|
|
144
|
|
- String activityStr = "Activity " + tripStr + " - " +
|
|
145
|
|
- decorate(activity);
|
|
146
|
|
-
|
|
147
|
|
- nbSet += activity.getSetCount();
|
|
148
|
|
- incrementsProgression();
|
|
149
|
|
-
|
|
150
|
|
- float totalCatchesWeight =
|
|
151
|
|
- activity.getElementaryCatchTotalWeightRf2(species);
|
|
152
|
|
-
|
|
|
128
|
+ float totalCatchesWeight = activity.getElementaryCatchTotalWeightRf2(species);
|
|
153
|
129
|
if (log.isDebugEnabled()) {
|
|
154
|
|
- log.debug("Total catches " + totalCatchesWeight +
|
|
155
|
|
- " for " + activityStr);
|
|
|
130
|
+ log.debug(String.format("Total catches %s for %s", totalCatchesWeight, activityStr));
|
|
156
|
131
|
}
|
|
|
132
|
+ int positiveSetCount = computePositiveSetCount(totalCatchesWeight > 0, activity);
|
|
|
133
|
+ activity.setPositiveSetCount(positiveSetCount);
|
|
|
134
|
+ if (positiveSetCount > 0) {
|
|
|
135
|
+ nbPositiveActivities++;
|
|
|
136
|
+ }
|
|
|
137
|
+ incrementsProgression();
|
|
157
|
138
|
|
|
158
|
|
- computePositiveSetCount(activityStr, totalCatchesWeight, activity);
|
|
159
|
|
-
|
|
|
139
|
+ float subCatch = totalCatchesWeight;
|
|
|
140
|
+ if (positiveSetCount > 0) {
|
|
|
141
|
+ subCatch = subCatch / activity.getPositiveSetCount();
|
|
|
142
|
+ }
|
|
|
143
|
+ int negativeSetCount = setCount - positiveSetCount;
|
|
|
144
|
+ Float setDuration = computeSetDuration(year, subCatch, activity, negativeSetCount);
|
|
|
145
|
+ activity.setSetDuration(setDuration);
|
|
160
|
146
|
incrementsProgression();
|
|
161
|
|
- computeSetDuration(year,
|
|
162
|
|
- totalCatchesWeight, activity);
|
|
163
|
147
|
|
|
164
|
148
|
String message = l(locale, "t3.level0.computeActivitySetDurationAndPositiveSetCount",
|
|
165
|
|
- activityStr,
|
|
166
|
|
- activity.getSetDuration(),
|
|
167
|
|
- activity.getPositiveSetCount()
|
|
168
|
|
- );
|
|
|
149
|
+ activityStr, setDuration, positiveSetCount, negativeSetCount);
|
|
169
|
150
|
addInfoMessage(message);
|
|
170
|
151
|
if (log.isDebugEnabled()) {
|
|
171
|
152
|
log.debug(message);
|
|
172
|
153
|
}
|
|
173
|
154
|
}
|
|
174
|
155
|
}
|
|
175
|
|
-
|
|
176
|
156
|
markTripAsTreated(trip);
|
|
177
|
|
-
|
|
178
|
157
|
getTimeLog().log(s0, "executeForTrip for " + tripStr);
|
|
179
|
|
-
|
|
180
|
158
|
// always commit since trip is always modified
|
|
181
|
159
|
return true;
|
|
182
|
160
|
}
|
|
183
|
161
|
|
|
184
|
|
- protected void computePositiveSetCount(String activityStr,
|
|
185
|
|
- float totalCatchesWeight,
|
|
186
|
|
- Activity activity) {
|
|
187
|
|
-
|
|
188
|
|
- int positiveSetCount = 0;
|
|
189
|
|
-
|
|
190
|
|
- if (totalCatchesWeight > 0) {
|
|
191
|
|
-
|
|
192
|
|
- // ok some catches found on activity, it means
|
|
193
|
|
- // positive set
|
|
194
|
|
-
|
|
195
|
|
- positiveSetCount = activity.getSetCount();
|
|
196
|
|
-
|
|
197
|
|
- } else {
|
|
198
|
|
- if (log.isDebugEnabled()) {
|
|
199
|
|
- log.debug("Catches with no catches! for " + activityStr);
|
|
200
|
|
- }
|
|
201
|
|
- }
|
|
202
|
|
-
|
|
203
|
|
- if (log.isDebugEnabled()) {
|
|
204
|
|
- String message = "Computed positive Set Count = " +
|
|
205
|
|
- positiveSetCount + " for " + activityStr;
|
|
206
|
|
- log.debug(message);
|
|
207
|
|
- }
|
|
208
|
|
- activity.setPositiveSetCount(positiveSetCount);
|
|
209
|
|
-
|
|
210
|
|
- if (positiveSetCount > 0) {
|
|
211
|
|
- nbPositiveActivities++;
|
|
|
162
|
+ private int computePositiveSetCount(boolean withCatches, Activity activity) {
|
|
|
163
|
+ if (withCatches) {
|
|
|
164
|
+ // ok some catches found on activity, it means positive set
|
|
|
165
|
+ return activity.getSetCount();
|
|
212
|
166
|
}
|
|
|
167
|
+ return 0;
|
|
213
|
168
|
}
|
|
214
|
169
|
|
|
215
|
|
- protected void computeSetDuration(int year,
|
|
216
|
|
- float totalCatchesWeight,
|
|
217
|
|
- Activity activity) throws TopiaException {
|
|
218
|
|
-
|
|
|
170
|
+ private Float computeSetDuration(int year, float subCatch, Activity activity, int negativeSetCount) throws TopiaException {
|
|
219
|
171
|
// get the correct setDuration
|
|
220
|
172
|
SetDuration setDuration = getSetDuration(activity, year);
|
|
221
|
|
-
|
|
222
|
173
|
if (setDuration == null) {
|
|
223
|
|
-
|
|
224
|
|
- activity.setSetDuration(0f);
|
|
225
|
|
-
|
|
226
|
|
- } else {
|
|
227
|
|
-
|
|
228
|
|
- Float setTime;
|
|
229
|
|
-
|
|
230
|
|
- if (activity.isSetNull()) {
|
|
231
|
|
-
|
|
232
|
|
- // use setDuration nullSetValue
|
|
233
|
|
- setTime = setDuration.getNullSetValue();
|
|
234
|
|
- if (log.isDebugEnabled()) {
|
|
235
|
|
- log.debug("Computed set duration (set is null) = " +
|
|
236
|
|
- setTime);
|
|
237
|
|
- }
|
|
238
|
|
- } else if (activity.isWithSetDuration()) {
|
|
239
|
|
-
|
|
240
|
|
- // use setDuration computedValue
|
|
241
|
|
- setTime = setDuration.getParameterB() +
|
|
242
|
|
- setDuration.getParameterA() * totalCatchesWeight;
|
|
243
|
|
- if (log.isDebugEnabled()) {
|
|
244
|
|
- log.debug("Computed set duration = " + setTime);
|
|
245
|
|
- }
|
|
246
|
|
- } else {
|
|
247
|
|
-
|
|
248
|
|
- // no setDuration
|
|
249
|
|
- setTime = null;
|
|
250
|
|
- if (log.isDebugEnabled()) {
|
|
251
|
|
- log.debug("Use null set duration (set is null) ");
|
|
252
|
|
- }
|
|
|
174
|
+ if (log.isDebugEnabled()) {
|
|
|
175
|
+ log.debug("No setDuration found, use 0 value.");
|
|
253
|
176
|
}
|
|
254
|
|
-
|
|
255
|
|
- activity.setSetDuration(setTime);
|
|
|
177
|
+ return 0f;
|
|
|
178
|
+ }
|
|
|
179
|
+ float nullSetDuration = negativeSetCount * setDuration.getNullSetValue();
|
|
|
180
|
+ if (activity.isSetNull()) {
|
|
|
181
|
+ if (log.isDebugEnabled()) {
|
|
|
182
|
+ log.debug(String.format("Use nullValue of set duration = %s", nullSetDuration));
|
|
|
183
|
+ }
|
|
|
184
|
+ return nullSetDuration;
|
|
|
185
|
+ }
|
|
|
186
|
+ if (activity.isWithSetDuration()) {
|
|
|
187
|
+ float subDuration = (setDuration.getParameterB() + setDuration.getParameterA() * subCatch);
|
|
|
188
|
+ float positiveSetDuration = activity.getPositiveSetCount() * subDuration;
|
|
|
189
|
+ if (log.isDebugEnabled()) {
|
|
|
190
|
+ log.debug(String.format("Computed positive set duration = %s", positiveSetDuration));
|
|
|
191
|
+ }
|
|
|
192
|
+ return nullSetDuration + positiveSetDuration;
|
|
|
193
|
+ }
|
|
|
194
|
+ // no setDuration
|
|
|
195
|
+ if (log.isDebugEnabled()) {
|
|
|
196
|
+ log.debug("Use null set duration (set is null) ");
|
|
256
|
197
|
}
|
|
|
198
|
+ return null;
|
|
257
|
199
|
}
|
|
258
|
200
|
|
|
259
|
|
- protected SetDuration getSetDuration(Activity activity, int year) throws TopiaException {
|
|
|
201
|
+ private SetDuration getSetDuration(Activity activity, int year) throws TopiaException {
|
|
260
|
202
|
Ocean ocean = activity.getOcean();
|
|
261
|
203
|
Country fleetCountry = activity.getTrip().getVessel().getFleetCountry();
|
|
262
|
204
|
SchoolType schoolType = activity.getSchoolType();
|
|
263
|
|
- String key = ocean.getCode() + ":" +
|
|
264
|
|
- fleetCountry.getCode() + ":" +
|
|
265
|
|
- schoolType.getCode() + ":" +
|
|
266
|
|
- year;
|
|
267
|
|
-
|
|
|
205
|
+ String key = ocean.getCode() + ":" + fleetCountry.getCode() + ":" + schoolType.getCode() + ":" + year;
|
|
268
|
206
|
SetDuration result = setDurations.get(key);
|
|
269
|
207
|
if (result == null) {
|
|
270
|
|
-
|
|
271
|
208
|
// no result found in cache
|
|
272
|
|
-
|
|
273
|
209
|
if (!setDurations.containsKey(key)) {
|
|
274
|
|
-
|
|
275
|
210
|
// try to load it
|
|
276
|
|
-
|
|
277
|
|
- result = setDurationDAO.findByActivityAndYear(
|
|
278
|
|
- activity,
|
|
279
|
|
- year
|
|
280
|
|
- );
|
|
281
|
|
-
|
|
|
211
|
+ result = setDurationDAO.findByActivityAndYear(activity, year);
|
|
282
|
212
|
if (result == null) {
|
|
283
|
213
|
addWarningMessage(
|
|
284
|
214
|
l(getLocale(), "t3.level0.computeSetDurationAndPositiveSetCount.error.noSetDurationFound",
|
| ... |
... |
@@ -288,11 +218,9 @@ public class ComputeSetDurationAndPositiveSetCountAction extends AbstractLevel0A |
|
288
|
218
|
year));
|
|
289
|
219
|
}
|
|
290
|
220
|
}
|
|
291
|
|
-
|
|
292
|
221
|
// store it once for all (even if result is null)
|
|
293
|
222
|
setDurations.put(key, result);
|
|
294
|
223
|
}
|
|
295
|
|
-
|
|
296
|
224
|
return result;
|
|
297
|
225
|
}
|
|
298
|
226
|
|