Class FWSimInterface<T>
java.lang.Object
net.simplace.sim.io.interfaces.FWSimInterface<T>
- Type Parameters:
T- type of the access object returned bygetInterface(FWSimVarMap, CONTENT_TYPE)
- Direct Known Subclasses:
DatabaseInterface, FILEInterface, JSONInterface, MEMORYInterface
Base class of the interfaces (
interface elements of the solution). An interface abstracts the access
to a data medium and is used by resources (reading) and outputs (writing):
- databases via SQL (
DatabaseInterface), DBF and shape files (DBFInterface) - files: CSV (
CSVInterface), XML (XMLInterface), NetCDF (NetCDFInterface) - JSON, e.g. from web services (
JSONInterface) - memory (
MEMORYInterface) - custom interfaces given by class name
getInterface(FWSimVarMap, CONTENT_TYPE) provides the access object (e.g. a database connection or a file
pool); read(FWSimSession, String, Object, LinkedHashMap, FWSimInputAdapter, String, INTERFACE_TYPE)
converts read data into a FWSimResourceCache. Files and connections are pooled.- Author:
- Andreas Enders
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumidentifiers for different interface types -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected LinkedHashMap<String, ColumnData> column data of a database table by column namecolumn index by column nameprotected LinkedHashMap<Integer, String> column name by column index -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFWSimInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, int aPoolsize) Creates the interface without element; to be called by the subclasses.protectedFWSimInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, org.jdom2.Element aInterfaceElement) Creates the interface from its element; to be called by the subclasses. -
Method Summary
Modifier and TypeMethodDescriptionprotected static MultiHashMap<Integer, Integer> checkHeaderForColMode(String[] aStrings, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables) Checks whether the file stores arrays in column mode, i.e.static FWSimInterface<?> createInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, org.jdom2.Element aInterfaceElement) Factory method: creates the interface of the given type.static FWSimInterface<?> createInterface(org.jdom2.Element aInterfaceElement, FWSimSession aSession) Factory method: creates the interface of the type given in the attributetype.createStatisticInterface(String aStatisticPrefix) Creates an interface of the same type for writing the statistics of an output: the name gets the prefix, and for file interfaces also the file name (in the same directory).abstract voidfinalize()Releases the resources of the interface (files, connections); to be implemented by the subclasses.abstract TgetInterface(FWSimVarMap aVarMap, FWSimVariable.CONTENT_TYPE aContentType) Returns the access object of the interface, e.g.org.jdom2.ElementgetName()intabstract FWSimResourceCacheread(FWSimSession aFwSimSession, String aProjectID, Object aCache, LinkedHashMap<String, String> aKeyMap, FWSimInputAdapter aResourceAdapter, String aUniqueID, FWSimInterface.INTERFACE_TYPE aInterfaceType) Converts read data into a resource cache: each data line is stored under the values of its key columns.protected static ObjectreadColArray(String aProjectID, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, MultiHashMap<Integer, Integer> aHeaderColModeMatch, List<?> aObjects, LinkedHashMap<String, String> aKeyMap, Object[] aArray) Converts the values of one data line into the data types of the header variables.protected static ObjectreadColArray(String aProjectID, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, MultiHashMap<Integer, Integer> aHeaderColModeMatch, List<?> aObjects, LinkedHashMap<String, String> aKeyMap, Object[] aArray, boolean aTypedCells) AsreadColArray(String, LinkedHashMap, MultiHashMap, List, LinkedHashMap, Object[]), optionally with cells that are already converted to the data type of their column (CSV table shared between resources, seeCSVTable).toString()org.jdom2.ElementtoXML()Creates theinterfaceelement with id, type and pool size; subclasses add their settings.protected static Object[]transformArrays(List<Object[]> tArrays, FWSimVariable<?>[] aHeaderVariables) Combines several data lines (row mode) into one line: array columns collect the value of each line (elementifrom linei), other columns take the value of the first line.protected voidwriteToDatabase(Object[] aValues, PreparedStatement aTargetStatement, boolean aAddIterationData) Writes a data line with a prepared insert statement into a database table (used for caching resources in the database).
-
Field Details
-
iColumnMap
column data of a database table by column name -
iIntColumnNames
column name by column index -
iColumnNamesInt
-
-
Constructor Details
-
FWSimInterface
protected FWSimInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, org.jdom2.Element aInterfaceElement) Creates the interface from its element; to be called by the subclasses. The pool size is read from the childpoolsize.- Parameters:
aName- name of the interfaceaInterfaceType- type of the interfaceaInterfaceElement- theinterfaceelement
-
FWSimInterface
Creates the interface without element; to be called by the subclasses.- Parameters:
aName- name of the interfaceaInterfaceType- type of the interfaceaPoolsize- number of pooled files or connections
-
-
Method Details
-
createInterface
public static FWSimInterface<?> createInterface(org.jdom2.Element aInterfaceElement, FWSimSession aSession) Factory method: creates the interface of the type given in the attributetype. The${...}parameters used in the children of the element are registered in the data frame of the session.- Parameters:
aInterfaceElement- theinterfaceelementaSession- the session, may benull- Returns:
- the interface, or
nullif a custom interface cannot be created
-
createStatisticInterface
Creates an interface of the same type for writing the statistics of an output: the name gets the prefix, and for file interfaces also the file name (in the same directory). JSON interfaces are not supported (they are treated as custom interfaces).- Parameters:
aStatisticPrefix- prefix for name and file name, e.g.stat_- Returns:
- the statistics interface
-
createInterface
public static FWSimInterface<?> createInterface(String aName, FWSimInterface.INTERFACE_TYPE aInterfaceType, org.jdom2.Element aInterfaceElement) Factory method: creates the interface of the given type. For SHP the file name is changed to the.dbffile of the shape file, which is then read by aDBFInterface.- Parameters:
aName- name of the interfaceaInterfaceType- type of the interfaceaInterfaceElement- theinterfaceelement- Returns:
- the interface, or
nullif a custom interface cannot be created
-
getInterface
Returns the access object of the interface, e.g. a database connection or a file pool.- Parameters:
aVarMap- variable map used for replacing tokens (e.g. in file names), may benullaContentType-resfor reading,outfor writing- Returns:
- the access object
-
read
public abstract FWSimResourceCache read(FWSimSession aFwSimSession, String aProjectID, Object aCache, LinkedHashMap<String, String> aKeyMap, FWSimInputAdapter aResourceAdapter, String aUniqueID, FWSimInterface.INTERFACE_TYPE aInterfaceType) throws MissingSimResourceExceptionConverts read data into a resource cache: each data line is stored under the values of its key columns.- Parameters:
aFwSimSession- the sessionaProjectID- id of the current projectaCache- the data read, in the format of the interface (e.g.String[][], result set, XML element)aKeyMap- key variables of the resourceaResourceAdapter- the adapter the cache belongs toaUniqueID- unique id of the source dataaInterfaceType- type of the source interface- Returns:
- the new resource cache
- Throws:
MissingSimResourceException- if the data cannot be converted
-
writeToDatabase
protected void writeToDatabase(Object[] aValues, PreparedStatement aTargetStatement, boolean aAddIterationData) throws SQLException Writes a data line with a prepared insert statement into a database table (used for caching resources in the database). The values are set according to the data types of the columns; type errors are logged.- Parameters:
aValues- the values of the lineaTargetStatement- the insert statement; nothing happens ifnullaAddIterationData-trueto fill the first five columns with iteration 0, project id, simulation name, no simulation number and the generated simulation id- Throws:
SQLException- if the insert fails
-
transformArrays
protected static Object[] transformArrays(List<Object[]> tArrays, FWSimVariable<?>[] aHeaderVariables) Combines several data lines (row mode) into one line: array columns collect the value of each line (elementifrom linei), other columns take the value of the first line.- Parameters:
tArrays- the data lines, each with one value per header variableaHeaderVariables- the header variables- Returns:
- the combined line
-
readColArray
protected static Object readColArray(String aProjectID, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, MultiHashMap<Integer, Integer> aHeaderColModeMatch, List<?> aObjects, LinkedHashMap<String, String> aKeyMap, Object[] aArray) Converts the values of one data line into the data types of the header variables. In column mode an array variable is built from several columns (seecheckHeaderForColMode(String[], LinkedHashMap)); its count is set to the number of columns. Number format errors are logged.- Parameters:
aProjectID- id of the current project (not used)aHeaderVariables- the header variablesaHeaderColModeMatch- columns per header variable in column modeaObjects- the raw values of the lineaKeyMap- key variables of the resourceaArray- the array the converted values are written to (one per header variable)- Returns:
- the key of the line: the value of the key column, or a list of values for several keys
-
readColArray
protected static Object readColArray(String aProjectID, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, MultiHashMap<Integer, Integer> aHeaderColModeMatch, List<?> aObjects, LinkedHashMap<String, String> aKeyMap, Object[] aArray, boolean aTypedCells) AsreadColArray(String, LinkedHashMap, MultiHashMap, List, LinkedHashMap, Object[]), optionally with cells that are already converted to the data type of their column (CSV table shared between resources, seeCSVTable).- Parameters:
aProjectID- id of the current project (not used)aHeaderVariables- the header variablesaHeaderColModeMatch- columns per header variable in column modeaObjects- the values of the lineaKeyMap- key variables of the resourceaArray- the array the converted values are written to (one per header variable)aTypedCells-trueif cells that are neithernullnor strings are already converted for the variable and are used as they are;falseto convert every value viatoString()(e.g. values from a database)- Returns:
- the key of the line: the value of the key column, or a list of values for several keys
-
checkHeaderForColMode
protected static MultiHashMap<Integer,Integer> checkHeaderForColMode(String[] aStrings, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables) Checks whether the file stores arrays in column mode, i.e. one column per array element named<name>_1,<name>_2, ... (or without underscore).- Parameters:
aStrings- the column names of the fileaHeaderVariables- the header variables- Returns:
- the column indices per header variable index, or
nullif the file has one column per variable or no array columns
-
getName
- Returns:
- the name (id) of the interface
-
getPoolSize
public int getPoolSize()- Returns:
- the number of pooled files or connections
-
getInterfaceType
- Returns:
- the type of the interface
-
finalize
-
getInterfaceElement
public org.jdom2.Element getInterfaceElement()- Returns:
- the
interfaceelement of the solution (with the unresolved${...}tokens), ornullif the interface was created without element
-
toXML
public org.jdom2.Element toXML()Creates theinterfaceelement with id, type and pool size; subclasses add their settings.- Returns:
- the
interfaceelement
-
toString
-