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
public class Configuration
extends Object
implements IConfigurationVisitable, IProjectListener, Iterable<IDecisionVariable>, IConfigurationElement, IConfiguration
Represents a configuration, i.e. a set of decision variables.
- Author:
- Holger Eichelberger, El-Sharkawy, Marcel Lueder
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Map<IDatatype, Map<IDecisionVariable, ReferenceValue>> private booleanprivate booleanprivate QueryCache(package private) static final ConfigurationInitializerRegistry.IConfigurationInitializerDefines the default initializer for configurations.private List<IConfigurationChangeListener> private ProjectThe origin project, where this configuration belongs to.private IAssignmentState -
Constructor Summary
ConstructorsConstructorDescriptionConfiguration(Configuration configuration) Copies aconfigurationby taking over its defined values and assignment states.Configuration(Project project) Creates a new configuration for the given project.Configuration(Project project, boolean assignValues) Alternative constructor to avoid initial value assignment byAssignmentResolver.Configuration(Project project, boolean assignValues, IAssignmentState resolutionState) Alternative constructor to avoid initial value assignment byAssignmentResolver.Configuration(Project project, IAssignmentState resolutionState) Creates a new configuration for the given project with explicit resolution state. -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(IConfigurationVisitor visitor) Accepts and calls the given visitor.protected voidaddAttributeDecision(Attribute attribute, Value value, IAssignmentState state) Adds an attribute decision to this configuration.private voidaddDecision(IDecisionVariable variable) Adda a decision.private voidaddVariables(List<AbstractVariable> topLevelDeclarations, boolean visible) CreatesIDecisionVariables for all givenAbstractVariables and adds them to the configuration.private voidChanges the all instance cache ifvaror one of its nested elements changed.voidclear()Clears all decisions.private voidcollectAllInstances(IDecisionVariable var, IDatatype type, Map<IDatatype, Reference> referenceTypes, Map<IDecisionVariable, ReferenceValue> instances) Collects all instances oftypestarting atvarand stores results intoreferenceTypesandinstances.Creates a visible decision in this configuration.private IDecisionVariablecreateDecision(AbstractVariable decl, boolean visible) Creates a decision in this configuration.private voidThis method looks for allDecisionVariableDeclarations inside the related projects, createsDecisionVariableDeclarations and adds them to this configuration.static Valuedereference(IConfiguration conf, Value value) Dereferences a value.static IDecisionVariableDereferences a variable.static booleanequalsByInstanceName(IDecisionVariable var1, IDecisionVariable var2) Returns whether two decision variables are equal via their instance name.static IDecisionVariablefindInParents(IDecisionVariable var, String name) Finds a variable callednamein the decision variable parents ofvar, starting at the direct parent ofvar, thus, returning the variable withnameof the closest enclosing scope.voidFreezes a single variable (must be a top layer element).voidfreeze(IFreezeSelector selector) Freezes on the whole configuration.private voidfreezeValues(AbstractVariable var, IFreezeSelector selector) SetsAssignmentState.FROZENstate to the given variablesvar.voidfreezeValues(Project project, FilterType filter) SetsAssignmentState.FROZENstate to already frozen variables.getAllInstances(IDatatype type) Returns all instances of the given type and sub-types.final ConfigurationReturns the topLevel parent.(package private) Map<AbstractVariable, IDecisionVariable> Returns the saved (AbstractVariable,IDecisionVariable) mapping.getDecision(String varName, boolean dereference) Returns the decision variable for a potentially nested/qualified name from this configuration.private IDecisionVariablegetDecision(IDecisionVariable var, String varName, boolean dereference) Returns the decision variable for a potentially nested/qualified name from this configuration using withvaras current search context.getDecision(AbstractVariable declaration) Returns the specifiedIDecisionVariable.intReturns the number of decisions.Returns the declaration of the variable.static StringReturns the instance name of a decision variable.static StringgetInstanceName(IDecisionVariable var, boolean qualified) Returns the instance name of a decision variable.private net.ssehub.easy.basics.modelManagement.ModelInfo<Project> Returns the related model information instance.getName()Returns the name of the configuration.static IDecisionVariablegetNestedElement(IDecisionVariable base, String name) Returns an element forbasespecified by its name.Returns the element this element is part of.Returns the related project.Returns theQueryCache, which should usually be used for query on thisConfiguration.Returns the assignment state to be used when assigning values in the configuration.getState()Returns the assignment state of this variable.static IDecisionVariableReturns the top-level decision variable ofvar.private voidinit()This method creates the list ofIDecisionVariables with initial value settings based on the given project.booleanReturn whether this configuration is exactly or approximative (heuristicall) frozen.booleanisNested()Returns whether this decision variable is nested.iterator()Returns an iterator for iterating over all storedIDecisionVariables.static IDecisionVariablemapVariable(IDecisionVariable var, Configuration cfg) Maps a variable to its configuration following nested elements up and down.voidnotifyReplaced(Project oldProject, Project newProject) static voidprintConfig(PrintStream out, Configuration cfg) Prints a configuration toout.private static voidprintVariable(PrintStream out, IDecisionVariable var, String indent) Prints a variable toout.voidprune()Creates a copy of the underlyingProjectand 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.voidrefresh()This method calls atm only the init() Method.voidregister(IConfigurationChangeListener listener) Registers a new listener.booleanremoveDecision(IDecisionVariable variable) Remove a specific decision.voidRemoves all by a reasoner propagated values from the configuration to clean it up.private voidremoveFromAllInstances(IDecisionVariable variable) Removesvariablefrom all instances.booleanrenameVariable(String oldName, String newName) Renames a variable.booleanrenameVariable(AbstractVariable declaration, String newName) Renames a variable.voidsetValue(Value value, IAssignmentState state, IConfigurationElement nested) Changes the value of a nested variable.voidshareQueryCacheWith(Configuration configToShare) toProject(boolean ownProject) Turns this configuration into a project, e.g.toProject(boolean ownProject, boolean onlyUserInput) Turns this configuration into a project, e.g.voidunfreeze(IAssignmentState state) Unfreezes the whole configuration (including nested variables).private voidunfreezeVariable(IDecisionVariable variable, IAssignmentState state) Unfreezes a variable and all its nested elements (if such exist).booleanunregister(IConfigurationChangeListener listener) Unregisters an listener.(package private) voidvariableChanged(IDecisionVariable var, Value oldValue) Notifies all registeredIConfigurationChangeListeners that aIDecisionVariablechanged.(package private) voidNotifies all registeredIConfigurationChangeListeners that aIDecisionVariablehas changed its state.booleanReturns whether the variable value was explicitly created.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
DEFAULT_INITIALIZER
Defines the default initializer for configurations. This is used as a default value byConfigurationInitializerRegistry. Shall be redefined by an appropriate reasoner. -
project
The origin project, where this configuration belongs to. -
assignValues
private boolean assignValues -
approximativeFreezing
private boolean approximativeFreezing -
decisions
-
listeners
-
allInstances
-
resolutionState
-
cache
-
-
Constructor Details
-
Configuration
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:
-
Configuration
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
Copies aconfigurationby taking over its defined values and assignment states.- Parameters:
configuration- the configuration to be copied
-
Configuration
Alternative constructor to avoid initial value assignment byAssignmentResolver. If activated, theAssignmentResolverwill work withAssignmentState.ASSIGNED.- Parameters:
project- to getConfigurationfrom.assignValues- Decision if values should be assigned byAssignmentResolver.
-
Configuration
Alternative constructor to avoid initial value assignment byAssignmentResolver.- Parameters:
project- to getConfigurationfrom.assignValues- Decision if values should be assigned byAssignmentResolver.resolutionState- the resolution state for the assignment resolver
-
-
Method Details
-
wasCreated
public boolean wasCreated()Description copied from interface:IConfigurationElementReturns whether the variable value was explicitly created.- Specified by:
wasCreatedin interfaceIConfigurationElement- Returns:
truefor created,falseelse
-
getResolutionState
Returns the assignment state to be used when assigning values in the configuration.- Returns:
- the resolution state
-
getProject
Returns the related project.- Returns:
- the related project
-
isApproximatelyFrozen
public boolean isApproximatelyFrozen()Return whether this configuration is exactly or approximative (heuristicall) frozen.- Returns:
falsefor exact freezing,truefor approximative freezing
-
getConfiguredDecisions
Map<AbstractVariable,IDecisionVariable> getConfiguredDecisions()Returns the saved (AbstractVariable,IDecisionVariable) mapping.- Returns:
- A flat copy of the stored (
AbstractVariable,IDecisionVariable) mappings.
-
register
Registers a new listener.- Parameters:
listener- A Listener which should be informed in case of changes.
-
unregister
Unregisters an listener.- Parameters:
listener- The listener which should be unregistered.- Returns:
trueif this configuration contained the specified listener
-
getModelInfo
Returns the related model information instance.- Returns:
- the model information instance
-
init
private void init()This method creates the list ofIDecisionVariables with initial value settings based on the given project. -
createVariables
private void createVariables()This method looks for allDecisionVariableDeclarations inside the related projects, createsDecisionVariableDeclarations and adds them to this configuration. While creating theseDecisionVariableDeclarations this method also tries assign the specified default values to the newly createdDecisionVariableDeclarations. -
addVariables
CreatesIDecisionVariables for all givenAbstractVariables and adds them to the configuration. Part of thecreateVariables()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
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 forvisible- whether the variable shall be visible- Returns:
- the created decision variable
- Throws:
ConfigurationException- in case that creating the variable failed
-
freezeValues
SetsAssignmentState.FROZENstate to already frozen variables.- Parameters:
project- the project to be frozenfilter- the filter type
-
freezeValues
SetsAssignmentState.FROZENstate to the given variablesvar.- Parameters:
var- the variable to be frozenselector- the freeze selector
-
refresh
public void refresh()This method calls atm only the init() Method. -
getQueryCache
Returns theQueryCache, which should usually be used for query on thisConfiguration.- Returns:
- The cache for this
Configuration.
-
addDecision
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
Description copied from interface:IConfigurationReturns the specifiedIDecisionVariable.- Specified by:
getDecisionin interfaceIConfiguration- Parameters:
declaration- The declaration for which the configuration entity should be returned.- Returns:
- The
IDecisionVariablefor the given declaration
-
renameVariable
Renames a variable. Works based ondeclarationor its name so that already renamed variables are considered.- Parameters:
declaration- the variable defining the the old namenewName- the new name- Returns:
trueif the element was found and renamed,falseelse- See Also:
-
renameVariable
Renames a variable. Works based ondeclarationor its name so that already renamed variables are considered.- Parameters:
oldName- the old namenewName- the new name- Returns:
trueif the element was found and renamed,falseelse
-
iterator
Returns an iterator for iterating over all storedIDecisionVariables.- Specified by:
iteratorin interfaceIterable<IDecisionVariable>- Returns:
- an
Iteratorover theIDecisionVariablein this configuration.
-
clear
public void clear()Clears all decisions. -
removeDecision
Remove a specific decision.- Parameters:
variable- the decision to be removed- Returns:
trueif this operation was successful,falseelse
-
removeFromAllInstances
Removesvariablefrom all instances.- Parameters:
variable- the variable to be removed
-
getName
Returns the name of the configuration.- Returns:
- the name of the configuration
-
accept
Description copied from interface:IConfigurationVisitableAccepts and calls the given visitor.- Specified by:
acceptin interfaceIConfigurationVisitable- Parameters:
visitor- the visitor
-
notifyReplaced
- Specified by:
notifyReplacedin interfacenet.ssehub.easy.basics.modelManagement.IModelListener<Project>
-
toProject
Turns this configuration into a project, e.g. for writing.- Parameters:
ownProject- return an own project (true) or add the configuration toproject(false)- Returns:
- the project or null if not implemented (default)
- Throws:
ConfigurationException- in case of any configuration errors
-
toProject
Turns this configuration into a project, e.g. for writing.- Parameters:
ownProject- return an own project (true) or add the configuration toproject(false)onlyUserInput- Specifies whether only user Input should be stored:true: Assignments in stateAssignmentState.ASSIGNEDandAssignmentState.FROZENwill be saved.false: Assignments in stateAssignmentState.ASSIGNED,AssignmentState.FROZEN, andAssignmentState.DERIVEDwill be saved (i.e. also computed values).
- Returns:
- the project or null if not implemented (default)
- Throws:
ConfigurationException- in case of any configuration errors
-
getConfiguration
Description copied from interface:IConfigurationElementReturns the topLevel parent.- Specified by:
getConfigurationin interfaceIConfigurationElement- Returns:
- should be the configuration
-
getParent
Description copied from interface:IConfigurationElementReturns the element this element is part of.- Specified by:
getParentin interfaceIConfigurationElement- Returns:
- the parent element (may be null in case of the top-level configuration)
-
variableChanged
Notifies all registeredIConfigurationChangeListeners that aIDecisionVariablechanged.- Parameters:
var- theIDecisionVariablewhich changed. ThisIDecisionVariableshould be part of this configuration (No validation).oldValue- the old value before the change,varthen contains the new value
-
allInstancesVariableChanged
Changes the all instance cache ifvaror one of its nested elements changed. Precondition is thatallInstanceswas already built up.- Parameters:
var- the changed variable
-
variableChangedState
Notifies all registeredIConfigurationChangeListeners that aIDecisionVariablehas changed its state.- Parameters:
var- theIDecisionVariablewhich changed. ThisIDecisionVariableshould 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 createdvalue- 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:IConfigurationElementReturns whether this decision variable is nested. Being member of aConfigurationdoes not imply.isNested== true- Specified by:
isNestedin interfaceIConfigurationElement- Returns:
trueif it is nested in another decision variable,falseelse
-
getState
Description copied from interface:IConfigurationElementReturns the assignment state of this variable.- Specified by:
getStatein interfaceIConfigurationElement- Returns:
- the assignment state
-
setValue
public void setValue(Value value, IAssignmentState state, IConfigurationElement nested) throws ConfigurationException Description copied from interface:IConfigurationElementChanges the value of a nested variable.- Specified by:
setValuein interfaceIConfigurationElement- Parameters:
value- the new valuestate- the new assignment statenested- the nested variable to be considered in the parent- Throws:
ConfigurationException- in case of type incompatibilities
-
getDeclaration
Returns the declaration of the variable.- Specified by:
getDeclarationin interfaceIConfigurationElement- Returns:
- the declaration of the decision variable
-
freeze
Freezes on the whole configuration.- Specified by:
freezein interfaceIConfigurationElement- Parameters:
selector- the selector deciding what to freeze
-
unfreeze
Unfreezes the whole configuration (including nested variables).- Specified by:
unfreezein interfaceIConfigurationElement- Parameters:
state- the target state after unfreezing (must not beAssignmentState.FROZEN)
-
unfreezeVariable
Unfreezes a variable and all its nested elements (if such exist).- Parameters:
variable-IDecisionVariableto unfreeze.state- state the target state after unfreezing (must not beAssignmentState.FROZEN)
-
freeze
Freezes a single variable (must be a top layer element).- Specified by:
freezein interfaceIConfigurationElement- Parameters:
nestedElement- The name of the top layer variable.
-
dereference
Dereferences a variable.- Parameters:
var- the variable to be dereferenced (may be null)- Returns:
- the dereferenced variable (null if
varwas null)
-
findInParents
Finds a variable callednamein the decision variable parents ofvar, starting at the direct parent ofvar, thus, returning the variable withnameof the closest enclosing scope.- Parameters:
var- the variable to start atname- 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 variabledereference- 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 withvaras current search context.- Parameters:
var- the variable to search within (null for a top-level call)varName- the name of the variabledereference- dereference intermediary reference variables- Returns:
- the variable or null if none exists
- Throws:
ModelQueryException- in case that querying the model fails
-
getAllInstances
Returns all instances of the given type and sub-types.- Specified by:
getAllInstancesin interfaceIConfiguration- 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 oftypestarting atvarand stores results intoreferenceTypesandinstances.- Parameters:
var- the variable to collect the instances fromtype- the type to collectreferenceTypes- the reference types for data types (to be modified as a side effect)instances- the actual instances oftype(to be modified as a side effect)
-
mapVariable
Maps a variable to its configuration following nested elements up and down.- Parameters:
var- the variable to be mappedcfg- the configuration containing the top-level variables- Returns:
- the mapped variable, null if there is no mapping
-
getInstanceName
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
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
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 ofgetInstanceName(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:
trueif the variables are equal by their instance name,falseif they are not equal, in particular if one is null
-
printConfig
Prints a configuration toout. [debugging]- Parameters:
out- the output streamcfg- the configuration to print
-
printVariable
Prints a variable toout. [debugging]- Parameters:
out- the output streamvar- the variable to printindent- 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 underlyingProjectand 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. -
getNestedElement
Returns an element forbasespecified 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
baseis null)
-
getTopLevelDecision
Returns the top-level decision variable ofvar.- Parameters:
var- the variable- Returns:
- the top-level decision variable, eventually
var, null if there is none
-
dereference
Dereferences a value.- Parameters:
conf- the configuration accessvalue- the value to be dereferenced- Returns:
- the dereferenced value
-