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
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
-
Nested Class Summary
Nested classes/interfaces inherited from class FWSimIOAdapter
FWSimIOAdapter.FREQUENCE -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intcapacity of the queue (attributecachesize, default 600 for DAILY and 50 otherwise, at most 5000)protected ArrayBlockingQueue<FWSimQueueObjectMap> Queue that temporarily stores output data until the writer is accessibleFields inherited from class FWSimIOAdapter
iArrayMode, iFieldcount, iFrequence, iHeaderVariables, iInterface, iJexlRule, iKeyMap, iName, iOrderNumber, iProcessTime, iResourceElement, iSession, iSourceKey -
Constructor Summary
ConstructorsModifierConstructorDescriptionFWSimOutputAdapter(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.protectedFWSimOutputAdapter(FWSimOutputAdapter aOutputAdapter, FWSimInterface<?> aInterface) Copy constructor used when the interface is replaced; to be called by the subclasses.protectedFWSimOutputAdapter(org.jdom2.Element aOutputElement, int aOrderNumber, FWSimVarMap aVarMap) Creates the adapter from anoutputelement; to be called by the constructors of the subclasses. -
Method Summary
Modifier and TypeMethodDescriptionstatic FWSimOutputAdaptercreateAdapter(org.jdom2.Element aOutputElement, int aOrderNumber, FWSimVarMap aVarMap) Factory method: creates the output adapter matching the type of the interface named in the attributeinterface(DATABASE, CSV, MEMORY or NetCDF).static FWSimOutputAdaptercreateOutputFromObservable(FWObservable aObservable, int aOrderNumber) Creates an output adapter from the data of the GUI form (fieldsName,Datatype,Frequence,Poolsizeand, depending on the type,Driver,URL,User,PassorFile,Divider).Returnsout.Returns the header variables, which are the inputs of the output.Returns an empty list; output adapters provide no variables to the model.intgetStatisticsResultCache(String aSimulationID) Returns the variable map of the solution of the session.booleanprotected abstract voidinit(FWSimVarMap aVarMap) Initializes the output media, e.g.voidinitialize(FWSimVarMap aVarMap) Creates the queue and initializes the adapter and its statistics adapter.booleanisActive()booleanChecks 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).voidreceiveData(FWSimQueueObjectMap aQueueObjectMap) Puts an output line into the queue (waits if the queue is full); the writing is done in another thread.Creates the HTML documentation of the adapter for the solution overview: name, class, interface, frequency and a table of the header variables.org.jdom2.ElementtoXML()Creates theoutputelement with id, interface, frequency, cache size, header and statistics (statisticelement with prefix; only the complete statistics).static FWSimOutputAdapterupdateAdapter(FWSimOutputAdapter aAdapter, FWSimInterface<?> aInterface) Factory method: creates a copy of an adapter for a new interface, with the adapter class matching the interface type.voidWriting loop, run in a separate thread: takes the lines from the queue and writes them withwriteData(FWSimQueueObjectMap)until the thread is interrupted.protected abstract voidwriteData(FWSimQueueObjectMap aObjectToWrite) Writes one output line to the interface; to be implemented by the subclasses.Methods inherited from class FWSimIOAdapter
addProcessTime, addVariable, checkCondition, createFormFields, createVariables, finalize, getCreateFormXML, getEditFormXML, getFieldMap, getFrequence, getHeaderVariables, getHeaderXML, getJexlRule, getKeyMap, getName, getOrderNumber, getVariable, isArrayMode, isConditionCheck, removeVariable, setFrequenceAndRule, setInterface, setOrderNumber, toString, writeHeaderList
-
Field Details
-
iQueue
Queue that temporarily stores output data until the writer is accessible -
iCacheSize
protected int iCacheSizecapacity of the queue (attributecachesize, 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 anoutputelement; 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- theoutputelement, requiredaOrderNumber- order number of the adapteraVarMap- template variable map of the solution- Throws:
SimFieldMissingException- if a header variable refers to a missing variable
-
FWSimOutputAdapter
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 copyaInterface- 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 adapteraOutputElement- theoutputelement;nullfor calibration outputsaInterface- interface to write toaFrequence- frequency of the outputaOrderNumber- order number of the adapteraCacheSize- capacity of the queue, ornullfor the defaultaProcessTime- runtime statistics of the adapteraSession- the session
-
-
Method Details
-
initialize
Creates the queue and initializes the adapter and its statistics adapter.- Parameters:
aVarMap- variable map used for replacing tokens
-
init
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
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 withwriteData(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
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 attributeinterface(DATABASE, CSV, MEMORY or NetCDF).- Parameters:
aOutputElement- theoutputelementaOrderNumber- order number of the adapteraVarMap- 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 copyaInterface- 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 (fieldsName,Datatype,Frequence,Poolsizeand, depending on the type,Driver,URL,User,PassorFile,Divider). Supported types are DATABASE, CSV and MEMORY.- Parameters:
aObservable- the form dataaOrderNumber- order number of the adapter- Returns:
- the created adapter, or
nullif creating it failed (the error is logged) - Throws:
MissingSimResourceException- if the type is not supported
-
getOutputVariables
Returns an empty list; output adapters provide no variables to the model.- Returns:
- the output variables of the field map
- See Also:
-
getInputVariables
Returns the header variables, which are the inputs of the output.- Returns:
- the input variables of the field map
- See Also:
-
getResetScript
- Returns:
- the rule deciding when the aggregation is reset, or
null
-
getContentType
Returnsout.- 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:
trueif the adapter or its statistics adapter was initialized (has a queue)
-
getInterface
- Overrides:
getInterfacein classFWSimIOAdapter- Returns:
- the interface the adapter writes to
-
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:
trueif the adapter has statistics
-
getStatisticsHeaderVariables
- Returns:
- the header variables of the collected statistics data
-
getStatistics
- Returns:
- the statistics of the output
-
getStatisticsResultCache
- Parameters:
aSimulationID- id of the simulation- Returns:
- the collected statistics data of the simulation, or
null
-
getStatisticsAdapter
- Returns:
- the adapter writing the statistics, or
nullwithout statistics
-
toXML
public org.jdom2.Element toXML()Creates theoutputelement with id, interface, frequency, cache size, header and statistics (statisticelement with prefix; only the complete statistics).- Returns:
- an XML element with all settings of this container
- See Also:
-
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:
truewhile lines are pending or a line is being written
-