Class FWSimResourceManager

java.lang.Object
net.simplace.sim.io.resources.FWSimResourceManager

public class FWSimResourceManager extends Object
Manages the resources of a session (one instance per session). It creates the resource adapters, transformers and aliases from the resources part of the solution, reads their data into FWSimResourceCaches when a project starts (updateResources(FWSimVarMap)), and copies the current data lines into the variable maps of the simulations in each step (updateVarMap(FWSimVarMap, boolean)). The media specific access (database, CSV, XML, ...) is done by the adapters via their interfaces.
Author:
Andreas Enders
  • Constructor Details

    • FWSimResourceManager

      public FWSimResourceManager()
  • Method Details

    • addResource

      public String addResource(FWSimSession aSession, org.jdom2.Element aResource) throws MissingSimResourceException, DuplicateSimFieldException
      Creates a resource adapter from its element and adds it.
      Parameters:
      aSession - the session
      aResource - the resource element
      Returns:
      the name of the resource
      Throws:
      MissingSimResourceException - if the interface is missing or not supported
      DuplicateSimFieldException - if a resource with the same id already exists
    • addTransform

      public String addTransform(FWSimSession aSession, org.jdom2.Element aTransform) throws MissingSimResourceException, DuplicateSimFieldException
      Creates a transformer from its element and adds it.
      Parameters:
      aSession - the session
      aTransform - the transform element
      Returns:
      the name of the transformer
      Throws:
      MissingSimResourceException - if the transformer cannot be created
      DuplicateSimFieldException - if a resource with the same id already exists
    • addAlias

      public String addAlias(FWSimSession aSession, org.jdom2.Element aAlias) throws MissingSimResourceException, DuplicateSimFieldException
      Creates an alias from its element and adds it.
      Parameters:
      aSession - the session
      aAlias - the alias element
      Returns:
      the name of the alias
      Throws:
      MissingSimResourceException - if the original resource is not given
      DuplicateSimFieldException - if a resource with the same id already exists
    • updateResources

      public boolean updateResources(FWSimVarMap aVarMap) throws MissingSimResourceException
      Reads the data of all resources and transformers into their caches; called when a project starts. Transformers are only recalculated if their source data or inputs changed; the resource PROJECT_DATA is skipped. If a resource delivers no data, its previous cache is kept. In check level ONLY the checker checks the resources instead. Finally the aliases are connected to the caches of their original resources.
      Parameters:
      aVarMap - variable map of the first simulation of the project
      Returns:
      always true
      Throws:
      MissingSimResourceException - if a resource or the original resource of an alias is missing
    • getResourceCreator

      public FWSimInputAdapter getResourceCreator(String aName)
      Parameters:
      aName - name of the resource, transformer or alias
      Returns:
      the FWSimResourceAdapter, FWSimResourceTransformer or FWSimResourceAlias, or null if unknown
    • addResourceCreator

      public void addResourceCreator(FWSimInputAdapter aAdapter)
      Adds an already created resource adapter, transformer or alias; an existing one with the same name is replaced.
      Parameters:
      aAdapter - the adapter to add
    • updateInterface

      public boolean updateInterface(FWSimInterface<?> aInterface, FWSimVarMap aVarMap)
      Replaces every resource adapter that uses an interface with the same name by a copy using the new interface (see FWSimResourceAdapter.updateAdapter(FWSimResourceAdapter, FWSimInterface)). Transformers and aliases are not replaced.
      Parameters:
      aInterface - the new interface
      aVarMap - not used
      Returns:
      true if at least one adapter was replaced
    • createVariables

      public HashMap<String, FWSimVariable<?>> createVariables()
      Creates the variables of all adapters (for transformers their header variables).
      Returns:
      all resource variables by name
    • getResourceCache

      public FWSimResourceCache getResourceCache(String aSourceName)
      Parameters:
      aSourceName - name of the resource or transformer
      Returns:
      its cache, or null if no data has been read yet
    • updateVarMap

      public void updateVarMap(FWSimVarMap aVarMap, boolean aForceUpdate)
      Copies the current data lines of all caches (and aliases) into the variable map of a simulation, for each cache only if its frequency requires an update in this step (see FWSimResourceCache.shouldUpdate(FWSimVarMap)). The cache PROJECT_DATA is skipped; it is only set at the start of a simulation. Aliases whose original resource has no cache are skipped (the missing resource was already reported by updateResources(FWSimVarMap)).
      Parameters:
      aVarMap - variable map of the simulation
      aForceUpdate - true to update all caches regardless of their frequency (e.g. at the start)
    • getResourceAdapters

      public Collection<FWSimInputAdapter> getResourceAdapters()
      Returns:
      all resource adapters, transformers and aliases (the live collection)
    • referenceKeys

      public static Map<String,String> referenceKeys(LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, FWSimSolution aSolution)
      Lets the user select the key variables of a new resource in the GUI: in a loop a model variable (source) and a header variable (target) are chosen as key pair until the user cancels or fewer than two fields are left.
      Parameters:
      aHeaderVariables - header variables of the resource
      aSolution - the solution providing the model variables
      Returns:
      the selected key pairs (model variable → header variable); empty if the solution is null
    • reset

      public void reset() throws Exception
      Finalizes all resource adapters, transformers and aliases (e.g. closes their interfaces) at the end of a session.
      Throws:
      Exception - if finalizing an adapter fails