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@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;
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:
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@3079: createFacets(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@3079: List container,
ingo@3079: SQResult[] sqr,
ingo@3079: String hash
ingo@3079: ) {
ingo@3099: 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@3101: "Measurement (Fraction " + i + ")",
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@3101: "CURVE AT " + i,
ingo@3101: hash,
ingo@3101: stateId
ingo@3101: ));
ingo@3101:
ingo@3101: for (int j = 0, C = result.getOutliersCount(); j < C; j++) {
ingo@3101: container.add(new SQOutlierFacet(
ingo@3101: res,
ingo@3101: i,
ingo@3101: j,
ingo@3101: getFractionFacetname(2, i),
ingo@3101: "OUTLIER AT " + i,
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 :