changeset 687:06689035024c facets-slt

Added a compute() method to WINFOArtifact which acts as a dispatcher for different computations. It triggers a calculation based on ComputeCallbacks that are generated by the current states. flys-artifacts/branches/facets-slt@2127 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 16 Jun 2011 07:50:56 +0000
parents 3dc61e00385e
children dc3613c4daf6
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WaterlevelState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ComputeCallback.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java
diffstat 9 files changed, 188 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Thu Jun 16 07:50:56 2011 +0000
@@ -30,6 +30,7 @@
 import de.intevation.artifactdatabase.DefaultArtifact;
 import de.intevation.artifactdatabase.data.DefaultStateData;
 import de.intevation.artifactdatabase.data.StateData;
+import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.State;
 import de.intevation.artifactdatabase.state.StateEngine;
 import de.intevation.artifactdatabase.transition.TransitionEngine;
@@ -100,6 +101,9 @@
     /** The data that have been inserted into this artifact.*/
     protected Map<String, StateData> data;
 
+    /** The list of facets supported by this artifact.*/
+    protected List<Facet> facets;
+
 
     /**
      * The default constructor that creates an empty FLYSArtifact.
@@ -107,6 +111,7 @@
     public FLYSArtifact() {
         data             = new TreeMap<String, StateData>();
         previousStateIds = new ArrayList<String>();
+        facets           = new ArrayList<Facet>();
     }
 
 
@@ -1037,7 +1042,7 @@
         Cache cache = CacheFactory.getCache(COMPUTING_CACHE);
 
         if (cache == null) {
-            return callback.compute();
+            return callback.compute(facets);
         }
 
         net.sf.ehcache.Element element = cache.get(key);
@@ -1046,7 +1051,7 @@
             return element.getValue();
         }
 
-        Object result = callback.compute();
+        Object result = callback.compute(facets);
 
         if (result != null) {
             cache.put(new net.sf.ehcache.Element(key, result));
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.artifacts;
 
 import java.util.List;
+import java.util.Map;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -27,6 +28,7 @@
 import de.intevation.flys.artifacts.context.FLYSContext;
 
 import de.intevation.flys.artifacts.model.ComputeCallback;
+import de.intevation.flys.artifacts.model.DischargeTables;
 import de.intevation.flys.artifacts.model.MainValuesFactory;
 import de.intevation.flys.artifacts.model.WQDay;
 import de.intevation.flys.artifacts.model.WQKms;
@@ -243,32 +245,38 @@
     //
 
 
-    public Object compute() {
-        return compute(hash());
+    /**
+     * Dispatches the computation request to compute(CallContext context, String
+     * hash) with the current hash value of the artifact which is provided by
+     * hash().
+     *
+     * @param context The CallContext.
+     */
+    public Object compute(CallContext context) {
+        return compute(context, hash());
     }
 
 
-    public Object compute(String hash) {
+    /**
+     * Dispatches computation requests to the current state which needs to
+     * implement a createComputeCallback(String hash, FLYSArtifact artifact)
+     * method.
+     *
+     * @param context The CallContext.
+     * @param hash The hash value which is used to fetch computed data from
+     * cache.
+     *
+     * @return the computed data.
+     */
+    public Object compute(CallContext context, String hash) {
         String calc = (String) getData(CalculationSelect.FIELD_MODE).getValue();
 
-        ComputeCallback callback = null;
+        DefaultState current = (DefaultState) getCurrentState(context);
 
-        if (calc.equals(CalculationSelect.CALCULATION_SURFACE_CURVE)) {
-            callback = createSurfaceCurveCallback();
-        }
-        else if (calc.equals(CalculationSelect.CALCULATION_DURATION_CURVE)) {
-            callback = createDurationCurveCallback();
-        }
-        else if (
-            calc.equals(
-                CalculationSelect.CALCULATION_DISCHARGE_LONGITUDINAL_CURVE))
-        {
-            callback = createDischargeLongitudinalCurveCallback();
-        }
-        else if (calc.equals(CalculationSelect.CALCULATION_DISCHARGE_CURVE)) {
-            callback = createDischargeCurveCallback();
-        }
-        else {
+        ComputeCallback callback = current.createComputeCallback(hash(), this);
+
+        if (callback == null) {
+            logger.info("There is no data which needs to be computed.");
             return null;
         }
 
@@ -276,42 +284,6 @@
     }
 
 
-    protected ComputeCallback createSurfaceCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getWaterlevelData();
-            }
-        };
-    }
-
-
-    protected ComputeCallback createDurationCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getDurationCurveData();
-            }
-        };
-    }
-
-
-    protected ComputeCallback createDischargeLongitudinalCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getDischargeLongitudinalSectionData();
-            }
-        };
-    }
-
-
-    protected ComputeCallback createDischargeCurveCallback() {
-        return new ComputeCallback() {
-            public Object compute() {
-                return getComputedDischargeCurveData();
-            }
-        };
-    }
-
-
     /**
      * Returns the data that is computed by a waterlevel computation.
      *
@@ -446,6 +418,37 @@
 
 
     /**
+     * Returns the data that is used to create discharge curves.
+     *
+     * @return a map where the key is the name of the gauge and a double[][]
+     * which are the values.
+     */
+    public Map<String, double[][]> getDischargeCurveData() {
+        River       river  = getRiver();
+        List<Gauge> gauges = getGauges();
+
+        if (gauges == null) {
+            logger.warn("No gauges found for the current kilometer range.");
+            return null;
+        }
+
+        int num = gauges.size();
+
+        logger.debug("Found " + num + " gauges.");
+
+        String[] gaugeNames = new String[num];
+
+        for (int i = 0; i < num; i++) {
+            gaugeNames[i] = gauges.get(i).getName();
+        }
+
+        DischargeTables dt = new DischargeTables(river.getName(), gaugeNames);
+
+        return dt.getValues(100d);
+    }
+
+
+    /**
      * Returns the data that is computed by a discharge curve computation.
      *
      * @return the data computed by a discharge curve computation.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WaterlevelState.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WaterlevelState.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Element;
@@ -9,6 +11,12 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.WINFOArtifact;
+import de.intevation.flys.artifacts.model.ComputeCallback;
+
 
 public class WaterlevelState extends DefaultState {
 
@@ -25,5 +33,19 @@
         logger.debug("WaterlevelState.createItems");
         return null;
     }
+
+
+    @Override
+    public ComputeCallback createComputeCallback(
+        String hash, FLYSArtifact flys)
+    {
+        final WINFOArtifact winfo = (WINFOArtifact) flys;
+
+        return new ComputeCallback() {
+            public Object compute(List<Facet> facets) {
+                return winfo.getWaterlevelData();
+            }
+        };
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ComputeCallback.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/ComputeCallback.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,7 +1,12 @@
 package de.intevation.flys.artifacts.model;
 
+import java.util.List;
+
+import de.intevation.artifactdatabase.state.Facet;
+
+
 public interface ComputeCallback {
 
-    Object compute();
+    Object compute(List<Facet> facets);
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,7 +1,15 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.WINFOArtifact;
+import de.intevation.flys.artifacts.model.ComputeCallback;
+
 /**
  * The final state that will be reached after the discharge curve calculation
  * mode has been chosen.
@@ -16,5 +24,19 @@
 
     public ComputedDischargeCurveState() {
     }
+
+
+    @Override
+    public ComputeCallback createComputeCallback(
+        String hash, FLYSArtifact flys)
+    {
+        final WINFOArtifact winfo = (WINFOArtifact) flys;
+
+        return new ComputeCallback() {
+            public Object compute(List<Facet> facets) {
+                return winfo.getComputedDischargeCurveData();
+            }
+        };
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DefaultState.java	Thu Jun 16 07:50:56 2011 +0000
@@ -23,6 +23,7 @@
 import de.intevation.artifactdatabase.state.AbstractState;
 
 import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.model.ComputeCallback;
 import de.intevation.flys.artifacts.resources.Resources;
 
 
@@ -255,5 +256,12 @@
     protected String getUIProvider() {
         return null;
     }
+
+
+    public ComputeCallback createComputeCallback(
+        String hash, FLYSArtifact flys)
+    {
+        return null;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,10 +1,33 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.WINFOArtifact;
+import de.intevation.flys.artifacts.model.ComputeCallback;
+
 public class DischargeLongitudinalSection extends DefaultState {
 
     private static Logger logger =
         Logger.getLogger(DischargeLongitudinalSection.class);
+
+
+    @Override
+    public ComputeCallback createComputeCallback(
+        String hash, FLYSArtifact flys)
+    {
+        final WINFOArtifact winfo = (WINFOArtifact) flys;
+
+        return new ComputeCallback() {
+            public Object compute(List<Facet> facets) {
+                return winfo.getDischargeLongitudinalSectionData();
+            }
+        };
+
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,7 +1,15 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.WINFOArtifact;
+import de.intevation.flys.artifacts.model.ComputeCallback;
+
 /**
  * The final state that will be reached after the duration curve calculation
  * mode has been chosen.
@@ -15,5 +23,19 @@
 
     public DurationCurveState() {
     }
+
+
+    @Override
+    public ComputeCallback createComputeCallback(
+        String hash, FLYSArtifact flys)
+    {
+        final WINFOArtifact winfo = (WINFOArtifact) flys;
+
+        return new ComputeCallback() {
+            public Object compute(List<Facet> facets) {
+                return winfo.getDurationCurveData();
+            }
+        };
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java	Wed Jun 15 15:28:54 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java	Thu Jun 16 07:50:56 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.states;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Element;
@@ -13,10 +15,13 @@
 
 import de.intevation.artifactdatabase.ProtocolUtils;
 import de.intevation.artifactdatabase.data.StateData;
+import de.intevation.artifactdatabase.state.Facet;
 
 import de.intevation.flys.model.River;
 
 import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.WINFOArtifact;
+import de.intevation.flys.artifacts.model.ComputeCallback;
 import de.intevation.flys.artifacts.model.RiverFactory;
 import de.intevation.flys.artifacts.resources.Resources;
 
@@ -290,5 +295,19 @@
 
         return values.toNativeArray();
     }
+
+
+    @Override
+    public ComputeCallback createComputeCallback(
+        String hash, FLYSArtifact flys)
+    {
+        final WINFOArtifact winfo = (WINFOArtifact) flys;
+
+        return new ComputeCallback() {
+            public Object compute(List<Facet> facets) {
+                return winfo.getDischargeCurveData();
+            }
+        };
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org