[Dive4elements-commits] [PATCH] Artifacts: First part of the official lines guessing

Wald Commits scm-commit at wald.intevation.org
Fri Jun 21 12:47:04 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1371811619 -7200
# Node ID ef08c4f57edeeacde283d55d5ff9ee46e1a831cc
# Parent  2987d81ea71986bb26b911a9da00d450dbe2eec3
Artifacts: First part of the official lines guessing.

diff -r 2987d81ea719 -r ef08c4f57ede artifacts/doc/conf/cache.xml
--- a/artifacts/doc/conf/cache.xml	Fri Jun 21 12:15:30 2013 +0200
+++ b/artifacts/doc/conf/cache.xml	Fri Jun 21 12:46:59 2013 +0200
@@ -142,6 +142,13 @@
            memoryStoreEvictionPolicy="LRU"
            overflowToDisk="true"
            diskPersistent="true"
+       />
+
+    <!-- This one is used to associate the offical lines to the respective
+         main values. -->
+    <cache name="official-lines"
+           maxElementsInMemory="2"
+           timeToLiveSeconds="14400"
            />
 
     <!-- This one is used for the cross section lookup
diff -r 2987d81ea719 -r ef08c4f57ede artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/OfficialLineFinder.java	Fri Jun 21 12:46:59 2013 +0200
@@ -0,0 +1,159 @@
+/* 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.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.cache.CacheFactory;
+import org.dive4elements.river.model.Gauge;
+import org.dive4elements.river.model.MainValue;
+import org.dive4elements.river.model.NamedMainValue;
+import org.dive4elements.river.model.OfficialLine;
+import org.dive4elements.river.model.River;
+import org.dive4elements.river.model.Wst;
+import org.dive4elements.river.model.WstColumn;
+
+public class OfficialLineFinder
+{
+    public static final String CACHE_NAME = "official-lines";
+
+    // We will only have one entry in this cache.
+    public static final String CACHE_KEY = CACHE_NAME;
+
+    public static final double EPSILON = 1e-4;
+
+
+    public static class ValueRange extends Range {
+
+        private double value;
+        private int    wstId;
+        private int    columnPos;
+
+        public ValueRange(
+            double start,
+            double end, 
+            double value,
+            int    wstId,
+            int    columnPos
+        ) {
+            super(start, end);
+            this.value     = value;
+            this.wstId     = wstId;
+            this.columnPos = columnPos;
+        }
+
+        public boolean sameValue(double value) {
+            return Math.abs(value - this.value) < EPSILON;
+        }
+
+        public int getWstId() {
+            return wstId;
+        }
+
+        public int getColumnPos() {
+            return columnPos;
+        }
+    }
+
+    public OfficialLineFinder() {
+    }
+
+    public static Map<String, List<ValueRange>> getAll() {
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        if (cache == null) {
+            return getAllUncached();
+        }
+
+        Element element  = cache.get(CACHE_KEY);
+
+        if (element != null) {
+            return (Map<String, List<ValueRange>>)element.getValue();
+        }
+
+        Map<String, List<ValueRange>> result = getAllUncached();
+        if (result != null) {
+            cache.put(new Element(CACHE_KEY, result));
+        }
+        return result;
+
+    }
+
+    public static Map<String, List<ValueRange>> getAllUncached() {
+
+        Map<String, List<ValueRange>> rivers2officialLines =
+            new HashMap<String, List<ValueRange>>();
+
+
+        for (OfficialLine line: OfficialLine.fetchAllOfficalLines()) {
+            String   name = line.getNamedMainValue().getName();
+            WstColumn wc  = line.getWstColumn();
+            Wst       wst = wc.getWst();
+
+            List<ValueRange> ranges = new ArrayList<ValueRange>();
+
+            River river = wst.getRiver();
+            List<Gauge> gauges = river.getGauges();
+            for (Gauge gauge: gauges) {
+                List<MainValue> mainValues = gauge.getMainValues();
+                for (MainValue mainValue: mainValues) {
+                    NamedMainValue nmv = mainValue.getMainValue();
+                    if (nmv.getName().equalsIgnoreCase(name)) {
+                        // found gauge with this main value
+
+                        double from = gauge.getRange().getA().doubleValue();
+                        double to   = gauge.getRange().getA().doubleValue();
+                        double value = mainValue.getValue().doubleValue();
+                        int    wstId = wst.getId();
+                        int    pos   = wc.getPosition();
+                        ValueRange range =
+                            new ValueRange(from, to, value, wstId, pos);
+                        ranges.add(range);
+                        break;
+                    }
+                }
+            }
+
+            if (!ranges.isEmpty()) {
+                rivers2officialLines.put(river.getName(), ranges);
+            }
+        }
+
+        return rivers2officialLines;
+    }
+
+    public static List<OfficialLine> findOfficialLines(D4EArtifact artifact) {
+
+        Map<String, List<ValueRange>> rivers2officialLines = getAll();
+
+        String riverName = artifact.getDataAsString("river");
+
+        if (riverName == null) {
+            return Collections.<OfficialLine>emptyList();
+        }
+
+        List<ValueRange> ranges = rivers2officialLines.get(riverName);
+
+        if (ranges.isEmpty()) {
+            return Collections.<OfficialLine>emptyList();
+        }
+
+        // TODO: Figure out all the cases here.
+
+        return Collections.<OfficialLine>emptyList();
+    }
+}


More information about the Dive4elements-commits mailing list