状态¶
按「作用域 + 键路径」读写状态,支持点分嵌套、变更观察者与持久化。
zoo_framework.statemachine.state_machine_manager.StateMachineManager ¶
State machine manager.
源代码位于: zoo_framework/statemachine/state_machine_manager.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
have_loaded ¶
load_state_machines ¶
Load the state machines.
Restore semantics (adjudicated in change fix-state-restore / #72): whole-table replacement. The only current consume path is loading from disk after the process's first resolution (the instance is necessarily empty then), where replacement and merge are equivalent; an explicit reload on a non-empty state is a deliberate user action.
Legacy defect (fixed): what got persisted was the ThreadSafeDict
returned by get_state_machines(), and it is not a dict
subclass - the old guard isinstance(state_machine, dict) was always
false for the framework's own files, the assignment never ran, yet
_local_store_loaded was set true: the state was never restored and
the "loaded" illusion blocked retries (see
tests/test_state_restore.py). The guard now dispatches on the real
type:
ThreadSafeDict: restored as-is (the framework's own on-disk form)- plain
dict: wrapped into aThreadSafeDict, so the declared type is true on every path (compat entry for old files / manual injection; storing a plain dict would blow uphas_key()at runtime) - any other non-None argument: rejected with an explicit
TypeError- silent ignoring is the same family of defect as before; theStateMachineWorkererror path falls back to a fresh state and leaves an error log
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state_machine
|
ThreadSafeDict | dict | None
|
the state scope mapping to restore; None means nothing to restore, going straight into new state |
None
|
引发:
| 类型 | 描述 |
|---|---|
TypeError
|
the argument is neither None nor a dict/ThreadSafeDict |
源代码位于: zoo_framework/statemachine/state_machine_manager.py
get_and_create_scope ¶
create_scope ¶
get_scope_identity ¶
Query the owning run identity of a state scope.
Ownership is determined by the run that first wrote the scope
(see StateScope.set_state_node). Returns None when the scope does
not exist or has no identified write yet.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
scope
|
str
|
the scope name |
必需 |
返回:
| 类型 | 描述 |
|---|---|
RunIdentity | None
|
The owning |
源代码位于: zoo_framework/statemachine/state_machine_manager.py
get_scope_session ¶
Query the owning session identity of a state scope; None when unowned.
源代码位于: zoo_framework/statemachine/state_machine_manager.py
set_state ¶
Set the value of a state node.
源代码位于: zoo_framework/statemachine/state_machine_manager.py
get_state ¶
Get the state node.
源代码位于: zoo_framework/statemachine/state_machine_manager.py
remove_state ¶
Remove the state node.
源代码位于: zoo_framework/statemachine/state_machine_manager.py
get_state_machines ¶
observe_state ¶
Observe a state node.
源代码位于: zoo_framework/statemachine/state_machine_manager.py
unobserve_state ¶
Remove a state node observer - fixes a memory leak.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
scope
|
str
|
the scope name |
必需 |
key
|
str
|
the state key name |
必需 |
effect
|
Callable
|
the observer callback |
必需 |
引发:
| 类型 | 描述 |
|---|---|
KeyError
|
when the scope or the state does not exist |
源代码位于: zoo_framework/statemachine/state_machine_manager.py
作用域与节点¶
zoo_framework.statemachine.state_scope.StateScope ¶
State scope - P2 optimized version.
P2 optimizations: 1. creates the index via a factory 2. supports multiple index implementations 3. supports switching the index type dynamically
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
_state_index |
StateIndex
|
the state-node index |
源代码位于: zoo_framework/statemachine/state_scope.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
observe_state_node ¶
Observe a state node.
When the key does not exist yet, a placeholder node is created before
the observer is registered: silently dropping the registration would
break the primary usage of "declare the observer first, wait for the
data". The asymmetry with unobserve_state_node is deliberate -
unregistering an observer that never existed usually means the caller
is wrong and it should surface.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the state key name |
必需 |
effect
|
Any
|
the observer callback |
必需 |
源代码位于: zoo_framework/statemachine/state_scope.py
unobserve_state_node ¶
Remove a state node observer - fixes a memory leak.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the state key name |
必需 |
effect
|
Any
|
the observer callback function |
必需 |
引发:
| 类型 | 描述 |
|---|---|
KeyError
|
when the state node does not exist |
源代码位于: zoo_framework/statemachine/state_scope.py
register_top_node ¶
Register a root node.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
value
|
Any
|
the node value |
必需 |
effect
|
list | None
|
the side-effect list |
None
|
源代码位于: zoo_framework/statemachine/state_scope.py
register_node ¶
Register a state node.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
value
|
Any
|
the node value |
必需 |
effect
|
list | None
|
the side-effect list |
None
|
源代码位于: zoo_framework/statemachine/state_scope.py
set_state_node ¶
Set the value of a state node.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
value
|
Any
|
the node value |
必需 |
effect
|
list | None
|
the side-effect list |
None
|
源代码位于: zoo_framework/statemachine/state_scope.py
update_state_node ¶
get_state_node ¶
Get a state node.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
返回:
| 类型 | 描述 |
|---|---|
StateNode | None
|
The state node, or None |
get_state_value ¶
Get a state node's value.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
The node value |
源代码位于: zoo_framework/statemachine/state_scope.py
get_state_children_value ¶
Get a state node's children values.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
A dict of child values |
源代码位于: zoo_framework/statemachine/state_scope.py
move_state_node ¶
Move a state node.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the original key name |
必需 |
target_key
|
str
|
the target key name |
必需 |
源代码位于: zoo_framework/statemachine/state_scope.py
remove_state_node ¶
Remove a state node.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the node key name |
必需 |
源代码位于: zoo_framework/statemachine/state_scope.py
copy_state_node ¶
Copy a state node's value.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
key
|
str
|
the original key name |
必需 |
target_key
|
Any
|
the target key name |
必需 |
源代码位于: zoo_framework/statemachine/state_scope.py
get_all_nodes ¶
Get all state nodes.
P2 optimization: supports fetching all nodes.
返回:
| 类型 | 描述 |
|---|---|
dict
|
A dict of nodes |
find_nodes_by_prefix ¶
Find nodes by key prefix.
P2 optimization: supports prefix lookup.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
prefix
|
str
|
the key prefix |
必需 |
返回:
| 类型 | 描述 |
|---|---|
list
|
A list of nodes |
源代码位于: zoo_framework/statemachine/state_scope.py
zoo_framework.statemachine.state_node.StateNode ¶
State node.
源代码位于: zoo_framework/statemachine/state_node.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
set_top ¶
is_top ¶
to_be_top ¶
get_key ¶
add_child ¶
get_type ¶
get_value ¶
get_children_value ¶
Get the children values.
源代码位于: zoo_framework/statemachine/state_node.py
set_key ¶
set_value ¶
Set the state node value.
源代码位于: zoo_framework/statemachine/state_node.py
get_state ¶
add_effect ¶
Add an effect to the state node.
源代码位于: zoo_framework/statemachine/state_node.py
remove_effect ¶
Remove an effect from the state node - fixes a memory leak.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
effect
|
FunctionType | None
|
the effect function to remove |
必需 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
when the effect is not in the list |
源代码位于: zoo_framework/statemachine/state_node.py
zoo_framework.statemachine.base_state_machine.BaseStateMachine ¶
Bases: dict
Base class for all state machines.
源代码位于: zoo_framework/statemachine/base_state_machine.py
索引与类型¶
zoo_framework.statemachine.state_node_type ¶
StateNodeType ¶
Bases: Enum
The state node type.
源代码位于: zoo_framework/statemachine/state_node_type.py
get_type_by_value
staticmethod
¶
Get the type by value.
源代码位于: zoo_framework/statemachine/state_node_type.py
zoo_framework.statemachine.state_node_index ¶
状态节点索引:按状态维护 effect 的记账副本。
历史缺陷(issue #208):StateEffectScheduler 自建立起就调用
add_state_effect / remove_state_effect,但本类并未实现这两个方法——该路径
一旦被构造即 AttributeError(此前无构造点,属死代码上的潜伏缺陷)。本类初始就
带有 state_effect_map / state_effect_set 两个空容器却无配套方法,二者当时都是
死状态;本次补齐接口,使其成为真正生效的记账结构。
StateNodeIndex ¶
状态节点索引。
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
state_node |
关联的状态节点 |
|
state_effect_map |
dict[Any, set[Any]]
|
状态 -> 该状态下的 effect 集合 |
state_effect_set |
set[Any]
|
全部已登记 effect 的合集 |
id |
索引实例标识 |
源代码位于: zoo_framework/statemachine/state_node_index.py
add_state_effect ¶
登记一个状态 effect:按状态入分组,同时并入全集。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state_effect
|
Any
|
待登记的状态 effect;以 |
必需 |
源代码位于: zoo_framework/statemachine/state_node_index.py
remove_state_effect ¶
移除一个状态 effect:同时从分组与全集中移除。
幂等:目标不存在时不抛错——索引是调度器记账的副本,重复移除不应让调用方的 簿记路径中断。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state_effect
|
Any
|
待移除的状态 effect。 |
必需 |
源代码位于: zoo_framework/statemachine/state_node_index.py
zoo_framework.statemachine.state_index_factory.StateIndexFactory ¶
State index factory.
P2 optimization: create indexes via the factory pattern.
源代码位于: zoo_framework/statemachine/state_index_factory.py
create_index
classmethod
¶
Create an index.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
index_type
|
str
|
the index type |
'dict'
|
返回:
| 类型 | 描述 |
|---|---|
StateIndex
|
An index instance |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
when the index type does not exist |
源代码位于: zoo_framework/statemachine/state_index_factory.py
register_index_type
classmethod
¶
Register a new index type.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str
|
the type name |
必需 |
index_class
|
type
|
the index class |
必需 |
get_available_types
classmethod
¶
状态 effect¶
zoo_framework.statemachine.state_effect.StateEffect ¶
The side effect of a state node.
源代码位于: zoo_framework/statemachine/state_effect.py
zoo_framework.statemachine.state_effect_scheduler.StateEffectScheduler ¶
The state effect scheduler.
源代码位于: zoo_framework/statemachine/state_effect_scheduler.py
add_state_effect ¶
Add a state effect.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state_effect
|
StateEffect
|
the state effect |
必需 |
源代码位于: zoo_framework/statemachine/state_effect_scheduler.py
remove_state_effect ¶
Remove a state effect.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state_effect
|
StateEffect
|
the state effect |
必需 |
源代码位于: zoo_framework/statemachine/state_effect_scheduler.py
get_state_effect ¶
Get the state effects for a state.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state
|
object
|
the state node |
必需 |
get_state_effect_index ¶
execute_state_effect ¶
Execute the state effects for a state.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
state
|
object
|
the state node |
必需 |
*args
|
Any
|
positional arguments passed to each effect |
()
|
**kwargs
|
Any
|
keyword arguments passed to each effect |
{}
|