Class Configuration

java.lang.Object
net.ssehub.easy.varModel.confModel.Configuration
All Implemented Interfaces:
Iterable<IDecisionVariable>, net.ssehub.easy.basics.modelManagement.IModelListener<Project>, IConfiguration, IConfigurationElement, IConfigurationVisitable, IProjectListener

Represents a configuration, i.e. a set of decision variables.
Author:
Holger Eichelberger, El-Sharkawy, Marcel Lueder
  • Field Details

  • Constructor Details

    • Configuration

      public Configuration(Project project)
      Creates a new configuration for the given project.
      Parameters:
      project - The project, where this configuration belongs to. This project should already be registered at the VarModel
      See Also:
      • #updateModel(net.ssehub.easy.basics.modelManagement.IModel, java.net.URI)
    • Configuration

      public Configuration(Project project, IAssignmentState resolutionState)
      Creates a new configuration for the given project with explicit resolution state. This project should already be registered at the VarModel #updateModel(net.ssehub.easy.basics.modelManagement.IModel, java.net.URI).
      Parameters:
      project - The project, where this configuration belongs to.
      resolutionState - the resolution state for the assignment resolver
    • Configuration

      public Configuration(Configuration configuration)
      Copies a configuration by taking over its defined values and assignment states.
      Parameters:
      configuration - the configuration to be copied
    • Configuration

      public Configuration(Project project, boolean assignValues)
      Alternative constructor to avoid initial value assignment by AssignmentResolver. If activated, the AssignmentResolver will work with AssignmentState.ASSIGNED.
      Parameters:
      project - to get Configuration from.
      assignValues - Decision if values should be assigned by AssignmentResolver.
    • Configuration

      public Configuration(Project project, boolean assignValues, IAssignmentState resolutionState)
      Alternative constructor to avoid initial value assignment by AssignmentResolver.
      Parameters:
      project - to get Configuration from.
      assignValues - Decision if values should be assigned by AssignmentResolver.
      resolutionState - the resolution state for the assignment resolver
  • Method Details

    • wasCreated

      public boolean wasCreated()
      Description copied from interface: IConfigurationElement
      Returns whether the variable value was explicitly created.
      Specified by:
      wasCreated in interface IConfigurationElement
      Returns:
      true for created, false else
    • getResolutionState

      public IAssignmentState getResolutionState()
      Returns the assignment state to be used when assigning values in the configuration.
      Returns:
      the resolution state
    • getProject

      public Project getProject()
      Returns the related project.
      Returns:
      the related project
    • isApproximatelyFrozen

      public boolean isApproximatelyFrozen()
      Return whether this configuration is exactly or approximative (heuristicall) frozen.
      Returns:
      false for exact freezing, true for approximative freezing
    • getConfiguredDecisions

      Map<AbstractVariable,IDecisionVariable> getConfiguredDecisions()
      Returns the saved (AbstractVariable, IDecisionVariable) mapping.
      Returns:
      A flat copy of the stored (AbstractVariable, IDecisionVariable) mappings.
    • register

      public void register(IConfigurationChangeListener listener)
      Registers a new listener.
      Parameters:
      listener - A Listener which should be informed in case of changes.
    • unregister

      public boolean unregister(IConfigurationChangeListener listener)
      Unregisters an listener.
      Parameters:
      listener - The listener which should be unregistered.
      Returns:
      true if this configuration contained the specified listener
    • getModelInfo

      private net.ssehub.easy.basics.modelManagement.ModelInfo<Project> getModelInfo()
      Returns the related model information instance.
      Returns:
      the model information instance
    • init

      private void init()
      This method creates the list of IDecisionVariables with initial value settings based on the given project.
    • createVariables

      private void createVariables()
      This method looks for all DecisionVariableDeclarations inside the related projects, creates DecisionVariableDeclarations and adds them to this configuration. While creating these DecisionVariableDeclarations this method also tries assign the specified default values to the newly created DecisionVariableDeclarations.
    • addVariables

      private void addVariables(List<AbstractVariable> topLevelDeclarations, boolean visible)
      Creates IDecisionVariables for all given AbstractVariables and adds them to the configuration. Part of the createVariables() method and should only be called from this method.
      Parameters:
      topLevelDeclarations - The variables to add.
      visible - indicates whether the variable is visible to the user:
      • true: The variable is visible by the user and can be displayed in the GUI and so on.
      • false: The variable is not visible by the user but must be considered in reasoning, instantiation, and so on.
    • createDecision

      public IDecisionVariable createDecision(AbstractVariable decl) throws ConfigurationException
      Creates a visible decision in this configuration. The declaration must be on top-level and not created before.
      Parameters:
      decl - the declaration to create the decision for
      Returns:
      the created decision variable, may be null if the conditions are not met
      Throws:
      ConfigurationException - in case that creating the variable failed
    • createDecision

      private IDecisionVariable createDecision(AbstractVariable decl, boolean visible) throws ConfigurationException
      Creates a decision in this configuration.
      Parameters:
      decl - the declaration to create the decision for
      visible - whether the variable shall be visible
      Returns:
      the created decision variable
      Throws:
      ConfigurationException - in case that creating the variable failed
    • freezeValues

      public void freezeValues(Project project, FilterType filter)
      Sets AssignmentState.FROZEN state to already frozen variables.
      Parameters:
      project - the project to be frozen
      filter - the filter type
    • freezeValues

      private void freezeValues(AbstractVariable var, IFreezeSelector selector)
      Sets AssignmentState.FROZEN state to the given variables var.
      Parameters:
      var - the variable to be frozen
      selector - the freeze selector
    • refresh

      public void refresh()
      This method calls atm only the init() Method.
    • getQueryCache

      public QueryCache getQueryCache()
      Returns the QueryCache, which should usually be used for query on this Configuration.
      Returns:
      The cache for this Configuration.
    • addDecision

      private void addDecision(IDecisionVariable variable)
      Adda a decision.
      Parameters:
      variable - the decision to be added
    • getDecisionCount

      public int getDecisionCount()
      Returns the number of decisions.
      Returns:
      the number of decisions
    • getDecision

      public IDecisionVariable getDecision(AbstractVariable declaration)
      Description copied from interface: IConfiguration
      Returns the specified IDecisionVariable.
      Specified by:
      getDecision in interface IConfiguration
      Parameters:
      declaration - The declaration for which the configuration entity should be returned.
      Returns:
      The IDecisionVariable for the given declaration
    • renameVariable

      public boolean renameVariable(AbstractVariable declaration, String newName)
      Renames a variable. Works based on declaration or its name so that already renamed variables are considered.
      Parameters:
      declaration - the variable defining the the old name
      newName - the new name
      Returns:
      true if the element was found and renamed, false else
      See Also:
    • renameVariable

      public boolean renameVariable(String oldName, String newName)
      Renames a variable. Works based on declaration or its name so that already renamed variables are considered.
      Parameters:
      oldName - the old name
      newName - the new name
      Returns:
      true if the element was found and renamed, false else
    • iterator

      public Iterator<IDecisionVariable> iterator()
      Returns an iterator for iterating over all stored IDecisionVariables.
      Specified by:
      iterator in interface Iterable<IDecisionVariable>
      Returns:
      an Iterator over the IDecisionVariable in this configuration.
    • clear

      public void clear()
      Clears all decisions.
    • removeDecision

      public boolean removeDecision(IDecisionVariable variable)
      Remove a specific decision.
      Parameters:
      variable - the decision to be removed
      Returns:
      true if this operation was successful, false else
    • removeFromAllInstances

      private void removeFromAllInstances(IDecisionVariable variable)
      Removes variable from all instances.
      Parameters:
      variable - the variable to be removed
    • getName

      public String getName()
      Returns the name of the configuration.
      Returns:
      the name of the configuration
    • accept

      public void accept(IConfigurationVisitor visitor)
      Description copied from interface: IConfigurationVisitable
      Accepts and calls the given visitor.
      Specified by:
      accept in interface IConfigurationVisitable
      Parameters:
      visitor - the visitor
    • notifyReplaced

      public void notifyReplaced(Project oldProject, Project newProject)
      Specified by:
      notifyReplaced in interface net.ssehub.easy.basics.modelManagement.IModelListener<Project>
    • toProject

      public Project toProject(boolean ownProject) throws ConfigurationException
      Turns this configuration into a project, e.g. for writing.
      Parameters:
      ownProject - return an own project (true) or add the configuration to project (false)
      Returns:
      the project or null if not implemented (default)
      Throws:
      ConfigurationException - in case of any configuration errors
    • toProject

      public Project toProject(boolean ownProject, boolean onlyUserInput) throws ConfigurationException
      Turns this configuration into a project, e.g. for writing.
      Parameters:
      ownProject - return an own project (true) or add the configuration to project (false)
      onlyUserInput - Specifies whether only user Input should be stored:
      Returns:
      the project or null if not implemented (default)
      Throws:
      ConfigurationException - in case of any configuration errors
    • getConfiguration

      public final Configuration getConfiguration()
      Description copied from interface: IConfigurationElement
      Returns the topLevel parent.
      Specified by:
      getConfiguration in interface IConfigurationElement
      Returns:
      should be the configuration
    • getParent

      public IConfigurationElement getParent()
      Description copied from interface: IConfigurationElement
      Returns the element this element is part of.
      Specified by:
      getParent in interface IConfigurationElement
      Returns:
      the parent element (may be null in case of the top-level configuration)
    • variableChanged

      void variableChanged(IDecisionVariable var, Value oldValue)
      Notifies all registered IConfigurationChangeListeners that a IDecisionVariable changed.
      Parameters:
      var - the IDecisionVariable which changed. This IDecisionVariable should be part of this configuration (No validation).
      oldValue - the old value before the change, var then contains the new value
    • allInstancesVariableChanged

      private void allInstancesVariableChanged(IDecisionVariable var)
      Changes the all instance cache if var or one of its nested elements changed. Precondition is that allInstances was already built up.
      Parameters:
      var - the changed variable
    • variableChangedState

      void variableChangedState(IDecisionVariable var)
      Notifies all registered IConfigurationChangeListeners that a IDecisionVariable has changed its state.
      Parameters:
      var - the IDecisionVariable which changed. This IDecisionVariable should be part of this configuration (No validation).
    • addAttributeDecision

      protected void addAttributeDecision(Attribute attribute, Value value, IAssignmentState state) throws ConfigurationException
      Adds an attribute decision to this configuration. This method is a specific functionality for user interaction. Subclass this class and make it accessible if needed, otherwise it may break the value protection provided by this class.
      Parameters:
      attribute - the attribute for which the decision shall be created
      value - the value of the attribute (may be null)
      state - the assignment state
      Throws:
      ConfigurationException - in case of type conflicts
    • isNested

      public boolean isNested()
      Description copied from interface: IConfigurationElement
      Returns whether this decision variable is nested. Being member of a Configuration does not imply isNested == true.
      Specified by:
      isNested in interface IConfigurationElement
      Returns:
      true if it is nested in another decision variable, false else
    • getState

      public IAssignmentState getState()
      Description copied from interface: IConfigurationElement
      Returns the assignment state of this variable.
      Specified by:
      getState in interface IConfigurationElement
      Returns:
      the assignment state
    • setValue

      public void setValue(Value value, IAssignmentState state, IConfigurationElement nested) throws ConfigurationException
      Description copied from interface: IConfigurationElement
      Changes the value of a nested variable.
      Specified by:
      setValue in interface IConfigurationElement
      Parameters:
      value - the new value
      state - the new assignment state
      nested - the nested variable to be considered in the parent
      Throws:
      ConfigurationException - in case of type incompatibilities
    • getDeclaration

      public AbstractVariable getDeclaration()
      Returns the declaration of the variable.
      Specified by:
      getDeclaration in interface IConfigurationElement
      Returns:
      the declaration of the decision variable
    • freeze

      public void freeze(IFreezeSelector selector)
      Freezes on the whole configuration.
      Specified by:
      freeze in interface IConfigurationElement
      Parameters:
      selector - the selector deciding what to freeze
    • unfreeze

      public void unfreeze(IAssignmentState state)
      Unfreezes the whole configuration (including nested variables).
      Specified by:
      unfreeze in interface IConfigurationElement
      Parameters:
      state - the target state after unfreezing (must not be AssignmentState.FROZEN)
    • unfreezeVariable

      private void unfreezeVariable(IDecisionVariable variable, IAssignmentState state)
      Unfreezes a variable and all its nested elements (if such exist).
      Parameters:
      variable - IDecisionVariable to unfreeze.
      state - state the target state after unfreezing (must not be AssignmentState.FROZEN)
    • freeze

      public void freeze(String nestedElement)
      Freezes a single variable (must be a top layer element).
      Specified by:
      freeze in interface IConfigurationElement
      Parameters:
      nestedElement - The name of the top layer variable.
    • dereference

      public static IDecisionVariable dereference(IDecisionVariable var)
      Dereferences a variable.
      Parameters:
      var - the variable to be dereferenced (may be null)
      Returns:
      the dereferenced variable (null if var was null)
    • findInParents

      public static IDecisionVariable findInParents(IDecisionVariable var, String name)
      Finds a variable called name in the decision variable parents of var, starting at the direct parent of var, thus, returning the variable with name of the closest enclosing scope.
      Parameters:
      var - the variable to start at
      name - the name of the variable to find
      Returns:
      the found variable or null if ther is none
    • getDecision

      public IDecisionVariable getDecision(String varName, boolean dereference) throws ModelQueryException
      Returns the decision variable for a potentially nested/qualified name from this configuration.
      Parameters:
      varName - the name of the variable
      dereference - dereference intermediary reference variables
      Returns:
      the variable or null if none exists
      Throws:
      ModelQueryException - in case that querying the model fails
    • getDecision

      private IDecisionVariable getDecision(IDecisionVariable var, String varName, boolean dereference) throws ModelQueryException
      Returns the decision variable for a potentially nested/qualified name from this configuration using with var as current search context.
      Parameters:
      var - the variable to search within (null for a top-level call)
      varName - the name of the variable
      dereference - dereference intermediary reference variables
      Returns:
      the variable or null if none exists
      Throws:
      ModelQueryException - in case that querying the model fails
    • getAllInstances

      public Value getAllInstances(IDatatype type)
      Returns all instances of the given type and sub-types.
      Specified by:
      getAllInstances in interface IConfiguration
      Parameters:
      type - the type to look for
      Returns:
      all instances of var, may be null if the instances cannot be retrieved, e.g., in case of an integer variable
    • collectAllInstances

      private void collectAllInstances(IDecisionVariable var, IDatatype type, Map<IDatatype,Reference> referenceTypes, Map<IDecisionVariable,ReferenceValue> instances)
      Collects all instances of type starting at var and stores results into referenceTypes and instances.
      Parameters:
      var - the variable to collect the instances from
      type - the type to collect
      referenceTypes - the reference types for data types (to be modified as a side effect)
      instances - the actual instances of type (to be modified as a side effect)
    • mapVariable

      public static IDecisionVariable mapVariable(IDecisionVariable var, Configuration cfg)
      Maps a variable to its configuration following nested elements up and down.
      Parameters:
      var - the variable to be mapped
      cfg - the configuration containing the top-level variables
      Returns:
      the mapped variable, null if there is no mapping
    • getInstanceName

      public static String getInstanceName(IDecisionVariable var)
      Returns the instance name of a decision variable. This name is composed from the names of the given variable and its parent variables. Please note that the instance name is typically different from the qualified name of the declaration, which, in case of compound slots, leads to the variable in the compound definition. The result is unqualified regarding the top-level variable.
      Parameters:
      var - the variable to return the name for (may be null)
      Returns:
      the instance name (may be empty if var == null
    • getInstanceName

      public static String getInstanceName(IDecisionVariable var, boolean qualified)
      Returns the instance name of a decision variable. This name is composed from the names of the given variable and its parent variables. Please note that the instance name is typically different from the qualified name of the declaration, which, in case of compound slots, leads to the variable in the compound definition.
      Parameters:
      var - the variable to return the name for (may be null)
      qualified - whether the name of the top-level variable shall be qualified
      Returns:
      the instance name (may be empty if var == null
    • equalsByInstanceName

      public static boolean equalsByInstanceName(IDecisionVariable var1, IDecisionVariable var2)
      Returns whether two decision variables are equal via their instance name. Please note that this method is faster and less resource consuming than comparing the respective results of getInstanceName(IDecisionVariable) as it compares the individual names rather than composing the full name and performing the comparison then.
      Parameters:
      var1 - the first variable to be compared (may be null)
      var2 - the second variable to be compared (may be null)
      Returns:
      true if the variables are equal by their instance name, false if they are not equal, in particular if one is null
    • printConfig

      public static void printConfig(PrintStream out, Configuration cfg)
      Prints a configuration to out. [debugging]
      Parameters:
      out - the output stream
      cfg - the configuration to print
    • printVariable

      private static void printVariable(PrintStream out, IDecisionVariable var, String indent)
      Prints a variable to out. [debugging]
      Parameters:
      out - the output stream
      var - the variable to print
      indent - the indentation
    • removeDerivedValues

      public void removeDerivedValues()
      Removes all by a reasoner propagated values from the configuration to clean it up.
    • prune

      public void prune()
      Creates a copy of the underlying Project and removes elements which are not needed for a runtime reasoning, e.g., constraints containing only frozen variables or comments.
      Attention: This method creates a modified, shallow copy of the visited project. Thus, the original project becomes invalid through this visitation. This visitor should only be used if the original is no longer needed, e.g., for performance tweaks in a automated setup which does not save any data.
    • shareQueryCacheWith

      public void shareQueryCacheWith(Configuration configToShare)
      Shares the cache of this Configuration with configToShare. This is useful if configToShare is a temporary Configuration / projection and can reuse queries based this Configuration.
      Parameters:
      configToShare - A temporary configuration which may reuse the cache from this config.
    • getNestedElement

      public static IDecisionVariable getNestedElement(IDecisionVariable base, String name)
      Returns an element for base specified by its name. This method is not recursive!
      Parameters:
      base - the base variable to search on (may be null)
      name - the name of the nested element
      Returns:
      the nested element (may be null if not found or base is null)
    • getTopLevelDecision

      public static IDecisionVariable getTopLevelDecision(IDecisionVariable var)
      Returns the top-level decision variable of var.
      Parameters:
      var - the variable
      Returns:
      the top-level decision variable, eventually var, null if there is none
    • dereference

      public static Value dereference(IConfiguration conf, Value value)
      Dereferences a value.
      Parameters:
      conf - the configuration access
      value - the value to be dereferenced
      Returns:
      the dereferenced value