view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java @ 3109:63ef889eea2b

SQ: Directly sample our Function objects instead of Function2D from JFreeChart. flys-artifacts/trunk@4708 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 19 Jun 2012 15:39:02 +0000
parents b31397addf2f
children 2feed9b50047
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import de.intevation.artifacts.CallContext;

import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.MINFOArtifact;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.CalculationResult;
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.SQOutlierFacet;
import de.intevation.flys.artifacts.model.sq.SQRelationCalculation;
import de.intevation.flys.artifacts.model.sq.SQResult;
import de.intevation.flys.artifacts.states.DefaultState;

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

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


    public SQRelation() {
    }


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

        List<Facet> newFacets = new ArrayList<Facet>();

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

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

        SQResult[] sqr = (SQResult[]) res.getData();
        logger.info("Calculation computed " + sqr.length + " SQResult objects");

        createFacets(newFacets, sqr, hash);

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

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

        newFacets.add(csv);
        newFacets.add(pdf);

        logger.debug("Created " + newFacets.size() + " new Facets.");

        facets.addAll(newFacets);

        return res;
    }


    protected void createFacets(
        List<Facet> container,
        SQResult[]  sqr,
        String      hash
    ) {
        String stateId = getID();

        for (int res = 0, n = sqr.length; res < n; res++) {

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

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

                container.add(new SQMeasurementFacet(
                    res,
                    i,
                    getFractionFacetname(1, i),
                    "Measurement (Fraction " + i + ")",
                    hash,
                    stateId
                ));

                container.add(new SQCurveFacet(
                    res,
                    i,
                    getFractionFacetname(0, i),
                    "CURVE AT " + i,
                    hash,
                    stateId
                ));

                for (int j = 0, C = result.getOutliersCount(); j < C; j++) {
                    container.add(new SQOutlierFacet(
                        res,
                        i,
                        j,
                        getFractionFacetname(2, i),
                        "OUTLIER AT " + i,
                        hash,
                        stateId
                    ));
                }
            }
        }
    }


    protected String getFractionFacetname(int type, int fractionIdx) {
        logger.debug("getFractionFacetname(): " + type + " | " + fractionIdx);

        switch (type) {
            case 0:
                switch (fractionIdx) {
                    case 0: return SQ_A_CURVE;
                    case 1: return SQ_B_CURVE;
                    case 2: return SQ_C_CURVE;
                    case 3: return SQ_D_CURVE;
                    case 4: return SQ_E_CURVE;
                    case 5: return SQ_F_CURVE;
                }
            case 1:
                switch (fractionIdx) {
                    case 0: return SQ_A_MEASUREMENT;
                    case 1: return SQ_B_MEASUREMENT;
                    case 2: return SQ_C_MEASUREMENT;
                    case 3: return SQ_D_MEASUREMENT;
                    case 4: return SQ_E_MEASUREMENT;
                    case 5: return SQ_F_MEASUREMENT;
                }
            case 2:
                switch (fractionIdx) {
                    case 0: return SQ_A_OUTLIER;
                    case 1: return SQ_B_OUTLIER;
                    case 2: return SQ_C_OUTLIER;
                    case 3: return SQ_D_OUTLIER;
                    case 4: return SQ_E_OUTLIER;
                    case 5: return SQ_F_OUTLIER;
                }
        }

        return null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org