raimund@3058: package de.intevation.flys.artifacts.states;
raimund@3058:
ingo@3072: import java.util.ArrayList;
ingo@3072: import java.util.List;
ingo@3072:
raimund@3058: import org.apache.log4j.Logger;
raimund@3058:
ingo@3072: import de.intevation.artifacts.CallContext;
ingo@3116: import de.intevation.artifacts.CallMeta;
ingo@3072:
ingo@3072: import de.intevation.artifactdatabase.state.Facet;
ingo@3072:
ingo@3072: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@3072: import de.intevation.flys.artifacts.MINFOArtifact;
ingo@3072: import de.intevation.flys.artifacts.model.DataFacet;
ingo@3072: import de.intevation.flys.artifacts.model.FacetTypes;
ingo@3072: import de.intevation.flys.artifacts.model.CalculationResult;
ingo@3072: import de.intevation.flys.artifacts.model.sq.SQCurveFacet;
ingo@3101: import de.intevation.flys.artifacts.model.sq.SQFractionResult;
ingo@3072: import de.intevation.flys.artifacts.model.sq.SQMeasurementFacet;
ingo@3072: import de.intevation.flys.artifacts.model.sq.SQOutlierFacet;
ingo@3072: import de.intevation.flys.artifacts.model.sq.SQRelationCalculation;
ingo@3079: import de.intevation.flys.artifacts.model.sq.SQResult;
ingo@3116: import de.intevation.flys.artifacts.resources.Resources;
raimund@3058: import de.intevation.flys.artifacts.states.DefaultState;
raimund@3058:
raimund@3058: /**
raimund@3058: * @author Raimund Renkert
raimund@3058: */
ingo@3072: public class SQRelation extends DefaultState implements FacetTypes {
raimund@3058:
raimund@3058: private static Logger logger = Logger.getLogger(SQRelation.class);
raimund@3058:
raimund@3058:
ingo@3116: public static final String I18N_FACET_CURVE =
ingo@3116: "facet.sq_relation.curve";
ingo@3116:
ingo@3116: public static final String I18N_FACET_MEASUREMENTS =
ingo@3116: "facet.sq_relation.measurements";
ingo@3116:
ingo@3116: public static final String I18N_FACET_OUTLIERS =
ingo@3116: "facet.sq_relation.outliers";
ingo@3116:
ingo@3116:
raimund@3058: public SQRelation() {
raimund@3058: }
raimund@3058:
ingo@3072:
raimund@3058: @Override
ingo@3072: public Object computeAdvance(
ingo@3072: FLYSArtifact artifact,
ingo@3072: String hash,
ingo@3072: CallContext context,
ingo@3072: List facets,
ingo@3072: Object old
ingo@3072: ) {
ingo@3072: logger.debug("SQRelation.computeAdvance");
ingo@3072:
ingo@3072: List newFacets = new ArrayList();
ingo@3072:
ingo@3072: CalculationResult res = old instanceof CalculationResult
ingo@3072: ? (CalculationResult) old
ingo@3072: : new SQRelationCalculation().calculate((MINFOArtifact) artifact);
ingo@3072:
ingo@3072: if (facets == null || res == null) {
ingo@3072: return res;
ingo@3072: }
ingo@3072:
ingo@3079: SQResult[] sqr = (SQResult[]) res.getData();
ingo@3101: logger.info("Calculation computed " + sqr.length + " SQResult objects");
ingo@3101:
ingo@3116: createFacets(context, newFacets, sqr, hash);
ingo@3072:
ingo@3072: Facet csv = new DataFacet(
ingo@3072: CSV, "CSV data", ComputeType.ADVANCE, hash, id);
ingo@3072:
ingo@3072: Facet pdf = new DataFacet(
ingo@3072: PDF, "PDF data", ComputeType.ADVANCE, hash, id);
ingo@3072:
ingo@3072: newFacets.add(csv);
ingo@3072: newFacets.add(pdf);
ingo@3072:
ingo@3072: logger.debug("Created " + newFacets.size() + " new Facets.");
ingo@3072:
ingo@3072: facets.addAll(newFacets);
ingo@3072:
ingo@3072: return res;
raimund@3058: }
raimund@3058:
ingo@3072:
ingo@3079: protected void createFacets(
ingo@3116: CallContext context,
ingo@3079: List container,
ingo@3079: SQResult[] sqr,
ingo@3079: String hash
ingo@3079: ) {
ingo@3116: CallMeta meta = context.getMeta();
ingo@3116: String stateId = getID();
ingo@3099:
ingo@3101: for (int res = 0, n = sqr.length; res < n; res++) {
ingo@3099:
ingo@3101: for (int i = 0; i < 6; i++) {
ingo@3101: SQFractionResult result = sqr[res].getFraction(i);
ingo@3072:
ingo@3101: if (result == null) {
ingo@3101: logger.warn("Fraction at index " + i + " is empty!");
ingo@3101: continue;
ingo@3101: }
ingo@3101:
ingo@3101: container.add(new SQMeasurementFacet(
ingo@3101: res,
ingo@3101: i,
ingo@3101: getFractionFacetname(1, i),
ingo@3116: Resources.getMsg(
ingo@3116: meta,
ingo@3116: I18N_FACET_MEASUREMENTS,
ingo@3116: I18N_FACET_MEASUREMENTS
ingo@3116: ),
ingo@3101: hash,
ingo@3101: stateId
ingo@3101: ));
ingo@3101:
ingo@3101: container.add(new SQCurveFacet(
ingo@3101: res,
ingo@3101: i,
ingo@3101: getFractionFacetname(0, i),
ingo@3116: Resources.getMsg(
ingo@3116: meta,
ingo@3116: I18N_FACET_CURVE,
ingo@3116: I18N_FACET_CURVE
ingo@3116: ),
ingo@3101: hash,
ingo@3101: stateId
ingo@3101: ));
ingo@3101:
ingo@3101: for (int j = 0, C = result.getOutliersCount(); j < C; j++) {
ingo@3119: int index = res;
ingo@3119: index = index << 16;
ingo@3119: index = index + j;
ingo@3119:
ingo@3119: if (logger.isDebugEnabled()) {
ingo@3119: logger.debug("new outliers facet (index=" +index+ ")");
ingo@3119: logger.debug(" result index = " + res);
ingo@3119: logger.debug(" fraction idx = " + i);
ingo@3119: logger.debug(" iteration = " + j);
ingo@3119: }
ingo@3119:
ingo@3101: container.add(new SQOutlierFacet(
ingo@3119: index,
ingo@3101: i,
ingo@3101: getFractionFacetname(2, i),
ingo@3116: Resources.getMsg(
ingo@3116: meta,
ingo@3116: I18N_FACET_OUTLIERS,
ingo@3116: I18N_FACET_OUTLIERS,
ingo@3116: new Object[] { j }
ingo@3116: ),
ingo@3101: hash,
ingo@3101: stateId
ingo@3101: ));
ingo@3101: }
ingo@3101: }
ingo@3079: }
ingo@3072: }
ingo@3101:
ingo@3101:
ingo@3101: protected String getFractionFacetname(int type, int fractionIdx) {
ingo@3101: logger.debug("getFractionFacetname(): " + type + " | " + fractionIdx);
ingo@3101:
ingo@3101: switch (type) {
ingo@3101: case 0:
ingo@3101: switch (fractionIdx) {
ingo@3101: case 0: return SQ_A_CURVE;
ingo@3101: case 1: return SQ_B_CURVE;
ingo@3101: case 2: return SQ_C_CURVE;
ingo@3101: case 3: return SQ_D_CURVE;
ingo@3101: case 4: return SQ_E_CURVE;
ingo@3101: case 5: return SQ_F_CURVE;
ingo@3101: }
ingo@3101: case 1:
ingo@3101: switch (fractionIdx) {
ingo@3101: case 0: return SQ_A_MEASUREMENT;
ingo@3101: case 1: return SQ_B_MEASUREMENT;
ingo@3101: case 2: return SQ_C_MEASUREMENT;
ingo@3101: case 3: return SQ_D_MEASUREMENT;
ingo@3101: case 4: return SQ_E_MEASUREMENT;
ingo@3101: case 5: return SQ_F_MEASUREMENT;
ingo@3101: }
ingo@3101: case 2:
ingo@3101: switch (fractionIdx) {
ingo@3101: case 0: return SQ_A_OUTLIER;
ingo@3101: case 1: return SQ_B_OUTLIER;
ingo@3101: case 2: return SQ_C_OUTLIER;
ingo@3101: case 3: return SQ_D_OUTLIER;
ingo@3101: case 4: return SQ_E_OUTLIER;
ingo@3101: case 5: return SQ_F_OUTLIER;
ingo@3101: }
ingo@3101: }
ingo@3101:
ingo@3101: return null;
ingo@3101: }
raimund@3058: }
raimund@3058: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :