[PATCH 17 of 45] (issue1754) Add distantce handling to WINFO differences state

Wald Commits scm-commit at wald.intevation.org
Tue Mar 10 17:05:45 CET 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1424087738 -3600
# Node ID 04367906f1584d184593f8c112d6a65baec330c0
# Parent  522f72f43ae695aae78e844dd468be7b957ba399
(issue1754) Add distantce handling to WINFO differences state

diff -r 522f72f43ae6 -r 04367906f158 artifacts/doc/conf/artifacts/winfo.xml
--- a/artifacts/doc/conf/artifacts/winfo.xml	Mon Feb 16 11:30:27 2015 +0100
+++ b/artifacts/doc/conf/artifacts/winfo.xml	Mon Feb 16 12:55:38 2015 +0100
@@ -47,7 +47,7 @@
 
         <transition transition="org.dive4elements.river.artifacts.transitions.ValueCompareTransition">
             <from state="state.winfo.calculation_mode"/>
-            <to state="state.winfo.waterlevel_pair_select"/>
+            <to state="state.winfo.distance_only"/>
             <condition data="calculation_mode" value="calc.w.differences" operator="equal"/>
         </transition>
 
@@ -63,6 +63,12 @@
             <condition data="calculation_mode" value="calc.extreme.curve" operator="equal"/>
         </transition>
 
+        <transition transition="org.dive4elements.river.artifacts.transitions.ValueCompareTransition">
+            <from state="state.winfo.distance_only"/>
+            <to state="state.winfo.waterlevel_pair_select"/>
+            <condition data="calculation_mode" value="calc.w.differences" operator="equal"/>
+        </transition>
+
         <transition transition="org.dive4elements.river.artifacts.transitions.DefaultTransition">
             <from state="state.winfo.waterlevel_pair_select"/>
             <to state="state.winfo.w_differences"/>
diff -r 522f72f43ae6 -r 04367906f158 artifacts/src/main/java/org/dive4elements/river/artifacts/StaticWKmsArtifact.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/StaticWKmsArtifact.java	Mon Feb 16 11:30:27 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/StaticWKmsArtifact.java	Mon Feb 16 12:55:38 2015 +0100
@@ -280,6 +280,14 @@
             Integer.parseInt(getDataAsString("wst_id")));
     }
 
+    public WKms getWKms(int idx, double from, double to) {
+        log.debug("StaticWKmsArtifact.getWKms");
+
+        return WKmsFactory.getWKms(
+            Integer.parseInt(getDataAsString("col_pos")),
+            Integer.parseInt(getDataAsString("wst_id")),
+            from, to);
+    }
 
     /**
      * Returns W at Km of WKms, linearly interpolated.
diff -r 522f72f43ae6 -r 04367906f158 artifacts/src/main/java/org/dive4elements/river/artifacts/model/StaticWQKmsCacheKey.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/StaticWQKmsCacheKey.java	Mon Feb 16 11:30:27 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,41 +0,0 @@
-/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
- * Software engineering by Intevation GmbH
- *
- * This file is Free Software under the GNU AGPL (>=v3)
- * and comes with ABSOLUTELY NO WARRANTY! Check out the
- * documentation coming with Dive4Elements River for details.
- */
-
-package org.dive4elements.river.artifacts.model;
-
-import java.io.Serializable;
-
-/**
- * Caching-Key object for 'static' wst- data.
- */
-public final class StaticWQKmsCacheKey
-implements         Serializable
-{
-    public static final String CACHE_NAME = "wst-wq-value-table-static";
-
-    private int column;
-    private int wst_id;
-
-    public StaticWQKmsCacheKey(int column, int wst_id) {
-        this.wst_id  = wst_id;
-        this.column  = column;
-    }
-
-    public int hashCode() {
-        return (wst_id << 8) | column;
-    }
-
-    public boolean equals(Object other) {
-        if (!(other instanceof StaticWQKmsCacheKey)) {
-            return false;
-        }
-        StaticWQKmsCacheKey o = (StaticWQKmsCacheKey) other;
-        return this.wst_id == o.wst_id && this.column == o.column;
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 522f72f43ae6 -r 04367906f158 artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java	Mon Feb 16 11:30:27 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/WKmsFactory.java	Mon Feb 16 12:55:38 2015 +0100
@@ -39,6 +39,11 @@
         "SELECT km, w FROM wst_w_values " +
         "WHERE wst_id = :wst_id AND column_pos = :column_pos";
 
+    public static final String SQL_SELECT_WS_FOR_RANGE =
+        "SELECT km, w FROM wst_w_values " +
+        "WHERE wst_id = :wst_id AND column_pos = :column_pos " +
+        "AND km BETWEEN :kmfrom AND :kmto";
+
     /** Query to get name for wst_id and column_pos. */
     public static final String SQL_SELECT_NAME =
         "SELECT name " +
@@ -82,31 +87,26 @@
     private WKmsFactory() {
     }
 
+    public static WKms getWKms(int column, int wst_id, double from, double to) {
+        log.debug("WKmsFactory.getWKms");
+        Cache cache = CacheFactory.getCache("waterlevels-static");
 
-    /**
-     * Get WKms for given column and wst_id, caring about the cache.
-     */
-    public static WKms getWKms(int column, int wst_id) {
-        log.debug("WKmsFactory.getWKms");
-        Cache cache = CacheFactory.getCache(StaticWKmsCacheKey.CACHE_NAME);
-
-        StaticWKmsCacheKey cacheKey;
+        String cacheKey = Integer.toString(column) + ":" + Integer.toString(wst_id);
 
         if (cache != null) {
-            cacheKey = new StaticWKmsCacheKey(wst_id, column);
+            if (!Double.isNaN(from) && ! Double.isNaN(to)) {
+                cacheKey += ":" + Double.toString(from) + ":" + Double.toString(to);
+            }
             Element element = cache.get(cacheKey);
             if (element != null) {
                 log.debug("Got static wst values from cache");
                 return (WKms)element.getValue();
             }
         }
-        else {
-            cacheKey = null;
-        }
 
-        WKms values = getWKmsUncached(column, wst_id);
+        WKms values = getWKmsUncached(column, wst_id, from, to);
 
-        if (values != null && cacheKey != null) {
+        if (values != null && cache != null) {
             log.debug("Store static wst values in cache.");
             Element element = new Element(cacheKey, values);
             cache.put(element);
@@ -114,6 +114,13 @@
         return values;
     }
 
+    /**
+     * Get WKms for given column and wst_id, caring about the cache.
+     */
+    public static WKms getWKms(int column, int wst_id) {
+        return getWKms(column, wst_id, Double.NaN, Double.NaN);
+    }
+
     /** Get name for a WKms wrapped in W, if suitable. */
     public static String getWKmsNameWWrapped(int wst_id) {
         return getWKmsNameWWrapped(-1, wst_id);
@@ -210,7 +217,7 @@
      * @param wst_id database id of the wst
      * @return according WKms.
      */
-    public static WKms getWKmsUncached(int column, int wst_id) {
+    public static WKms getWKmsUncached(int column, int wst_id, double from, double to) {
 
         if (log.isDebugEnabled()) {
             log.debug("WKmsFactory.getWKmsUncached c/" + column + ", wst_id/" + wst_id);
@@ -219,9 +226,17 @@
         WKmsImpl wkms = new WKmsImpl(getWKmsName(column, wst_id));
 
         Session session = SessionHolder.HOLDER.get();
-        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT_WS)
-            .addScalar("km", StandardBasicTypes.DOUBLE)
-            .addScalar("w",  StandardBasicTypes.DOUBLE);
+        SQLQuery sqlQuery;
+        if (Double.isNaN(from) || Double.isNaN(to)) {
+            sqlQuery = session.createSQLQuery(SQL_SELECT_WS);
+        } else {
+            sqlQuery = session.createSQLQuery(SQL_SELECT_WS_FOR_RANGE);
+            sqlQuery.setDouble("kmfrom", from);
+            sqlQuery.setDouble("kmto", to);
+        }
+
+        sqlQuery.addScalar("km", StandardBasicTypes.DOUBLE)
+                .addScalar("w",  StandardBasicTypes.DOUBLE);
         sqlQuery.setInteger("wst_id",     wst_id);
         sqlQuery.setInteger("column_pos", column);
 
diff -r 522f72f43ae6 -r 04367906f158 artifacts/src/main/java/org/dive4elements/river/artifacts/states/WDifferencesState.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WDifferencesState.java	Mon Feb 16 11:30:27 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/WDifferencesState.java	Mon Feb 16 12:55:38 2015 +0100
@@ -19,6 +19,7 @@
 
 import org.dive4elements.artifacts.CallContext;
 import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.river.artifacts.access.RangeAccess;
 import org.dive4elements.river.artifacts.ChartArtifact;
 import org.dive4elements.river.artifacts.D4EArtifact;
 import org.dive4elements.river.artifacts.FixationArtifact;
@@ -108,25 +109,12 @@
     /**
      * Access the data (wkms) of an artifact, coded in mingle.
      */
-    public WKms getWKms(String mingle, CallContext context) {
+    public WKms getWKms(String mingle, CallContext context, double from, double to) {
         log.debug("WDifferencesState.getWKms " + mingle);
         String[] def  = mingle.split(";");
         String   uuid = def[0];
         String   name = def[1];
         int      idx  = Integer.parseInt(def[2]);
-
-        if (name.startsWith("staticwkms")) {
-            StaticWKmsArtifact staticWKms =
-                (StaticWKmsArtifact) RiverUtils.getArtifact(
-                    uuid,
-                    context);
-            log.debug("WDifferencesState obtain data from StaticWKms");
-            WKms wkms = staticWKms.getWKms(idx);
-            if (wkms == null)
-                log.error("No WKms from artifact.");
-            return wkms;
-        }
-
         D4EArtifact d4eArtifact = RiverUtils.getArtifact(
             uuid,
             context);
@@ -136,7 +124,16 @@
             return null;
         }
 
-        if (d4eArtifact instanceof WINFOArtifact) {
+        if (d4eArtifact instanceof StaticWKmsArtifact) {
+            StaticWKmsArtifact staticWKms = (StaticWKmsArtifact) d4eArtifact;
+            log.debug("WDifferencesState obtain data from StaticWKms");
+            WKms wkms = staticWKms.getWKms(idx, from, to);
+            if (wkms == null) {
+                log.error("No WKms from Static artifact for this range.");
+                return new WQKms();
+            }
+            return wkms;
+        } else if (d4eArtifact instanceof WINFOArtifact) {
             log.debug("Get WKms from WINFOArtifact");
             WINFOArtifact flys = (WINFOArtifact) d4eArtifact;
 
@@ -150,13 +147,11 @@
                 return new WQKms();
             }
             return wkms[idx];
-        }
-        else if (d4eArtifact instanceof MINFOArtifact) {
+        } else if (d4eArtifact instanceof MINFOArtifact) {
             log.warn("Get WKms from MINFOArtifact not implemented!");
 //            CalculationResult r = (CalculationResult)
 //                d4eArtifact.compute(context, ComputeType.ADVANCE, false);
-        }
-        else if (d4eArtifact instanceof FixationArtifact) {
+        } else if (d4eArtifact instanceof FixationArtifact) {
             log.debug ("Get WKms from FixationArtifact.");
             CalculationResult r = (CalculationResult)
                 d4eArtifact.compute(context, ComputeType.ADVANCE, false);
@@ -188,7 +183,9 @@
         }
         WINFOArtifact winfo = (WINFOArtifact) artifact;
         String id = getID();
-
+        RangeAccess rangeAccess = new RangeAccess(artifact);
+        double from = rangeAccess.getFrom();
+        double to = rangeAccess.getTo();
         // Load the Artifacts/facets that we want to subtract and display.
         // Expected format is:
         // [42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[1231f2-....]
@@ -213,9 +210,9 @@
             // e.g.:
             // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1
             WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]),
-                context);
+                context, from, to);
             WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]),
-                context);
+                context, from, to);
 
             String facetName = "diff ()";
             String minName = "min";


More information about the Dive4Elements-commits mailing list