raimund@3058: package de.intevation.flys.artifacts.states;
raimund@3058:
sascha@3222: import de.intevation.artifactdatabase.state.Facet;
sascha@3559: import de.intevation.artifactdatabase.state.FacetActivity;
raimund@3058:
sascha@3559: import de.intevation.artifacts.Artifact;
ingo@3072: import de.intevation.artifacts.CallContext;
ingo@3116: import de.intevation.artifacts.CallMeta;
ingo@3072:
sascha@3559: import de.intevation.artifacts.common.utils.StringUtils;
sascha@3559:
sascha@3222: import de.intevation.flys.artifacts.FLYSArtifact;
ingo@3072:
sascha@3222: import de.intevation.flys.artifacts.access.SQRelationAccess;
sascha@3222:
sascha@3222: import de.intevation.flys.artifacts.model.CalculationResult;
ingo@3072: import de.intevation.flys.artifacts.model.DataFacet;
ingo@3072: import de.intevation.flys.artifacts.model.FacetTypes;
sascha@3222:
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;
sascha@3552: import de.intevation.flys.artifacts.model.sq.SQOutlierCurveFacet;
ingo@3072: import de.intevation.flys.artifacts.model.sq.SQOutlierFacet;
sascha@3552: import de.intevation.flys.artifacts.model.sq.SQOutlierMeasurementFacet;
sascha@3552: import de.intevation.flys.artifacts.model.sq.SQOverviewFacet;
ingo@3072: import de.intevation.flys.artifacts.model.sq.SQRelationCalculation;
ingo@3079: import de.intevation.flys.artifacts.model.sq.SQResult;
sascha@3222:
ingo@3116: import de.intevation.flys.artifacts.resources.Resources;
sascha@3222:
sascha@3222: import java.util.List;
sascha@3222:
sascha@3222: import org.apache.log4j.Logger;
sascha@3222:
raimund@3058: /**
raimund@3058: * @author Raimund Renkert
raimund@3058: */
ingo@3072: public class SQRelation extends DefaultState implements FacetTypes {
raimund@3058:
sascha@3222: private static Logger log = 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:
sascha@3552: public static final String I18N_FACET_OUTLIER_CURVE =
sascha@3552: "facet.sq_relation.outlier.curve";
sascha@3552:
sascha@3552: public static final String I18N_FACET_OUTLIER_MEASUREMENT =
sascha@3552: "facet.sq_relation.outlier.measurement";
ingo@3116:
sascha@3559: public static final int CURVE_INDEX = 0;
sascha@3559: public static final int MEASURREMENT_INDEX = 1;
sascha@3559: public static final int OUTLIER_INDEX = 2;
sascha@3559: public static final int OUTLIER_CURVE_INDEX = 3;
sascha@3559: public static final int OUTLIER_MEASUREMENT_INDEX = 4;
sascha@3559:
sascha@3559: public static final String [][] FACET_NAMES = {
sascha@3559: { SQ_A_CURVE, SQ_B_CURVE, SQ_C_CURVE,
sascha@3559: SQ_D_CURVE, SQ_E_CURVE, SQ_F_CURVE
sascha@3559: },
sascha@3559: { SQ_A_MEASUREMENT, SQ_B_MEASUREMENT, SQ_C_MEASUREMENT,
sascha@3559: SQ_D_MEASUREMENT, SQ_E_MEASUREMENT, SQ_F_MEASUREMENT
sascha@3559: },
sascha@3559: { SQ_A_OUTLIER, SQ_B_OUTLIER, SQ_C_OUTLIER,
sascha@3559: SQ_D_OUTLIER, SQ_E_OUTLIER, SQ_F_OUTLIER
sascha@3559: },
sascha@3559: { SQ_A_OUTLIER_CURVE, SQ_B_OUTLIER_CURVE, SQ_C_OUTLIER_CURVE,
sascha@3559: SQ_D_OUTLIER_CURVE, SQ_E_OUTLIER_CURVE, SQ_F_OUTLIER_CURVE
sascha@3559: },
sascha@3559: { SQ_A_OUTLIER_MEASUREMENT, SQ_B_OUTLIER_MEASUREMENT,
sascha@3559: SQ_C_OUTLIER_MEASUREMENT, SQ_D_OUTLIER_MEASUREMENT,
sascha@3559: SQ_E_OUTLIER_MEASUREMENT, SQ_F_OUTLIER_MEASUREMENT
sascha@3559: }
sascha@3559: };
sascha@3559:
sascha@3559:
sascha@3559: static {
sascha@3559: // Active/deactivate facets.
sascha@3559: FacetActivity.Registry.getInstance().register(
sascha@3559: "minfo",
sascha@3559: new FacetActivity() {
sascha@3559: @Override
sascha@3559: public Boolean isInitialActive(
sascha@3559: Artifact artifact,
sascha@3559: Facet facet,
sascha@3559: String output
sascha@3559: ) {
sascha@3559: String name = facet.getName();
sascha@3559:
sascha@3563: if (StringUtils.contains(
sascha@3563: name, FACET_NAMES[CURVE_INDEX])
sascha@3559: || StringUtils.contains(
sascha@3563: name, FACET_NAMES[OUTLIER_INDEX])
sascha@3563: || StringUtils.contains(
sascha@3563: name, FACET_NAMES[MEASURREMENT_INDEX])
sascha@3559: ) {
sascha@3559: // TODO: Only the last should be active.
sascha@3559: return Boolean.TRUE;
sascha@3559: }
sascha@3559:
sascha@3559: if (StringUtils.contains(
sascha@3563: name, FACET_NAMES[OUTLIER_CURVE_INDEX])
sascha@3563: || StringUtils.contains(
sascha@3563: name, FACET_NAMES[OUTLIER_MEASUREMENT_INDEX])
sascha@3559: ) {
sascha@3559: return Boolean.FALSE;
sascha@3559: }
sascha@3559:
sascha@3559: return null;
sascha@3559: }
sascha@3559: });
sascha@3559: }
sascha@3559:
sascha@3563:
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: ) {
sascha@3222: log.debug("SQRelation.computeAdvance");
ingo@3072:
ingo@3072: CalculationResult res = old instanceof CalculationResult
sascha@3222: ? (CalculationResult)old
sascha@3222: : new SQRelationCalculation(
sascha@3222: new SQRelationAccess(artifact)).calculate();
ingo@3072:
sascha@3222: if (facets == null) {
ingo@3072: return res;
ingo@3072: }
ingo@3072:
sascha@3222: SQResult [] sqr = (SQResult [])res.getData();
sascha@3222: if (sqr == null) {
sascha@3222: return res;
sascha@3222: }
ingo@3101:
sascha@3222: createFacets(context, facets, 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:
sascha@3222: facets.add(csv);
sascha@3222: facets.add(pdf);
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: ) {
sascha@3552: boolean debug = log.isDebugEnabled();
sascha@3552:
ingo@3116: CallMeta meta = context.getMeta();
ingo@3116: String stateId = getID();
raimund@3296: for (int i = 0; i < 6; i++) {
raimund@3296: container.add(new SQOverviewFacet(
sascha@3559: i,
sascha@3559: i,
sascha@3559: "sq_chart_overview",
sascha@3559: Resources.getMsg(
sascha@3559: meta,
sascha@3559: I18N_FACET_CURVE,
sascha@3559: I18N_FACET_CURVE
sascha@3559: ),
sascha@3559: hash,
sascha@3559: getID()
sascha@3559: ));
raimund@3296: }
ingo@3101: for (int res = 0, n = sqr.length; res < n; res++) {
ingo@3099:
sascha@3552: for (int i = 0; i < SQResult.NUMBER_FRACTIONS; i++) {
ingo@3101: SQFractionResult result = sqr[res].getFraction(i);
ingo@3072:
ingo@3101: if (result == null) {
sascha@3222: log.warn("Fraction at index " + i + " is empty!");
ingo@3101: continue;
ingo@3101: }
ingo@3101:
ingo@3101: container.add(new SQCurveFacet(
ingo@3101: res,
ingo@3101: i,
sascha@3559: getFractionFacetname(CURVE_INDEX, 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:
sascha@3552: for (int j = 0, C = result.numIterations()-1; j < C; j++) {
sascha@3552:
sascha@3552: Object [] round = new Object [] { j + 1 };
sascha@3552:
ingo@3119: int index = res;
ingo@3119: index = index << 16;
ingo@3119: index = index + j;
ingo@3119:
sascha@3552: if (debug) {
sascha@3222: log.debug("new outliers facet (index=" +index+ ")");
sascha@3222: log.debug(" result index = " + res);
sascha@3222: log.debug(" fraction idx = " + i);
sascha@3222: log.debug(" iteration = " + j);
ingo@3119: }
ingo@3119:
ingo@3101: container.add(new SQOutlierFacet(
ingo@3119: index,
ingo@3101: i,
sascha@3559: getFractionFacetname(OUTLIER_INDEX, i),
ingo@3116: Resources.getMsg(
ingo@3116: meta,
ingo@3116: I18N_FACET_OUTLIERS,
ingo@3116: I18N_FACET_OUTLIERS,
sascha@3552: round
ingo@3116: ),
ingo@3101: hash,
ingo@3101: stateId
ingo@3101: ));
sascha@3552:
sascha@3552: container.add(new SQOutlierCurveFacet(
sascha@3552: index,
sascha@3552: i,
sascha@3559: getFractionFacetname(OUTLIER_CURVE_INDEX, i),
sascha@3552: Resources.getMsg(
sascha@3552: meta,
sascha@3552: I18N_FACET_OUTLIER_CURVE,
sascha@3552: I18N_FACET_OUTLIER_CURVE,
sascha@3552: round
sascha@3552: ),
sascha@3552: hash,
sascha@3552: stateId
sascha@3552: ));
sascha@3552:
sascha@3552: container.add(new SQOutlierMeasurementFacet(
sascha@3552: index,
sascha@3552: i,
sascha@3559: getFractionFacetname(OUTLIER_MEASUREMENT_INDEX, i),
sascha@3552: Resources.getMsg(
sascha@3552: meta,
sascha@3552: I18N_FACET_OUTLIER_MEASUREMENT,
sascha@3552: I18N_FACET_OUTLIER_MEASUREMENT,
sascha@3552: round
sascha@3552: ),
sascha@3552: hash,
sascha@3552: stateId
sascha@3552: ));
sascha@3552: } // for all outliers
sascha@3552:
sascha@3552: container.add(new SQMeasurementFacet(
sascha@3552: res,
sascha@3552: i,
sascha@3559: getFractionFacetname(MEASURREMENT_INDEX, i),
sascha@3552: Resources.getMsg(
sascha@3552: meta,
sascha@3552: I18N_FACET_MEASUREMENTS,
sascha@3552: I18N_FACET_MEASUREMENTS
sascha@3552: ),
sascha@3552: hash,
sascha@3552: stateId
sascha@3552: ));
sascha@3552: } // for all fractions
sascha@3552: } // for all results
sascha@3552: }
sascha@3552:
sascha@3552: protected static String getFractionFacetname(int type, int idx) {
sascha@3552: if (log.isDebugEnabled()) {
sascha@3552: log.debug("getFractionFacetname(): " + type + " | " + idx);
ingo@3101: }
sascha@3552: type %= FACET_NAMES.length;
sascha@3552: return FACET_NAMES[type][idx % FACET_NAMES[type].length];
ingo@3101: }
raimund@3058: }
raimund@3058: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :