view artifacts/src/main/java/org/dive4elements/river/artifacts/states/sq/StaticSQRelationFacet.java @ 9425:3f49835a00c3

Extended CrossSectionFacet so it may fetch different data from within the artifact result. Also allows to have acces to the potentially already computed artifact result via its normal computation cache.
author gernotbelger
date Fri, 17 Aug 2018 15:31:02 +0200
parents e4606eae8ea5
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