data
tuprolog.solve.data
¶
Custom data: values that libraries and programs attach to the state of a solver.
Primitives and Prolog programs can store arbitrary values, by key, in the state of a solver
(its tuprolog.solve.ExecutionContext), in three tiers, by how long values survive:
- persistent data lives as long as the solver, i.e. across queries;
- durable data lives during a query, longer than ephemeral data;
- ephemeral data is the first to be discarded, e.g. at each resolution step.
Prolog programs read and write them with get_persistent/2 and set_persistent/2 (and the
durable and ephemeral variants); primitives with the side effects of
tuprolog.solve.sideffects (e.g. set_persistent_data).
Main types and functions:
CustomData: a JVM map from keys (strings) to values, created bycustom_data;CustomDataStore: the three tiers of a solver, created bycustom_data_store;EMPTY_DATAandEMPTY_DATA_STORE.
Examples:
>>> from tuprolog.solve.prolog import prolog_solver
>>> solver = prolog_solver()
>>> solver.solve_once("set_persistent(counter, 1)").is_yes
True
>>> solver.solve_once("get_persistent(counter, Value)")["Value"]
1
CustomDataStore
module-attribute
¶
CustomDataStore = _data.CustomDataStore
The custom data of a solver, in three tiers: persistent, durable and ephemeral.
Each tier is a CustomData map. Members: persistent, durable, ephemeral,
copy(persistent, durable, ephemeral), discard_ephemeral() (a copy without ephemeral data),
preserve_persistent() (a copy with persistent data only). Create stores with
custom_data_store.
CustomData
module-attribute
¶
CustomData = Map
Custom data: a JVM map from keys (strings) to arbitrary values, created by custom_data.
EMPTY_DATA
module-attribute
¶
EMPTY_DATA: CustomData = _jutils.Collections.emptyMap()
The empty custom data.
EMPTY_DATA_STORE
module-attribute
¶
EMPTY_DATA_STORE: CustomDataStore = CustomDataStore.empty()
The custom data store with no data in any tier.
custom_data
¶
custom_data(
data: Mapping[str, Any] = {}, **kwargs: Any
) -> CustomData
Creates custom data, i.e. a JVM map from keys to values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any]
|
The values, by key. |
{}
|
**kwargs
|
Any
|
More values, by key. |
{}
|
Returns:
| Type | Description |
|---|---|
CustomData
|
The custom data. |
Examples:
>>> custom_data({"a": 1}, b="x")
{a=1, b=x}
custom_data_store
¶
custom_data_store(
persistent: CustomData = EMPTY_DATA,
durable: CustomData = EMPTY_DATA,
ephemeral: CustomData = EMPTY_DATA,
) -> CustomDataStore
Creates a custom data store, from the data of its three tiers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
persistent
|
CustomData
|
The data living as long as the solver. |
EMPTY_DATA
|
durable
|
CustomData
|
The data living during a query. |
EMPTY_DATA
|
ephemeral
|
CustomData
|
The data discarded first. |
EMPTY_DATA
|
Returns:
| Type | Description |
|---|---|
CustomDataStore
|
The custom data store. |
Examples:
>>> custom_data_store(persistent=custom_data(a=1))
CustomDataStore(persistent={a=1}, durable={}, ephemeral={})