Class ExpressionSampler
java.lang.Object
edu.cmu.tetrad.sem.ExpressionSampler
- All Implemented Interfaces:
Sampler
The
ExpressionSampler class provides functionality for parsing, storing, and
evaluating mathematical expressions. It uses a parsed Expression representation
of the mathematical formula and a Context for resolving variable values during
evaluation.
This class is useful in scenarios where mathematical expressions need to be dynamically
evaluated, with the ability to specify variable values through an external context.-
Constructor Summary
ConstructorsConstructorDescriptionExpressionSampler(String expression) Constructs a newExpressionSamplerwith the specified mathematical expression. -
Method Summary
Modifier and TypeMethodDescriptionRetrieves the currentContextinstance associated with thisExpressionSampler.Retrieves the stored mathematical expression associated with this instance.doublesample()Evaluates the stored mathematical expression using an empty context and returns the result.voidsetContext(Context context) Updates the currentContextinstance associated with thisExpressionSampler.
-
Constructor Details
-
ExpressionSampler
Constructs a newExpressionSamplerwith the specified mathematical expression. The given expression is parsed and stored internally for evaluation.- Parameters:
expression- the mathematical expression to parse and store. Must be a valid expression string that can be parsed byExpressionParser.- Throws:
ParseException- if the provided expression cannot be parsed into a valid mathematical expression.
-
-
Method Details
-
getExpression
Retrieves the stored mathematical expression associated with this instance. The returnedExpressionrepresents the parsed representation of the mathematical expression provided during construction.- Returns:
- the
Expressionobject that encapsulates the stored mathematical expression.
-
sample
-
getContext
Retrieves the currentContextinstance associated with thisExpressionSampler. TheContextis used during expression evaluation to resolve the values of variables within the mathematical expression.- Returns:
- the currently assigned
Contextinstance for thisExpressionSampler. This is by default set to an empty context.
-
setContext
Updates the currentContextinstance associated with thisExpressionSampler. TheContextis used during expression evaluation to resolve the values of variables within the mathematical expression.- Parameters:
context- theContextinstance to be assigned. This context will be used to look up variable values during expression evaluation. If set tonull, the evaluation will proceed without variable resolution.
-