view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java @ 4768:99f9e371371b

Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 04 Jan 2013 15:35:51 +0100
parents 5063c93dfb8e
children
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.FacetActivity;

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

import de.intevation.artifacts.common.utils.StringUtils;

import de.intevation.flys.artifacts.FLYSArtifact;

import de.intevation.flys.artifacts.access.SQRelationAccess;

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.sq.SQCurveFacet;
import de.intevation.flys.artifacts.model.sq.SQFractionResult;
import de.intevation.flys.artifacts.model.sq.SQMeasurementFacet;
import de.intevation.flys.artifacts.model.sq.SQOutlierCurveFacet;
import de.intevation.flys.artifacts.model.sq.SQOutlierFacet;
import de.intevation.flys.artifacts.model.sq.SQOutlierMeasurementFacet;
import de.intevation.flys.artifacts.model.sq.SQOverviewFacet;
import de.intevation.flys.artifacts.model.sq.SQRelationCalculation;
import de.intevation.flys.artifacts.model.sq.SQResult;

import de.intevation.flys.artifacts.resources.Resources;

import java.util.List;

import org.apache.log4j.Logger;

/**
 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
 */
public class SQRelation extends DefaultState implements FacetTypes {

    private static Logger log = Logger.getLogger(SQRelation.class);


    public static final String I18N_FACET_CURVE =
        "facet.sq_relation.curve";

    public static final String I18N_FACET_MEASUREMENTS =
        "facet.sq_relation.measurements";

    public static final String I18N_FACET_OUTLIERS =
        "facet.sq_relation.outliers";

    public static final String I18N_FACET_OUTLIER_CURVE =
        "facet.sq_relation.outlier.curve";

    public static final String I18N_FACET_OUTLIER_MEASUREMENT =
        "facet.sq_relation.outlier.measurement";

    public static final int CURVE_INDEX               = 0;
    public static final int MEASURREMENT_INDEX        = 1;
    public static final int OUTLIER_INDEX             = 2;
    public static final int OUTLIER_CURVE_INDEX       = 3;
    public static final int OUTLIER_MEASUREMENT_INDEX = 4;

    public static final String [][] FACET_NAMES = {
        { SQ_A_CURVE, SQ_B_CURVE, SQ_C_CURVE,
          SQ_D_CURVE, SQ_E_CURVE, SQ_F_CURVE
        },
        { SQ_A_MEASUREMENT, SQ_B_MEASUREMENT, SQ_C_MEASUREMENT,
          SQ_D_MEASUREMENT, SQ_E_MEASUREMENT, SQ_F_MEASUREMENT
        },
        { SQ_A_OUTLIER, SQ_B_OUTLIER, SQ_C_OUTLIER,
          SQ_D_OUTLIER, SQ_E_OUTLIER, SQ_F_OUTLIER
        },
        { SQ_A_OUTLIER_CURVE, SQ_B_OUTLIER_CURVE, SQ_C_OUTLIER_CURVE,
          SQ_D_OUTLIER_CURVE, SQ_E_OUTLIER_CURVE, SQ_F_OUTLIER_CURVE
        },
        { SQ_A_OUTLIER_MEASUREMENT, SQ_B_OUTLIER_MEASUREMENT,
          SQ_C_OUTLIER_MEASUREMENT, SQ_D_OUTLIER_MEASUREMENT,
          SQ_E_OUTLIER_MEASUREMENT, SQ_F_OUTLIER_MEASUREMENT
        }
    };


    static {
        // Active/deactivate facets.
        FacetActivity.Registry.getInstance().register(
            "minfo",
            new FacetActivity() {
                @Override
                public Boolean isInitialActive(
                    Artifact artifact,
                    Facet    facet,
                    String   output
                ) {
                    String name = facet.getName();

                    if (StringUtils.contains(
                        name, FACET_NAMES[CURVE_INDEX])
                    ||  StringUtils.contains(
                        name, FACET_NAMES[OUTLIER_INDEX])
                    ||  StringUtils.contains(
                        name, FACET_NAMES[MEASURREMENT_INDEX])
                    ) {
                        // TODO: Only the last should be active.
                        return Boolean.TRUE;
                    }

                    if (StringUtils.contains(
                        name, FACET_NAMES[OUTLIER_CURVE_INDEX])
                     || StringUtils.contains(
                        name, FACET_NAMES[OUTLIER_MEASUREMENT_INDEX])
                    ) {
                        return Boolean.FALSE;
                    }

                    return null;
                }
            });
    }


    public SQRelation() {
    }


    @Override
    public Object computeAdvance(
        FLYSArtifact artifact,
        String       hash,
        CallContext  context,
        List<Facet>  facets,
        Object       old
    ) {
        log.debug("SQRelation.computeAdvance");

        CalculationResult res = old instanceof CalculationResult
            ? (CalculationResult)old
            : new SQRelationCalculation(
                new SQRelationAccess(artifact)).calculate();

        if (facets == null) {
            return res;
        }

        SQResult [] sqr = (SQResult [])res.getData();
        if (sqr == null) {
            return res;
        }

        createFacets(context, facets, sqr, hash);

        Facet csv = new DataFacet(
            CSV, "CSV data", ComputeType.ADVANCE, hash, id);

        Facet pdf = new DataFacet(
            PDF, "PDF data", ComputeType.ADVANCE, hash, id);

        facets.add(csv);
        facets.add(pdf);

        return res;
    }


    protected void createFacets(
        CallContext context,
        List<Facet> container,
        SQResult[]  sqr,
        String      hash
    ) {
        boolean debug = log.isDebugEnabled();

        CallMeta meta    = context.getMeta();
        String   stateId = getID();
        for (int i = 0; i < 6; i++) {
            container.add(new SQOverviewFacet(
                i,
                i,
                "sq_chart_overview",
                Resources.getMsg(
                    meta,
                    I18N_FACET_CURVE,
                    I18N_FACET_CURVE
                ),
                hash,
                getID()
            ));
        }
        for (int res = 0, n = sqr.length; res < n; res++) {

            for (int i = 0; i < SQResult.NUMBER_FRACTIONS; i++) {
                SQFractionResult result = sqr[res].getFraction(i);

                if (result == null) {
                    log.warn("Fraction at index " + i + " is empty!");
                    continue;
                }

                container.add(new SQCurveFacet(
                    res,
                    i,
                    getFractionFacetname(CURVE_INDEX, i),
                    Resources.getMsg(
                        meta,
                        I18N_FACET_CURVE,
                        I18N_FACET_CURVE
                    ),
                    hash,
                    stateId
                ));

                for (int j = 0, C = result.numIterations()-1; j < C; j++) {

                    Object [] round = new Object [] { j + 1 };

                    int index = res;
                    index     = index << 16;
                    index     = index + j;

                    if (debug) {
                        log.debug("new outliers facet (index=" +index+ ")");
                        log.debug("   result index = " + res);
                        log.debug("   fraction idx = " + i);
                        log.debug("   iteration    = " + j);
                    }

                    container.add(new SQOutlierFacet(
                        index,
                        i,
                        getFractionFacetname(OUTLIER_INDEX, i),
                        Resources.getMsg(
                            meta,
                            I18N_FACET_OUTLIERS,
                            I18N_FACET_OUTLIERS,
                            round
                        ),
                        hash,
                        stateId
                    ));

                    container.add(new SQOutlierCurveFacet(
                        index,
                        i,
                        getFractionFacetname(OUTLIER_CURVE_INDEX, i),
                        Resources.getMsg(
                            meta,
                            I18N_FACET_OUTLIER_CURVE,
                            I18N_FACET_OUTLIER_CURVE,
                            round
                        ),
                        hash,
                        stateId
                    ));

                    container.add(new SQOutlierMeasurementFacet(
                        index,
                        i,
                        getFractionFacetname(OUTLIER_MEASUREMENT_INDEX, i),
                        Resources.getMsg(
                            meta,
                            I18N_FACET_OUTLIER_MEASUREMENT,
                            I18N_FACET_OUTLIER_MEASUREMENT,
                            round
                        ),
                        hash,
                        stateId
                    ));
                } // for all outliers

                container.add(new SQMeasurementFacet(
                    res,
                    i,
                    getFractionFacetname(MEASURREMENT_INDEX, i),
                    Resources.getMsg(
                        meta,
                        I18N_FACET_MEASUREMENTS,
                        I18N_FACET_MEASUREMENTS
                    ),
                    hash,
                    stateId
                ));
            } // for all fractions
        } // for all results
    }

    protected static String getFractionFacetname(int type, int idx) {
        if (log.isDebugEnabled()) {
            log.debug("getFractionFacetname(): " + type + " | " + idx);
        }
        type %= FACET_NAMES.length;
        return FACET_NAMES[type][idx % FACET_NAMES[type].length];
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org