Class FWSimOutput

java.lang.Object
net.simplace.sim.io.output.FWSimOutput
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
FWSimCalibrationOutput

public class FWSimOutput extends Object implements Runnable
An output of the solution (output element). In each simulation step OUTPUT(LocalDateTime, FWSimVarMap, PROJECT_MODE) collects the values of the header variables per simulation in an FWSimOutputCache and, depending on the frequency (DAILY, YEARLY, COMPLEX rule, ...), hands the aggregated line (SUM, AVG, MIN, MAX, ...) to its FWSimOutputAdapter, which writes it to the interface. As Runnable the output runs the writing thread of its adapter. Optionally statistics (e.g. model efficiency of simulated against observed values) are calculated at the end of a simulation.
Author:
Andreas Enders
  • Field Details

    • iName

      protected String iName
      Name (id) of the output
    • iFrequence

      protected FWSimIOAdapter.FREQUENCE iFrequence
      Frequency of the output
    • iOutputScript

      protected JexlRuleScript iOutputScript
      Rule deciding when a line is written (for frequencies COMPLEX and BOOLEAN)
    • iResetScript

      protected JexlRuleScript iResetScript
      Rule deciding when the aggregation is reset (for frequencies COMPLEX and BOOLEAN)
    • iAdapter

      protected FWSimOutputAdapter iAdapter
      adapter writing the data to the interface
    • iOutputCache

      protected HashMap<String, FWSimOutputCache> iOutputCache
      aggregation caches by simulation id
    • iProjectMode

      protected FWSimProject.PROJECT_MODE iProjectMode
      project mode the output is active in; calls in other modes are ignored
    • iSpinupOutput

      protected boolean iSpinupOutput
      true if the output is also written during the spin-up period (attribute spinup="true"); the system variable FWSimVarMap.SPINUP_ACTIVE tells the spin-up lines apart
  • Constructor Details

    • FWSimOutput

      public FWSimOutput(org.jdom2.Element aOutputElement, int aOrderNumber, FWSimVarMap aVarMap, FWSimProject.PROJECT_MODE aProjectMode)
      Constructor from output Element: format like
      The adapter is created matching the interface of the output (see FWSimOutputAdapter.createAdapter(Element, int, FWSimVarMap)).
      Parameters:
      aOutputElement - the output element
      aOrderNumber - order number of the adapter
      aVarMap - template variable map of the solution
      aProjectMode - project mode the output is active in
    • FWSimOutput

      protected FWSimOutput(String aName, FWSimIOAdapter.FREQUENCE aFrequence, FWSimVarMap aVarMap, String aRule, String aResetRule)
      Creates an output for calibration (project mode CALIBRATION) without solution element; the data is written to a DatabaseOutputAdapter.
      Parameters:
      aName - name of the output
      aFrequence - frequency of the output
      aVarMap - template variable map of the solution
      aRule - rule deciding when a line is written
      aResetRule - not used; the reset rule is taken from the adapter
  • Method Details

    • updateAdapter

      public void updateAdapter(FWSimInterface<?> aInterface, FWSimVarMap aVarMap)
      Replaces the adapter by a copy using the new interface. Errors are logged.
      Parameters:
      aInterface - the new interface
      aVarMap - not used
    • setFrequenceAndRule

      public void setFrequenceAndRule(FWSimIOAdapter.FREQUENCE aFrequence, String aRule)
      Changes frequency and rule of the output and its adapter (e.g. in the GUI).
      Parameters:
      aFrequence - the frequency
      aRule - the rule, empty for none (required for COMPLEX and BOOLEAN)
      Throws:
      IllegalArgumentException - if the frequency is COMPLEX or BOOLEAN and no rule is given
    • initialize

      public void initialize(FWSimVarMap aVarMap)
      Initializes the adapter (e.g. creates the output file or table).
      Parameters:
      aVarMap - variable map used for replacing tokens
    • OUTPUT

      public void OUTPUT(LocalDateTime aCurrentDate, FWSimVarMap aVarMap, FWSimProject.PROJECT_MODE aCurrentProjectMode)
      Called in every simulation step: adds the current values of the header variables to the aggregation cache of the simulation and, if the frequency or rule requires it, passes the aggregated line to the adapter queue. DAILY and CAL outputs pass a line in every step. For COMPLEX and BOOLEAN the reset rule resets the aggregation.
      Parameters:
      aCurrentDate - current simulation date
      aVarMap - variable map of the simulation
      aCurrentProjectMode - current project mode; the call is ignored if it differs from the mode of the output
      Throws:
      org.apache.commons.jexl3.JexlException - if the output rule cannot be evaluated (logged and rethrown)
    • getName

      public String getName()
      Returns:
      the name (id) of the output
    • getFrequence

      public FWSimIOAdapter.FREQUENCE getFrequence()
      Returns:
      the frequency of the output
    • isSpinupOutput

      public boolean isSpinupOutput()
      Returns:
      true if the output is also written during the spin-up period (attribute spinup="true")
    • getOutputScript

      public JexlRuleScript getOutputScript()
      Returns:
      the rule deciding when a line is written
    • getResetScript

      public JexlRuleScript getResetScript()
      Returns:
      the rule deciding when the aggregation is reset
    • hasStatistics

      public boolean hasStatistics()
      Returns:
      true if the adapter defines statistics
    • calculateStatistics

      public void calculateStatistics(String aSimulationID)
      Calculates the statistics of a simulation from the collected pairs of observed and simulated values and writes one line (project id, simulation id and one value per statistic) to the statistics adapter.
      Parameters:
      aSimulationID - id of the simulation
    • removeSimulationCache

      public void removeSimulationCache(FWSimVarMap aVarMap)
      Removes the aggregation cache of a finished simulation (nothing to do for DAILY and CAL outputs, which do not aggregate).
      Parameters:
      aVarMap - variable map of the simulation
    • getProjectMode

      public FWSimProject.PROJECT_MODE getProjectMode()
      Returns:
      the project mode the output is active in
    • run

      public void run()
      Runs the writing loop of the adapter (in its own thread).
      Specified by:
      run in interface Runnable
      See Also:
    • isWriting

      public boolean isWriting()
      Returns:
      true if the adapter is active and still writing or has data in its queue
    • finalize

      protected void finalize() throws Exception
      Finalizes the adapter and its statistics adapter (e.g. closes files and connections).
      Overrides:
      finalize in class Object
      Throws:
      Exception - if finalizing an adapter fails
      See Also:
    • getAdapter

      public FWSimOutputAdapter getAdapter()
      Returns:
      the adapter of the output
    • getOutputElement

      public org.jdom2.Element getOutputElement()
      Returns:
      the output element of the solution, or null for calibration outputs
    • toString

      public String toString()
      Overrides:
      toString in class Object