Graph
Graph
Bases: BaseModel
Create and execute a graph defined by a list of edges.
Set the required State and Shared classes via the Generic Typing Parameters. Because of variance its possible to use nodes, that use more general State and Shared classes (ancestors) as the Generic Typing Parameters.
The edges are defined as a list of tuples, where the first element is the source node and the second element reveals the next node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
A list of edges of compatible nodes that build the graph |
required | |
instant_edge
|
A list of edges of compatible nodes that run parallel to there source node |
required |
Source code in src/edgygraph/graph.py
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 | |
model_post_init(_)
Index the edges by source node
Source code in src/edgygraph/graph.py
44 45 46 47 48 49 | |
index_edges(edges)
Index the edges by source node
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
list[Edge[T, S]]
|
The edges to index |
required |
Returns:
| Type | Description |
|---|---|
dict[Node[T, S] | Type[START], list[NextType[T, S]]]
|
A mapping from source node (or START) to the next objects of the edge |
Source code in src/edgygraph/graph.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
__call__(state, shared)
async
Execute the graph based on the edges
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
T
|
State of the first generic type of the graph or a subtype |
required |
shared
|
S
|
Shared of the second generic type of the graph or a subtype |
required |
Returns:
| Type | Description |
|---|---|
Tuple[T, S]
|
New State instance and the same Shared instance |
Source code in src/edgygraph/graph.py
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 | |
get_next_nodes(state, shared, current_nodes, edge_index)
async
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
T
|
The current state |
required |
shared
|
S
|
The shared state |
required |
current_nodes
|
list[Node[T, S]] | list[Node[T, S] | Type[START]]
|
The current nodes |
required |
Returns:
| Type | Description |
|---|---|
list[Node[T, S]]
|
The list of the next nodes to run based on the current nodes and edges. |
list[Node[T, S]]
|
If an edge is a callable, it will be called with the current state and shared state. |
Source code in src/edgygraph/graph.py
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 | |
merge_states(current_state, result_states)
Merges the result states into the current state. First the changes are calculated for each result state. Then the changes are checked for conflicts. If there are conflicts, a ChangeConflictException is raised. The changes are applied in the order of the result states list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state
|
T
|
The current state |
required |
result_states
|
list[T]
|
The result states |
required |
Returns:
| Type | Description |
|---|---|
T
|
The new merged State instance. |
Raises:
| Type | Description |
|---|---|
ChangeConflictException
|
If there are conflicts in the changes. |
Source code in src/edgygraph/graph.py
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 | |
ChangeTypes
Bases: StrEnum
Enum for the types of changes that can be made to a State.
Source code in src/edgygraph/graph.py
231 232 233 234 235 236 237 238 | |
Change
Bases: RichReprMixin, BaseModel
Represents a change made to a State.
Source code in src/edgygraph/graph.py
240 241 242 243 244 245 246 247 | |
Diff
Utility class for computing differences between states.
Source code in src/edgygraph/graph.py
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
find_conflicts(changes)
classmethod
Finds conflicts in a list of changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
changes
|
list[dict[str, Change]]
|
A list of dictionaries representing changes to a state. |
required |
Source code in src/edgygraph/graph.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
recursive_diff(old, new, path='')
classmethod
Recursively computes the differences between two dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old
|
Any
|
Part of the old dictionary. |
required |
new
|
Any
|
Part of the new dictionary. |
required |
path
|
str
|
The current path of the parts in the full dictionary, seperated with dots. |
''
|
Returns:
| Type | Description |
|---|---|
dict[str, Change]
|
A mapping of the path to the changes directly on that level. |
Source code in src/edgygraph/graph.py
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 | |
apply_changes(target, changes)
classmethod
Applies a set of changes to the target dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
dict[str, Any]
|
The dictionary to apply the changes to. |
required |
changes
|
dict[str, Change]
|
A mapping of paths, separated by dots, to changes. The changes are applied in the dictionary on that level. |
required |
Source code in src/edgygraph/graph.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
ChangeConflictException
Bases: Exception
Exception raised when a conflict between changes to a state is detected.
Source code in src/edgygraph/graph.py
349 350 351 352 353 | |