changeset 3121:0b86b005bb9a

FixA: Respect the selected events and reference period correctly. flys-artifacts/trunk@4722 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 20 Jun 2012 11:25:21 +0000
parents e52a3b62fc20
children 721298eeb694
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java
diffstat 5 files changed, 121 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Jun 20 11:24:30 2012 +0000
+++ b/flys-artifacts/ChangeLog	Wed Jun 20 11:25:21 2012 +0000
@@ -1,3 +1,19 @@
+2012-06-20	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java:
+	  Now respects the selected events and reference period correctly.
+
+	* src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java:
+	  referenceStart and referenceEnd are now melted into referencePeriod.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java:
+	  Add a new filter IdsFilter which is more efficent to check than a
+	  OrFilter with a list of IdFilters inside.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java:
+	  Expose the new IdsFilter to the XML representation in form of
+	  <columns cids="<list of whitespace separated ids"/>
+
 2012-06-20  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/sq/SQRelationGenerator.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java	Wed Jun 20 11:24:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FixationArtifactAccess.java	Wed Jun 20 11:25:21 2012 +0000
@@ -34,9 +34,7 @@
     protected Integer qSectorStart;
     protected Integer qSectorEnd;
 
-    protected Long referenceStart;
-    protected Long referenceEnd;
-
+    protected DateRange    referencePeriod;
     protected DateRange [] analysisPeriods;
 
     protected int [] events;
@@ -254,50 +252,32 @@
         return events;
     }
 
-    public Long getReferenceStart() {
-        if (referenceStart == null) {
-            StateData sd = artifact.getData("ref_start");
-            if (sd == null) {
-                log.warn("missing 'ref_start' value");
+    public DateRange getReferencePeriod() {
+        if (referencePeriod == null) {
+            StateData refStart = artifact.getData("ref_start");
+            StateData refEnd   = artifact.getData("ref_end");
+
+            if (refStart == null || refEnd == null) {
+                log.warn("missing 'ref_start' or 'ref_start' value");
                 return null;
             }
+
             try {
-                referenceStart = Long.valueOf((String)sd.getValue());
+                long rs = Long.parseLong((String)refStart.getValue());
+                long re = Long.parseLong((String)refEnd  .getValue());
+
+                if (rs > re) { long t = rs; rs = re; re = t; }
+
+                Date from = new Date(rs);
+                Date to   = new Date(re);
+                referencePeriod = new DateRange(from, to);
             }
             catch (NumberFormatException nfe) {
-                log.warn("ref_start '"
-                    + sd.getValue() + "' is not an integer.");
+                log.warn("ref_start or ref_end is not an integer.");
             }
         }
 
-        if (log.isDebugEnabled()) {
-            log.debug("referenceStart: '" + referenceStart + "'");
-        }
-
-        return referenceStart;
-    }
-
-    public Long getReferenceEnd() {
-        if (referenceEnd == null) {
-            StateData sd = artifact.getData("ref_end");
-            if (sd == null) {
-                log.warn("missing 'ref_end' value");
-                return null;
-            }
-            try {
-                referenceEnd = Long.valueOf((String)sd.getValue());
-            }
-            catch (NumberFormatException nfe) {
-                log.warn("ref_end '"
-                    + sd.getValue() + "' is not an integer.");
-            }
-        }
-
-        if (log.isDebugEnabled()) {
-            log.debug("referenceEnd: '" + referenceEnd + "'");
-        }
-
-        return referenceEnd;
+        return referencePeriod;
     }
 
     public DateRange [] getAnalysisPeriods() {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java	Wed Jun 20 11:24:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java	Wed Jun 20 11:25:21 2012 +0000
@@ -7,6 +7,7 @@
 import de.intevation.flys.artifacts.model.FixingsOverview.Fixing.Filter;
 
 import de.intevation.flys.artifacts.model.FixingsOverview.IdFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.IdsFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.KmFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.NotFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.OrFilter;
@@ -165,6 +166,24 @@
                     }
                 }
             }
+            else if ("columns".equals(name)) {
+                String cidsS = element.getAttribute("cids").trim();
+                String [] parts = cidsS.split("\\s+");
+                List<Integer> ids = new ArrayList<Integer>();
+                for (String part: parts) {
+                    try {
+                        ids.add(Integer.valueOf(part));
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn(nfe);
+                    }
+                }
+                int [] cids = new int[ids.size()];
+                for (int j = 0; j < cids.length; ++j) {
+                    cids[i] = ids.get(j);
+                }
+                filters.add(new IdsFilter(cids));
+            }
             else if ("date".equals(name)) {
                 String when = element.getAttribute("when").trim();
                 if (when.length() > 0) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Wed Jun 20 11:24:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Wed Jun 20 11:25:21 2012 +0000
@@ -765,8 +765,9 @@
             this.children = children;
         }
 
-        public void add(Fixing.Filter filter) {
+        public ComponentFilter add(Fixing.Filter filter) {
             children.add(filter);
+            return this;
         }
     } // class ComponentFilter
 
@@ -824,6 +825,26 @@
         }
     } // class IdFilter
 
+    public static class IdsFilter implements Fixing.Filter {
+
+        protected int [] columnIds;
+
+        public IdsFilter(int [] columnIds) {
+            this.columnIds = columnIds;
+        }
+
+        @Override
+        public boolean accept(Fixing.Column column) {
+            int cid = column.getId();
+            for (int i = columnIds.length-1; i >= 0; --i) {
+                if (columnIds[i] == cid) {
+                    return true;
+                }
+            }
+            return false;
+        }
+    } // class IdFilter
+
     public static class DateFilter implements Fixing.Filter {
 
         protected Date date;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java	Wed Jun 20 11:24:30 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java	Wed Jun 20 11:25:21 2012 +0000
@@ -12,12 +12,15 @@
 
 import de.intevation.flys.artifacts.model.FixingsOverview.AndFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.DateRangeFilter;
+
+import de.intevation.flys.artifacts.model.FixingsOverview.Fixing.Filter;
+
 import de.intevation.flys.artifacts.model.FixingsOverview.Fixing;
-import de.intevation.flys.artifacts.model.FixingsOverview.IdFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.IdsFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.KmFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.Range;
+import de.intevation.flys.artifacts.model.FixingsOverview.SectorFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.SectorRangeFilter;
-import de.intevation.flys.artifacts.model.FixingsOverview.SectorFilter;
 
 import de.intevation.flys.artifacts.model.FixingsOverview;
 import de.intevation.flys.artifacts.model.FixingsOverviewFactory;
@@ -49,6 +52,7 @@
     protected boolean      preprocessing;
     protected String       function;
     protected int []       events;
+    protected DateRange    referencePeriod;
     protected DateRange [] analysisPeriods;
     protected int          qSectorStart;
     protected int          qSectorEnd;
@@ -58,16 +62,17 @@
 
     public FixCalculation(FixationArtifactAccess access) {
 
-        String    river              = access.getRiver();
-        Double    from               = access.getFrom();
-        Double    to                 = access.getTo();
-        Double    step               = access.getStep();
-        String    function           = access.getFunction();
-        int []    events             = access.getEvents();
+        String       river           = access.getRiver();
+        Double       from            = access.getFrom();
+        Double       to              = access.getTo();
+        Double       step            = access.getStep();
+        String       function        = access.getFunction();
+        int []       events          = access.getEvents();
+        DateRange    referencePeriod = access.getReferencePeriod();
         DateRange [] analysisPeriods = access.getAnalysisPeriods();
-        Integer   qSectorStart       = access.getQSectorStart();
-        Integer   qSectorEnd         = access.getQSectorEnd();
-        Boolean   preprocessing      = access.getPreprocessing();
+        Integer      qSectorStart    = access.getQSectorStart();
+        Integer      qSectorEnd      = access.getQSectorEnd();
+        Boolean      preprocessing   = access.getPreprocessing();
 
         if (river == null) {
             // TODO: i18n
@@ -99,6 +104,11 @@
             addProblem("fix.missing.events");
         }
 
+        if (referencePeriod == null) {
+            // TODO: i18n
+            addProblem("fix.missing.reference.period");
+        }
+
         if (analysisPeriods == null || analysisPeriods.length < 1) {
             // TODO: i18n
             addProblem("fix.missing.analysis.periods");
@@ -126,6 +136,7 @@
             this.step            = step;
             this.function        = function;
             this.events          = events;
+            this.referencePeriod = referencePeriod;
             this.analysisPeriods = analysisPeriods;
             this.qSectorStart    = qSectorStart;
             this.qSectorEnd      = qSectorEnd;
@@ -222,7 +233,6 @@
             // Fill Qs and Ws from event columns.
             for (int j = 0; j < ws.length; ++j) {
                 interpolated[j] = eventColumns.get(j).getQW(km, qs, ws, j);
-                // TODO: mark as interpolated.
             }
 
             fitting.reset();
@@ -281,31 +291,31 @@
 
         FixingsColumnFactory fcf = FixingsColumnFactory.getInstance();
 
-        List<Column> columns = new ArrayList<Column>(events.length);
-
-        for (int eventId: events) {
-            IdFilter idFilter = new IdFilter(eventId);
-
-            List<Fixing.Column> metas = overview.filter(null, idFilter);
+        IdsFilter ids = new IdsFilter(events);
+        DateRangeFilter rdf = new DateRangeFilter(
+            referencePeriod.getFrom(),
+            referencePeriod.getTo());
+        Filter filter = new AndFilter().add(rdf).add(ids);
 
-            if (metas.isEmpty()) {
-                // TODO: i18n
-                addProblem("fix.missing.column", eventId);
-                continue;
-            }
+        List<Fixing.Column> metas = overview.filter(null, filter);
 
-            FixingsColumn data = fcf.getColumnData(metas.get(0));
+        List<Column> columns = new ArrayList<Column>(metas.size());
+
+        for (Fixing.Column meta: metas) {
+
+            FixingsColumn data = fcf.getColumnData(meta);
             if (data == null) {
                 // TODO: i18n
-                addProblem("fix.cannot.load.data", eventId);
-                continue;
+                addProblem("fix.cannot.load.data");
             }
-
-            columns.add(new Column(metas.get(0), data));
+            else {
+                columns.add(new Column(metas.get(0), data));
+            }
         }
 
         return columns;
     }
+    
 
     protected KMIndex<AnalysisPeriod []> calculateAnalysisPeriods(
         Function        function,
@@ -331,6 +341,8 @@
         KMIndex<AnalysisPeriod []> results =
             new KMIndex<AnalysisPeriod []>(parameters.size());
 
+        IdsFilter idsFilter = new IdsFilter(events);
+
         for (int row = 0, R = parameters.size(); row < R; ++row) {
             double km = parameters.get(row, kmIndex);
             parameters.get(row, parameterIndices, parameterValues);
@@ -355,10 +367,11 @@
                 // for all Q sectors.
                 for (int qSector = qSectorStart; qSector < qSectorEnd; ++qSector) {
 
-                    AndFilter filter = new AndFilter();
-                    filter.add(kmFilter);
-                    filter.add(new SectorFilter(qSector));
-                    filter.add(drf);
+                    Filter filter = new AndFilter()
+                        .add(kmFilter)
+                        .add(new SectorFilter(qSector))
+                        .add(drf)
+                        .add(idsFilter);
 
                     List<Fixing.Column> metas = overview.filter(range, filter);
 

http://dive4elements.wald.intevation.org