---
title: FunctionGeneratedTableFactory
---

The [`FunctionGeneratedTableFactory`](/core/javadoc/io/deephaven/engine/table/impl/util/FunctionGeneratedTableFactory.html)'s `create` method is useful for creating tables that are dependent on one or more ticking tables, or for creating tables that need to be refreshed at a regular interval.
The method creates a table by running the user-defined `tableGenerator` function. This function will be run once when the table is created, and then again when either the `sourceTables...` tick or when `refreshIntervalMs` milliseconds have elapsed.

> [!NOTE]
> The `tableGenerator` may access data in the `sourceTables`, but should not perform any further operations on them without careful handling. Since table operations may be memoized, a table operation may return a table created by a previous invocation of the same operation. Since that result will not have been included in the `sourceTables`, it is not automatically treated as a dependency for purposes of determining when it is safe to invoke `tableGenerator`.
> This allows race conditions to exist between accessing the operation result and that result's own update processing. It is best to include all dependencies directly in `sourceTables` or only compute on-demand inputs under a `LivenessScope`.

When transforming a table, users should generally prefer to use regular table operations instead of this factory because they can perform some operations incrementally. However, `create` might require less development effort for small tables.

## Syntax

```groovy syntax
create(tableGenerator, refreshIntervalMs)
create(tableGenerator, sourceTables...)
```

## Parameters

<ParamTable>
<Param name="tableGenerator" type="Supplier<Table>">

A function that returns a table to copy into the output table.

</Param>
<Param name="refreshIntervalMs" type="int">

The interval (in milliseconds) at which the `tableGenerator` function is re-run.

</Param>
<Param name="sourceTables" type="Table...">

If the `tableGenerator` function is dependent on one or more ticking tables, the tables must be specified here. This will cause the generated table to be recomputed on each tick.

</Param>
</ParamTable>

## Returns

A ticking table (assuming `sourceTables` have been specified), generated by the `tableGenerator` function.

## Example

In the following example, we create an [execution context](../../../conceptual/execution-context.md) and a `tableGenerator` function, which we then use to generate a table that re-runs the `tableGenerator` function every 2000ms.

```groovy order=result
import io.deephaven.engine.context.ExecutionContext
import io.deephaven.util.SafeCloseable
import io.deephaven.engine.table.impl.util.FunctionGeneratedTableFactory

// create execution context
defaultCtx = ExecutionContext.getContext()

// define tableGenerator function
tableGenerator = { ->
    Random rand = new Random()
    for (int i = 0; i < 5; ++i) {
        try (SafeCloseable ignored = defaultCtx.open()) {
            myTable = emptyTable(5).update("X = randomInt(0, 10)", "Y = randomDouble(0.0, 100.0)")
        }
        sleep(10)
    }
    return myTable
}

// create a function-generated table that re-runs tableGenerator every 2000ms
result = FunctionGeneratedTableFactory.create(tableGenerator, 2000)
```

If the `tableGenerator` function depends on one or more ticking tables, the tables must be specified. Adding `timeTable1` as a `sourceTables...` parameter causes the generated table to be recomputed on each tick of the time table.

```groovy order=result
import io.deephaven.engine.context.ExecutionContext
import io.deephaven.csv.util.MutableBoolean
import io.deephaven.engine.table.ColumnDefinition
import io.deephaven.engine.table.TableDefinition
import io.deephaven.stream.TablePublisher
import io.deephaven.util.SafeCloseable
import io.deephaven.engine.table.impl.util.FunctionGeneratedTableFactory

defaultCtx = ExecutionContext.getContext()

tableGenerator = { ->
    Random rand = new Random()
    for (int i = 0; i < 5; ++i) {
        try (SafeCloseable ignored = defaultCtx.open()) {
            myTable = emptyTable(5).update("X = randomInt(0, 10)", "Y = randomDouble(0.0, 100.0)")
        }
        sleep(10)
    }
    return myTable
}

timeTable1 = timeTable("PT1S")

result = FunctionGeneratedTableFactory.create(tableGenerator, timeTable1)
```

## Related documentation

- [Execution Context](../../../conceptual/execution-context.md)
- [Javadoc](/core/javadoc/io/deephaven/engine/table/impl/util/FunctionGeneratedTableFactory.html)
