Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/WQAdapted.java @ 9277:2323d005f9a5
compile error fix
author | gernotbelger |
---|---|
date | Fri, 20 Jul 2018 10:39:02 +0200 |
parents | 5e38e2924c07 |
children |
comparison
equal
deleted
inserted
replaced
9276:3141f0e7314e | 9277:2323d005f9a5 |
---|---|
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.artifacts.states; | 9 package org.dive4elements.river.artifacts.states; |
10 | 10 |
11 import java.util.ArrayList; | 11 import java.util.ArrayList; |
12 import java.util.Collections; | |
13 import java.util.Comparator; | |
12 import java.util.List; | 14 import java.util.List; |
13 import java.util.Comparator; | |
14 import java.util.Collections; | |
15 | 15 |
16 import org.apache.log4j.Logger; | 16 import org.apache.log4j.Logger; |
17 | 17 import org.dive4elements.artifactdatabase.ProtocolUtils; |
18 import org.w3c.dom.Element; | 18 import org.dive4elements.artifactdatabase.data.StateData; |
19 | |
20 import org.dive4elements.artifacts.Artifact; | 19 import org.dive4elements.artifacts.Artifact; |
21 import org.dive4elements.artifacts.CallContext; | 20 import org.dive4elements.artifacts.CallContext; |
22 | |
23 import org.dive4elements.artifactdatabase.ProtocolUtils; | |
24 import org.dive4elements.artifactdatabase.data.StateData; | |
25 | |
26 import org.dive4elements.artifacts.common.utils.XMLUtils; | 21 import org.dive4elements.artifacts.common.utils.XMLUtils; |
27 | 22 import org.dive4elements.river.artifacts.D4EArtifact; |
28 import org.dive4elements.river.artifacts.access.RangeAccess; | 23 import org.dive4elements.river.artifacts.access.RangeAccess; |
29 | 24 import org.dive4elements.river.artifacts.model.RangeWithValues; |
25 import org.dive4elements.river.artifacts.model.WstFactory; | |
30 import org.dive4elements.river.model.Gauge; | 26 import org.dive4elements.river.model.Gauge; |
31 import org.dive4elements.river.model.Range; | 27 import org.dive4elements.river.model.Range; |
32 import org.dive4elements.river.model.River; | 28 import org.dive4elements.river.model.River; |
33 import org.dive4elements.river.model.Wst; | 29 import org.dive4elements.river.model.Wst; |
34 | |
35 import org.dive4elements.river.artifacts.D4EArtifact; | |
36 | |
37 import org.dive4elements.river.artifacts.model.RangeWithValues; | |
38 import org.dive4elements.river.artifacts.model.WstFactory; | |
39 import org.dive4elements.river.utils.RiverUtils; | 30 import org.dive4elements.river.utils.RiverUtils; |
40 | 31 import org.w3c.dom.Element; |
41 | 32 |
42 /** | 33 /** |
43 * State to input W/Q data. | 34 * State to input W/Q data. |
35 * | |
44 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
45 */ | 37 */ |
46 public class WQAdapted extends DefaultState { | 38 public class WQAdapted extends DefaultState { |
47 | 39 |
48 /** The log used in this state.*/ | 40 /** The log used in this state. */ |
49 private static Logger log = Logger.getLogger(WQAdapted.class); | 41 private static Logger log = Logger.getLogger(WQAdapted.class); |
50 | 42 |
51 public static final String FIELD_WQ_MODE = "wq_isq"; | 43 public static final String FIELD_WQ_MODE = "wq_isq"; |
52 | 44 |
53 public static final String FIELD_WQ_VALUES = "wq_values"; | 45 public static final String FIELD_WQ_VALUES = "wq_values"; |
54 | 46 |
55 public static final class GaugeOrder implements Comparator<Gauge> { | 47 public static final class GaugeOrder implements Comparator<Gauge> { |
56 private int order; | 48 private final int order; |
57 | 49 |
58 public GaugeOrder(boolean up) { | 50 public GaugeOrder(final boolean up) { |
59 order = up ? 1 : -1; | 51 this.order = up ? 1 : -1; |
60 } | 52 } |
61 | 53 |
62 public int compare(Gauge a, Gauge b) { | 54 @Override |
63 return order * a.getRange().getA().compareTo(b.getRange().getA()); | 55 public int compare(final Gauge a, final Gauge b) { |
56 return this.order * a.getRange().getA().compareTo(b.getRange().getA()); | |
64 } | 57 } |
65 } // class GaugeOrder | 58 } // class GaugeOrder |
66 | 59 |
67 public static final GaugeOrder GAUGE_UP = new GaugeOrder(true); | 60 public static final GaugeOrder GAUGE_UP = new GaugeOrder(true); |
68 public static final GaugeOrder GAUGE_DOWN = new GaugeOrder(false); | 61 public static final GaugeOrder GAUGE_DOWN = new GaugeOrder(false); |
69 | |
70 | 62 |
71 /** Trivial, empty constructor. */ | 63 /** Trivial, empty constructor. */ |
72 public WQAdapted() { | 64 public WQAdapted() { |
73 } | 65 } |
74 | |
75 | 66 |
76 /** | 67 /** |
77 * This method creates one element for each gauge of selected river that | 68 * This method creates one element for each gauge of selected river that |
78 * is intersected by the given kilometer range. Each element is a tuple of | 69 * is intersected by the given kilometer range. Each element is a tuple of |
79 * (from;to) where <i>from</i> is the lower bounds of the gauge or | 70 * (from;to) where <i>from</i> is the lower bounds of the gauge or |
80 * the lower | 71 * the lower |
81 * kilometer range. <i>to</i> is the upper bounds of the gauge or the upper | 72 * kilometer range. <i>to</i> is the upper bounds of the gauge or the upper |
82 * kilometer range. | 73 * kilometer range. |
83 * | 74 * |
84 * @param cr The ElementCreator. | 75 * @param cr |
85 * @param artifact The FLYS artifact. | 76 * The ElementCreator. |
86 * @param name The name of the data item. | 77 * @param artifact |
87 * @param context The CallContext. | 78 * The FLYS artifact. |
79 * @param name | |
80 * The name of the data item. | |
81 * @param context | |
82 * The CallContext. | |
88 * | 83 * |
89 * @return a list of elements that consist of tuples of the intersected | 84 * @return a list of elements that consist of tuples of the intersected |
90 * gauges of the selected river. | 85 * gauges of the selected river. |
91 */ | 86 */ |
92 @Override | 87 @Override |
93 protected Element[] createItems( | 88 protected Element[] createItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) { |
94 XMLUtils.ElementCreator cr, | |
95 Artifact artifact, | |
96 String name, | |
97 CallContext context) | |
98 { | |
99 log.debug("WQAdapted.createItems"); | 89 log.debug("WQAdapted.createItems"); |
100 | 90 |
101 if (name != null && name.equals(FIELD_WQ_MODE)) { | 91 if (name != null && name.equals(FIELD_WQ_MODE)) { |
102 return createModeItems(cr, artifact, name, context); | 92 return createModeItems(cr, artifact, name, context); |
103 } | 93 } else if (name != null && name.equals(FIELD_WQ_VALUES)) { |
104 else if (name != null && name.equals(FIELD_WQ_VALUES)) { | |
105 return createValueItems(cr, artifact, name, context); | 94 return createValueItems(cr, artifact, name, context); |
106 } | 95 } else { |
107 else { | |
108 log.warn("Unknown data object: " + name); | 96 log.warn("Unknown data object: " + name); |
109 return null; | 97 return null; |
110 } | 98 } |
111 } | 99 } |
112 | 100 |
113 | |
114 /** Creates "Q" and "W" items. */ | 101 /** Creates "Q" and "W" items. */ |
115 protected Element[] createModeItems( | 102 protected Element[] createModeItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) { |
116 XMLUtils.ElementCreator cr, | |
117 Artifact artifact, | |
118 String name, | |
119 CallContext context) | |
120 { | |
121 log.debug("WQAdapted.createModeItems"); | 103 log.debug("WQAdapted.createModeItems"); |
122 | 104 |
123 Element w = createItem(cr, new String[] { "w", "W" }); | 105 final Element w = createItem(cr, new String[] { "w", "W" }); |
124 Element q = createItem(cr, new String[] { "q", "Q" }); | 106 final Element q = createItem(cr, new String[] { "q", "Q" }); |
125 | 107 |
126 return new Element[] { w, q }; | 108 return new Element[] { w, q }; |
127 } | 109 } |
128 | 110 |
129 | |
130 /** Create the items for input to the ranges per mode. */ | 111 /** Create the items for input to the ranges per mode. */ |
131 protected Element[] createValueItems( | 112 protected Element[] createValueItems(final XMLUtils.ElementCreator cr, final Artifact artifact, final String name, final CallContext context) { |
132 XMLUtils.ElementCreator cr, | |
133 Artifact artifact, | |
134 String name, | |
135 CallContext context) | |
136 { | |
137 log.debug("WQAdapted.createValueItems"); | 113 log.debug("WQAdapted.createValueItems"); |
138 | 114 |
139 D4EArtifact flysArtifact = (D4EArtifact) artifact; | 115 final D4EArtifact flysArtifact = (D4EArtifact) artifact; |
140 | 116 |
141 RangeAccess rangeAccess = new RangeAccess(flysArtifact); | 117 final RangeAccess rangeAccess = new RangeAccess(flysArtifact); |
142 double[] dist = rangeAccess.getKmRange(); | 118 final double[] dist = rangeAccess.getKmRange(); |
143 // TODO use Access to get River and gauges. | 119 // TODO use Access to get River and gauges. |
144 River river = RiverUtils.getRiver(flysArtifact); | 120 final River river = RiverUtils.getRiver(flysArtifact); |
145 Wst wst = WstFactory.getWst(river); | 121 final Wst wst = WstFactory.getWst(river); |
146 List<Gauge> gauges = RiverUtils.getGauges(flysArtifact); | 122 final List<Gauge> gauges = RiverUtils.getGauges(flysArtifact); |
147 | 123 |
148 int num = gauges != null ? gauges.size() : 0; | 124 final int num = gauges != null ? gauges.size() : 0; |
149 | 125 |
150 if (num == 0) { | 126 if (num == 0) { |
151 log.warn("Selected distance matches no gauges."); | 127 log.warn("Selected distance matches no gauges."); |
152 return null; | 128 return null; |
153 } | 129 } |
154 | 130 |
155 List<Element> elements = new ArrayList<Element>(); | 131 final List<Element> elements = new ArrayList<>(); |
156 | 132 |
157 double rangeFrom = dist[0]; | 133 double rangeFrom = dist[0]; |
158 double rangeTo = dist[1]; | 134 double rangeTo = dist[1]; |
159 | 135 |
160 if (rangeFrom < rangeTo) { | 136 if (rangeFrom < rangeTo) { |
161 Collections.sort(gauges, GAUGE_UP); | 137 Collections.sort(gauges, GAUGE_UP); |
162 for (Gauge gauge: gauges) { | 138 for (final Gauge gauge : gauges) { |
163 Range range = gauge.getRange(); | 139 final Range range = gauge.getRange(); |
164 double lower = range.getA().doubleValue(); | 140 final double lower = range.getA().doubleValue(); |
165 double upper = range.getB().doubleValue(); | 141 final double upper = range.getB().doubleValue(); |
166 | 142 |
167 // If gauge out of range, skip it. | 143 // If gauge out of range, skip it. |
168 if (upper <= rangeFrom || lower >= rangeTo) { | 144 if (upper <= rangeFrom || lower >= rangeTo) { |
169 continue; | 145 continue; |
170 } | 146 } |
171 | 147 |
172 double from = lower < rangeFrom ? rangeFrom : lower; | 148 final double from = lower < rangeFrom ? rangeFrom : lower; |
173 double to = upper > rangeTo ? rangeTo : upper; | 149 final double to = upper > rangeTo ? rangeTo : upper; |
174 | 150 |
175 double[] mmQ = determineMinMaxQ(gauge, wst); | 151 final double[] mmQ = determineMinMaxQ(gauge, wst); |
176 double[] mmW = gauge.determineMinMaxW(); | 152 final double[] mmW = gauge.determineMinMaxW(); |
177 | 153 |
178 elements.add(createItem( | 154 elements.add(createItem(cr, new String[] { from + ";" + to, gauge.getName() }, mmQ, mmW)); |
179 cr, | 155 } |
180 new String[] { from + ";" + to, | 156 } else { |
181 gauge.getName()}, | |
182 mmQ, | |
183 mmW)); | |
184 } | |
185 } | |
186 else { | |
187 Collections.sort(gauges, GAUGE_DOWN); | 157 Collections.sort(gauges, GAUGE_DOWN); |
188 rangeFrom = dist[1]; | 158 rangeFrom = dist[1]; |
189 rangeTo = dist[0]; | 159 rangeTo = dist[0]; |
190 for (Gauge gauge: gauges) { | 160 for (final Gauge gauge : gauges) { |
191 Range range = gauge.getRange(); | 161 final Range range = gauge.getRange(); |
192 double lower = range.getA().doubleValue(); | 162 final double lower = range.getA().doubleValue(); |
193 double upper = range.getB().doubleValue(); | 163 final double upper = range.getB().doubleValue(); |
194 | 164 |
195 double from = lower < rangeFrom ? rangeFrom : lower; | 165 final double from = lower < rangeFrom ? rangeFrom : lower; |
196 double to = upper > rangeTo ? rangeTo : upper; | 166 final double to = upper > rangeTo ? rangeTo : upper; |
197 | 167 |
198 // TODO probably need to continue out if oof range (see above). | 168 // TODO probably need to continue out if oof range (see above). |
199 | 169 |
200 double[] mmQ = determineMinMaxQ(gauge, wst); | 170 final double[] mmQ = determineMinMaxQ(gauge, wst); |
201 double[] mmW = gauge.determineMinMaxW(); | 171 final double[] mmW = gauge.determineMinMaxW(); |
202 | 172 |
203 elements.add(createItem( | 173 elements.add(createItem(cr, new String[] { to + ";" + from, gauge.getName() }, mmQ, mmW)); |
204 cr, | 174 } |
205 new String[] { to + ";" + from, | 175 } |
206 gauge.getName()}, | 176 |
207 mmQ, | 177 final Element[] els = new Element[elements.size()]; |
208 mmW)); | |
209 } | |
210 } | |
211 | |
212 Element[] els = new Element[elements.size()]; | |
213 return elements.toArray(els); | 178 return elements.toArray(els); |
214 } | 179 } |
215 | 180 |
216 | 181 // dead code! |
217 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { | 182 // protected Element createItem(XMLUtils.ElementCreator cr, Object obj) { |
218 return createItem(cr, obj, null, null); | 183 // return createItem(cr, obj, null, null); |
219 } | 184 // } |
220 | |
221 | 185 |
222 /** In obj: 0 is label, 1 is value. */ | 186 /** In obj: 0 is label, 1 is value. */ |
223 protected Element createItem( | 187 protected Element createItem(final XMLUtils.ElementCreator cr, final Object obj, final double[] q, final double[] w) { |
224 XMLUtils.ElementCreator cr, | 188 final Element item = ProtocolUtils.createArtNode(cr, "item", null, null); |
225 Object obj, | 189 final Element label = ProtocolUtils.createArtNode(cr, "label", null, null); |
226 double[] q, | 190 final Element value = ProtocolUtils.createArtNode(cr, "value", null, null); |
227 double[] w) | 191 |
228 { | 192 final String[] arr = (String[]) obj; |
229 Element item = ProtocolUtils.createArtNode(cr, "item", null, null); | |
230 Element label = ProtocolUtils.createArtNode(cr, "label", null, null); | |
231 Element value = ProtocolUtils.createArtNode(cr, "value", null, null); | |
232 | |
233 String[] arr = (String[]) obj; | |
234 | 193 |
235 label.setTextContent(arr[0]); | 194 label.setTextContent(arr[0]); |
236 value.setTextContent(arr[1]); | 195 value.setTextContent(arr[1]); |
237 | 196 |
238 item.appendChild(label); | 197 item.appendChild(label); |
239 item.appendChild(value); | 198 item.appendChild(value); |
240 | 199 |
241 if (q != null) { | 200 if (q != null) { |
242 Element qRange = createRangeElement(cr, q, "Q"); | 201 final Element qRange = createRangeElement(cr, q, "Q"); |
243 item.appendChild(qRange); | 202 item.appendChild(qRange); |
244 } | 203 } |
245 | 204 |
246 if (w != null) { | 205 if (w != null) { |
247 Element wRange = createRangeElement(cr, w, "W"); | 206 final Element wRange = createRangeElement(cr, w, "W"); |
248 item.appendChild(wRange); | 207 item.appendChild(wRange); |
249 } | 208 } |
250 | 209 |
251 return item; | 210 return item; |
252 } | 211 } |
253 | 212 |
254 | 213 protected Element createRangeElement(final XMLUtils.ElementCreator cr, final double[] mm, final String type) { |
255 protected Element createRangeElement( | 214 final Element range = ProtocolUtils.createArtNode(cr, "range", new String[] { "type" }, new String[] { type }); |
256 XMLUtils.ElementCreator cr, | 215 |
257 double[] mm, | 216 final Element min = ProtocolUtils.createArtNode(cr, "min", null, null); |
258 String type) | |
259 { | |
260 Element range = ProtocolUtils.createArtNode( | |
261 cr, "range", | |
262 new String[] {"type"}, | |
263 new String[] {type}); | |
264 | |
265 Element min = ProtocolUtils.createArtNode(cr, "min", null, null); | |
266 min.setTextContent(String.valueOf(mm[0])); | 217 min.setTextContent(String.valueOf(mm[0])); |
267 | 218 |
268 Element max = ProtocolUtils.createArtNode(cr, "max", null, null); | 219 final Element max = ProtocolUtils.createArtNode(cr, "max", null, null); |
269 max.setTextContent(String.valueOf(mm[1])); | 220 max.setTextContent(String.valueOf(mm[1])); |
270 | 221 |
271 range.appendChild(min); | 222 range.appendChild(min); |
272 range.appendChild(max); | 223 range.appendChild(max); |
273 | 224 |
274 return range; | 225 return range; |
275 } | 226 } |
276 | |
277 | 227 |
278 /** | 228 /** |
279 * Determines the min and max Q value for the given gauge. If no min and | 229 * Determines the min and max Q value for the given gauge. If no min and |
280 * max values could be determined, this method will return | 230 * max values could be determined, this method will return |
281 * [Double.MIN_VALUE, Double.MAX_VALUE]. | 231 * [Double.MIN_VALUE, Double.MAX_VALUE]. |
283 * @param gauge | 233 * @param gauge |
284 * @param wst | 234 * @param wst |
285 * | 235 * |
286 * @return the min and max Q values for the given gauge. | 236 * @return the min and max Q values for the given gauge. |
287 */ | 237 */ |
288 protected double[] determineMinMaxQ(Gauge gauge, Wst wst) { | 238 protected double[] determineMinMaxQ(final Gauge gauge, final Wst wst) { |
289 log.debug("WQAdapted.determineMinMaxQ"); | 239 log.debug("WQAdapted.determineMinMaxQ"); |
290 | 240 |
291 double[] minmaxQ = gauge != null | 241 final double[] minmaxQ = gauge != null ? wst.determineMinMaxQ(gauge.getRange()) : null; |
292 ? wst.determineMinMaxQ(gauge.getRange()) | 242 |
293 : null; | 243 final double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE; |
294 | 244 final double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE; |
295 double minQ = minmaxQ != null ? minmaxQ[0] : Double.MIN_VALUE; | |
296 double maxQ = minmaxQ != null ? minmaxQ[1] : Double.MAX_VALUE; | |
297 | 245 |
298 return new double[] { minQ, maxQ }; | 246 return new double[] { minQ, maxQ }; |
299 } | 247 } |
300 | |
301 | 248 |
302 /** Indicate client which input elements to use. */ | 249 /** Indicate client which input elements to use. */ |
303 @Override | 250 @Override |
304 protected String getUIProvider() { | 251 protected String getUIProvider() { |
305 return "wq_panel_adapted"; | 252 return "wq_panel_adapted"; |
306 } | 253 } |
307 | 254 |
308 | |
309 @Override | 255 @Override |
310 public boolean validate(Artifact artifact) | 256 public boolean validate(final Artifact artifact) throws IllegalArgumentException { |
311 throws IllegalArgumentException | |
312 { | |
313 log.debug("WQAdapted.validate"); | 257 log.debug("WQAdapted.validate"); |
314 | 258 |
315 D4EArtifact flys = (D4EArtifact) artifact; | 259 final D4EArtifact flys = (D4EArtifact) artifact; |
316 StateData data = getData(flys, FIELD_WQ_MODE); | 260 final StateData data = getData(flys, FIELD_WQ_MODE); |
317 | 261 |
318 String mode = data != null ? (String) data.getValue() : null; | 262 final String mode = data != null ? (String) data.getValue() : null; |
319 boolean isQ = mode != null | 263 final boolean isQ = mode != null ? Boolean.valueOf(mode) : false; |
320 ? Boolean.valueOf(mode) | |
321 : false; | |
322 | 264 |
323 if (!isQ) { | 265 if (!isQ) { |
324 return validateW(artifact); | 266 return validateW(artifact); |
325 } | 267 } else if (isQ) { |
326 else if (isQ) { | |
327 return validateQ(artifact); | 268 return validateQ(artifact); |
328 } | 269 } else { |
329 else { | 270 throw new IllegalArgumentException("error_feed_no_wq_mode_selected"); |
330 throw new IllegalArgumentException( | 271 } |
331 "error_feed_no_wq_mode_selected"); | 272 } |
332 } | 273 |
333 } | 274 protected boolean validateW(final Artifact artifact) throws IllegalArgumentException { |
334 | |
335 | |
336 protected boolean validateW(Artifact artifact) | |
337 throws IllegalArgumentException | |
338 { | |
339 log.debug("WQAdapted.validateW"); | 275 log.debug("WQAdapted.validateW"); |
340 D4EArtifact flys = (D4EArtifact) artifact; | 276 final D4EArtifact flys = (D4EArtifact) artifact; |
341 | 277 |
342 RangeWithValues[] rwvs = extractInput(getData(flys, "wq_values")); | 278 final RangeWithValues[] rwvs = extractInput(getData(flys, "wq_values")); |
343 | 279 |
344 if (rwvs == null) { | 280 if (rwvs == null) { |
345 throw new IllegalArgumentException("error_missing_wq_data"); | 281 throw new IllegalArgumentException("error_missing_wq_data"); |
346 } | 282 } |
347 | 283 |
348 List<Gauge> gauges = RiverUtils.getGauges((D4EArtifact) artifact); | 284 final List<Gauge> gauges = RiverUtils.getGauges((D4EArtifact) artifact); |
349 | 285 |
350 for (Gauge gauge: gauges) { | 286 for (final Gauge gauge : gauges) { |
351 Range range = gauge.getRange(); | 287 final Range range = gauge.getRange(); |
352 double lower = range.getA().doubleValue(); | 288 final double lower = range.getA().doubleValue(); |
353 double upper = range.getB().doubleValue(); | 289 final double upper = range.getB().doubleValue(); |
354 | 290 |
355 for (RangeWithValues rwv: rwvs) { | 291 for (final RangeWithValues rwv : rwvs) { |
356 if (lower <= rwv.getStart() && upper >= rwv.getEnd()) { | 292 if (lower <= rwv.getStart() && upper >= rwv.getEnd()) { |
357 compareWsWithGauge(gauge, rwv.getValues()); | 293 compareWsWithGauge(gauge, rwv.getValues()); |
358 } | 294 } |
359 } | 295 } |
360 } | 296 } |
361 | 297 |
362 return true; | 298 return true; |
363 } | 299 } |
364 | 300 |
365 | 301 protected boolean validateQ(final Artifact artifact) throws IllegalArgumentException { |
366 protected boolean validateQ(Artifact artifact) | |
367 throws IllegalArgumentException | |
368 { | |
369 log.debug("WQAdapted.validateQ"); | 302 log.debug("WQAdapted.validateQ"); |
370 D4EArtifact flys = (D4EArtifact) artifact; | 303 final D4EArtifact flys = (D4EArtifact) artifact; |
371 | 304 |
372 RangeWithValues[] rwvs = extractInput(getData(flys, "wq_values")); | 305 final RangeWithValues[] rwvs = extractInput(getData(flys, "wq_values")); |
373 | 306 |
374 if (rwvs == null) { | 307 if (rwvs == null) { |
375 throw new IllegalArgumentException("error_missing_wq_data"); | 308 throw new IllegalArgumentException("error_missing_wq_data"); |
376 } | 309 } |
377 | 310 |
378 List<Gauge> gauges = RiverUtils.getGauges(flys); | 311 final List<Gauge> gauges = RiverUtils.getGauges(flys); |
379 River river = RiverUtils.getRiver(flys); | 312 final River river = RiverUtils.getRiver(flys); |
380 Wst wst = WstFactory.getWst(river); | 313 final Wst wst = WstFactory.getWst(river); |
381 | 314 |
382 for (Gauge gauge: gauges) { | 315 for (final Gauge gauge : gauges) { |
383 Range range = gauge.getRange(); | 316 final Range range = gauge.getRange(); |
384 double lower = range.getA().doubleValue(); | 317 final double lower = range.getA().doubleValue(); |
385 double upper = range.getB().doubleValue(); | 318 final double upper = range.getB().doubleValue(); |
386 | 319 |
387 for (RangeWithValues rwv: rwvs) { | 320 for (final RangeWithValues rwv : rwvs) { |
388 if (lower <= rwv.getStart() && upper >= rwv.getEnd()) { | 321 if (lower <= rwv.getStart() && upper >= rwv.getEnd()) { |
389 compareQsWithGauge(wst, gauge, rwv.getValues()); | 322 compareQsWithGauge(wst, gauge, rwv.getValues()); |
390 } | 323 } |
391 } | 324 } |
392 } | 325 } |
393 | 326 |
394 return true; | 327 return true; |
395 } | 328 } |
396 | 329 |
397 | 330 protected boolean compareQsWithGauge(final Wst wst, final Gauge gauge, final double[] qs) throws IllegalArgumentException { |
398 protected boolean compareQsWithGauge(Wst wst, Gauge gauge, double[] qs) | 331 final double[] minmax = gauge != null ? wst.determineMinMaxQ(gauge.getRange()) : null; |
399 throws IllegalArgumentException | |
400 { | |
401 double[] minmax = gauge != null | |
402 ? wst.determineMinMaxQ(gauge.getRange()) | |
403 : null; | |
404 | 332 |
405 if (minmax == null) { | 333 if (minmax == null) { |
406 log.warn("Could not determine min/max Q of gauge."); | 334 log.warn("Could not determine min/max Q of gauge."); |
407 return true; | 335 return true; |
408 } | 336 } |
412 log.debug("-- Gauge: " + gauge.getName()); | 340 log.debug("-- Gauge: " + gauge.getName()); |
413 log.debug("-- Gauge min: " + minmax[0]); | 341 log.debug("-- Gauge min: " + minmax[0]); |
414 log.debug("-- Gauge max: " + minmax[1]); | 342 log.debug("-- Gauge max: " + minmax[1]); |
415 } | 343 } |
416 | 344 |
417 for (double q: qs) { | 345 for (final double q : qs) { |
418 if (q < minmax[0] || q > minmax[1]) { | 346 if (q < minmax[0] || q > minmax[1]) { |
419 throw new IllegalArgumentException( | 347 throw new IllegalArgumentException("error_feed_q_values_invalid"); |
420 "error_feed_q_values_invalid"); | |
421 } | 348 } |
422 } | 349 } |
423 | 350 |
424 return true; | 351 return true; |
425 } | 352 } |
426 | 353 |
427 | 354 protected boolean compareWsWithGauge(final Gauge gauge, final double[] ws) throws IllegalArgumentException { |
428 protected boolean compareWsWithGauge(Gauge gauge, double[] ws) | 355 final double[] minmax = gauge != null ? gauge.determineMinMaxW() : null; |
429 throws IllegalArgumentException | |
430 { | |
431 double[] minmax = gauge != null | |
432 ? gauge.determineMinMaxW() | |
433 : null; | |
434 | 356 |
435 if (minmax == null) { | 357 if (minmax == null) { |
436 log.warn("Could not determine min/max W of gauge."); | 358 log.warn("Could not determine min/max W of gauge."); |
437 return true; | 359 return true; |
438 } | 360 } |
442 log.debug("-- Gauge: " + gauge.getName()); | 364 log.debug("-- Gauge: " + gauge.getName()); |
443 log.debug("-- Gauge min: " + minmax[0]); | 365 log.debug("-- Gauge min: " + minmax[0]); |
444 log.debug("-- Gauge max: " + minmax[1]); | 366 log.debug("-- Gauge max: " + minmax[1]); |
445 } | 367 } |
446 | 368 |
447 for (double w: ws) { | 369 for (final double w : ws) { |
448 if (w < minmax[0] || w > minmax[1]) { | 370 if (w < minmax[0] || w > minmax[1]) { |
449 throw new IllegalArgumentException( | 371 throw new IllegalArgumentException("error_feed_w_values_invalid"); |
450 "error_feed_w_values_invalid"); | |
451 } | 372 } |
452 } | 373 } |
453 | 374 |
454 return true; | 375 return true; |
455 } | 376 } |
456 | 377 |
457 | 378 protected RangeWithValues[] extractInput(final StateData data) { |
458 protected RangeWithValues[] extractInput(StateData data) { | |
459 if (data == null) { | 379 if (data == null) { |
460 return null; | 380 return null; |
461 } | 381 } |
462 | 382 |
463 String dataString = (String) data.getValue(); | 383 final String dataString = (String) data.getValue(); |
464 String[] ranges = dataString.split(":"); | 384 final String[] ranges = dataString.split(":"); |
465 | 385 |
466 List<RangeWithValues> rwv = new ArrayList<RangeWithValues>(); | 386 final List<RangeWithValues> rwv = new ArrayList<>(); |
467 | 387 |
468 for (String range: ranges) { | 388 for (final String range : ranges) { |
469 String[] parts = range.split(";"); | 389 final String[] parts = range.split(";"); |
470 | 390 |
471 double lower = Double.parseDouble(parts[0]); | 391 final double lower = Double.parseDouble(parts[0]); |
472 double upper = Double.parseDouble(parts[1]); | 392 final double upper = Double.parseDouble(parts[1]); |
473 | 393 |
474 String[] values = parts[3].split(","); | 394 final String[] values = parts[3].split(","); |
475 | 395 |
476 int num = values.length; | 396 final int num = values.length; |
477 double[] res = new double[num]; | 397 final double[] res = new double[num]; |
478 | 398 |
479 for (int i = 0; i < num; i++) { | 399 for (int i = 0; i < num; i++) { |
480 try { | 400 try { |
481 res[i] = Double.parseDouble(values[i]); | 401 res[i] = Double.parseDouble(values[i]); |
482 } | 402 } |
483 catch (NumberFormatException nfe) { | 403 catch (final NumberFormatException nfe) { |
484 log.warn(nfe, nfe); | 404 log.warn(nfe, nfe); |
485 } | 405 } |
486 } | 406 } |
487 | 407 |
488 rwv.add(new RangeWithValues(lower, upper, res)); | 408 rwv.add(new RangeWithValues(lower, upper, res)); |