Skip to content

Commit

Permalink
add deeocopy example to readme for HSM (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleneum committed Apr 7, 2021
1 parent 9061011 commit 845fd0f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,7 @@ Besides semantic order, nested states are very handy if you want to specify stat
Before *0.8.0*, a `HierarchicalMachine` would not integrate the machine instance itself but the states and transitions by creating copies of them.
However, since *0.8.0* `(Nested)State` instances are just **referenced** which means changes in one machine's collection of states and events will influence the other machine instance. Models and their state will not be shared though.
Note that events and transitions are also copied by reference and will be shared by both instances if you do not use the `remap` keyword.
This change was done to be more in line with `Machine` which also uses passed `State` instances by reference.

```python
count_states = ['1', '2', '3', 'done']
Expand Down Expand Up @@ -1485,6 +1486,21 @@ As mentioned above, using `remap` will **copy** events and transitions since the
If a reused state machine does not have a final state, you can of course add the transitions manually.
If 'counter' had no 'done' state, we could just add `['done', 'counter_3', 'waiting']` to achieve the same behaviour.

In cases where you want states and transitions to be copied by value rather than reference (for instance, if you want to keep the pre-0.8 behaviour) you can do so by creating a `NestedState` and assign deep copies of the machine's events and states to it.

```python
from transitions.extensions.nesting import NestedState
from copy import deepcopy

# ... configuring and creating counter

counting_state = NestedState(name="counting", initial='1')
counting_state.states = deepcopy(counter.states)
counting_state.events = deepcopy(counter.events)

states = ['waiting', 'collecting', counting_state]
```

For complex state machines, sharing configurations rather than instantiated machines might be more feasible.
Especially since instantiated machines must be derived from `HierarchicalMachine`.
Such configurations can be stored and loaded easily via JSON or YAML (see the [FAQ](examples/Frequently%20asked%20questions.ipynb)).
Expand Down

0 comments on commit 845fd0f

Please sign in to comment.