crem icon indicating copy to clipboard operation
crem copied to clipboard

Input and outputs dependent on vertex

Open marcosh opened this issue 2 years ago • 1 comments

Currently a BaseMachine has type BaseMachine (topology :: Topology vertex) input output.

In practice we are allowed to specify a specific state space for every vertex of the topology, while the inputs and the outputs must remain constant throughout the whole machine.

It would be interesting to allow inputs and outputs to be dependent on the current vertex,

In practice, this would allow to specify for every vertex which inputs could be received and which outputs could be emitted

marcosh avatar Feb 01 '23 08:02 marcosh

To back this ticket with my own understanding:

Inspired by the blog post: https://marcosh.github.io/post/2021/10/27/ddd-state-machines.html

Here is an illustration of how I think of this problem:

data ReadModel
data Command
data Event

aggregate :: Command -> [Event]
policy :: Event -> [Command]
projection :: ReadModel -> Event -> ReadModel
poolCommand :: IO Command

processACommand :: ReadModel -> Command -> ReadModel
processACommand rm cmd =
  let events = aggregate cmd
      commands = foldMap policy events
      rm' = foldl' projection rm events
  in foldl' processACommand rm commands

loop :: ReadModel -> IO ()
loop rm = do
  rmRef <- newIORef rm
  forever $ do
    rm' <- readIORef rmRef
    cmd <- poolCommand
    let rm'' = processACommand rm cmd
    writeIORef rmRef rm''

Within this DDD model, if I understood correctly, the "crem" currently can encode valid projection function (Topology vertex). However, for the policy function, we cannot encode what Command are valid to be sequenced by such a policy.

If this is the correct understanding, it is a case to be made that when "policy" is developed independently, there is a lot of value in enforcing the correctness of "policy" in a software project.

hellwolf avatar Jun 09 '25 12:06 hellwolf