changeset 4812:65bfb6faa538

Bring Access pattern to "W fuer ungleichwertige Abflusslaengsschnitte".
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 18 Jan 2013 19:33:26 +0100
parents a06e443f159a
children 47c529e2be26
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/Calculation4Access.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java
diffstat 5 files changed, 185 insertions(+), 104 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Jan 18 10:30:09 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Jan 18 19:33:26 2013 +0100
@@ -10,6 +10,8 @@
 
 import de.intevation.artifacts.common.utils.StringUtils;
 
+import de.intevation.flys.artifacts.access.Calculation4Access;
+
 import de.intevation.flys.artifacts.geom.Lines;
 
 import de.intevation.flys.artifacts.model.Calculation1;
@@ -22,15 +24,16 @@
 import de.intevation.flys.artifacts.model.CalculationResult;
 import de.intevation.flys.artifacts.model.DischargeTables;
 import de.intevation.flys.artifacts.model.FacetTypes;
-import de.intevation.flys.artifacts.model.Segment;
 import de.intevation.flys.artifacts.model.WQCKms;
 import de.intevation.flys.artifacts.model.WQKms;
 import de.intevation.flys.artifacts.model.WW;
 import de.intevation.flys.artifacts.model.WstValueTable;
 import de.intevation.flys.artifacts.model.WstValueTableFactory;
+
 import de.intevation.flys.artifacts.model.extreme.ExtremeResult;
 
 import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
+
 import de.intevation.flys.artifacts.states.LocationDistanceSelect;
 
 import de.intevation.flys.model.DischargeTable;
@@ -47,7 +50,6 @@
 
 import java.util.Arrays;
 import java.util.Calendar;
-import java.util.Collections;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Map;
@@ -169,6 +171,12 @@
         return this.getWaterlevelData(null);
     }
 
+    // THIS IS FREAKY BULLSHIT! Felix, why do you call the calculation directly????
+    protected CalculationResult getDischargeLongitudinalSectionData() {
+        // XXX: THIS AN _EXPENSIVE_ CALCULATION! CACHE IT!
+        return new Calculation4(new Calculation4Access(this)).calculate();
+    }
+
     /**
      * Returns the data that is computed by a waterlevel computation.
      *
@@ -405,48 +413,6 @@
 
 
     /**
-     * Returns the data computed by the discharge longitudinal section
-     * computation.
-     *
-     * @return an array of WQKms object - one object for each given Q value.
-     */
-    public CalculationResult getDischargeLongitudinalSectionData() {
-
-        logger.debug("WINFOArtifact.getDischargeLongitudinalSectionData");
-
-        River river = FLYSUtils.getRiver(this);
-        if (river == null) {
-            logger.debug("No river selected.");
-            return error(new WQKms[0], "no.river.selected");
-        }
-
-        WstValueTable table = WstValueTableFactory.getTable(river);
-        if (table == null) {
-            logger.debug("No wst found for selected river.");
-            return error(new WQKms[0], "no.wst.for.river");
-        }
-
-        List<Segment> segments = getSegments();
-
-        if (segments == null) {
-            logger.debug("Cannot create segments.");
-            return error(new WQKms[0], "cannot.create.segments");
-        }
-
-        double [] range = getFromToStep();
-
-        if (range == null) {
-            logger.debug("Cannot figure out range.");
-            return error(new WQKms[0], "no.range.found");
-        }
-
-        Calculation4 calc4 = new Calculation4(segments, river, isQ());
-
-        return calc4.calculate(table, range[0], range[1], range[2]);
-    }
-
-
-    /**
      * Returns the data that is computed by a reference curve computation.
      *
      * @return the data computed by a reference curve computation.
@@ -574,21 +540,6 @@
     }
 
 
-    public List<Segment> getSegments() {
-        StateData wqValues = getData("wq_values");
-        if (wqValues == null) {
-            logger.warn("no wq_values given");
-            return Collections.<Segment>emptyList();
-        }
-        String input = (String) wqValues.getValue();
-        if (input == null || (input = input.trim()).length() == 0) {
-            logger.warn("wq_values are empty");
-            return Collections.<Segment>emptyList();
-        }
-        return Segment.parseSegments(input);
-    }
-
-
     /**
      * Get corrected waterline against surface/profile.
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/Calculation4Access.java	Fri Jan 18 19:33:26 2013 +0100
@@ -0,0 +1,90 @@
+package de.intevation.flys.artifacts.access;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+import de.intevation.flys.artifacts.model.Segment;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.utils.DoubleUtil;
+import de.intevation.flys.utils.FLYSUtils;
+
+public class Calculation4Access
+extends      RiverAccess
+{
+    private static Logger log = Logger.getLogger(Calculation4Access.class);
+
+    protected List<Segment> segments;
+
+    protected double [] fromToStep;
+
+    protected Boolean isQ;
+
+    protected Boolean isRange;
+
+    public Calculation4Access() {
+    }
+
+    public Calculation4Access(FLYSArtifact artifact) {
+        super(artifact);
+    }
+
+    public List<Segment> getSegments() {
+        if (segments == null) {
+            String input = getString("wq_values");
+            if (input == null || (input = input.trim()).length() == 0) {
+                log.warn("no wq_values given");
+                segments = Collections.<Segment>emptyList();
+            }
+        }
+        return segments;
+    }
+
+    public boolean isQ() {
+        if (isQ == null) {
+            Boolean value = getBoolean("wq_isq");
+            isQ = value != null && value;
+        }
+        return isQ;
+    }
+
+    public boolean isRange() {
+        if (isRange == null) {
+            String mode = getString("ld_mode");
+            isRange = mode == null || mode.equals("distance");
+        }
+        return isRange;
+    }
+
+    public double [] getFromToStep() {
+        if (fromToStep == null) {
+            // XXX: Is this really needed in this calculation?
+            if (!isRange()) {
+                return null;
+            }
+
+            // XXX: FLYSArtifact sucks!
+            double [] fromTo = FLYSUtils.getKmRange(artifact);
+
+            if (fromTo == null) {
+                return null;
+            }
+
+            Double dStep = getDouble("ld_step");
+            if (dStep == null) {
+                return null;
+            }
+
+            fromToStep =  new double [] {
+                fromTo[0],
+                fromTo[1],
+                DoubleUtil.round(dStep / 1000d)
+            };
+        }
+        return fromToStep;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java	Fri Jan 18 10:30:09 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation4.java	Fri Jan 18 19:33:26 2013 +0100
@@ -5,12 +5,15 @@
 import de.intevation.flys.artifacts.math.Identity;
 import de.intevation.flys.artifacts.math.Linear;
 
+import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.artifacts.model.WstValueTable.QPosition;
 
 import de.intevation.flys.model.River;
 
 import de.intevation.flys.utils.DoubleUtil;
 
+import de.intevation.flys.artifacts.access.Calculation4Access;
+
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -26,23 +29,69 @@
 
     protected List<Segment> segments;
 
-    protected boolean       isQ;
+    protected boolean isQ;
+    protected double  from;
+    protected double  to;
+    protected double  step;
+    protected String  river;
 
     public Calculation4() {
     }
 
-    public Calculation4(List<Segment> segments, River river, boolean isQ) {
+    public Calculation4(Calculation4Access access) {
+        String        river    = access.getRiver();
+        List<Segment> segments = access.getSegments();
+        double []     range    = access.getFromToStep();
+        boolean       isQ      = access.isQ();
 
-        this.segments = segments;
-        this.isQ      = isQ;
+        if (river == null) {
+            addProblem("no.river.selected");
+        }
 
-        Segment.setReferencePointConvertQ(segments, river, isQ, this);
+        if (range == null) {
+            addProblem("no.range.found");
+        }
+
+        if (segments == null || segments.isEmpty()) {
+            addProblem("cannot.create.segments");
+        }
+
+        if (!hasProblems()) {
+            this.river    = river;
+            this.segments = segments;
+            this.from     = range[0];
+            this.to       = range[1];
+            this.step     = range[2];
+            this.isQ      = isQ;
+        }
     }
 
-    public CalculationResult calculate(
-        WstValueTable table,
-        double from, double to, double step
-    ) {
+    public CalculationResult calculate() {
+        if (hasProblems()) {
+            return new CalculationResult(new WQKms[0], this);
+        }
+
+        WstValueTable table = null;
+        River r = RiverFactory.getRiver(river);
+        if (r == null) {
+            addProblem("no.river.found");
+        }
+        else {
+            table = WstValueTableFactory.getTable(r);
+            if (table == null) {
+                addProblem("no.wst.for.river");
+            }
+            else {
+                Segment.setReferencePointConvertQ(segments, r, isQ, this);
+            }
+        }
+
+        return hasProblems()
+            ? new CalculationResult(new WQKms[0], this)
+            : innerCalculate(table);
+    }
+
+    protected CalculationResult innerCalculate(WstValueTable table) {
         boolean debug = logger.isDebugEnabled();
 
         if (debug) {
@@ -54,12 +103,6 @@
             }
         }
 
-        if (segments.isEmpty()) {
-            logger.debug("no segments found");
-            addProblem("no.segments.found");
-            return new CalculationResult(new WQKms[0], this);
-        }
-
         int numResults = segments.get(0).values.length;
 
         if (numResults < 1) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java	Fri Jan 18 10:30:09 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java	Fri Jan 18 19:33:26 2013 +0100
@@ -1,37 +1,36 @@
 package de.intevation.flys.artifacts.states;
 
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.artifacts.ChartArtifact;
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+import de.intevation.flys.artifacts.access.Calculation4Access;
+
+import de.intevation.flys.artifacts.model.Calculation4;
+import de.intevation.flys.artifacts.model.CalculationResult;
+import de.intevation.flys.artifacts.model.CrossSectionWaterLineFacet;
+import de.intevation.flys.artifacts.model.DataFacet;
+import de.intevation.flys.artifacts.model.EmptyFacet;
+import de.intevation.flys.artifacts.model.FacetTypes;
+import de.intevation.flys.artifacts.model.ReportFacet;
+import de.intevation.flys.artifacts.model.WQCKms;
+import de.intevation.flys.artifacts.model.WQKms;
+import de.intevation.flys.artifacts.model.WaterlevelFacet;
+
 import java.util.List;
 
 import org.apache.log4j.Logger;
 
-import de.intevation.artifacts.CallContext;
-
-import de.intevation.artifactdatabase.state.Facet;
-
-import de.intevation.flys.artifacts.FLYSArtifact;
-import de.intevation.flys.artifacts.WINFOArtifact;
-import de.intevation.flys.artifacts.ChartArtifact;
-
-import de.intevation.flys.artifacts.model.CrossSectionWaterLineFacet;
-import de.intevation.flys.artifacts.model.FacetTypes;
-import de.intevation.flys.artifacts.model.ReportFacet;
-import de.intevation.flys.artifacts.model.WaterlevelFacet;
-import de.intevation.flys.artifacts.model.EmptyFacet;
-
-import de.intevation.flys.artifacts.model.WQKms;
-import de.intevation.flys.artifacts.model.WQCKms;
-import de.intevation.flys.artifacts.model.CalculationResult;
-
-import de.intevation.flys.artifacts.model.DataFacet;
-
 public class DischargeLongitudinalSection
 extends      DefaultState
 implements   FacetTypes
 {
-    private static Logger logger =
+    private static Logger log =
         Logger.getLogger(DischargeLongitudinalSection.class);
 
-
     @Override
     public Object computeAdvance(
         FLYSArtifact artifact,
@@ -46,11 +45,11 @@
             return null;
         }
 
-        WINFOArtifact winfo = (WINFOArtifact)artifact;
+        Calculation4Access access = new Calculation4Access(artifact);
 
         CalculationResult res = old instanceof CalculationResult
             ? (CalculationResult)old
-            : winfo.getDischargeLongitudinalSectionData();
+            : new Calculation4(access).calculate();
 
         if (facets == null) {
             return res;
@@ -62,7 +61,7 @@
             String nameW = null;
             String nameQ = null;
 
-            if (winfo.isQ()) {
+            if (access.isQ()) {
                 nameQ = wqkms[i].getName();
                 nameW = "W(" + nameQ + ")";
             }
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Fri Jan 18 10:30:09 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java	Fri Jan 18 19:33:26 2013 +0100
@@ -225,15 +225,13 @@
         if (mode == null || mode.length() == 0) {
             return KM_MODE.NONE;
         }
-        else if (mode.equals("distance"))  {
+        if (mode.equals("distance"))  {
             return KM_MODE.RANGE;
         }
-        else if (mode.equals("locations")) {
+        if (mode.equals("locations")) {
             return KM_MODE.LOCATIONS;
         }
-        else {
-            return KM_MODE.NONE;
-        }
+        return KM_MODE.NONE;
     }
 
     /**

http://dive4elements.wald.intevation.org