Class FWSimHelper

java.lang.Object
net.simplace.sim.util.FWSimHelper

public class FWSimHelper extends Object
Static helper methods for simulations: converting values and arrays, handling project data files and simulation ids, and more. Parameter tokens and expressions are replaced by FWParameterResolver.
Author:
Andreas Enders
  • Field Details

    • FIELD_COMPERATOR

      public static Comparator<FWSimFieldContainer> FIELD_COMPERATOR
      Compares field containers by their order number.
    • SIM_VAR_COMARATOR

      public static final Comparator<FWSimVariable<?>> SIM_VAR_COMARATOR
      Orders variables within a SimComponent for readable output: by content type (sys, constant, input, res, state, rate, out, privat), then by name.
  • Constructor Details

    • FWSimHelper

      public FWSimHelper()
  • Method Details

    • ArrayToLinkedMap

      public static LinkedHashMap<String,String> ArrayToLinkedMap(Object[] aArray)
      Converts an array into a map where each element (as string) is both key and value, e.g. for choice fields of forms.
      Parameters:
      aArray - the elements
      Returns:
      the map in array order
    • ArrayToMap

      public static TreeMap<Double,Double> ArrayToMap(Double[] aArray)
      Interprets an array as key/value pairs {k1, v1, k2, v2, ...} and returns them as sorted map.
      Parameters:
      aArray - the key/value pairs; must have an even length
      Returns:
      the map sorted by key
    • findSimComponents

      public static List<String> findSimComponents()
      Returns:
      the names of all classes on the class path extending FWSimComponent (except DefaultManagement)
    • findResources

      public static Collection<String> findResources(String aPackage, String aRegexp)
      Finds resources on the class path whose name matches a regular expression.
      Parameters:
      aPackage - not used
      aRegexp - regular expression for the resource names
      Returns:
      the matching resource names
      See Also:
    • findResourceTransformers

      public static List<String> findResourceTransformers()
      Returns:
      the names of all classes on the class path extending FWSimResourceTransformer
    • findSelectors

      public static List<String> findSelectors()
      Returns:
      the names of all classes on the class path extending FWSimulationSelector
    • findGenerators

      public static List<String> findGenerators()
      Returns:
      the names of all classes on the class path extending FWSimulationGenerator
    • findIterators

      public static List<String> findIterators()
      Returns:
      the names of all classes on the class path extending FWSimulationIterator
    • getDataTypeFromValue

      public static FWSimVariable.DATA_TYPE getDataTypeFromValue(String aFirstValue)
      Guesses the data type from a value string: DOUBLE, INT, INTARRAY/DOUBLEARRAY ({1,2}), BOOLEAN, DATE (yyyy-MM-dd or dd.MM.yyyy style) or CHARARRAY; otherwise CHAR.
      Parameters:
      aFirstValue - value to analyse
      Returns:
      the guessed data type
    • getObjectArrayFromVarMap

      public static Object[][] getObjectArrayFromVarMap(FWSimVarMap aVarMap)
      Converts a var map into an array of key/value pairs.
      Parameters:
      aVarMap - the var map
      Returns:
      one row per entry: {key, value} (values are not evaluated from rules)
    • getParameterName

      public static String getParameterName(String aKey)
      Returns the variable name referenced by a key: the name of the first parameter token (${name}), or otherwise the part before !.
      Parameters:
      aKey - key, e.g. ${vValues} or vValues!2
      Returns:
      the variable name
    • unzipArray

      public static Object unzipArray(FWSimVariable<?> aTargetVariable, ArrayList<String> aFieldMapIndizes, Object[] aSourceArray, Set<String> aParametersFromString, FWSimFieldContainer aContainer)
      Evaluates the rules !UNZIP_KEYS(${C})! and !UNZIP_VALUES(${C})!: from an array of key/value pairs C = {k1, v1, k2, v2, ...} the keys (even positions) or values (odd positions) are extracted. Double, int and text arrays are supported.
      Parameters:
      aTargetVariable - variable with the rule; its data type decides the array type
      aFieldMapIndizes - names of the source fields, in the order of aSourceArray
      aSourceArray - values of the source fields
      aParametersFromString - parameter names found in the rule; must contain exactly one name
      aContainer - container evaluating the rule (for log messages)
      Returns:
      the extracted array, or null if the rule is invalid (a warning is logged)
    • zipArray

      public static Object zipArray(FWSimVariable<?> aTargetVariable, ArrayList<String> aFieldMapIndizes, Object[] aSourceArray, Set<String> aParametersFromString, FWSimFieldContainer aContainer)
      Evaluates the rule !ZIP(${A},${B})!: the arrays A and B are merged alternately into C = {a1, b1, a2, b2, ...}. Double, int and text arrays are supported. A is the parameter that comes first in the rule.
      Parameters:
      aTargetVariable - variable with the rule; its data type decides the array type
      aFieldMapIndizes - names of the source fields, in the order of aSourceArray
      aSourceArray - values of the source fields
      aParametersFromString - parameter names found in the rule; must contain exactly two names
      aContainer - container evaluating the rule (for log messages)
      Returns:
      the merged array, or null if the rule is invalid (a warning is logged)
    • filterVarMap

      public static HashMap<String, FWSimVariable<?>> filterVarMap(HashMap<String, FWSimVariable<?>> aMap, String aRegexp)
      Filters a map of variables by key.
      Parameters:
      aMap - map to filter
      aRegexp - regular expression the whole key must match
      Returns:
      a new map containing only the entries whose keys match
    • addResourceFiles

      public static File addResourceFiles(String aWorkDir, String aSimulationExperimentPath, String aSolutionFileName, String aProjectFileName) throws MissingSimResourceException
      Packs a simulation experiment into a ZIP file: the solution (folder solution/), the project file if given (folder project/) and the resource files of the experiment.
      Parameters:
      aWorkDir - the work directory
      aSimulationExperimentPath - directory of the simulation experiment; must be located in the work directory
      aSolutionFileName - path of the solution file (*.sol.xml)
      aProjectFileName - path of the project file, or empty for a run without project
      Returns:
      the ZIP file in the temp directory
      Throws:
      MissingSimResourceException - if the experiment is not located in the work directory or a file cannot be packed
    • logInitialInfo

      public static void logInitialInfo(boolean aForce)
      Logs a banner with system information (user, cores, memory, version) at the start of the application. The banner is logged only once unless forced.
      Parameters:
      aForce - if true, the banner is logged even if it was logged before
    • prepareDatabase

      public static PreparedStatement prepareDatabase(String aTableName, HashMap<String, FWSimVariable<?>> aHeaderVariables, String aConnectionPoolDefault, boolean aDropIfExists, boolean aDoCreateTable) throws SQLException
      Prepares a database table for writing variables: optionally drops and (re)creates the table with one column per variable, and returns a prepared INSERT statement with one placeholder per variable. Dots in variable names become double underscores in column names.
      Parameters:
      aTableName - name of the table
      aHeaderVariables - the variables to store, in column order
      aConnectionPoolDefault - name of the connection pool
      aDropIfExists - if true, an existing table is dropped (and then created)
      aDoCreateTable - if true, the table is created
      Returns:
      the prepared INSERT statement
      Throws:
      SQLException - if a database operation fails
    • prepareLimitedSelect

      public static PreparedStatement prepareLimitedSelect(String aTableName, HashMap<String, FWSimVariable<?>> aFieldMap, String aConnectionPoolDefault) throws SQLException
      Prepares a paged SELECT (HSQLDB syntax SELECT LIMIT offset count ...) of the given columns for a project. Parameters: offset, count, project iteration, project id.
      Parameters:
      aTableName - name of the table
      aFieldMap - the variables to select (keys are the column names, dots as double underscores)
      aConnectionPoolDefault - name of the connection pool
      Returns:
      the prepared SELECT statement
      Throws:
      SQLException - if the statement cannot be prepared
    • prepareCountSelect

      public static PreparedStatement prepareCountSelect(String aTableName, String aConnectionPoolDefault) throws SQLException
      Prepares a statement counting the simulations of a project. Parameters: project iteration, project id.
      Parameters:
      aTableName - name of the table
      aConnectionPoolDefault - name of the connection pool
      Returns:
      the prepared COUNT statement
      Throws:
      SQLException - if the statement cannot be prepared
    • generateSimulationID

      public static String generateSimulationID(Integer aProjectIteration, String aProjectID, String aSimulationName, Integer aSimulationNumber)
      Builds a simulation id of the form projectid_II__NAME_NNNNNNN: project id, two-digit iteration, simulation name in upper case and, if given, the seven-digit simulation number.
      Parameters:
      aProjectIteration - iteration number of the project
      aProjectID - project id
      aSimulationName - simulation name
      aSimulationNumber - simulation number, or null
      Returns:
      the simulation id
    • splitProjectDataBySimulationCount

      public static LinkedHashMap<String,String> splitProjectDataBySimulationCount(File aProjectDataFile, String aDivider, int aSplitCount)
      Splits a project data file into about aSplitCount ranges of lines for parallel processing. Lines of the same project (first column) are never split. For an unsorted file a sorted copy is written (see getSortedCopyFile(File)); the ranges then refer to the copy. The application exits if the file cannot be read.
      Parameters:
      aProjectDataFile - CSV file with the project data (first column: project id)
      aDivider - column divider; tab if empty
      aSplitCount - number of parts
      Returns:
      map from line range (e.g. 1-2000) to the project id at the end of the range
    • splitProjectDataBySimulationCount

      public static LinkedHashMap<String,String> splitProjectDataBySimulationCount(File aProjectDataFile, String aDivider, int aSplitNumber, String[][] aReadCSVtoArray)
      Splits already read project data into ranges of about aSplitNumber lines. Lines of the same project (first column) are never split. For an unsorted file a sorted copy is written (see getSortedCopyFile(File)); the ranges then refer to the copy.
      Parameters:
      aProjectDataFile - CSV file with the project data (never changed)
      aDivider - column divider
      aSplitNumber - number of lines per range
      aReadCSVtoArray - content of the file including the header line
      Returns:
      map from line range (e.g. 1-2000) to the project id at the end of the range
    • splitProjectDataByNumberOfCores

      public static List<String> splitProjectDataByNumberOfCores(File aProjectDataFile, String aDivider, int[] aListOfCores)
      Splits a project data file into line ranges for several machines with different numbers of cores; machines with more cores get proportionally more lines. Lines of the same project (first column) are never split. For an unsorted file a sorted copy is written (see getSortedCopyFile(File)); the ranges then refer to the copy. The application exits if the file cannot be read.
      Parameters:
      aProjectDataFile - CSV file with the project data (first column: project id)
      aDivider - column divider; tab if empty
      aListOfCores - number of cores per machine
      Returns:
      the line ranges, e.g. 1-2000
    • getSortedCopyFile

      public static File getSortedCopyFile(File aProjectDataFile)
      Returns the file the sorted copy of an unsorted project data file is written to: <name>_sorted.<extension> next to the original.
      Parameters:
      aProjectDataFile - the project data file
      Returns:
      the file for the sorted copy
    • checkForAction

      public static Boolean checkForAction(LocalDateTime aCurrentDate, FWSimIOAdapter.FREQUENCE aFrequence, JexlRuleScript aJexlScript, FWSimVarMap aVarMap)
      Checks whether an action (e.g. writing an aggregated output) is due on the current date:
      • YEARLY: on the last day of the year,
      • MONTHLY: on the last day of the month,
      • COMPLEX: if the rule evaluates to true,
      • BOOLEAN: if the variable named by the rule is true,
      • END: at the end of the simulation.
      Parameters:
      aCurrentDate - the current model date
      aFrequence - the frequency
      aJexlScript - the rule (for COMPLEX and BOOLEAN)
      aVarMap - var map used to evaluate the rule
      Returns:
      true if the action is due; false for other frequencies