view artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/StaticSQRelationFacet.java @ 8202:e4606eae8ea5

sed src/**/*.java 's/logger/log/g'
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 05 Sep 2014 12:58:17 +0200
parents 5b2126d21c2e
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.states.sq;

import java.util.List;

import org.apache.log4j.Logger;

import org.dive4elements.artifactdatabase.state.DefaultFacet;
import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.DataProvider;
import org.dive4elements.river.artifacts.math.fitting.Function;
import org.dive4elements.river.artifacts.math.fitting.FunctionFactory;
import org.dive4elements.river.artifacts.model.sq.SQFunction;
import org.dive4elements.river.artifacts.model.sq.StaticSQRelation;


public class StaticSQRelationFacet
extends DefaultFacet
implements Facet
{
    public static final String FUNCTION = "sq-pow";

    private StaticSQRelation relation;

    private static final Logger log =
        Logger.getLogger(StaticSQRelationFacet.class);

    public StaticSQRelationFacet(
        int ndx,
        String name,
        String description,
        StaticSQRelation relation) {
        super(ndx, name, description);
        this.relation = relation;
    }

    @Override
    public Object getData(Artifact artifact, CallContext context) {
        double qmax = relation.getQmax();
        double[] coeffs = new double[] {relation.getA(), relation.getB()};
        Function func = FunctionFactory.getInstance().getFunction(FUNCTION);
        org.dive4elements.river.artifacts.math.Function function =
            func.instantiate(coeffs);

        /* Figure out a good starting point by checking for calculated
         * SQ Curves and using their starting point */

        // this is ok because we are a DefaultFacet and not a DataFacet
        // and so we are not registred with Mr. Blackboard
        List<DataProvider> providers = context.getDataProvider(name);

        double startingPoint = Double.MAX_VALUE;

        for (DataProvider dp: providers) {
            SQFunction other = (SQFunction) dp.provideData(
                name,
                null,
                context);
            if (other == null) {
                // name is not really unique here but it's our only key
                // should not happen anyhow.
                log.error("Did not get data from: " + name);
                continue;
            }
            startingPoint = Math.min(other.getMinQ(), startingPoint);
        }
        if (startingPoint == Double.MAX_VALUE) {
            startingPoint = 0;
        }

        SQFunction sqf = new SQFunction(function, startingPoint, qmax);
        return sqf;
    }

    @Override
    public Facet deepCopy() {
        StaticSQRelationFacet copy =
            new StaticSQRelationFacet(index, name, description, relation);
        copy.set(this);
        return copy;
    }
}

http://dive4elements.wald.intevation.org