comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java @ 6148:b12ec9f240bc

Bed height differences: DO NOT STORE ARTIFACTS IN CALCULATION RESULTS!!! This would lead to caching them which is _really_ wrong.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 01 Jun 2013 20:08:21 +0200
parents 073268a137d5
children 0f5cacdd60a9
comparison
equal deleted inserted replaced
6147:073268a137d5 6148:b12ec9f240bc
3 * 3 *
4 * This file is Free Software under the GNU AGPL (>=v3) 4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the 5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8
9 package org.dive4elements.river.artifacts.model.minfo; 8 package org.dive4elements.river.artifacts.model.minfo;
10 9
11 import gnu.trove.TDoubleArrayList; 10 import gnu.trove.TDoubleArrayList;
12 11
13 import java.util.Date; 12 import java.util.Date;
14 import java.util.LinkedList;
15 import java.util.List;
16 13
17 import org.apache.log4j.Logger; 14 import org.apache.log4j.Logger;
18 15
19 import org.dive4elements.river.artifacts.D4EArtifact; 16 import org.dive4elements.artifacts.CallContext;
20 import org.dive4elements.river.artifacts.access.BedDifferencesAccess; 17 import org.dive4elements.river.artifacts.access.BedDifferencesAccess;
21 import org.dive4elements.river.artifacts.model.Calculation; 18 import org.dive4elements.river.artifacts.model.Calculation;
22 import org.dive4elements.river.artifacts.model.CalculationResult; 19 import org.dive4elements.river.artifacts.model.CalculationResult;
23 20
24 21
25 public class BedDiffCalculation 22 public class BedDiffCalculation
26 extends Calculation 23 extends Calculation
27 { 24 {
25 private static final Logger logger =
26 Logger.getLogger(BedDiffCalculation.class);
28 27
29 private static final Logger logger = Logger 28 protected String river;
30 .getLogger(BedDiffCalculation.class); 29 protected String yearEpoch;
31 30 protected int [][] heightIds;
32 protected String river;
33 protected String yearEpoch;
34 protected D4EArtifact[][] artifacts;
35 31
36 public BedDiffCalculation() { 32 public BedDiffCalculation() {
37 } 33 }
38 34
39 public CalculationResult calculate(BedDifferencesAccess access) { 35 public CalculationResult calculate(BedDifferencesAccess access, CallContext context) {
40 logger.info("BedDiffCalculation.calculate"); 36 logger.info("BedDiffCalculation.calculate");
41 37
42 String river = access.getRiver(); 38 String river = access.getRiver();
43 String yearEpoch = access.getYearEpoch(); 39 String yearEpoch = access.getYearEpoch();
44 D4EArtifact[][] artifacts = access.getDifferenceArtifacts(); 40 int [][] heightIds = access.extractHeightIds(context);
45 41
46 logger.debug("got artifacts: " + artifacts.length + "; " + artifacts[0].length);
47 if (river == null) { 42 if (river == null) {
48 // TODO: i18n 43 // TODO: i18n
49 addProblem("minfo.missing.river"); 44 addProblem("minfo.missing.river");
50 } 45 }
51 46
52 if (yearEpoch == null) { 47 if (yearEpoch == null) {
53 addProblem("minfo.missing.year_epoch"); 48 addProblem("minfo.missing.year_epoch");
54 } 49 }
55 50
56 if (artifacts == null) {
57 addProblem("minfo.missing.differences");
58 }
59
60 if (!hasProblems()) { 51 if (!hasProblems()) {
61 this.river = river; 52 this.river = river;
62 this.yearEpoch = yearEpoch; 53 this.yearEpoch = yearEpoch;
63 this.artifacts = artifacts; 54 this.heightIds = heightIds;
64 return internalCalculate(); 55 return internalCalculate();
65 } 56 }
66 57
67 return new CalculationResult(); 58 return new CalculationResult();
68 } 59 }
69 60
70 private CalculationResult internalCalculate() { 61 private CalculationResult internalCalculate() {
71 62
72 if (yearEpoch.equals("year")) { 63 if (yearEpoch.equals("year")) {
73 List<BedDiffYearResult> results = 64 BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length];
74 new LinkedList<BedDiffYearResult>();
75 65
76 for (int i = 0; i < artifacts.length; i++) { 66 for (int i = 0; i < heightIds.length; i++) {
77 BedHeight[] pair = 67 BedHeight [] pair = getHeightPair(heightIds[i], "single");
78 getHeightPair(artifacts[i][0], artifacts[i][1], "single"); 68 results[i] = calculateYearDifference(pair);
79 BedDiffYearResult res = calculateYearDifference(pair);
80 results.add(res);
81 } 69 }
82 return new CalculationResult( 70 return new CalculationResult(results, this);
83 results.toArray(new BedDiffYearResult[results.size()]), this);
84 } 71 }
72
85 if (yearEpoch.equals("epoch")) { 73 if (yearEpoch.equals("epoch")) {
86 List<BedDiffEpochResult> results = 74 BedDiffEpochResult [] results = new BedDiffEpochResult[heightIds.length];
87 new LinkedList<BedDiffEpochResult>(); 75
88 for (int i = 0; i < artifacts.length; i++) { 76 for (int i = 0; i < heightIds.length; i++) {
89 BedHeight[] pair = 77 BedHeight[] pair = getHeightPair(heightIds[i], "epoch");
90 getHeightPair(artifacts[i][0], artifacts[i][1], "epoch"); 78 results[i] = calculateEpochDifference(pair);
91 BedDiffEpochResult res = calculateEpochDifference(pair);
92 results.add(res);
93 } 79 }
94 return new CalculationResult( 80 return new CalculationResult(results, this);
95 results.toArray(new BedDiffEpochResult[results.size()]), this);
96 } 81 }
97 82
98 return new CalculationResult(); 83 return new CalculationResult();
99 } 84 }
100 85
101 private BedHeight[] getHeightPair( 86 private static BedHeight [] getHeightPair(int [] ids, String type) {
102 D4EArtifact art1, 87 return new BedHeight [] {
103 D4EArtifact art2, 88 BedHeightFactory.getHeight(type, ids[0], 0),
104 String type 89 BedHeightFactory.getHeight(type, ids[1], 0)
105 ) { 90 };
106 int id1 = BedDifferencesAccess.getHeightId(art1);
107 int id2 = BedDifferencesAccess.getHeightId(art2);
108
109 BedHeight[] heights = new BedHeight[2];
110 heights[0] = BedHeightFactory.getHeight(type, id1, 0);
111 heights[1] = BedHeightFactory.getHeight(type, id2, 0);
112 return heights;
113 } 91 }
114 92
115 private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) { 93 private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) {
116 94
117 BedHeight bh1 = pair[0]; 95 BedHeight bh1 = pair[0];

http://dive4elements.wald.intevation.org