Class FWSimSolution

java.lang.Object
net.simplace.sim.FWSimSolution
All Implemented Interfaces:
FWObserver, FWSimFieldContainer

public final class FWSimSolution extends Object implements FWSimFieldContainer, FWObserver
A solution is the configuration of a simulation read from a solution file (.sol.xml, grammar SimSolution.dtd). It contains:
  • the interfaces for reading and writing external data (interfaces)
  • the solution variables (variables, elements var and dyn)
  • the resources, transformers and aliases (resources)
  • the template model with its SimComponents and links (simmodel)
  • the outputs (outputs)
Older solution versions are migrated on creation (see FWSimMigrator); init() then sets up the parts in this order. Each simulation runs on a clone of the template model.

Values are initialized hierarchically: solution variables overwrite defaults, project data overwrite solution variables, and links from resources overwrite constants in the first simulation step. As FWSimFieldContainer the solution provides its variables (named Initial_Values).

Author:
Andreas Enders
  • Field Details

    • CONNECTION_POOL_DEFAULT

      public String CONNECTION_POOL_DEFAULT
      name of the default database connection pool; after init() <pool id>_<session id>
    • CHECKER_CLASS

      public String CHECKER_CLASS
      The individual class the simulation is checked with while running
  • Constructor Details

    • FWSimSolution

      public FWSimSolution(FWSimSession aSession, FWSimSolution aFwSimSolution)
      Copy constructor for a new session: uses the same solution element, a clone of the variable map for the session and a clone of the template model. init() has to be called afterwards; the model is not created again.
      Parameters:
      aSession - the new session
      aFwSimSolution - the solution to copy
    • FWSimSolution

      public FWSimSolution(org.jdom2.Element aSimSolutionElement, FWSimVarMap aVarMap, String aSolutionName)
      Creates a solution from its XML element. Solutions older than FWConstants.SOLUTION_VERSION are migrated in place. If the engine has no check level yet, it is taken from the attribute check (default LAZY); if the check level is CUSTOM, the checker class is taken from the attribute class. The description is read. The parts of the solution are set up by init().
      Parameters:
      aSimSolutionElement - root element of the solution
      aVarMap - variable map with the system parameters; becomes the template variable map
      aSolutionName - name of the solution (the solution file name)
  • Method Details

    • init

      public void init()
      Sets up the parts of the solution in this order: interfaces, solution variables (the element simulation of versions before 2.0 is renamed to variables), resources, template model (only if not yet present) with the skip rule, and outputs. Errors in variables and model links are logged.
    • getInterface

      public FWSimInterface<?> getInterface(String aInterface)
      Parameters:
      aInterface - name of the interface
      Returns:
      the interface, or null if unknown
    • getInterfaces

      public Collection<FWSimInterface<?>> getInterfaces()
      Returns:
      all interfaces of the solution (read only)
    • setInterface

      public void setInterface(FWSimInterface<?> aInterface)
      Sets an interface. If an interface with the same name exists, it is replaced in the resource adapters or, if no resource uses it, in the output adapters.
      Parameters:
      aInterface - the interface
    • getModelTemplate

      public FWSimModel getModelTemplate()
      Returns:
      the template model
    • getResourceAdapters

      public Collection<FWSimInputAdapter> getResourceAdapters()
      Returns:
      a collection of all resource adapters including transformer adapters
    • getOutputAdapters

      public ArrayList<FWSimOutputAdapter> getOutputAdapters()
      Returns:
      the output adapters
    • createVariables

      public HashMap<String, FWSimVariable<?>> createVariables()
      Returns the solution variables (nothing is created).
      Specified by:
      createVariables in interface FWSimFieldContainer
      Returns:
      the variables, keyed by name
      See Also:
    • addVariable

      public void addVariable(FWSimVariable<?> aVariable)
      Adds a variable to the solution variables only (not to the variable map) - use with care.
      Specified by:
      addVariable in interface FWSimFieldContainer
      Parameters:
      aVariable - the variable to add
      See Also:
    • getVariable

      public FWSimVariable<?> getVariable(String aID)
      Returns a solution variable, or null if unknown.
      Specified by:
      getVariable in interface FWSimFieldContainer
      Parameters:
      aID - id of the variable
      Returns:
      the variable
      See Also:
    • removeVariable

      public void removeVariable(String aID)
      Removes a variable from the solution variables only (not from the variable map) - use with care.
      Specified by:
      removeVariable in interface FWSimFieldContainer
      Parameters:
      aID - id of the variable
      See Also:
    • getName

      public String getName()
      Always returns Initial_Values, the name of the solution as field container (e.g. in the graph panel and as name of the description file).
      Specified by:
      getName in interface FWSimFieldContainer
      Returns:
      the name of the container
      See Also:
    • getOrderNumber

      public int getOrderNumber()
      Specified by:
      getOrderNumber in interface FWSimFieldContainer
      Returns:
      position of this container in the order of execution
      See Also:
    • getAuthor

      public String getAuthor()
      Returns:
      the author from the description, or null
    • getDescription

      public String getDescription()
      Returns:
      the description text, or null
    • getTitle

      public String getTitle()
      Returns:
      the title from the description, or null
    • getSkipSimulation

      public FWSimVariable<Boolean> getSkipSimulation()
      Returns:
      the variable with the skip rule, or null if the model has no skip attribute
    • getOutputVariables

      public Collection<FWSimVariable<?>> getOutputVariables()
      Returns all solution variables.
      Specified by:
      getOutputVariables in interface FWSimFieldContainer
      Returns:
      the output variables of the field map
      See Also:
    • getInputVariables

      public Collection<FWSimVariable<?>> getInputVariables()
      Returns an empty list; the solution has no input variables.
      Specified by:
      getInputVariables in interface FWSimFieldContainer
      Returns:
      the input variables of the field map
      See Also:
    • getFieldMap

      public HashMap<String, FWSimVariable<?>> getFieldMap()
      Returns the solution variables (the live map).
      Specified by:
      getFieldMap in interface FWSimFieldContainer
      Returns:
      the whole field map as created in FWSimFieldContainer.createVariables()
      See Also:
    • toString

      public String toString()
      Returns the name, see getName().
      Overrides:
      toString in class Object
      See Also:
    • getContentType

      public FWSimVariable.CONTENT_TYPE getContentType()
      Returns var, the content type of solution variables.
      Specified by:
      getContentType in interface FWSimFieldContainer
      Returns:
      the content type of the variables of this container (e.g. sys, mgm)
      See Also:
    • setCheckerClassName

      public void setCheckerClassName(String aClassNameString)
      Parameters:
      aClassNameString - name of the checker class used in check level CUSTOM
    • getCheckerClassName

      public String getCheckerClassName()
      Returns:
      name of the checker class used in check level CUSTOM, or null
    • toXML

      public org.jdom2.Element toXML()
      Rebuilds the solution element from the current state: the content of description, variables, interfaces, resources, simmodel and outputs is replaced by the XML of the description text, the solution variables (var and dyn), interfaces, resource adapters, SimComponents and output adapters. Missing elements are created in the order of the DTD; parts without XML are left out.
      Specified by:
      toXML in interface FWSimFieldContainer
      Returns:
      the solution element (changed in place)
    • createSaveBaseline

      public void createSaveBaseline()
      Remembers the XML of all containers (variables, interfaces, resources, components, outputs) as they are now, e.g. after the solution was opened for editing. toSavedDocument() then writes only the containers whose XML has changed since.
    • hasSaveBaseline

      public boolean hasSaveBaseline()
      Returns:
      true if createSaveBaseline() was called
    • hasUnsavedChanges

      public boolean hasUnsavedChanges()
      Returns:
      true if a container was changed, added or removed since createSaveBaseline(), i.e. the solution has to be saved; false without baseline
    • toSavedDocument

      public org.jdom2.Document toSavedDocument()
      Returns the solution document for saving after editing (#62): the file as read, in which only the containers are replaced whose XML has changed since createSaveBaseline() (in place), containers removed since are deleted and new ones are appended to their section. Everything else - unchanged containers, comments, formatting, parts unknown to the object model - stays as in the file. (Writing the whole solution with toXML() loses information, e.g. the interface of transformers.) Without baseline, toXML() is used.
      Returns:
      the document to write
    • setInputSource

      public void setInputSource(FWSimComponent aComponent, String aInputId, String aSource) throws SimLinkInvalidException
      Links an input of a SimComponent to a source variable, replaces or removes its link (e.g. from the input dialog of the GUI, Redmine #62). The source is a name of the template (solution variable, resource or component variable, e.g. CURRENT.DOY).
      Parameters:
      aComponent - the component
      aInputId - id of the input (without component name)
      aSource - name of the source variable, or null/empty to remove the link
      Throws:
      SimLinkInvalidException - if the input or the source does not exist or the data types differ (the old link is kept then)
    • addLink

      public void addLink(SimVarLink aSimVarLink) throws SimLinkInvalidException
      Adds a link created in the graph panel: links the destination variable to the source variable in the template variable map and registers the source as input of the destination SimComponent.
      Parameters:
      aSimVarLink - the link; its destination must belong to a FWSimComponent
      Throws:
      SimLinkInvalidException - if the link is invalid
    • removeLink

      public void removeLink(SimVarLink aSimVarLink)
      Removes a link created in the graph panel: from the header of an output adapter, or from the template variable map and the inputs of the destination SimComponent.
      Parameters:
      aSimVarLink - the link
    • removeSimFieldContainer

      public void removeSimFieldContainer(FWSimFieldContainer aFieldContainer, Collection<ArrayList<SimFieldLink>> aSimFieldLinks)
      Removes a SimComponent, output adapter or resource adapter/transformer from the solution, including all links from and to it.
      Parameters:
      aFieldContainer - the field container to remove
      aSimFieldLinks - the links of the graph panel (grouped by field container)
    • getFrequence

      public FWSimIOAdapter.FREQUENCE getFrequence()
      Returns FWSimIOAdapter.FREQUENCE.NON; the solution is not processed during the simulation.
      Specified by:
      getFrequence in interface FWSimFieldContainer
      Returns:
      the frequency in which this container is called, or null if not applicable
      See Also:
    • getCreateFormXML

      public org.jdom2.Element getCreateFormXML()
      Returns null; a solution cannot be created by a form.
      Specified by:
      getCreateFormXML in interface FWSimFieldContainer
      Returns:
      the XML form definition used by the GUI to create this container
      See Also:
    • getEditFormXML

      public org.jdom2.Element getEditFormXML(boolean aEnabled)
      Returns a form with a choice of the solution variables to edit. aEnabled is not used.
      Specified by:
      getEditFormXML in interface FWSimFieldContainer
      Parameters:
      aEnabled - if false, the form fields are read-only
      Returns:
      the XML form definition used by the GUI to edit the settings of this container
      See Also:
    • getDynamicForm

      public org.jdom2.Element getDynamicForm(FWObservable aObservable)
      Creates the form for the dynamic solution variables (dyn), sorted by their order (or by name if an order is missing). Values already present in the observable are set to the variables first. The solution registers itself as observer, so changes in the form are written to the variables (see update(FWObservable, Object)).
      Parameters:
      aObservable - the form data
      Returns:
      the form element, or null if there are no dynamic variables
    • getDynamicForm

      public org.jdom2.Element getDynamicForm(FWObservable aObservable, Set<String> aSetByProject)
      Like getDynamicForm(FWObservable), but the variables set by a project (project data columns, calibration/sensitivity targets) are shown disabled with the note "set by project": a project overwrites them, so a change in the form would have no effect.
      Parameters:
      aObservable - the form data
      aSetByProject - names of the variables the project sets
      Returns:
      the form element, or null if there is no dynamic variable left to change
    • update

      public void update(FWObservable aFormObservable, Object aArg)
      Writes a changed form value to the variable of the same name in the template variable map.
      Specified by:
      update in interface FWObserver
      Parameters:
      aFormObservable - the form data
      aArg - name of the changed field
    • showDescription

      public void showDescription()
      Creates an HTML overview of the solution (description, variables, resources, SimComponents with links to their documentation on simplace.net, outputs), writes it to a temporary file and opens it in the external browser. Errors are logged. The description is converted with createHTML(String) (TracWiki parts); the description of the solution itself is not changed.
    • getVarMap

      public FWSimVarMap getVarMap()
      Returns the template variable map.
      Specified by:
      getVarMap in interface FWSimFieldContainer
      Returns:
      the var map this container belongs to
      See Also:
    • getSolutionElement

      public org.jdom2.Element getSolutionElement()
      Returns:
      the root element of the solution
    • checkCondition

      public void checkCondition(Boolean aCheckResult, String aMessage)
      Passes the check to the checker of the session.
      Specified by:
      checkCondition in interface FWSimFieldContainer
      Parameters:
      aCheckResult - result of the check; false means the condition is violated
      aMessage - message describing the condition
      See Also:
    • isConditionCheck

      public boolean isConditionCheck()
      Asks the checker of the session.
      Specified by:
      isConditionCheck in interface FWSimFieldContainer
      Returns:
      true if conditions are checked with the current check level of the session
      See Also: