ingo@105: package de.intevation.flys.artifacts; ingo@105: sascha@721: import java.util.Arrays; ingo@693: import java.util.ArrayList; ingo@693: import java.util.HashSet; ingo@109: import java.util.List; ingo@687: import java.util.Map; ingo@693: import java.util.Set; ingo@109: ingo@105: import org.w3c.dom.Document; ingo@110: import org.w3c.dom.Element; ingo@124: import org.w3c.dom.Node; ingo@105: ingo@105: import org.apache.log4j.Logger; ingo@105: ingo@110: import de.intevation.artifacts.ArtifactNamespaceContext; ingo@105: import de.intevation.artifacts.CallContext; ingo@105: ingo@110: import de.intevation.artifactdatabase.ProtocolUtils; ingo@693: import de.intevation.artifactdatabase.state.DefaultOutput; ingo@693: import de.intevation.artifactdatabase.state.Facet; ingo@144: import de.intevation.artifactdatabase.state.Output; ingo@110: import de.intevation.artifactdatabase.state.State; ingo@110: import de.intevation.artifactdatabase.state.StateEngine; ingo@112: import de.intevation.artifactdatabase.transition.TransitionEngine; ingo@110: ingo@109: import de.intevation.artifacts.common.utils.XMLUtils; ingo@109: ingo@385: import de.intevation.flys.model.Gauge; ingo@362: import de.intevation.flys.model.River; ingo@362: ingo@134: import de.intevation.flys.artifacts.states.DefaultState; ingo@109: import de.intevation.flys.artifacts.context.FLYSContext; sascha@655: ingo@687: import de.intevation.flys.artifacts.model.DischargeTables; ingo@385: import de.intevation.flys.artifacts.model.MainValuesFactory; ingo@362: import de.intevation.flys.artifacts.model.WQKms; ingo@362: import de.intevation.flys.artifacts.model.WstValueTable; sascha@443: import de.intevation.flys.artifacts.model.WstValueTableFactory; sascha@709: import de.intevation.flys.artifacts.model.Calculation; ingo@686: import de.intevation.flys.artifacts.model.Calculation1; ingo@686: import de.intevation.flys.artifacts.model.Calculation2; ingo@686: import de.intevation.flys.artifacts.model.Calculation3; sascha@655: import de.intevation.flys.artifacts.model.Calculation4; sascha@709: import de.intevation.flys.artifacts.model.CalculationResult; sascha@655: import de.intevation.flys.artifacts.model.Segment; sascha@451: ingo@105: ingo@105: /** ingo@105: * The default WINFO artifact. ingo@105: * ingo@105: * @author Ingo Weinzierl ingo@105: */ ingo@119: public class WINFOArtifact extends FLYSArtifact { ingo@105: ingo@105: /** The logger for this class */ ingo@105: private static Logger logger = Logger.getLogger(WINFOArtifact.class); ingo@105: ingo@105: ingo@121: /** The name of the artifact.*/ ingo@121: public static final String ARTIFACT_NAME = "winfo"; ingo@121: ingo@124: /** XPath */ ingo@124: public static final String XPATH_STATIC_UI ="/art:result/art:ui/art:static"; ingo@124: ingo@121: ingo@105: /** ingo@105: * The default constructor. ingo@105: */ ingo@105: public WINFOArtifact() { ingo@105: } ingo@105: ingo@105: ingo@105: /** ingo@105: * This method returns a description of this artifact. ingo@105: * ingo@105: * @param data Some data. ingo@128: * @param context The CallContext. ingo@105: * ingo@105: * @return the description of this artifact. ingo@105: */ ingo@105: public Document describe(Document data, CallContext context) { ingo@119: logger.debug("Describe: the current state is: " + getCurrentStateId()); ingo@105: ingo@624: if (logger.isDebugEnabled()) { ingo@624: dumpArtifact(); ingo@624: } ingo@624: sascha@706: FLYSContext flysContext = getFlysContext(context); ingo@110: ingo@112: StateEngine stateEngine = (StateEngine) flysContext.get( ingo@112: FLYSContext.STATE_ENGINE_KEY); ingo@112: ingo@112: TransitionEngine transitionEngine = (TransitionEngine) flysContext.get( ingo@112: FLYSContext.TRANSITION_ENGINE_KEY); ingo@112: ingo@112: List reachable = transitionEngine.getReachableStates( ingo@355: this, getCurrentState(context), stateEngine); ingo@112: ingo@112: Document description = XMLUtils.newDocument(); ingo@110: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@110: description, ingo@110: ArtifactNamespaceContext.NAMESPACE_URI, ingo@110: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@110: ingo@110: Element root = ProtocolUtils.createRootNode(creator); ingo@115: description.appendChild(root); ingo@115: ingo@119: State current = getCurrentState(context); ingo@119: ingo@110: ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash()); ingo@119: ProtocolUtils.appendState(creator, root, current); ingo@112: ProtocolUtils.appendReachableStates(creator, root, reachable); ingo@110: ingo@303: Element name = ProtocolUtils.createArtNode( ingo@303: creator, "name", ingo@303: new String[] { "value" }, ingo@303: new String[] { getName() }); ingo@303: ingo@127: Element ui = ProtocolUtils.createArtNode( ingo@127: creator, "ui", null, null); ingo@110: ingo@127: Element staticUI = ProtocolUtils.createArtNode( ingo@127: creator, "static", null, null); ingo@127: ingo@144: Element outs = ProtocolUtils.createArtNode( ingo@144: creator, "outputmodes", null, null); ingo@144: appendOutputModes(description, outs, context, identifier()); ingo@144: ingo@134: appendStaticUI(description, staticUI, context, identifier()); ingo@128: ingo@127: Element dynamic = current.describe( ingo@306: this, ingo@124: description, ingo@127: root, ingo@127: context, ingo@127: identifier()); ingo@127: ingo@144: if (dynamic != null) { ingo@144: ui.appendChild(dynamic); ingo@144: } ingo@144: ingo@128: ui.appendChild(staticUI); ingo@144: ingo@303: root.appendChild(name); ingo@127: root.appendChild(ui); ingo@144: root.appendChild(outs); ingo@124: ingo@110: return description; ingo@105: } ingo@121: ingo@121: ingo@121: /** ingo@121: * Returns the name of the concrete artifact. ingo@121: * ingo@121: * @return the name of the concrete artifact. ingo@121: */ ingo@121: public String getName() { ingo@121: return ARTIFACT_NAME; ingo@121: } ingo@124: ingo@124: ingo@144: protected void appendOutputModes( ingo@144: Document doc, ingo@144: Element outs, ingo@144: CallContext context, ingo@144: String uuid) ingo@144: { sascha@661: List stateIds = getPreviousStateIds(); ingo@144: ingo@144: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@144: doc, ingo@144: ArtifactNamespaceContext.NAMESPACE_URI, ingo@144: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@144: ingo@144: FLYSContext flysContext = getFlysContext(context); ingo@144: StateEngine engine = (StateEngine) flysContext.get( ingo@144: FLYSContext.STATE_ENGINE_KEY); ingo@144: ingo@144: for (String stateId: stateIds) { ingo@144: logger.debug("Append output modes for state: " + stateId); ingo@693: DefaultState state = (DefaultState) engine.getState(stateId); ingo@144: ingo@144: List list = state.getOutputs(); ingo@144: if (list == null || list.size() == 0) { ingo@624: logger.debug("-> No output modes for this state."); ingo@144: continue; ingo@144: } ingo@144: ingo@693: List fs = facets.get(stateId); ingo@693: if (fs == null || fs.size() == 0) { ingo@693: logger.debug("No facets found."); ingo@693: continue; ingo@693: } ingo@693: ingo@696: logger.debug("Found " + fs.size() + " facets in previous states."); ingo@693: ingo@693: List generated = generateOutputs(list, fs); ingo@693: ingo@693: ProtocolUtils.appendOutputModes(creator, outs, generated); ingo@144: } ingo@144: ingo@355: try { ingo@355: DefaultState cur = (DefaultState) getCurrentState(context); ingo@355: if (cur.validate(this, context)) { ingo@355: List list = cur.getOutputs(); ingo@355: if (list != null && list.size() > 0) { ingo@355: logger.debug( ingo@355: "Append output modes for state: " + cur.getID()); ingo@355: ingo@693: List fs = facets.get(cur.getID()); ingo@693: if (fs != null && fs.size() > 0) { ingo@693: List generated = generateOutputs(list, fs); ingo@693: ingo@696: logger.debug("Found " + fs.size() + " current facets."); sascha@714: if (!generated.isEmpty()) { sascha@714: ProtocolUtils.appendOutputModes( sascha@714: creator, outs, generated); sascha@714: } ingo@693: } ingo@693: else { ingo@693: logger.debug("No facets found for the current state."); ingo@693: } ingo@693: ingo@355: } ingo@355: } ingo@355: } ingo@355: catch (IllegalArgumentException iae) { ingo@355: // state is not valid, so we do not append its outputs. ingo@355: } ingo@144: } ingo@144: ingo@144: ingo@693: protected List generateOutputs(List list, List fs) { ingo@693: List generated = new ArrayList(); ingo@693: sascha@714: boolean debug = logger.isDebugEnabled(); sascha@714: ingo@693: for (Output out: list) { ingo@693: Output o = new DefaultOutput( ingo@693: out.getName(), ingo@693: out.getDescription(), ingo@693: out.getMimeType()); ingo@693: sascha@705: Set outTypes = new HashSet(); ingo@693: ingo@693: for (Facet f: out.getFacets()) { sascha@714: if (outTypes.add(f.getName()) && debug) { sascha@714: logger.debug("configured facet " + f); sascha@714: } ingo@693: } ingo@693: sascha@714: boolean facetAdded = false; ingo@693: for (Facet f: fs) { ingo@693: String type = f.getName(); ingo@693: sascha@705: if (outTypes.contains(type)) { sascha@714: if (debug) { sascha@714: logger.debug("Add facet " + f); sascha@714: } sascha@714: facetAdded = true; ingo@693: o.addFacet(f); ingo@693: } ingo@693: } ingo@693: sascha@714: if (facetAdded) { sascha@714: generated.add(o); sascha@714: } ingo@693: } ingo@693: ingo@693: return generated; ingo@693: } ingo@693: ingo@693: ingo@124: /** ingo@124: * This method appends the static data - that has already been inserted by ingo@124: * the user - to the static node of the DESCRIBE document. ingo@124: * ingo@134: * @param doc The document. ingo@134: * @param ui The root node. ingo@134: * @param context The CallContext. ingo@134: * @param uuid The identifier of the artifact. ingo@124: */ ingo@128: protected void appendStaticUI( ingo@134: Document doc, ingo@134: Node ui, ingo@134: CallContext context, ingo@134: String uuid) ingo@128: { sascha@661: List stateIds = getPreviousStateIds(); ingo@124: ingo@134: FLYSContext flysContext = getFlysContext(context); ingo@134: StateEngine engine = (StateEngine) flysContext.get( ingo@134: FLYSContext.STATE_ENGINE_KEY); ingo@124: ingo@134: for (String stateId: stateIds) { ingo@134: logger.debug("Append static data for state: " + stateId); ingo@134: DefaultState state = (DefaultState) engine.getState(stateId); ingo@302: ingo@624: ui.appendChild(state.describeStatic(this, doc, ui, context, uuid)); ingo@134: } ingo@362: } ingo@124: ingo@362: ingo@362: // ingo@362: // METHODS FOR RETRIEVING COMPUTED DATA FOR DIFFERENT CHART TYPES ingo@362: // ingo@362: ingo@687: /** ingo@362: * Returns the data that is computed by a waterlevel computation. ingo@362: * ingo@362: * @return an array of data triples that consist of W, Q and Kms. ingo@362: */ sascha@709: public CalculationResult getWaterlevelData() ingo@362: { ingo@362: logger.debug("WINFOArtifact.getWaterlevelData"); ingo@362: ingo@362: River river = getRiver(); ingo@362: if (river == null) { sascha@709: return error(new WQKms[0], "No river selected."); ingo@362: } ingo@362: ingo@362: double[] kms = getKms(); ingo@362: if (kms == null) { sascha@709: return error(new WQKms[0], "No Kms selected."); ingo@362: } ingo@362: ingo@447: double[] qs = getQs(); ingo@447: double[] ws = null; ingo@447: boolean qSel = true; ingo@447: ingo@362: if (qs == null) { ingo@377: logger.debug("Determine Q values based on a set of W values."); ingo@447: qSel = false; ingo@447: ws = getWs(); ingo@447: qs = getQsForWs(ws); sascha@735: if (qs == null) { sascha@735: return error(new WQKms[0], "conversion ws to qs failed."); sascha@735: } ingo@362: } ingo@362: sascha@443: WstValueTable wst = WstValueTableFactory.getTable(river); ingo@362: if (wst == null) { sascha@709: return error(new WQKms[0], "No Wst found for selected river."); ingo@362: } ingo@362: sascha@708: sascha@738: double [] range = getDistance(); sascha@738: if (range == null) { sascha@738: return error(new WQKms[0], "No range found"); sascha@738: } sascha@736: sascha@738: double refKm; sascha@738: sascha@738: if (isFreeQ()) { sascha@738: refKm = range[0]; sascha@738: logger.debug("'free' calculation (km " + refKm + ")"); sascha@738: } sascha@738: else { sascha@736: Gauge gauge = river.determineGaugeByPosition(range[0]); sascha@708: if (gauge == null) { sascha@736: return error( sascha@736: new WQKms[0], "No gauge found for km " + range[0]); sascha@708: } sascha@736: sascha@736: refKm = gauge.getStation().doubleValue(); sascha@736: sascha@736: logger.debug( sascha@736: "reference gauge: " + gauge.getName() + " (km " + refKm + ")"); sascha@708: } sascha@708: sascha@738: return computeWaterlevelData(kms, qs, ws, wst, refKm); ingo@447: } ingo@447: ingo@362: /** ingo@362: * Computes the data of a waterlevel computation based on the interpolation ingo@362: * in WstValueTable. ingo@362: * ingo@362: * @param kms The kilometer values. ingo@362: * @param qa The discharge values. ingo@362: * @param wst The WstValueTable used for the interpolation. ingo@362: * ingo@362: * @return an array of data triples that consist of W, Q and Kms. ingo@362: */ sascha@709: public static CalculationResult computeWaterlevelData( ingo@686: double [] kms, ingo@686: double [] qs, ingo@686: double [] ws, sascha@635: WstValueTable wst, sascha@738: double refKm sascha@635: ) { ingo@362: logger.info("WINFOArtifact.computeWaterlevelData"); ingo@362: sascha@738: Calculation1 calc1 = new Calculation1(kms, qs, ws, refKm); sascha@636: sascha@709: return calc1.calculate(wst); ingo@124: } ingo@385: ingo@385: ingo@385: /** ingo@385: * Returns the data that is computed by a duration curve computation. ingo@385: * ingo@385: * @return the data computed by a duration curve computation. ingo@385: */ sascha@709: public CalculationResult getDurationCurveData() { ingo@385: logger.debug("WINFOArtifact.getDurationCurveData"); ingo@385: ingo@385: River r = getRiver(); ingo@385: ingo@385: if (r == null) { sascha@709: return error(null, "Cannot determine river."); ingo@385: } ingo@385: ingo@385: Gauge g = getGauge(); ingo@385: ingo@385: if (g == null) { sascha@709: return error(null, "Cannot determine gauge."); ingo@385: } ingo@385: ingo@385: double[] locations = getLocations(); ingo@385: ingo@385: if (locations == null) { sascha@709: return error(null, "Cannot determine location."); ingo@385: } ingo@385: sascha@443: WstValueTable wst = WstValueTableFactory.getTable(r); ingo@385: if (wst == null) { sascha@709: return error(null, "No Wst found for selected river."); ingo@385: } ingo@385: ingo@385: return computeDurationCurveData(g, wst, locations[0]); ingo@385: } ingo@385: ingo@385: ingo@385: /** ingo@385: * Computes the data used to create duration curves. ingo@385: * ingo@385: * @param gauge The selected gauge. ingo@385: * @param location The selected location. ingo@385: * ingo@385: * @return the computed data. ingo@385: */ sascha@709: public static CalculationResult computeDurationCurveData( ingo@385: Gauge gauge, ingo@385: WstValueTable wst, ingo@385: double location) ingo@385: { ingo@385: logger.info("WINFOArtifact.computeDurationCurveData"); ingo@385: ingo@385: Object[] obj = MainValuesFactory.getDurationCurveData(gauge); ingo@385: ingo@385: int[] days = (int[]) obj[0]; ingo@385: double[] qs = (double[]) obj[1]; ingo@385: ingo@686: Calculation3 calculation = new Calculation3(location, days, qs); ingo@385: ingo@686: return calculation.calculate(wst); ingo@385: } ingo@393: ingo@393: ingo@393: /** ingo@687: * Returns the data that is used to create discharge curves. ingo@687: * ingo@687: */ sascha@721: public CalculationResult getDischargeCurveData() { ingo@687: sascha@721: River river = getRiver(); sascha@721: if (river == null) { sascha@721: return error(new WQKms[0], "no river found"); ingo@687: } ingo@687: sascha@721: double [] distance = getDistance(); ingo@687: sascha@721: if (distance == null) { sascha@721: return error(new WQKms[0], "no range found"); ingo@687: } ingo@687: sascha@721: List gauges = river.determineGauges(distance[0], distance[1]); ingo@687: sascha@721: if (gauges.isEmpty()) { sascha@721: return error(new WQKms[0], "no gauges found"); sascha@721: } sascha@721: sascha@721: String [] names = new String[gauges.size()]; sascha@721: sascha@721: for (int i = 0; i < names.length; ++i) { sascha@721: names[i] = gauges.get(i).getName(); sascha@721: } sascha@721: sascha@721: DischargeTables dt = new DischargeTables(river.getName(), names); sascha@721: sascha@721: Map map = dt.getValues(100d); sascha@721: sascha@721: ArrayList res = new ArrayList(); sascha@721: sascha@721: for (Gauge gauge: gauges) { sascha@721: String name = gauge.getName(); sascha@721: double [][] values = map.get(name); sascha@721: if (values == null) { sascha@721: continue; sascha@721: } sascha@721: double [] kms = new double[values[0].length]; sascha@721: Arrays.fill(kms, gauge.getStation().doubleValue()); sascha@723: res.add(new WQKms(kms, values[0], values[1], name)); sascha@721: } sascha@721: sascha@721: return new CalculationResult( sascha@721: res.toArray(new WQKms[res.size()]), sascha@721: new Calculation()); ingo@687: } ingo@687: ingo@687: ingo@687: /** ingo@393: * Returns the data that is computed by a discharge curve computation. ingo@393: * ingo@393: * @return the data computed by a discharge curve computation. ingo@393: */ sascha@709: public CalculationResult getComputedDischargeCurveData() ingo@393: throws NullPointerException ingo@393: { ingo@393: logger.debug("WINFOArtifact.getComputedDischargeCurveData"); ingo@393: ingo@393: River r = getRiver(); ingo@393: ingo@393: if (r == null) { sascha@709: return error(new WQKms[0], "Cannot determine river."); ingo@393: } ingo@393: ingo@393: double[] locations = getLocations(); ingo@393: ingo@393: if (locations == null) { sascha@709: return error(new WQKms[0], "Cannot determine location."); ingo@393: } ingo@393: sascha@443: WstValueTable wst = WstValueTableFactory.getTable(r); ingo@393: if (wst == null) { sascha@709: return error(new WQKms[0], "No Wst found for selected river."); ingo@393: } ingo@393: sascha@709: return computeDischargeCurveData(wst, locations[0]); ingo@456: } ingo@456: ingo@456: ingo@456: /** ingo@393: * Computes the data used to create computed discharge curves. ingo@393: * ingo@393: * @param wst The WstValueTable that is used for the interpolation. ingo@393: * @param location The location where the computation should be based on. ingo@393: * ingo@393: * @return an object that contains tuples of W/Q values at the specified ingo@393: * location. ingo@393: */ sascha@709: public static CalculationResult computeDischargeCurveData( ingo@393: WstValueTable wst, ingo@393: double location) ingo@393: { ingo@393: logger.info("WINFOArtifact.computeDischargeCurveData"); ingo@393: ingo@686: Calculation2 calculation = new Calculation2(location); ingo@393: sascha@709: return calculation.calculate(wst); sascha@709: } ingo@393: sascha@709: protected static final CalculationResult error(Object data, String msg) { sascha@709: return new CalculationResult(data, new Calculation(msg)); ingo@393: } ingo@402: ingo@402: /** ingo@402: * Returns the data computed by the discharge longitudinal section ingo@402: * computation. ingo@402: * ingo@402: * @return an array of WQKms object - one object for each given Q value. ingo@402: */ sascha@709: public CalculationResult getDischargeLongitudinalSectionData() { sascha@451: ingo@402: logger.debug("WINFOArtifact.getDischargeLongitudinalSectionData"); ingo@402: ingo@402: River river = getRiver(); ingo@402: if (river == null) { sascha@709: logger.debug("No river selected."); sascha@709: return error(new WQKms[0], "No river selected."); ingo@402: } ingo@402: sascha@655: WstValueTable table = WstValueTableFactory.getTable(river); sascha@655: if (table == null) { sascha@709: logger.debug("No wst found for selected river."); sascha@709: return error(new WQKms[0], "No wst found for selected river."); ingo@402: } ingo@402: sascha@655: List segments = getSegments(); sascha@637: sascha@655: if (segments == null) { sascha@709: logger.debug("Cannot create segments."); sascha@709: return error(new WQKms[0], "Cannot create segments."); sascha@451: } ingo@450: sascha@655: double [] range = getFromToStep(); sascha@451: sascha@655: if (range == null) { sascha@709: logger.debug("Cannot figure out range."); sascha@709: return error(new WQKms[0], "Cannot figure out range."); sascha@451: } sascha@451: sascha@655: Calculation4 calc4 = new Calculation4(segments, river, isQ()); sascha@455: sascha@709: return calc4.calculate(table, range[0], range[1], range[2]); ingo@450: } ingo@105: } ingo@105: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :