| 1 | # State Machine guards |
| 2 | |
| 3 | ## Decision |
| 4 | |
| 5 | The EDC will provide a way to permit external interactions over internal (automatic) ones in the state machine processes through guards. |
| 6 | |
| 7 | ## Rationale |
| 8 | |
| 9 | In the EDC domain, the state machine is currently a completely automatic engine, every state transition is pre-defined |
| 10 | and they are following completely automatic logic. |
| 11 | |
| 12 | With the upcoming introduction of the so-called "counter offer" feature, there's the need to permit to avoid the state machine to |
| 13 | pick up certain states and having the user interacting with it through commands in the Management API. |
| 14 | |
| 15 | This will be permitted in the most generic way possible, giving to every state machine the possibility to "interrupt" the |
| 16 | automatic flow and let the user act on it. |
| 17 | |
| 18 | ## Approach |
| 19 | |
| 20 | This implementation is based on two pillars: |
| 21 | - adding a generic guard functionality to the state machine |
| 22 | - add a flag that permits to recognize entities that are `pending` (waiting for external interactions) |
| 23 | |
| 24 | The first one will be implemented in the `StateProcessorImpl`, that's the component that executes the state machine logic. |
| 25 | In the new implementation, there will be the possibility to add a `Guard`, that's a tuple of a `Predicate` and a `Function`. |
| 26 | The new `StateProcessorImpl` flow will be: |
| 27 | ```java |
| 28 | entities.get().stream() |
| 29 | .map(entity -> { |
| 30 | if (hook.predicate().test(entity)) { |
| 31 | return hook.process().apply(entity); |
| 32 | } else { |
| 33 | return process.apply(entity); |
| 34 | } |
| 35 | }) |
| 36 | .filter(isEqual(true)) |
| 37 | .count() |
| 38 | ``` |
| 39 | |
| 40 | so the `Guard` will take over in the case its predicate matches the entity. |
| 41 | |
| 42 | This way it will be possible to control the flow, and every `*Manager` (`ContractNegotiation`, `TransferProcess`, ...) |
| 43 | can then register their own `PendingGuard` on the state machine configuration. |
| 44 | In particular, the default hook function implementation will set the "pending" flag on the entity, |
| 45 | like: |
| 46 | ``` |
| 47 | Function<Entity> hookFunction = entity -> { |
| 48 | entity.setPending(true); |
| 49 | update(entity); |
| 50 | return true; |
| 51 | ``` |
| 52 | |
| 53 | The flag will be then used as an additional filter passed to the `store.nextNotLeased` method used by the state machine |
| 54 | to filter out such entities. This way they will just stand still in the database, pending. |
| 55 | |
| 56 | The hook predicate will be completely extensible and will permit the implementors to decide if a specific entity state needs |
| 57 | external interaction based on the input, like: |
| 58 | ```java |
| 59 | class EntityPendingGuard implements PendingGuard<Entity> { |
| 60 | |
| 61 | // custom collaborators as other services |
| 62 | |
| 63 | boolean test(Entity entity) { |
| 64 | // custom condition |
| 65 | return entity.getState() = SPECIFIC_STATE.code() && otherCondition; // if true, the entity will be pending |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | ``` |