Class RuntimeEnvironment<V extends VariableDeclaration,M extends IModel>
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.common.RuntimeEnvironment<V,M>
-
- Type Parameters:
V- the variable declaration typeM- the model type
- All Implemented Interfaces:
IRestrictionEvaluationContext,IRuntimeEnvironment
- Direct Known Subclasses:
RuntimeEnvironment,RuntimeEnvironment
public abstract class RuntimeEnvironment<V extends VariableDeclaration,M extends IModel> extends java.lang.Object implements IRuntimeEnvironment, IRestrictionEvaluationContext
Provides the runtime environment for executing a VIL models. After creating a runtime environment, first the context must be defined using#switchContext(IModel).- Author:
- Holger Eichelberger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classRuntimeEnvironment.Context<D extends VariableDeclaration,O extends IModel>Defines a context for executing one model within.private static classRuntimeEnvironment.Level<V extends VariableDeclaration>Defines a nested level of value assignments.
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Class<V>clsprivate java.util.Map<IModel,RuntimeEnvironment.Context<V,M>>contextsprivate RuntimeEnvironment.Context<V,M>currentContextprivate java.util.function.Supplier<java.lang.String>modelNameSupplierprivate IResolvableModel<V,M>mostSpecificModelprivate java.util.Set<IArtifact>noAutoStoreprivate TypeRegistrytypeRegistry
-
Constructor Summary
Constructors Constructor Description RuntimeEnvironment(java.lang.Class<V> cls)Creates a new runtime environment using the default type registry.RuntimeEnvironment(java.lang.Class<V> cls, TypeRegistry typeRegistry)Creates a new runtime environment with a given type registry instance.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddValue(V var, java.lang.Object object)Changes the value ofvarto the top level.private static java.lang.StringappendModelName(java.lang.String msg, java.util.function.Supplier<java.lang.String> modelName)Appends the model name frommodelNamesupplier if feasible.private static java.lang.ObjectcheckInitialCollectionValue(TypeDescriptor<?> type, java.lang.Object object)Checks initial collection values for type compliance.static java.lang.ObjectcheckType(java.lang.String name, TypeDescriptor<?> type, java.lang.Object object, TypeRegistry registry, java.util.function.Supplier<java.lang.String> modelName)Checks whetherobjectcan be assigned to some form of variable/field with namenameand typetype.private java.lang.ObjectcheckType(V var, java.lang.Object object)Checks whetherobjectcan be assigned tovar.protected abstract IExpressionVisitorcreateEvaluationProcessor()Creates the expression visitor for this environment.voiddecreaseIndentation()Decreases the indentation by one step.voiddeleteContext(ITypedModel model)Explicitly deletes a context if it is not the current context.voidendEvaluation(java.lang.Object processor)Notifies the context about the end of the evaluation.private static java.lang.ObjectevaluatePrimitives(java.lang.Object object, DecisionVariable decVar, TypeDescriptor<?> varType)EvaluatesdecVarto its primitive value according tovarType.Vget(java.lang.String name)Returns the variable of namenameif defined.IResolvableModel<?,M>getContextModel()Returns the model assigned to the context.java.lang.String[]getContextPaths()Returns the search paths of the current context.intgetIndentation()Returns the current indentation.IndentationConfigurationgetIndentationConfiguration()Returns the indentation information for the underlying model.java.lang.ObjectgetIvmlValue(java.lang.String name)Returns the value of the IVML element denoted bynamein the closest scope defining an IVML configuration.IResolvableModel<?,M>getMostSpecificContextModel()ConfigurationgetTopLevelConfiguration()Returns the top-level configuration based on defining global VIL/VTL script parameters.TypeRegistrygetTypeRegistry()Returns the (local) type registry of this runtime environment.java.lang.ObjectgetValue(IResolvableModel<V,M> contextModel, java.lang.String name)Returns the value ofnamewithincontextModel.java.lang.ObjectgetValue(IResolvable resolvable)Returns the value ofresolvable.java.lang.ObjectgetValue(V var)Returns the value ofvar.voidincreaseIndentation()Increases the indentation by one step.booleanisDefined(V var)Returns whethervaris defined (a value was assigned).voidmarkNoAutoStore(IArtifact artifact)Marks artifacts as excluded from auto-storing when related variabeles become unavailable.voidpopLevel()Removes the topmost level.voidpushLevel()Pushes a new level.protected abstract voidreleaseEvaluationProcessor(IExpressionVisitor processor)Releases the expression visitor for this environment.voidremoveValue(V var)Removes the variable binding forvarif it exists.voidsetContextPaths(java.util.List<java.io.File> paths)Defines the (sub-script) search paths of the current context.voidsetIndentation(int indentation)Changes the current indentation.voidsetIndentationSteps(int steps)Changes the current indentation (in steps).voidsetValue(IVariable variable, Version version)Defines the value for the given variable in this context.voidsetValue(IResolvable var, java.lang.Object object)Changes the value ofvarwhere it is defined.voidsetValue(V var, java.lang.Object object)Changes the value ofvarwhere it is defined.java.lang.ObjectstartEvaluation()Notifies the context about starting the evaluation.voidstoreArtifacts(boolean force)Stores the artifacts in the current context.IResolvableModel<V,M>switchContext(IResolvableModel<V,M> model)Switches the context.voidunmarkNoAutoStore(IArtifact artifact)Marks artifacts as again included from auto-storing when related variabeles become unavailable.voidunsetValue(IVariable variable)Unsets the value for the given variable.
-
-
-
Field Detail
-
contexts
private java.util.Map<IModel,RuntimeEnvironment.Context<V extends VariableDeclaration,M extends IModel>> contexts
-
currentContext
private RuntimeEnvironment.Context<V extends VariableDeclaration,M extends IModel> currentContext
-
mostSpecificModel
private IResolvableModel<V extends VariableDeclaration,M extends IModel> mostSpecificModel
-
typeRegistry
private TypeRegistry typeRegistry
-
cls
private java.lang.Class<V extends VariableDeclaration> cls
-
noAutoStore
private java.util.Set<IArtifact> noAutoStore
-
modelNameSupplier
private java.util.function.Supplier<java.lang.String> modelNameSupplier
-
-
Constructor Detail
-
RuntimeEnvironment
public RuntimeEnvironment(java.lang.Class<V> cls)
Creates a new runtime environment using the default type registry.- Parameters:
cls- the variable declaration type
-
RuntimeEnvironment
public RuntimeEnvironment(java.lang.Class<V> cls, TypeRegistry typeRegistry)
Creates a new runtime environment with a given type registry instance.- Parameters:
cls- the variable declaration typetypeRegistry- the type registry to use
-
-
Method Detail
-
getTypeRegistry
public TypeRegistry getTypeRegistry()
Description copied from interface:IRuntimeEnvironmentReturns the (local) type registry of this runtime environment.- Specified by:
getTypeRegistryin interfaceIRuntimeEnvironment- Returns:
- the type registry
-
switchContext
public IResolvableModel<V,M> switchContext(IResolvableModel<V,M> model)
Switches the context.- Parameters:
model- the model the context is assigned to- Returns:
- the old context for switching back
-
deleteContext
public void deleteContext(ITypedModel model)
Explicitly deletes a context if it is not the current context.- Parameters:
model- the model to delete the context for
-
setContextPaths
public void setContextPaths(java.util.List<java.io.File> paths)
Defines the (sub-script) search paths of the current context.- Parameters:
paths- the search paths
-
getContextPaths
public java.lang.String[] getContextPaths()
Description copied from interface:IRuntimeEnvironmentReturns the search paths of the current context.- Specified by:
getContextPathsin interfaceIRuntimeEnvironment- Returns:
- the (qualified) search paths (may be null)
-
getContextModel
public IResolvableModel<?,M> getContextModel()
Returns the model assigned to the context.- Returns:
- the model (may be null if
#switchContext(IModel)was not called before)
-
getMostSpecificContextModel
public IResolvableModel<?,M> getMostSpecificContextModel()
-
getValue
public java.lang.Object getValue(IResolvable resolvable) throws VilException
Description copied from interface:IRuntimeEnvironmentReturns the value ofresolvable.- Specified by:
getValuein interfaceIRuntimeEnvironment- Parameters:
resolvable- the resolvable- Returns:
- the value of
resolvable - Throws:
VilException- in case thatresolvablewas not defined
-
getValue
public java.lang.Object getValue(IResolvableModel<V,M> contextModel, java.lang.String name) throws VilException
Returns the value ofnamewithincontextModel.- Parameters:
contextModel- the model indicating the contextname- the name of the variable- Returns:
- the value of
name - Throws:
VilException- in case that the context/variable is not known
-
getValue
public java.lang.Object getValue(V var) throws VilException
Returns the value ofvar.- Parameters:
var- the variable to return the value for- Returns:
- the value of
var - Throws:
VilException- in case thatvarwas not defined
-
storeArtifacts
public void storeArtifacts(boolean force) throws VilExceptionDescription copied from interface:IRuntimeEnvironmentStores the artifacts in the current context.- Specified by:
storeArtifactsin interfaceIRuntimeEnvironment- Parameters:
force- forces storing artifacts even over (dynamic) exclusion criteria- Throws:
VilException- in case that storing artifacts fails
-
get
public V get(java.lang.String name)
Returns the variable of namenameif defined.- Specified by:
getin interfaceIRuntimeEnvironment- Parameters:
name- the name of the variable- Returns:
- the variable or null if not found
-
isDefined
public boolean isDefined(V var)
Returns whethervaris defined (a value was assigned).- Parameters:
var- the variable to look for- Returns:
trueifvaris defined,falseelse
-
setValue
public void setValue(IResolvable var, java.lang.Object object) throws VilException
Changes the value ofvarwhere it is defined.- Specified by:
setValuein interfaceIRuntimeEnvironment- Parameters:
var- the variable to be modifiedobject- the value ofvar- Throws:
VilException- in case of an attempt of modifying a constant
-
setValue
public void setValue(V var, java.lang.Object object) throws VilException
Changes the value ofvarwhere it is defined.- Parameters:
var- the variable to be modifiedobject- the value ofvar- Throws:
VilException- in case of an attempt of modifying a constant
-
checkInitialCollectionValue
private static java.lang.Object checkInitialCollectionValue(TypeDescriptor<?> type, java.lang.Object object)
Checks initial collection values for type compliance. Actually, the parser is not able to create the right type for all "{}", so we correct this here.- Parameters:
type- the typeobject- the object value- Returns:
objector a more specific / better matching type
-
pushLevel
public void pushLevel()
Pushes a new level.
-
popLevel
public void popLevel() throws VilExceptionRemoves the topmost level.- Throws:
VilException- in case that storing artifacts fails, the level will be popped anyway
-
addValue
public void addValue(V var, java.lang.Object object) throws VilException
Changes the value ofvarto the top level.- Parameters:
var- the variable to be modifiedobject- the value ofvar- Throws:
VilException- in case of an attempt of modifying a constant
-
removeValue
public void removeValue(V var)
Removes the variable binding forvarif it exists.- Parameters:
var- the variable to be removed
-
checkType
private java.lang.Object checkType(V var, java.lang.Object object) throws VilException
Checks whetherobjectcan be assigned tovar.- Parameters:
var- the variable to assignobjecttoobject- the object to be assigned (may be null)- Returns:
objector null if object isNullValue.- Throws:
VilException- in case of type incompatibilities
-
checkType
public static java.lang.Object checkType(java.lang.String name, TypeDescriptor<?> type, java.lang.Object object, TypeRegistry registry, java.util.function.Supplier<java.lang.String> modelName) throws VilExceptionChecks whetherobjectcan be assigned to some form of variable/field with namenameand typetype.- Parameters:
name- the name of the variable/field to assignobjecttotype- the type of the variable/field to assignobjecttoobject- the object to be assigned (may be null)registry- the type registry to usemodelName- optional model name supplier called in error cases, may be null for none, may return null- Returns:
objector null if object isNullValue.- Throws:
VilException- in case of type incompatibilities
-
appendModelName
private static java.lang.String appendModelName(java.lang.String msg, java.util.function.Supplier<java.lang.String> modelName)Appends the model name frommodelNamesupplier if feasible.- Parameters:
msg- the message to appendmodelName- the model name supplier, may be null or return null (ignored then)- Returns:
- the message (with appended) model name
-
evaluatePrimitives
private static java.lang.Object evaluatePrimitives(java.lang.Object object, DecisionVariable decVar, TypeDescriptor<?> varType)EvaluatesdecVarto its primitive value according tovarType. Late evaluation.- Parameters:
object- the evaluated value so fardecVar- the decision variablevarType- the variable type- Returns:
objector the evaluated value
-
getIndentationConfiguration
public IndentationConfiguration getIndentationConfiguration()
Returns the indentation information for the underlying model.- Returns:
- the indentation information
-
getIndentation
public int getIndentation()
Returns the current indentation.- Returns:
- the current indentation (in single whitespaces)
-
setIndentationSteps
public void setIndentationSteps(int steps)
Changes the current indentation (in steps).- Parameters:
steps- the number of indentation steps
-
setIndentation
public void setIndentation(int indentation)
Changes the current indentation.- Parameters:
indentation- the new indentation (in single whitespaces)
-
increaseIndentation
public void increaseIndentation()
Increases the indentation by one step.
-
decreaseIndentation
public void decreaseIndentation()
Decreases the indentation by one step.
-
getIvmlValue
public java.lang.Object getIvmlValue(java.lang.String name) throws VilExceptionDescription copied from interface:IRuntimeEnvironmentReturns the value of the IVML element denoted bynamein the closest scope defining an IVML configuration.- Specified by:
getIvmlValuein interfaceIRuntimeEnvironment- Parameters:
name- the name- Returns:
- the value
- Throws:
VilException- in case thatnamewas not defined
-
setValue
public void setValue(IVariable variable, Version version) throws RestrictionEvaluationException
Description copied from interface:IRestrictionEvaluationContextDefines the value for the given variable in this context. May imply that the given variable is added to the context if adequate.- Specified by:
setValuein interfaceIRestrictionEvaluationContext- Parameters:
variable- the variable to be definedversion- the actual value- Throws:
RestrictionEvaluationException- in case that setting the variable fails, e.g., due to type conflicts
-
unsetValue
public void unsetValue(IVariable variable) throws RestrictionEvaluationException
Description copied from interface:IRestrictionEvaluationContextUnsets the value for the given variable. May imply that the variable is removed from the context if adequate.- Specified by:
unsetValuein interfaceIRestrictionEvaluationContext- Parameters:
variable- the variable to be unset- Throws:
RestrictionEvaluationException- in case that setting the variable fails, e.g., due to type conflicts
-
createEvaluationProcessor
protected abstract IExpressionVisitor createEvaluationProcessor()
Creates the expression visitor for this environment.- Returns:
- the expression visitor
-
releaseEvaluationProcessor
protected abstract void releaseEvaluationProcessor(IExpressionVisitor processor)
Releases the expression visitor for this environment.- Parameters:
processor- the expression visitor
-
startEvaluation
public java.lang.Object startEvaluation() throws RestrictionEvaluationExceptionDescription copied from interface:IRestrictionEvaluationContextNotifies the context about starting the evaluation.- Specified by:
startEvaluationin interfaceIRestrictionEvaluationContext- Returns:
- returns the evaluation processor
- Throws:
RestrictionEvaluationException- in case that a sub context cannot be created
-
endEvaluation
public void endEvaluation(java.lang.Object processor) throws RestrictionEvaluationExceptionDescription copied from interface:IRestrictionEvaluationContextNotifies the context about the end of the evaluation.- Specified by:
endEvaluationin interfaceIRestrictionEvaluationContext- Parameters:
processor- the evaluation processor for release- Throws:
RestrictionEvaluationException- in case that a sub context cannot be removed
-
getTopLevelConfiguration
public Configuration getTopLevelConfiguration()
Returns the top-level configuration based on defining global VIL/VTL script parameters.- Returns:
- the top-level configuration (may be null if unknown)
-
markNoAutoStore
public void markNoAutoStore(IArtifact artifact)
Marks artifacts as excluded from auto-storing when related variabeles become unavailable.- Parameters:
artifact- the artifact
-
unmarkNoAutoStore
public void unmarkNoAutoStore(IArtifact artifact)
Marks artifacts as again included from auto-storing when related variabeles become unavailable.- Parameters:
artifact- the artifact
-
-