Class VariableValueCopier
- java.lang.Object
-
- net.ssehub.easy.varModel.cstEvaluation.VariableValueCopier
-
public class VariableValueCopier extends java.lang.ObjectCopies specified variable values, i.e., single variables to a single variable or a compatible container or a container to a container. In case of a container as source, the first value will be copied. May create further variables in case of target reference types. Actually, this functionality might be done by a more generic VIL function, but for now a simpler (internal) implementation is sufficient.- Author:
- Holger Eichelberger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classVariableValueCopier.CopySpecDefines a copy specification.static classVariableValueCopier.EnumAttributeFreezeProviderA default freeze provider for but-expressions based on attributes of type enum.static interfaceVariableValueCopier.IAssignmentListenerOptional listener interface to generically track primary value assignments.static interfaceVariableValueCopier.IFreezeProviderAn interface providing information to create freeze blocks for new variables.private static classVariableValueCopier.SourceBasedFreezeProviderA freeze provider based on known frozen assignment states.
-
Field Summary
Fields Modifier and Type Field Description private intcountprivate VariableValueCopier.IAssignmentListenerlistenerprivate java.lang.StringnamePrefixprivate IAssignmentStatenewStateprivate java.util.Map<IDatatype,java.util.Map<java.lang.String,VariableValueCopier.CopySpec>>specs
-
Constructor Summary
Constructors Constructor Description VariableValueCopier(java.lang.String namePrefix, IAssignmentState newState, VariableValueCopier.CopySpec... copySpecs)Creates an instance with given assignment state.VariableValueCopier(java.lang.String namePrefix, VariableValueCopier.CopySpec... copySpecs)Creates an instance for copying with stateAssignmentState.ASSIGNED.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) static IDecisionVariablecopy(IDecisionVariable variable, java.lang.String namePrefix)Copies the given variable.private Valuecopy(IDecisionVariable source, IDecisionVariable target, VariableValueCopier.IFreezeProvider freezeProvider)Copies the value ofsourcevariable to targetvariable.private ValuecopySingleVariable(IDecisionVariable source, IDecisionVariable target, VariableValueCopier.IFreezeProvider freezeProvider, boolean adding)Copies a single variable fromsourceto target and ensures in case of a reference target that a respective variable is created.static booleandoCreateNewVars(java.lang.String namePrefix)Whether new variables shall be created.private voiddoFreeze(VariableValueCopier.IFreezeProvider freezeProvider, Configuration cfg, IDecisionVariable var, IFreezable freezable)Tries to freezefreezable.private ContainerVariableensureContainerVariable(ContainerVariable target)Ensures that the given container variable is configured and empty.static IDecisionVariablefindVariable(IDecisionVariable base, java.lang.String qname)Finds a variable via its (qualified access) name.protected EASyLoggerFactory.EASyLoggergetLogger()Returns the logger of this class.private voidhandleFurther(IDecisionVariable base, java.lang.String[] names, Value value)Assignsvalueto the further variables onbasegiven innamesat index positions larger than 0.private static ValuekeepFirst(Value actual, Value newValue)Keeps the first value ofactualandnewValue.private voidnotifyAssigned(IDecisionVariable target, Value value, boolean added)Notifies about assigned values.private voidnotifyCreated(IDecisionVariable origin, IDecisionVariable target)Notifies the listener about a new variable created for a given origin variable.voidprocess(Configuration config)Processes a configuration according to the copy specifications.private voidprocess(IDecisionVariable variable, IDecisionVariable base, java.lang.String prefix, java.util.Map<java.lang.String,VariableValueCopier.CopySpec> fieldSpecs, java.util.Set<IDecisionVariable> done)Processes a decision variable.voidsetAssignmentListener(VariableValueCopier.IAssignmentListener listener)Defines the assignment listener.
-
-
-
Field Detail
-
specs
private java.util.Map<IDatatype,java.util.Map<java.lang.String,VariableValueCopier.CopySpec>> specs
-
namePrefix
private java.lang.String namePrefix
-
count
private int count
-
newState
private IAssignmentState newState
-
listener
private VariableValueCopier.IAssignmentListener listener
-
-
Constructor Detail
-
VariableValueCopier
public VariableValueCopier(java.lang.String namePrefix, VariableValueCopier.CopySpec... copySpecs)Creates an instance for copying with stateAssignmentState.ASSIGNED.- Parameters:
namePrefix- the name prefix for new variables (no new variables will be created if null or empty).copySpecs- the copy specifications to take into account, individual specs may be null but will be ignored then
-
VariableValueCopier
public VariableValueCopier(java.lang.String namePrefix, IAssignmentState newState, VariableValueCopier.CopySpec... copySpecs)Creates an instance with given assignment state.- Parameters:
namePrefix- the name prefix for new variables (no new variables will be created if null or empty).newState- the state to use (if null,AssignmentState.ASSIGNEDis used)copySpecs- the copy specifications to take into account
-
-
Method Detail
-
setAssignmentListener
public void setAssignmentListener(VariableValueCopier.IAssignmentListener listener)
Defines the assignment listener.- Parameters:
listener- the listener (may be null to disable notifications)
-
getLogger
protected EASyLoggerFactory.EASyLogger getLogger()
Returns the logger of this class.- Returns:
- the logger
-
process
public void process(Configuration config) throws ConfigurationException, ValueDoesNotMatchTypeException, CSTSemanticException
Processes a configuration according to the copy specifications.- Parameters:
config- the configuration to process- Throws:
ConfigurationException- in case that the configuration is not possibleValueDoesNotMatchTypeException- in case that value types do not matchCSTSemanticException- in case that expressions such as freeze-buts are not correctly constructed
-
process
private void process(IDecisionVariable variable, IDecisionVariable base, java.lang.String prefix, java.util.Map<java.lang.String,VariableValueCopier.CopySpec> fieldSpecs, java.util.Set<IDecisionVariable> done) throws ConfigurationException, ValueDoesNotMatchTypeException, CSTSemanticException
Processes a decision variable.- Parameters:
variable- the variable to processbase- the base variable where processing initially startedprefix- the prefix access name processed so farfieldSpecs- the field specifications forbasedone- already processed variables - avoiding cycles- Throws:
ConfigurationException- in case that the configuration is not possibleValueDoesNotMatchTypeException- in case that value types do not matchCSTSemanticException- in case that expressions such as freeze-buts are not correctly constructed
-
handleFurther
private void handleFurther(IDecisionVariable base, java.lang.String[] names, Value value) throws ConfigurationException
Assignsvalueto the further variables onbasegiven innamesat index positions larger than 0.- Parameters:
base- the base variablenames- the further variable names to initializevalue- the value to use for initialization- Throws:
ConfigurationException- in case that the configuration is not possible
-
findVariable
public static IDecisionVariable findVariable(IDecisionVariable base, java.lang.String qname)
Finds a variable via its (qualified access) name.- Parameters:
base- the base variable to search onqname- the variable name to search for- Returns:
- the resulting variable or null if there is none
-
copy
private Value copy(IDecisionVariable source, IDecisionVariable target, VariableValueCopier.IFreezeProvider freezeProvider) throws ConfigurationException, ValueDoesNotMatchTypeException, CSTSemanticException
Copies the value ofsourcevariable to targetvariable.- Parameters:
source- the source variable to copy fromtarget- the target variable to copy tofreezeProvider- optional freeze provider to be applied to new variables (may be null for no freezing)- Returns:
- the first value set (may be null)
- Throws:
ConfigurationException- in case that the configuration is not possibleValueDoesNotMatchTypeException- in case that value types do not matchCSTSemanticException- in case that expressions such as freeze-buts are not correctly constructed
-
keepFirst
private static Value keepFirst(Value actual, Value newValue)
Keeps the first value ofactualandnewValue.- Parameters:
actual- the actual valuenewValue- the new value- Returns:
actualifactualwas previously null,newValueelse
-
ensureContainerVariable
private ContainerVariable ensureContainerVariable(ContainerVariable target) throws ConfigurationException, ValueDoesNotMatchTypeException
Ensures that the given container variable is configured and empty.- Parameters:
target- the variable- Returns:
target- Throws:
ConfigurationException- in case that the configuration is not possibleValueDoesNotMatchTypeException- in case that value types do not match
-
doCreateNewVars
public static boolean doCreateNewVars(java.lang.String namePrefix)
Whether new variables shall be created.- Parameters:
namePrefix- the name prefix- Returns:
truefor new variables,falseelse
-
copySingleVariable
private Value copySingleVariable(IDecisionVariable source, IDecisionVariable target, VariableValueCopier.IFreezeProvider freezeProvider, boolean adding) throws ConfigurationException, ValueDoesNotMatchTypeException, CSTSemanticException
Copies a single variable fromsourceto target and ensures in case of a reference target that a respective variable is created.- Parameters:
source- the source variabletarget- the target variable (may be a nested variable, e.g., of a collection)freezeProvider- optional freeze provider to be applied to new variables (may be null for no freezing)adding- whether the value is being added to a collection or just set- Returns:
- the actual value set
- Throws:
ConfigurationException- in case that the configuration is not possibleValueDoesNotMatchTypeException- in case that value types do not matchCSTSemanticException- in case that expressions such as freeze-buts are not correctly constructed
-
doFreeze
private void doFreeze(VariableValueCopier.IFreezeProvider freezeProvider, Configuration cfg, IDecisionVariable var, IFreezable freezable) throws ValueDoesNotMatchTypeException, CSTSemanticException
Tries to freezefreezable.- Parameters:
freezeProvider- the freeze provider (may be null, ignored then)cfg- the configuration to freeze onvar- the variable to freezefreezable- the freezeable (the declaration ofvar, used for constructing a temporary freeze block of no direct freeze selector is available- Throws:
ValueDoesNotMatchTypeException- in case that value types do not matchCSTSemanticException- in case that expressions such as freeze-buts are not correctly constructed
-
notifyAssigned
private void notifyAssigned(IDecisionVariable target, Value value, boolean added)
Notifies about assigned values.- Parameters:
target- the target variablevalue- the new valueadded- whether value is being added to a collection or set as the single absolute value
-
notifyCreated
private void notifyCreated(IDecisionVariable origin, IDecisionVariable target)
Notifies the listener about a new variable created for a given origin variable. AnotifyAssigned(IDecisionVariable, Value, boolean)shall follow.- Parameters:
origin- the origin variabletarget- the new variable
-
copy
static IDecisionVariable copy(IDecisionVariable variable, java.lang.String namePrefix) throws CSTSemanticException, ValueDoesNotMatchTypeException, ConfigurationException
Copies the given variable.- Parameters:
variable- the variable to copynamePrefix- the name prefix for new variables.- Returns:
- the created (local, temporary) variable including value; further variables may have been created
- Throws:
ConfigurationException- in case that the configuration is not possibleValueDoesNotMatchTypeException- in case that value types do not matchCSTSemanticException- in case that expressions are not correctly constructed
-
-