Class CSVResourceAdapter

All Implemented Interfaces:
FWSimFieldContainer

public class CSVResourceAdapter extends FWSimResourceAdapter<List<String>>
Resource adapter for CSV files (CSVInterface). The file is registered in the file pool of the interface; its lines (optionally only the lines to read, after skipping header lines) are read into a FWSimResourceCache. A file is only read again if its unique id changed.
Author:
Andreas Enders
  • Constructor Details

    • CSVResourceAdapter

      public CSVResourceAdapter(org.jdom2.Element aResourceElement, FWSimSession aSession, int aOrderNumber)
      Creates the adapter from a resource element of the solution.
      Parameters:
      aResourceElement - the resource element
      aSession - the session
      aOrderNumber - order number of the adapter
    • CSVResourceAdapter

      public CSVResourceAdapter(String aFileName, String aContent, String aComponentName, String aDivider, int aOrderNumber, FWSimSession aFwSimSession, FWSimIOAdapter.FREQUENCE aFrequence, Map<String,String> aKeyMap, Integer[] aLinesToRead, Integer aSkipHeaderLines) throws IOException, SimFieldMissingException
      Creates the adapter from the graph panel: creates the CSV interface and derives the header variables from the file or content (see readHeaderList(String, String, String, String, Map, FWSimSession)).
      Parameters:
      aFileName - CSV file
      aContent - CSV content instead of a file, may be null
      aComponentName - name of the adapter and the interface
      aDivider - column divider
      aOrderNumber - order number of the adapter
      aFwSimSession - the session
      aFrequence - frequency the data is read with
      aKeyMap - key variables; if empty, the user is asked to select them
      aLinesToRead - lines to read, or null for all
      aSkipHeaderLines - number of header lines to skip
      Throws:
      IOException - if the file cannot be read
      SimFieldMissingException - if no key variables are given or selected
    • CSVResourceAdapter

      public CSVResourceAdapter(FWSimResourceAdapter<?> aAdapter, CSVInterface aInterface)
      Copy constructor used when the interface is replaced; the key variables are taken over.
      Parameters:
      aAdapter - the adapter to copy
      aInterface - the new interface
  • Method Details

    • getData

      Registers the file of the current simulation in the file pool and reads it, if it was updated.
      Specified by:
      getData in class FWSimInputAdapter
      Parameters:
      aVarMap - variable map of the simulation (e.g. for the key values of the rule)
      Returns:
      the new cache, or null if the file is unchanged (the previous cache stays valid)
      Throws:
      MissingSimResourceException - if the data cannot be read
      See Also:
    • getData

      public FWSimResourceCache getData(FWSimVarMap aVarMap, FileData aFileData) throws MissingSimResourceException
      Reads a CSV file into a new cache, unless it is the same file (unique id) as last time.
      Parameters:
      aVarMap - variable map of the simulation
      aFileData - the file
      Returns:
      the new cache, or null if the file was already read
      Throws:
      MissingSimResourceException - if the file is not available, has fewer than two columns (wrong divider or encoding) or cannot be read
    • readHeaderList

      public Map<String,String> readHeaderList(String aFileName, String aContent, String aComponentName, String aDivider, Map<String,String> aKeyMap, FWSimSession aSession) throws IOException
      Creates the header variables from the first line of the file or content (column names); the data type of each column is guessed from the value in the second line (a missing line or value counts as empty text, i.e. FWSimVariable.DATA_TYPE.CHAR). If no key variables are given, the user is asked to select them.
      Parameters:
      aFileName - CSV file, only read if aContent is null
      aContent - CSV content instead of a file, may be null
      aComponentName - name of the adapter (not used)
      aDivider - column divider, default TAB
      aKeyMap - key variables, may be null or empty
      aSession - the session (provides the model variables for the key selection)
      Returns:
      the key variables (model variable → column), or null if none were given or selected
      Throws:
      IOException - if the file cannot be read or the file/content is empty