Class ExpressionEvaluator
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.expressions.Expression
-
- net.ssehub.easy.instantiation.core.model.expressions.ExpressionEvaluator
-
- All Implemented Interfaces:
IStringValueProvider,IVilType
public class ExpressionEvaluator extends Expression implements IVilType, IStringValueProvider
A wrapper type to pass and evaluate expressions in VIL languages. This is helpful to realize expression-based collection selection and projection operators. In case of aggregating expressions, consider multiple declarators, in particulargetResultType()and for evaluationinitializeDeclarators()andgetResultValue().- Author:
- Holger Eichelberger
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface net.ssehub.easy.instantiation.core.model.vilTypes.IStringValueProvider
IStringValueProvider.StringComparator
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<VariableDeclaration>declaratorsprivate EvaluationVisitorevaluationVisitorprivate Expressionexpressionprivate VariableDeclarationiterator
-
Constructor Summary
Constructors Constructor Description ExpressionEvaluator(Expression expression, VariableDeclaration iterator, java.util.List<? extends VariableDeclaration> declarators)Creates an expression evaluator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectaccept(IExpressionVisitor visitor)Visits the expression.voidbind(EvaluationVisitor evaluationVisitor)Binds the evaluation visitor.java.lang.Objectevaluate(java.lang.Object iteratorValue)Evaluates the wrapped expression.VariableDeclarationgetDeclarator(int index)Returns the specified declarator.intgetDeclaratorsCount()Returns the number of declarators.ExpressiongetExpression()Returns the expression.VariableDeclarationgetIteratorVariable()Returns the iterator variable.private VariableDeclarationgetResultDeclarator()Returns the explicit result declarator of an aggregating iterator.TypeDescriptor<?>getResultType()Returns the explicit result type of an aggregating iterator.java.lang.ObjectgetResultValue()Returns the explicit result value of an aggregating iterator.java.lang.StringgetStringValue(IStringValueProvider.StringComparator comparator)Returns the string value of this object.TypeDescriptor<?>inferType()Infers the type of this expression including the type of the contained sub-expressions.voidinitializeDeclarators()Initializes the declarators in case of default values given and changes the runtime environment accordingly.voidunbind()Unbinds the evaluation visitor (seeevaluate(Object)).-
Methods inherited from class net.ssehub.easy.instantiation.core.model.expressions.Expression
getFormattingHint, replaceEmptyLine
-
-
-
-
Field Detail
-
evaluationVisitor
private EvaluationVisitor evaluationVisitor
-
expression
private Expression expression
-
iterator
private VariableDeclaration iterator
-
declarators
private java.util.List<VariableDeclaration> declarators
-
-
Constructor Detail
-
ExpressionEvaluator
public ExpressionEvaluator(Expression expression, VariableDeclaration iterator, java.util.List<? extends VariableDeclaration> declarators)
Creates an expression evaluator.- Parameters:
expression- the expression to evaluate oniterator- the iterator variable, one ofdeclaratorsdeclarators- all declarators of this expression in source sequence, if null a list consisting ofiteratorwill be created
-
-
Method Detail
-
getDeclaratorsCount
public int getDeclaratorsCount()
Returns the number of declarators.- Returns:
- the number of declarators
-
getDeclarator
public VariableDeclaration getDeclarator(int index)
Returns the specified declarator.- Parameters:
index- the 0-based index- Returns:
- the declarator
- Throws:
java.lang.IndexOutOfBoundsException- ifindex < 0 || index >=getDeclaratorsCount()
-
getResultType
public TypeDescriptor<?> getResultType()
Returns the explicit result type of an aggregating iterator.- Returns:
- the explicit result type (if there are multiple declarators than it's the first one if it has a default value), null else
-
getResultDeclarator
private VariableDeclaration getResultDeclarator()
Returns the explicit result declarator of an aggregating iterator.- Returns:
- the explicit declarator (if there are multiple declarators than it's the first one if it has a default value), null else
-
getResultValue
public java.lang.Object getResultValue() throws VilExceptionReturns the explicit result value of an aggregating iterator.- Returns:
- the explicit value, null else
- Throws:
VilException- in case that obtaining the result value fails
-
bind
public void bind(EvaluationVisitor evaluationVisitor)
Binds the evaluation visitor. Must be called beforeevaluate(Object).- Parameters:
evaluationVisitor- the evaluation visitor used for evaluating the contained expression
-
unbind
public void unbind()
Unbinds the evaluation visitor (seeevaluate(Object)).
-
evaluate
public java.lang.Object evaluate(java.lang.Object iteratorValue) throws VilExceptionEvaluates the wrapped expression. This method is intended to be called from the type being evaluated (which does not have access to theevaluationVisitor). Thus,bind(EvaluationVisitor)must be called before andunbind()shall be called by the type being evaluated.- Parameters:
iteratorValue- the current value when iterating with the expression- Returns:
- the result of evaluating
expressionin the current context ofevaluationVisitor - Throws:
VilException- in case that the evaluation fails
-
getExpression
public Expression getExpression()
Returns the expression. Before evaluating this expression, theiterator variablemust be bound with an actual value.- Returns:
- the expression to be evaluated
-
getIteratorVariable
public VariableDeclaration getIteratorVariable()
Returns the iterator variable.- Returns:
- the iterator variable
-
inferType
public TypeDescriptor<?> inferType() throws VilException
Description copied from class:ExpressionInfers the type of this expression including the type of the contained sub-expressions.- Specified by:
inferTypein classExpression- Returns:
- the type of this expression
- Throws:
VilException- in case that inferring the type fails
-
accept
public java.lang.Object accept(IExpressionVisitor visitor) throws VilException
Description copied from class:ExpressionVisits the expression.- Specified by:
acceptin classExpression- Parameters:
visitor- the visitor- Returns:
- the result of visiting this expression (may be null)
- Throws:
VilException- in case that visiting fails (e.g., execution)
-
getStringValue
public java.lang.String getStringValue(IStringValueProvider.StringComparator comparator)
Description copied from interface:IStringValueProviderReturns the string value of this object.- Specified by:
getStringValuein interfaceIStringValueProvider- Parameters:
comparator- if the sequence of elements, e.g., in case of collections, shall be ordered according to the comparator, null if the original sequence shall be returned- Returns:
- the string value
-
initializeDeclarators
public void initializeDeclarators() throws VilExceptionInitializes the declarators in case of default values given and changes the runtime environment accordingly.- Throws:
VilException- if evaluating or setting the evaluated value fails
-
-