view artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation5.java @ 7471:fff862f4ef76

Experimental caching of datacage recommendations. The respective hook is called a lot and running the datacage over and over again when loading data can be expensive. So the generated recommendations are cached for some time. Hopefully this improves the overall speed of loading data from the datacage.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 30 Oct 2013 15:26:21 +0100
parents af13ceeba52a
children 0a5239a1e46e
line wrap: on
line source
/* 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 org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.CallMeta;

import org.dive4elements.river.utils.Formatter;

import java.text.NumberFormat;

import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

public class Calculation5
extends      Calculation
{
    private static Logger log = Logger.getLogger(Calculation5.class);

    protected double    startKm;
    protected double [] endKms;

    public Calculation5() {
    }

    public Calculation5(double startKm, double [] endKms) {
        this.startKm = startKm;
        this.endKms  = endKms;
    }

    public CalculationResult calculate(
        WstValueTable       wst,
        Map<Double, Double> kms2gaugeDatums,
        CallContext         context
    ) {
        ArrayList<WWQQ> results = new ArrayList<WWQQ>();

        int numProblems = numProblems();

        CallMeta meta = context.getMeta();

        for (double endKm: endKms) {

            double [][] wws = wst.relateWs(startKm, endKm, this);
            int newNumProblems = numProblems();

            if (wws.length == 4) {
                WWQQ wwqq = new WWQQ(
                    generateName(meta, startKm, endKm),
                    startKm, kms2gaugeDatums.get(startKm), wws[0], wws[1],
                    endKm,   kms2gaugeDatums.get(endKm),   wws[2], wws[3]);

                if (newNumProblems > numProblems) {
                    wwqq.removeNaNs();
                }

                results.add(wwqq);
            }
            numProblems = newNumProblems;
        }

        return new CalculationResult(
            results.toArray(new WWQQ[results.size()]),
            this);
    }

    protected static String generateName(
        CallMeta meta,
        double   startKm,
        double   endKm
    ) {
        NumberFormat nf = Formatter.getCalculationKm(meta);
        return "W(km " + nf.format(startKm) +
               ") ~ W(km " + nf.format(endKm) + ")";
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org