Elffuss BraidHub

kikocis/EDC public

main · 2.7 KiBRenderedRawPermalink
1# State Machine guards
2
3## Decision
4
5The EDC will provide a way to permit external interactions over internal (automatic) ones in the state machine processes through guards.
6
7## Rationale
8
9In the EDC domain, the state machine is currently a completely automatic engine, every state transition is pre-defined
10and they are following completely automatic logic.
11
12With the upcoming introduction of the so-called "counter offer" feature, there's the need to permit to avoid the state machine to
13pick up certain states and having the user interacting with it through commands in the Management API.
14
15This will be permitted in the most generic way possible, giving to every state machine the possibility to "interrupt" the
16automatic flow and let the user act on it.
17
18## Approach
19
20This 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
24The first one will be implemented in the `StateProcessorImpl`, that's the component that executes the state machine logic.
25In the new implementation, there will be the possibility to add a `Guard`, that's a tuple of a `Predicate` and a `Function`.
26The new `StateProcessorImpl` flow will be:
27```java
28entities.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
40so the `Guard` will take over in the case its predicate matches the entity.
41
42This way it will be possible to control the flow, and every `*Manager` (`ContractNegotiation`, `TransferProcess`, ...)
43can then register their own `PendingGuard` on the state machine configuration.
44In particular, the default hook function implementation will set the "pending" flag on the entity,
45like:
46```
47Function<Entity> hookFunction = entity -> {
48 entity.setPending(true);
49 update(entity);
50 return true;
51```
52
53The flag will be then used as an additional filter passed to the `store.nextNotLeased` method used by the state machine
54to filter out such entities. This way they will just stand still in the database, pending.
55
56The hook predicate will be completely extensible and will permit the implementors to decide if a specific entity state needs
57external interaction based on the input, like:
58```java
59class 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```