Class ExpressionSampler

java.lang.Object
edu.cmu.tetrad.sem.ExpressionSampler
All Implemented Interfaces:
Sampler

public class ExpressionSampler extends Object implements 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

    Constructors
    Constructor
    Description
    Constructs a new ExpressionSampler with the specified mathematical expression.
  • Method Summary

    Modifier and Type
    Method
    Description
    Retrieves the current Context instance associated with this ExpressionSampler.
    Retrieves the stored mathematical expression associated with this instance.
    double
    Evaluates the stored mathematical expression using an empty context and returns the result.
    void
    Updates the current Context instance associated with this ExpressionSampler.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ExpressionSampler

      public ExpressionSampler(String expression) throws ParseException
      Constructs a new ExpressionSampler with 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 by ExpressionParser.
      Throws:
      ParseException - if the provided expression cannot be parsed into a valid mathematical expression.
  • Method Details

    • getExpression

      public Expression getExpression()
      Retrieves the stored mathematical expression associated with this instance. The returned Expression represents the parsed representation of the mathematical expression provided during construction.
      Returns:
      the Expression object that encapsulates the stored mathematical expression.
    • sample

      public double sample()
      Evaluates the stored mathematical expression using an empty context and returns the result.
      Specified by:
      sample in interface Sampler
      Returns:
      the result of evaluating the mathematical expression as a double
    • getContext

      public Context getContext()
      Retrieves the current Context instance associated with this ExpressionSampler. The Context is used during expression evaluation to resolve the values of variables within the mathematical expression.
      Returns:
      the currently assigned Context instance for this ExpressionSampler. This is by default set to an empty context.
    • setContext

      public void setContext(Context context)
      Updates the current Context instance associated with this ExpressionSampler. The Context is used during expression evaluation to resolve the values of variables within the mathematical expression.
      Parameters:
      context - the Context instance to be assigned. This context will be used to look up variable values during expression evaluation. If set to null, the evaluation will proceed without variable resolution.