view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQCurveFacet.java @ 4255:670e98f5a441

Fixed leak while merging facets. The ThemeList that is used by OutputHelper to sort the Facets for an Output now uses a list to store the ManagedFacets. The correct order is made up by sorting the List using Collections.sort() function of the Java JDK. Therfore, the ManagedFacet class implements the Comparable interface. The return value of its compareTo(other) method depends on the value of the 'position' field.
author Ingo Weinzierl <weinzierl.ingo@googlemail.com>
date Thu, 25 Oct 2012 14:01:46 +0200
parents 1df6984628c3
children
line wrap: on
line source
package de.intevation.flys.artifacts.model.sq;

import org.apache.log4j.Logger;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.math.fitting.Function;
import de.intevation.flys.artifacts.math.fitting.FunctionFactory;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.Parameters;

import de.intevation.flys.artifacts.states.DefaultState.ComputeType;


/**
 * Facet to show the curve in a sq relation.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class SQCurveFacet extends DataFacet implements FacetTypes {

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


    public static final String FUNCTION = "sq-pow";


    private int fractionIdx;


    public SQCurveFacet() {
    }


    public SQCurveFacet(
        int    idx,
        int    fractionIdx,
        String name,
        String description,
        String hash,
        String stateId
    ) {
        super(idx, name, description, ComputeType.ADVANCE, hash, stateId);
        this.fractionIdx = fractionIdx;
    }


    @Override
    public Object getData(Artifact artifact, CallContext context) {
        log.debug("SQCurveFacet.getData");

        if (!(artifact instanceof FLYSArtifact)) {
            return null;
        }

        FLYSArtifact flys = (FLYSArtifact) artifact;

        CalculationResult res = (CalculationResult) flys.compute(
            context, ComputeType.ADVANCE, false);

        SQResult[]       results = (SQResult[]) res.getData();
        SQFractionResult result  = results[index].getFraction(fractionIdx);

        Parameters params = result.getParameters();

        if (params == null) {
            log.debug("no parameters found");
            return null;
        }

        Function func = FunctionFactory.getInstance().getFunction(FUNCTION);
        String[] paramNames = func.getParameterNames();

        double [] coeffs = params.get(0, paramNames);

        if (log.isDebugEnabled()) {
            for (int i = 0, N = paramNames.length; i < N; i++) {
                log.debug("retrieved parameter " + paramNames[i] +
                          " = " + coeffs[i]);
            }
        }

        de.intevation.flys.artifacts.math.Function mf =
            func.instantiate(coeffs);

        double [] extent = result.getQExtent();
        return new SQFunction(mf, extent[0], extent[1]);
    }


    @Override
    public SQCurveFacet deepCopy() {
        SQCurveFacet copy = new SQCurveFacet();
        copy.set(this);

        return copy;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org