Interface CalculationTaskRunner

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface CalculationTaskRunner
    extends java.lang.AutoCloseable
    Component that provides the ability to run calculation tasks.

    This interface is the lower-level counterpart to CalculationRunner. It provides the ability to calculate results based on CalculationTasks. Unless you need to optimize, the CalculationRunner is a simpler entry point.

    The purpose of the runner is to produce a grid of results, with a row for each target and a column for each measure. The targets and columns that define the grid of results are passed in using an instance of CalculationTasks.

    The CalculationTasks instance is obtained using static factory method. It consists of a list of CalculationTask instances, where each task instance corresponds to a single cell in the grid of results. When the CalculationTasks instance is created for a set of trades and measures some one-off initialization is performed. Providing access to the instance allows the initialization to occur once, which could be a performance optimization if many different calculations are performed with the same set of trades and measures.

    Once obtained, the CalculationTasks instance may be used to calculate results. The four "calculate" methods handle the combination of single versus scenario market data, and synchronous versus asynchronous.

    A calculation runner is typically obtained using the static methods on this interface. The instance contains an executor thread-pool, thus care should be taken to ensure the thread-pool is correctly managed. For example, try-with-resources could be used:

      try (CalculationTaskRunner runner = CalculationTaskRunner.ofMultiThreaded()) {
        // use the runner
      }
     
    • Method Detail

      • ofMultiThreaded

        static CalculationTaskRunner ofMultiThreaded()
        Creates a standard multi-threaded calculation task runner capable of performing calculations.

        This factory creates an executor basing the number of threads on the number of available processors. It is recommended to use try-with-resources to manage the runner:

          try (CalculationTaskRunner runner = CalculationTaskRunner.ofMultiThreaded()) {
            // use the runner
          }
         
        Returns:
        the calculation task runner
      • of

        static CalculationTaskRunner of​(java.util.concurrent.ExecutorService executor)
        Creates a calculation task runner capable of performing calculations, specifying the executor.

        It is the callers responsibility to manage the life-cycle of the executor.

        Parameters:
        executor - the executor to use
        Returns:
        the calculation task runner
      • calculate

        Results calculate​(CalculationTasks tasks,
                          MarketData marketData,
                          ReferenceData refData)
        Performs calculations for a single set of market data.

        This returns a grid of results based on the specified tasks and market data. The grid will contain a row for each target and a column for each measure.

        If the thread is interrupted while this method is blocked, calculations will stop and a result returned indicating the failed tasks, with the interrupted flag set. For additional control, use calculateAsync(CalculationTasks, MarketData, ReferenceData, CalculationListener).

        Parameters:
        tasks - the calculation tasks to invoke
        marketData - the market data to be used in the calculations
        refData - the reference data to be used in the calculations
        Returns:
        the grid of calculation results, based on the tasks and market data
      • calculateAsync

        void calculateAsync​(CalculationTasks tasks,
                            MarketData marketData,
                            ReferenceData refData,
                            CalculationListener listener)
        Performs calculations asynchronously for a single set of market data, invoking a listener as each calculation completes.

        This method requires the listener to assemble the results, but it can be much more memory efficient when calculating aggregate results. If the individual results are discarded after they are incorporated into the aggregate they can be garbage collected.

        Parameters:
        tasks - the calculation tasks to invoke
        marketData - the market data to be used in the calculations
        refData - the reference data to be used in the calculations
        listener - listener that is invoked when individual results are calculated
      • calculateMultiScenario

        Results calculateMultiScenario​(CalculationTasks tasks,
                                       ScenarioMarketData marketData,
                                       ReferenceData refData)
        Performs calculations for multiple scenarios, each with a different set of market data.

        This returns a grid of results based on the specified tasks and market data. The grid will contain a row for each target and a column for each measure. Each cell will contain multiple results, one for each scenario.

        If the thread is interrupted while this method is blocked, calculations will stop and a result returned indicating the failed tasks, with the interrupted flag set. For additional control, use calculateMultiScenarioAsync(CalculationTasks, ScenarioMarketData, ReferenceData, CalculationListener).

        Parameters:
        tasks - the calculation tasks to invoke
        marketData - the market data to be used in the calculations
        refData - the reference data to be used in the calculations
        Returns:
        the grid of calculation results, based on the tasks and market data
      • calculateMultiScenarioAsync

        void calculateMultiScenarioAsync​(CalculationTasks tasks,
                                         ScenarioMarketData marketData,
                                         ReferenceData refData,
                                         CalculationListener listener)
        Performs calculations asynchronously for multiple scenarios, each with a different set of market data, invoking a listener as each calculation completes.

        This method requires the listener to assemble the results, but it can be much more memory efficient when calculating aggregate results. If the individual results are discarded after they are incorporated into the aggregate they can be garbage collected.

        Parameters:
        tasks - the calculation tasks to invoke
        marketData - the market data to be used in the calculations
        refData - the reference data to be used in the calculations
        listener - listener that is invoked when individual results are calculated
      • close

        void close()
        Closes any resources held by the component.

        If the component holds an ExecutorService, this method will typically call ExecutorService.shutdown().

        Specified by:
        close in interface java.lang.AutoCloseable