Class FWSimOutputAdapter

java.lang.Object
net.simplace.sim.io.FWSimIOAdapter
net.simplace.sim.io.output.FWSimOutputAdapter
All Implemented Interfaces:
FWSimFieldContainer
Direct Known Subclasses:
CacheOutputAdapter, CSVOutputAdapter, DatabaseOutputAdapter, NetCDFOutputAdapter

public abstract class FWSimOutputAdapter extends FWSimIOAdapter
Base class of the adapters that write output data to an interface (database, CSV, memory, NetCDF). The model puts the output lines into a blocking queue (receiveData(FWSimQueueObjectMap)); a separate thread takes them from the queue and writes them (writeData()), so the model is decoupled from the writing. The cache size is the capacity of the queue; when it is full, the model waits.

Before the application ends, it has to be checked that all output is written, i.e. the queue is empty (see isWriting() and getQueueLenth()).

The adapter reads the output element, e.g. <output id="WEATHER" interface="weatherout" frequence="YEARLY" cachesize="10"> with a header of out elements (id, mode SUM/AVG/MIN/MAX/..., datatype, rule). An optional statistic child defines statistics, which are written by a second adapter of the same type to a statistics interface.

Author:
Andreas Enders
  • Field Details

    • iQueue

      Queue that temporarily stores output data until the writer is accessible
    • iCacheSize

      protected int iCacheSize
      capacity of the queue (attribute cachesize, default 600 for DAILY and 50 otherwise, at most 5000)
  • Constructor Details

    • FWSimOutputAdapter

      protected FWSimOutputAdapter(org.jdom2.Element aOutputElement, int aOrderNumber, FWSimVarMap aVarMap)
      Creates the adapter from an output element; to be called by the constructors of the subclasses. Fills the meta data of the header variables from their sources and initializes the statistics.
      Parameters:
      aOutputElement - the output element, required
      aOrderNumber - order number of the adapter
      aVarMap - template variable map of the solution
      Throws:
      SimFieldMissingException - if a header variable refers to a missing variable
    • FWSimOutputAdapter

      protected FWSimOutputAdapter(FWSimOutputAdapter aOutputAdapter, FWSimInterface<?> aInterface)
      Copy constructor used when the interface is replaced; to be called by the subclasses. Cache size and reset rule are taken over.
      Parameters:
      aOutputAdapter - the adapter to copy
      aInterface - the new interface
    • FWSimOutputAdapter

      public FWSimOutputAdapter(String aName, org.jdom2.Element aOutputElement, FWSimInterface<?> aInterface, FWSimIOAdapter.FREQUENCE aFrequence, int aOrderNumber, String aCacheSize, ProcessTime aProcessTime, FWSimSession aSession)
      Creates the adapter from the graph panel or for calibration outputs.
      Parameters:
      aName - name of the adapter
      aOutputElement - the output element; null for calibration outputs
      aInterface - interface to write to
      aFrequence - frequency of the output
      aOrderNumber - order number of the adapter
      aCacheSize - capacity of the queue, or null for the default
      aProcessTime - runtime statistics of the adapter
      aSession - the session
  • Method Details

    • initialize

      public void initialize(FWSimVarMap aVarMap)
      Creates the queue and initializes the adapter and its statistics adapter.
      Parameters:
      aVarMap - variable map used for replacing tokens
    • init

      protected abstract void init(FWSimVarMap aVarMap)
      Initializes the output media, e.g. creates the file or table; to be implemented by the subclasses.
      Parameters:
      aVarMap - variable map used for replacing tokens
    • receiveData

      public void receiveData(FWSimQueueObjectMap aQueueObjectMap)
      Puts an output line into the queue (waits if the queue is full); the writing is done in another thread. The line is also passed to the statistics.
      Parameters:
      aQueueObjectMap - the output line
    • writeData

      public void writeData()
      Writing loop, run in a separate thread: takes the lines from the queue and writes them with writeData(FWSimQueueObjectMap) until the thread is interrupted. Returns immediately if the adapter was not initialized. A line that cannot be written (runtime exception) is logged and skipped, so the loop keeps emptying the queue and the model is not blocked by a full queue.
    • writeData

      protected abstract void writeData(FWSimQueueObjectMap aObjectToWrite)
      Writes one output line to the interface; to be implemented by the subclasses.
      Parameters:
      aObjectToWrite - the output line
    • createAdapter

      public static FWSimOutputAdapter createAdapter(org.jdom2.Element aOutputElement, int aOrderNumber, FWSimVarMap aVarMap) throws MissingSimResourceException
      Factory method: creates the output adapter matching the type of the interface named in the attribute interface (DATABASE, CSV, MEMORY or NetCDF).
      Parameters:
      aOutputElement - the output element
      aOrderNumber - order number of the adapter
      aVarMap - template variable map of the solution
      Returns:
      the created adapter
      Throws:
      MissingSimResourceException - if the interface type is not supported
    • updateAdapter

      public static FWSimOutputAdapter updateAdapter(FWSimOutputAdapter aAdapter, FWSimInterface<?> aInterface) throws MissingSimResourceException
      Factory method: creates a copy of an adapter for a new interface, with the adapter class matching the interface type.
      Parameters:
      aAdapter - the adapter to copy
      aInterface - the new interface
      Returns:
      the new adapter
      Throws:
      MissingSimResourceException - if the interface type is not supported
    • createOutputFromObservable

      public static FWSimOutputAdapter createOutputFromObservable(FWObservable aObservable, int aOrderNumber) throws MissingSimResourceException
      Creates an output adapter from the data of the GUI form (fields Name, Datatype, Frequence, Poolsize and, depending on the type, Driver, URL, User, Pass or File, Divider). Supported types are DATABASE, CSV and MEMORY.
      Parameters:
      aObservable - the form data
      aOrderNumber - order number of the adapter
      Returns:
      the created adapter, or null if creating it failed (the error is logged)
      Throws:
      MissingSimResourceException - if the type is not supported
    • getOutputVariables

      public Collection<FWSimVariable<?>> getOutputVariables()
      Returns an empty list; output adapters provide no variables to the model.
      Returns:
      the output variables of the field map
      See Also:
    • getInputVariables

      public Collection<FWSimVariable<?>> getInputVariables()
      Returns the header variables, which are the inputs of the output.
      Returns:
      the input variables of the field map
      See Also:
    • getResetScript

      public String getResetScript()
      Returns:
      the rule deciding when the aggregation is reset, or null
    • getContentType

      public FWSimVariable.CONTENT_TYPE getContentType()
      Returns out.
      Returns:
      the content type of the variables of this container (e.g. sys, mgm)
      See Also:
    • getQueueLenth

      public int getQueueLenth()
      Returns:
      number of lines waiting in the queue, including the queue of the statistics adapter
    • isActive

      public boolean isActive()
      Returns:
      true if the adapter or its statistics adapter was initialized (has a queue)
    • getInterface

      public FWSimInterface<?> getInterface()
      Overrides:
      getInterface in class FWSimIOAdapter
      Returns:
      the interface the adapter writes to
    • getVarMap

      public FWSimVarMap getVarMap()
      Returns the variable map of the solution of the session.
      Returns:
      the var map this container belongs to
      See Also:
    • hasStatistics

      public boolean hasStatistics()
      Returns:
      true if the adapter has statistics
    • getStatisticsHeaderVariables

      public LinkedHashMap<String, FWSimVariable<?>> getStatisticsHeaderVariables()
      Returns:
      the header variables of the collected statistics data
    • getStatistics

      public LinkedList<FWSimStatisticVariable> getStatistics()
      Returns:
      the statistics of the output
    • getStatisticsResultCache

      public FWSimResultCache getStatisticsResultCache(String aSimulationID)
      Parameters:
      aSimulationID - id of the simulation
      Returns:
      the collected statistics data of the simulation, or null
    • getStatisticsAdapter

      public FWSimOutputAdapter getStatisticsAdapter()
      Returns:
      the adapter writing the statistics, or null without statistics
    • toXML

      public org.jdom2.Element toXML()
      Creates the output element with id, interface, frequency, cache size, header and statistics (statistic element with prefix; only the complete statistics).
      Returns:
      an XML element with all settings of this container
      See Also:
    • toHTMLTablestring

      public String toHTMLTablestring()
      Creates the HTML documentation of the adapter for the solution overview: name, class, interface, frequency and a table of the header variables.
      Returns:
      the HTML fragment
    • isWriting

      public boolean isWriting()
      Checks whether received lines are still queued or being written right now (also in the short moment between taking a line from the queue and writing it).
      Returns:
      true while lines are pending or a line is being written