changeset 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 3c8147ba35a6
files artifacts/src/main/java/org/dive4elements/river/artifacts/access/BedDifferencesAccess.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/DifferencesState.java
diffstat 3 files changed, 78 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/access/BedDifferencesAccess.java	Sat Jun 01 18:59:52 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/BedDifferencesAccess.java	Sat Jun 01 20:08:21 2013 +0200
@@ -22,14 +22,14 @@
 extends RiverAccess
 {
     private static Logger logger = Logger.getLogger(BedDifferencesAccess.class);
-    private String yearEpoch;
-    private String[] diffs;
 
-    private CallContext context;
+    private String    yearEpoch;
+    private String [] diffs;
 
-    public BedDifferencesAccess(D4EArtifact artifact, CallContext context) {
+    private String [][] differenceArtifactIds;
+
+    public BedDifferencesAccess(D4EArtifact artifact) {
         super(artifact);
-        this.context = context;
     }
 
     public String getYearEpoch() {
@@ -37,20 +37,47 @@
         return yearEpoch;
     }
 
-    public D4EArtifact[][] getDifferenceArtifacts() {
-        diffs = getString("diffids").split("#");
-        logger.debug("diffs: " + Arrays.toString(diffs));
-        D4EArtifact[][] artifacts = new D4EArtifact[diffs.length/2][2];
-        for (int i = 0; i < diffs.length; i += 2) {
-            String diff1 = StringUtil.unbracket(diffs[0 + 2*i]);
-            String diff2 = StringUtil.unbracket(diffs[1 + 2*i]);
-            String[] diff1parts = diff1.split(";");
-            String[] diff2parts = diff2.split(";");
-            logger.debug("creating 2 artifacts." + diff1parts[0] + "; " + diff2parts[0]);
-            artifacts[i][0] = RiverUtils.getArtifact(diff1parts[0], context);
-            artifacts[i][1] = RiverUtils.getArtifact(diff2parts[0], context);
+    public String [] getDiffs() {
+        if (diffs == null) {
+            diffs = getString("diffids").split("#");
+            if (logger.isDebugEnabled()) {
+                logger.debug("diffs: " + Arrays.toString(diffs));
+            }
         }
-        return artifacts;
+        return diffs;
+    }
+
+    public String[][] getDifferenceArtifactIds() {
+
+        if (differenceArtifactIds == null) {
+            String [] diffs = getDiffs();
+            differenceArtifactIds = new String[diffs.length/2][2];
+            for (int i = 0; i < diffs.length; i += 2) {
+                String diff1 = StringUtil.unbracket(diffs[0 + 2*i]);
+                String diff2 = StringUtil.unbracket(diffs[1 + 2*i]);
+                String[] diff1parts = diff1.split(";");
+                String[] diff2parts = diff2.split(";");
+                if (logger.isDebugEnabled()) {
+                    logger.debug("creating 2 artifacts." + diff1parts[0] + "; " + diff2parts[0]);
+                }
+                differenceArtifactIds[i][0] = diff1parts[0];
+                differenceArtifactIds[i][1] = diff1parts[1];
+            }
+        }
+
+        return differenceArtifactIds;
+    }
+
+    public int [][] extractHeightIds(CallContext context) {
+        String [][] artifactsIds = getDifferenceArtifactIds();
+        int [][] ids = new int[artifactsIds.length][2];
+        for (int i = 0; i < artifactsIds.length; ++i) {
+            D4EArtifact a1 = RiverUtils.getArtifact(artifactsIds[i][0], context);
+            D4EArtifact a2 = RiverUtils.getArtifact(artifactsIds[i][1], context);
+            ids[i][0] = getHeightId(a1);
+            ids[i][1] = getHeightId(a2);
+        }
+        return ids;
     }
 
     public static int getHeightId(D4EArtifact artifact) {
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java	Sat Jun 01 18:59:52 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java	Sat Jun 01 20:08:21 2013 +0200
@@ -5,18 +5,15 @@
  * and comes with ABSOLUTELY NO WARRANTY! Check out the
  * documentation coming with Dive4Elements River for details.
  */
-
 package org.dive4elements.river.artifacts.model.minfo;
 
 import gnu.trove.TDoubleArrayList;
 
 import java.util.Date;
-import java.util.LinkedList;
-import java.util.List;
 
 import org.apache.log4j.Logger;
 
-import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.river.artifacts.access.BedDifferencesAccess;
 import org.dive4elements.river.artifacts.model.Calculation;
 import org.dive4elements.river.artifacts.model.CalculationResult;
@@ -25,25 +22,23 @@
 public class BedDiffCalculation
 extends Calculation
 {
+    private static final Logger logger =
+        Logger.getLogger(BedDiffCalculation.class);
 
-    private static final Logger logger = Logger
-        .getLogger(BedDiffCalculation.class);
-
-    protected String river;
-    protected String yearEpoch;
-    protected D4EArtifact[][] artifacts;
+    protected String   river;
+    protected String   yearEpoch;
+    protected int [][] heightIds;
 
     public BedDiffCalculation() {
     }
 
-    public CalculationResult calculate(BedDifferencesAccess access) {
+    public CalculationResult calculate(BedDifferencesAccess access, CallContext context) {
         logger.info("BedDiffCalculation.calculate");
 
-        String river = access.getRiver();
-        String yearEpoch = access.getYearEpoch();
-        D4EArtifact[][] artifacts = access.getDifferenceArtifacts();
+        String river       = access.getRiver();
+        String yearEpoch   = access.getYearEpoch();
+        int [][] heightIds = access.extractHeightIds(context);
 
-        logger.debug("got artifacts: " + artifacts.length + "; " + artifacts[0].length);
         if (river == null) {
             // TODO: i18n
             addProblem("minfo.missing.river");
@@ -53,14 +48,10 @@
             addProblem("minfo.missing.year_epoch");
         }
 
-        if (artifacts == null) {
-            addProblem("minfo.missing.differences");
-        }
-
         if (!hasProblems()) {
-            this.river = river;
+            this.river     = river;
             this.yearEpoch = yearEpoch;
-            this.artifacts = artifacts;
+            this.heightIds = heightIds;
             return internalCalculate();
         }
 
@@ -70,46 +61,33 @@
     private CalculationResult internalCalculate() {
 
         if (yearEpoch.equals("year")) {
-            List<BedDiffYearResult> results =
-                new LinkedList<BedDiffYearResult>();
+            BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length];
 
-            for (int i = 0; i < artifacts.length; i++) {
-                BedHeight[] pair =
-                    getHeightPair(artifacts[i][0], artifacts[i][1], "single");
-                BedDiffYearResult res = calculateYearDifference(pair);
-                results.add(res);
+            for (int i = 0; i < heightIds.length; i++) {
+                BedHeight [] pair = getHeightPair(heightIds[i], "single");
+                results[i] = calculateYearDifference(pair);
             }
-            return new CalculationResult(
-                results.toArray(new BedDiffYearResult[results.size()]), this);
+            return new CalculationResult(results, this);
         }
+
         if (yearEpoch.equals("epoch")) {
-            List<BedDiffEpochResult> results =
-                new LinkedList<BedDiffEpochResult>();
-            for (int i = 0; i < artifacts.length; i++) {
-                BedHeight[] pair =
-                    getHeightPair(artifacts[i][0], artifacts[i][1], "epoch");
-                BedDiffEpochResult res = calculateEpochDifference(pair);
-                results.add(res);
+            BedDiffEpochResult [] results = new BedDiffEpochResult[heightIds.length];
+
+            for (int i = 0; i < heightIds.length; i++) {
+                BedHeight[] pair = getHeightPair(heightIds[i], "epoch");
+                results[i] = calculateEpochDifference(pair);
             }
-            return new CalculationResult(
-                results.toArray(new BedDiffEpochResult[results.size()]), this);
+            return new CalculationResult(results, this);
         }
 
        return new CalculationResult();
     }
 
-    private BedHeight[] getHeightPair(
-        D4EArtifact art1,
-        D4EArtifact art2,
-        String type
-    ) {
-        int id1 = BedDifferencesAccess.getHeightId(art1);
-        int id2 = BedDifferencesAccess.getHeightId(art2);
-
-        BedHeight[] heights = new BedHeight[2];
-        heights[0] = BedHeightFactory.getHeight(type, id1, 0);
-        heights[1] = BedHeightFactory.getHeight(type, id2, 0);
-        return heights;
+    private static BedHeight [] getHeightPair(int [] ids, String type) {
+        return new BedHeight [] {
+            BedHeightFactory.getHeight(type, ids[0], 0),
+            BedHeightFactory.getHeight(type, ids[1], 0)
+        };
     }
 
     private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) {
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/DifferencesState.java	Sat Jun 01 18:59:52 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/DifferencesState.java	Sat Jun 01 20:08:21 2013 +0200
@@ -71,11 +71,11 @@
 
         List<Facet> newFacets = new ArrayList<Facet>();
 
-        BedDifferencesAccess access =
-            new BedDifferencesAccess(artifact, context);
+        BedDifferencesAccess access = new BedDifferencesAccess(artifact);
 
-        CalculationResult res = old instanceof CalculationResult ? (CalculationResult) old
-            : new BedDiffCalculation().calculate(access);
+        CalculationResult res = old instanceof CalculationResult
+            ? (CalculationResult)old
+            : new BedDiffCalculation().calculate(access, context);
 
         if (facets == null || res == null) {
             return res;

http://dive4elements.wald.intevation.org