view flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 362:d79a51fc4f1d

Added necessary methods to start the computation of waterlevel data. flys-artifacts/trunk@1770 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 29 Apr 2011 07:46:27 +0000
parents d97982627596
children 0ccf7200fc51
line wrap: on
line source
package de.intevation.flys.artifacts;

import java.util.List;
import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.apache.log4j.Logger;

import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.state.Output;
import de.intevation.artifactdatabase.state.State;
import de.intevation.artifactdatabase.state.StateEngine;
import de.intevation.artifactdatabase.transition.TransitionEngine;

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.model.River;

import de.intevation.flys.artifacts.states.DefaultState;
import de.intevation.flys.artifacts.context.FLYSContext;
import de.intevation.flys.artifacts.model.WQKms;
import de.intevation.flys.artifacts.model.WstValueTable;


/**
 * The default WINFO artifact.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class WINFOArtifact extends FLYSArtifact {

    /** The logger for this class */
    private static Logger logger = Logger.getLogger(WINFOArtifact.class);


    /** The name of the artifact.*/
    public static final String ARTIFACT_NAME = "winfo";

    /** XPath */
    public static final String XPATH_STATIC_UI ="/art:result/art:ui/art:static";


    /**
     * The default constructor.
     */
    public WINFOArtifact() {
    }


    /**
     * This method returns a description of this artifact.
     *
     * @param data Some data.
     * @param context The CallContext.
     *
     * @return the description of this artifact.
     */
    public Document describe(Document data, CallContext context) {
        logger.debug("Describe: the current state is: " + getCurrentStateId());

        FLYSContext flysContext = null;
        if (context instanceof FLYSContext) {
            flysContext = (FLYSContext) context;
        }
        else {
            flysContext = (FLYSContext) context.globalContext();
        }

        StateEngine stateEngine = (StateEngine) flysContext.get(
            FLYSContext.STATE_ENGINE_KEY);

        TransitionEngine transitionEngine = (TransitionEngine) flysContext.get(
            FLYSContext.TRANSITION_ENGINE_KEY);

        List<State> reachable = transitionEngine.getReachableStates(
            this, getCurrentState(context), stateEngine);

        Document description            = XMLUtils.newDocument();
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            description,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element root = ProtocolUtils.createRootNode(creator);
        description.appendChild(root);

        State current = getCurrentState(context);

        ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
        ProtocolUtils.appendState(creator, root, current);
        ProtocolUtils.appendReachableStates(creator, root, reachable);

        Element name = ProtocolUtils.createArtNode(
            creator, "name",
            new String[] { "value" },
            new String[] { getName() });

        Element ui = ProtocolUtils.createArtNode(
            creator, "ui", null, null);

        Element staticUI  = ProtocolUtils.createArtNode(
            creator, "static", null, null);

        Element outs = ProtocolUtils.createArtNode(
            creator, "outputmodes", null, null);
        appendOutputModes(description, outs, context, identifier());

        appendStaticUI(description, staticUI, context, identifier());

        Element dynamic = current.describe(
            this,
            description,
            root,
            context,
            identifier());

        if (dynamic != null) {
            ui.appendChild(dynamic);
        }

        ui.appendChild(staticUI);

        root.appendChild(name);
        root.appendChild(ui);
        root.appendChild(outs);

        return description;
    }


    /**
     * Returns the name of the concrete artifact.
     *
     * @return the name of the concrete artifact.
     */
    public String getName() {
        return ARTIFACT_NAME;
    }


    protected void appendOutputModes(
        Document    doc,
        Element     outs,
        CallContext context,
        String      uuid)
    {
        Vector<String> stateIds = getPreviousStateIds();

        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        FLYSContext flysContext = getFlysContext(context);
        StateEngine engine      = (StateEngine) flysContext.get(
            FLYSContext.STATE_ENGINE_KEY);

        for (String stateId: stateIds) {
            logger.debug("Append output modes for state: " + stateId);
            State state = engine.getState(stateId);

            List<Output> list = state.getOutputs();
            if (list == null || list.size() == 0) {
                continue;
            }

            ProtocolUtils.appendOutputModes(creator, outs, list);
        }

        try {
            DefaultState cur = (DefaultState) getCurrentState(context);
            if (cur.validate(this, context)) {
                List<Output> list = cur.getOutputs();
                if (list != null && list.size() > 0) {
                    logger.debug(
                        "Append output modes for state: " + cur.getID());

                    ProtocolUtils.appendOutputModes(creator, outs, list);
                }
            }
        }
        catch (IllegalArgumentException iae) {
            // state is not valid, so we do not append its outputs.
        }
    }


    /**
     * This method appends the static data - that has already been inserted by
     * the user - to the static node of the DESCRIBE document.
     *
     * @param doc The document.
     * @param ui The root node.
     * @param context The CallContext.
     * @param uuid The identifier of the artifact.
     */
    protected void appendStaticUI(
        Document    doc,
        Node        ui,
        CallContext context,
        String uuid)
    {
        Vector<String> stateIds = getPreviousStateIds();

        FLYSContext flysContext = getFlysContext(context);
        StateEngine engine      = (StateEngine) flysContext.get(
            FLYSContext.STATE_ENGINE_KEY);

        for (String stateId: stateIds) {
            logger.debug("Append static data for state: " + stateId);
            DefaultState state = (DefaultState) engine.getState(stateId);
            state = (DefaultState) fillState(state);

            ui.appendChild(state.describeStatic(doc, ui, context, uuid));
        }
    }


    //
    // METHODS FOR RETRIEVING COMPUTED DATA FOR DIFFERENT CHART TYPES
    //

    /**
     * Returns the data that is computed by a waterlevel computation.
     *
     * @return an array of data triples that consist of W, Q and Kms.
     */
    public WQKms[] getWaterlevelData()
    throws NullPointerException
    {
        logger.debug("WINFOArtifact.getWaterlevelData");

        River river = getRiver();
        if (river == null) {
            throw new NullPointerException("No river selected.");
        }

        double[] kms = getKms();
        if (kms == null) {
            throw new NullPointerException("No Kms selected.");
        }

        double[] qs = getQs();
        if (qs == null) {
            // TODO Use DischargeTableValue.getQForW() to compute the Qs.
        }

        WstValueTable wst = WstValueTable.getTable(river);
        if (wst == null) {
            throw new NullPointerException("No Wst found for selected river.");
        }

        // TODO Introduce a caching mechanism here!

        return computeWaterlevelData(kms, qs, wst);
    }


    /**
     * Computes the data of a waterlevel computation based on the interpolation
     * in WstValueTable.
     *
     * @param kms The kilometer values.
     * @param qa The discharge values.
     * @param wst The WstValueTable used for the interpolation.
     *
     * @return an array of data triples that consist of W, Q and Kms.
     */
    public static WQKms[] computeWaterlevelData(
        double[]      kms,
        double[]      qs,
        WstValueTable wst)
    {
        logger.info("WINFOArtifact.computeWaterlevelData");

        WQKms[] wqkms = new WQKms[qs.length];
        for (int i = 0; i < wqkms.length; i++) {
            wqkms[i] = new WQKms();
        }

        for (double km: kms) {
            double[] interpolatedW = wst.interpolateW(km, qs);

            // TODO Modify the interpolation to return a better formed data
            // structure.
            for (int i = 0; i < interpolatedW.length; i++) {
                wqkms[i].add(interpolatedW[i], qs[i], km);
            }
        }

        return wqkms;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org