teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.states; felix@1151: felix@1643: import java.util.ArrayList; felix@1151: import java.util.List; felix@1151: felix@1151: import org.apache.log4j.Logger; felix@1151: teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; rrenkert@7792: import org.dive4elements.artifactdatabase.state.FacetActivity; teichmann@5831: import org.dive4elements.artifactdatabase.data.StateData; felix@1151: teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.Artifact; andre@8547: import org.dive4elements.river.artifacts.access.RangeAccess; felix@6582: import org.dive4elements.river.artifacts.ChartArtifact; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; felix@6582: import org.dive4elements.river.artifacts.FixationArtifact; felix@6582: import org.dive4elements.river.artifacts.MINFOArtifact; teichmann@5831: import org.dive4elements.river.artifacts.StaticWKmsArtifact; teichmann@5831: import org.dive4elements.river.artifacts.WINFOArtifact; felix@1151: teichmann@5831: import org.dive4elements.river.artifacts.math.WKmsOperation; raimund@2136: teichmann@5831: import org.dive4elements.river.artifacts.model.CalculationResult; teichmann@5831: import org.dive4elements.river.artifacts.model.DataFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.DifferenceCurveFacet; rrenkert@7792: import org.dive4elements.river.artifacts.model.DifferenceCurveFilterFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.EmptyFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes; teichmann@5831: import org.dive4elements.river.artifacts.model.WKms; teichmann@5831: import org.dive4elements.river.artifacts.model.WQKms; teichmann@5831: felix@6582: import org.dive4elements.river.artifacts.model.fixings.FixRealizingResult; rrenkert@7792: import org.dive4elements.river.artifacts.resources.Resources; felix@6582: teichmann@5865: import org.dive4elements.river.utils.RiverUtils; teichmann@8186: import org.dive4elements.river.backend.utils.StringUtil; felix@1151: felix@6573: /** State of a WINFOArtifact to get differences of data of other artifacts. */ felix@1151: public class WDifferencesState felix@1151: extends DefaultState felix@1151: implements FacetTypes felix@1151: { teichmann@8202: /** The log that is used in this state. */ teichmann@8202: private static Logger log = Logger.getLogger(WDifferencesState.class); felix@1151: rrenkert@7792: private static final String I18N_DIFFERENCES_FACET_NAME = rrenkert@7792: "facet.w_differences"; rrenkert@7792: private static final String I18N_DIFFERENCES_FACET_NAME_RAW = rrenkert@7792: "facet.w_differences.raw"; rrenkert@7792: rrenkert@7792: static { rrenkert@7792: // Active/deactivate facets. rrenkert@7792: FacetActivity.Registry.getInstance().register( rrenkert@7792: "winfo", rrenkert@7792: new FacetActivity() { rrenkert@7792: @Override rrenkert@7792: public Boolean isInitialActive( rrenkert@7792: Artifact artifact, rrenkert@7792: Facet facet, rrenkert@7792: String output rrenkert@7792: ) { rrenkert@7792: String name = facet.getName(); rrenkert@7792: rrenkert@7792: if (name.equals(FacetTypes.W_DIFFERENCES)) { rrenkert@7792: return Boolean.FALSE; rrenkert@7792: } rrenkert@7792: return Boolean.TRUE; rrenkert@7792: } rrenkert@7792: }); rrenkert@7792: } felix@1173: felix@1173: /** Specify to display nothing (this is kind of a "final" state). */ felix@1151: @Override felix@1151: protected String getUIProvider() { felix@1173: return "noinput"; felix@1151: } felix@1151: felix@1151: felix@1151: @Override felix@1183: public boolean validate(Artifact artifact) felix@1183: throws IllegalArgumentException felix@1183: { teichmann@5867: D4EArtifact flys = (D4EArtifact) artifact; felix@7029: if (artifact instanceof ChartArtifact) { felix@7029: return true; felix@7029: } felix@1183: felix@1183: StateData data = flys.getData("diffids"); felix@1183: felix@1183: if (data == null) { felix@1183: throw new IllegalArgumentException("diffids is empty"); felix@1183: } felix@1183: felix@1183: // TODO: Also validate format. felix@1183: felix@1183: return true; felix@1183: } felix@1183: felix@1183: felix@3122: /** felix@6582: * Access the data (wkms) of an artifact, coded in mingle. felix@3122: */ gernotbelger@8853: // FIXME: meanwhile used by several places outside this context; refactor into separate helper class to access waterlevels tom@8856: public WKms getWKms( tom@8856: String mingle, tom@8856: CallContext context, tom@8856: double from, tom@8856: double to tom@8856: ) { teichmann@8202: log.debug("WDifferencesState.getWKms " + mingle); felix@1732: String[] def = mingle.split(";"); felix@1732: String uuid = def[0]; felix@1732: String name = def[1]; felix@1732: int idx = Integer.parseInt(def[2]); felix@6582: D4EArtifact d4eArtifact = RiverUtils.getArtifact( felix@1732: uuid, felix@1732: context); felix@1732: felix@6582: if (d4eArtifact == null) { tom@8856: log.warn("One of the artifacts (1) for diff calculation " tom@8856: + "could not be loaded"); felix@1732: return null; felix@1732: } felix@6582: andre@8552: WKms retval = null; andre@8547: if (d4eArtifact instanceof StaticWKmsArtifact) { andre@8547: StaticWKmsArtifact staticWKms = (StaticWKmsArtifact) d4eArtifact; andre@8547: log.debug("WDifferencesState obtain data from StaticWKms"); andre@8547: WKms wkms = staticWKms.getWKms(idx, from, to); andre@8547: if (wkms == null) { andre@8547: log.error("No WKms from Static artifact for this range."); andre@8547: return new WQKms(); andre@8547: } andre@8552: return wkms; /* No need for additional km filtering */ andre@8547: } else if (d4eArtifact instanceof WINFOArtifact) { teichmann@8202: log.debug("Get WKms from WINFOArtifact"); felix@6582: WINFOArtifact flys = (WINFOArtifact) d4eArtifact; felix@6582: felix@6618: WKms[] wkms = (WKms[]) flys.getWaterlevelData(context). felix@1732: getData(); felix@6618: if (wkms == null || wkms.length == 0) { teichmann@8202: log.warn("no waterlevels in artifact"); felix@6582: } felix@6618: else if (wkms.length < idx+1) { teichmann@8202: log.warn("Not enough waterlevels in artifact."); andre@8552: retval = new WQKms(); felix@6582: } andre@8552: retval = wkms[idx]; andre@8547: } else if (d4eArtifact instanceof MINFOArtifact) { andre@8546: log.warn("Get WKms from MINFOArtifact not implemented!"); andre@8546: // CalculationResult r = (CalculationResult) andre@8546: // d4eArtifact.compute(context, ComputeType.ADVANCE, false); andre@8547: } else if (d4eArtifact instanceof FixationArtifact) { teichmann@8202: log.debug ("Get WKms from FixationArtifact."); felix@6582: CalculationResult r = (CalculationResult) felix@6582: d4eArtifact.compute(context, ComputeType.ADVANCE, false); felix@6582: FixRealizingResult frR = (FixRealizingResult) r.getData(); andre@8552: retval = frR.getWQKms()[idx]; felix@6582: } felix@6582: andre@8552: andre@8552: if (retval == null) { tom@8856: log.error( tom@8856: "Do not know how to handle (getWKms) minuend/subtrahend"); andre@8552: } else if (!Double.isNaN(from) && !Double.isNaN(to)) { tom@8856: /* Filter out only relevant data points for calulation results.*/ andre@8552: log.debug("Before filter: " + retval.size()); andre@8552: retval = retval.filteredKms(from, to); andre@8552: log.debug("After filter: " + retval.size()); andre@8552: } andre@8552: andre@8552: return retval; felix@1732: } felix@1732: felix@1732: felix@1643: /** felix@1652: * Return CalculationResult with Array of WKms that are difference of felix@1652: * Waterlevels. Add respective facets (DifferencesCurveFacet, DataFacet). felix@1643: */ felix@1183: @Override felix@1151: public Object computeAdvance( teichmann@5867: D4EArtifact artifact, felix@1151: String hash, felix@1151: CallContext context, felix@1151: List facets, felix@1151: Object old felix@1151: ) { raimund@2136: if (artifact instanceof ChartArtifact) { raimund@2136: ChartArtifact chart = (ChartArtifact)artifact; raimund@2136: facets.add(new EmptyFacet()); raimund@2136: return null; raimund@2136: } felix@1732: WINFOArtifact winfo = (WINFOArtifact) artifact; felix@1173: String id = getID(); andre@8547: RangeAccess rangeAccess = new RangeAccess(artifact); andre@8647: double from = rangeAccess.getFrom(true); andre@8647: double to = rangeAccess.getTo(true); felix@1183: // Load the Artifacts/facets that we want to subtract and display. felix@1183: // Expected format is: tom@8856: //[42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[...] felix@1661: String diffids = winfo.getDataAsString("diffids"); teichmann@8202: log.debug("WDifferencesState has: " + diffids); felix@1661: String datas[] = diffids.split("#"); felix@1183: andre@8608: log.debug("Difference from: " + from + " to: " + to); andre@8608: /* Check if we need to obtain the data in a different order */ felix@1183: // Validate the Data-Strings. felix@1183: for (String s: datas) { felix@4054: if (!WaterlevelSelectState.isValueValid(s)) { felix@1183: // TODO: escalate. felix@1183: } felix@1183: } felix@1183: felix@1626: if (datas.length < 2) { felix@1183: // TODO crash with style felix@1183: } felix@1151: felix@1643: List wkmss = new ArrayList(); felix@1151: felix@1661: for(int i = 0; i < datas.length; i+=2) { felix@1652: // e.g.: felix@1652: // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1 felix@1732: WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]), andre@8647: context, from, to); felix@1732: WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]), andre@8647: context, from, to); felix@1732: felix@1626: String facetName = "diff ()"; rrenkert@7792: String minName = "min"; rrenkert@7792: String subName = "sub"; felix@1626: felix@1732: if (minuendWKms != null && subtrahendWKms != null) { rrenkert@7792: minName = StringUtil.wWrap(minuendWKms.getName()); rrenkert@7792: subName = StringUtil.wWrap(subtrahendWKms.getName()); rrenkert@7792: facetName = minName + " - " + subName; felix@1732: WKms wkms = WKmsOperation.SUBTRACTION.operate(minuendWKms, felix@1732: subtrahendWKms); felix@1643: wkms.setName(facetName); felix@1643: wkmss.add(wkms); teichmann@8202: log.debug("WKMSSubtraction happened"); felix@1626: } felix@1626: if (facets != null) { rrenkert@7792: facets.add(new DifferenceCurveFacet( rrenkert@7792: i/2, rrenkert@7792: W_DIFFERENCES, rrenkert@7792: Resources.getMsg( rrenkert@7792: context.getMeta(), rrenkert@7792: I18N_DIFFERENCES_FACET_NAME_RAW, rrenkert@7792: facetName, rrenkert@7792: new Object[] { minName, subName }), rrenkert@7792: ComputeType.ADVANCE, rrenkert@7792: id, rrenkert@7792: hash)); rrenkert@7792: facets.add(new DifferenceCurveFilterFacet(i/2, rrenkert@7792: W_DIFFERENCES_FILTERED, rrenkert@7792: Resources.getMsg( rrenkert@7792: context.getMeta(), rrenkert@7792: I18N_DIFFERENCES_FACET_NAME, rrenkert@7792: facetName, rrenkert@7792: new Object[] { minName, subName }), rrenkert@7792: ComputeType.ADVANCE, rrenkert@7792: id, rrenkert@7792: hash)); felix@1626: } raimund@2180: } raimund@2180: raimund@2180: if (facets != null) { raimund@2180: facets.add(new DataFacet(CSV, "CSV data")); raimund@2180: facets.add(new DataFacet(PDF, "PDF data")); teichmann@8202: log.debug("Adding facets in WDifferencesState."); raimund@2180: } raimund@2180: else { teichmann@8202: log.debug("Not adding facets in WDifferencesState."); felix@1151: } felix@1151: felix@1652: // TODO Evaluate whether null is okay as reports. felix@1643: WKms[] diffs = wkmss.toArray(new WKms[wkmss.size()]); felix@1652: CalculationResult result = new CalculationResult(diffs, null); felix@1643: return result; felix@1151: } felix@1151: } felix@1151: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :