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; teichmann@5831: import org.dive4elements.artifactdatabase.data.StateData; felix@1151: teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.Artifact; 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; 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; felix@6582: teichmann@5865: import org.dive4elements.river.utils.RiverUtils; teichmann@5831: import org.dive4elements.river.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: { felix@1151: /** The logger that is used in this state. */ felix@1151: private static Logger logger = Logger.getLogger(WDifferencesState.class); felix@1151: 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: */ aheinecke@7697: public WKms getWKms(String mingle, CallContext context) { felix@6582: logger.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@1732: felix@1732: if (name.startsWith("staticwkms")) { felix@1732: StaticWKmsArtifact staticWKms = teichmann@5865: (StaticWKmsArtifact) RiverUtils.getArtifact( felix@1732: uuid, felix@1732: context); felix@1732: logger.debug("WDifferencesState obtain data from StaticWKms"); felix@1732: WKms wkms = staticWKms.getWKms(idx); felix@1732: if (wkms == null) felix@1732: logger.error("No WKms from artifact."); felix@1732: return wkms; felix@1732: } sascha@3076: felix@6582: D4EArtifact d4eArtifact = RiverUtils.getArtifact( felix@1732: uuid, felix@1732: context); felix@1732: felix@6582: if (d4eArtifact == null) { felix@1732: logger.warn("One of the artifacts (1) for diff calculation could not be loaded"); felix@1732: return null; felix@1732: } felix@6582: felix@6582: if (d4eArtifact instanceof WINFOArtifact) { felix@6582: logger.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) { felix@6582: logger.warn("no waterlevels in artifact"); felix@6582: } felix@6618: else if (wkms.length < idx+1) { felix@6618: logger.warn("Not enough waterlevels in artifact."); felix@6582: return new WQKms(); felix@6582: } felix@6618: return wkms[idx]; felix@1732: } felix@6582: else if (d4eArtifact instanceof MINFOArtifact) { felix@6582: logger.debug("Get WKms from MINFOArtifact"); felix@6582: CalculationResult r = (CalculationResult) felix@6582: d4eArtifact.compute(context, ComputeType.ADVANCE, false); felix@6582: } felix@6582: else if (d4eArtifact instanceof FixationArtifact) { felix@6582: logger.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(); felix@6582: return frR.getWQKms()[idx]; felix@6582: } felix@6582: felix@6582: logger.error("Do not know how to handle (getWKms) minuend/subtrahend"); felix@6582: return null; 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(); felix@1151: felix@1183: // Load the Artifacts/facets that we want to subtract and display. felix@1183: // Expected format is: felix@1183: // [42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[1231f2-....] felix@1661: String diffids = winfo.getDataAsString("diffids"); felix@1732: logger.debug("WDifferencesState has: " + diffids); felix@1661: String datas[] = diffids.split("#"); felix@1183: 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]), felix@1626: context); felix@1732: WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]), felix@1732: context); felix@1732: felix@1626: String facetName = "diff ()"; felix@1626: felix@1732: if (minuendWKms != null && subtrahendWKms != null) { sascha@3076: facetName = StringUtil.wWrap(minuendWKms.getName()) felix@1732: + " - " + StringUtil.wWrap(subtrahendWKms.getName()); felix@1732: WKms wkms = WKmsOperation.SUBTRACTION.operate(minuendWKms, felix@1732: subtrahendWKms); felix@1643: wkms.setName(facetName); felix@1643: wkmss.add(wkms); felix@1626: logger.debug("WKMSSubtraction happened"); felix@1626: } felix@1626: if (facets != null) { felix@1696: facets.add(new DifferenceCurveFacet(i/2, W_DIFFERENCES, facetName, felix@1643: ComputeType.ADVANCE, id, 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")); raimund@2180: logger.debug("Adding facets in WDifferencesState."); raimund@2180: } raimund@2180: else { raimund@2180: logger.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 :